Files
momentry_core/scripts/test_florence2_stamps.py
Warren e75c4d6f07 cleanup: remove dead code and duplicate docs
- Remove session-ses_2f27.md (161KB raw session log)
- Remove 49 ROOT_* duplicate files across REFERENCE/
- Remove 14 duplicate files between REFERENCE/ root and history/
- Remove asr_legacy.rs (dead code, replaced by asr.rs)
- Remove src/core/worker/ (duplicate JobWorker)
- Remove src/core/layers/ (empty directory)
- Remove 4 .bak files in src/
- Remove 7 dead private methods in worker/processor.rs
- Remove backup directory from git tracking
2026-05-04 01:31:21 +08:00

82 lines
2.4 KiB
Python

#!/opt/homebrew/bin/python3.11
"""
Test Florence-2 for "Stamps" Detection
Florence-2 is superior to OWL-ViT for small objects and detailed description.
"""
import os
import cv2
from PIL import Image
from transformers import AutoProcessor, AutoModelForCausalLM
UUID = "384b0ff44aaaa1f1"
VIDEO_PATH = f"output/{UUID}/{UUID}.mp4"
OUTPUT_DIR = f"output/{UUID}/florence2_results"
os.makedirs(OUTPUT_DIR, exist_ok=True)
# Frame where "stamp" is heavily discussed
TIMESTAMP = 6846.0
print(f"📽️ Extracting frame at {TIMESTAMP}s...")
cap = cv2.VideoCapture(VIDEO_PATH)
cap.set(cv2.CAP_PROP_POS_MSEC, TIMESTAMP * 1000)
ret, frame = cap.read()
cap.release()
if not ret:
print("❌ Failed to read frame.")
exit()
# Save raw frame
raw_path = os.path.join(OUTPUT_DIR, f"raw_{int(TIMESTAMP)}.jpg")
cv2.imwrite(raw_path, frame)
print("💾 Raw frame saved.")
image_pil = Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
print("🧠 Loading Florence-2 model (this may take a moment)...")
try:
processor = AutoProcessor.from_pretrained(
"microsoft/Florence-2-base", trust_remote_code=True
)
model = AutoModelForCausalLM.from_pretrained(
"microsoft/Florence-2-base", trust_remote_code=True
)
except Exception as e:
print(f"❌ Error loading model: {e}")
exit()
# Test 1: Open Vocabulary Detection
print("🔍 Testing Open Vocabulary Detection for 'stamp'...")
prompt = "stamp"
inputs = processor(text=prompt, images=image_pil, return_tensors="pt")
generated_ids = model.generate(
input_ids=inputs["input_ids"],
pixel_values=inputs["pixel_values"],
max_new_tokens=1024,
num_beams=3,
)
generated_text = processor.batch_decode(generated_ids, skip_special_tokens=False)[0]
parsed_answer = processor.post_process_generation(
generated_text,
task="<OPEN_VOCABULARY_DETECTION>",
image_size=(image_pil.width, image_pil.height),
)
print(f"📝 Florence-2 Result: {parsed_answer}")
# Test 2: Detailed Caption (To see if it notices the stamp in context)
print("📝 Testing Detailed Caption...")
inputs = processor(text="<DETAILED_CAPTION>", images=image_pil, return_tensors="pt")
generated_ids = model.generate(
input_ids=inputs["input_ids"],
pixel_values=inputs["pixel_values"],
max_new_tokens=1024,
)
caption = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
print(f"📝 Caption: {caption}")
print("🏁 Done.")