#!/opt/homebrew/bin/python3.11
"""
Scan key stamp scenes from Charade (1963) for stamp-related objects
Looking for: envelopes, letters, stamp albums, small rectangular paper objects
"""

import cv2
import numpy as np
import os
import glob

BASE_DIR = "output/384b0ff44aaaa1f1/stamp_scenes"
OUTPUT_DIR = "output/384b0ff44aaaa1f1/stamp_scenes_crops"
os.makedirs(OUTPUT_DIR, exist_ok=True)

frames = sorted(glob.glob(os.path.join(BASE_DIR, "scene_*.jpg")))
print(f"🎬 Scanning {len(frames)} key stamp scene frames...")

for frame_path in frames:
    img = cv2.imread(frame_path)
    if img is None:
        continue

    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

    # 1. Look for rectangular objects (edges + contours)
    edges = cv2.Canny(gray, 50, 150)
    contours, _ = cv2.findContours(edges, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

    frame_crops = 0

    for contour in contours:
        area = cv2.contourArea(contour)
        if area < 500 or area > 100000:
            continue

        # Approximate polygon
        epsilon = 0.02 * cv2.arcLength(contour, True)
        approx = cv2.approxPolyDP(contour, epsilon, True)

        # Look for 4-sided shapes (rectangles)
        if len(approx) == 4:
            x, y, w, h = cv2.boundingRect(contour)
            aspect = w / h if h > 0 else 0

            # Stamp/letter/envelope proportions
            if 0.3 < aspect < 3.0:
                # Check if it's paper-like (light colored)
                roi = gray[y : y + h, x : x + w]
                mean_val = np.mean(roi)

                # Paper is typically light (high pixel values)
                if mean_val > 120:
                    frame_crops += 1
                    crop = img[y : y + h, x : x + w]
                    basename = os.path.basename(frame_path).replace(".jpg", "")
                    crop_path = os.path.join(
                        OUTPUT_DIR, f"paper_{basename}_{x}_{y}.jpg"
                    )
                    cv2.imwrite(crop_path, crop)

                    # Draw on full frame
                    cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2)

    if frame_crops > 0:
        print(
            f"  📍 {os.path.basename(frame_path)}: {frame_crops} paper-like rectangles"
        )
        ann_path = os.path.join(OUTPUT_DIR, f"annotated_{os.path.basename(frame_path)}")
        cv2.imwrite(ann_path, img)

print(f"\n🏁 Done. Check {OUTPUT_DIR} for paper object crops.")
