#!/opt/homebrew/bin/python3.11
"""
Find ANY Small Rectangular Object in Hands
"""

import cv2
import numpy as np
import os

UUID = "384b0ff44aaaa1f1"
BASE_DIR = f"output/{UUID}/florence2_results"

# Frames to check
FRAMES = [
    "scan_6756.jpg",  # 112:36
    "scan_6763.jpg",  # 112:43
    "scan_6790.jpg",  # 113:10
    "scan_6813.jpg",  # 113:33
    "scan_6832.jpg",  # 113:52
]

print("🖐️ Searching for SMALL OBJECTS in hands...")

for frame_name in FRAMES:
    img_path = os.path.join(BASE_DIR, frame_name)
    if not os.path.exists(img_path):
        continue

    img = cv2.imread(img_path)
    hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)

    # 1. Hand Detection (Skin Tone) - Adjusted for lighting
    # Broad range to catch hands in shadow or bright light
    skin_mask = cv2.inRange(hsv, np.array([0, 15, 40]), np.array([25, 160, 255]))

    # Morphological cleaning
    kernel = np.ones((5, 5), np.uint8)
    skin_mask = cv2.morphologyEx(skin_mask, cv2.MORPH_CLOSE, kernel)
    skin_mask = cv2.morphologyEx(skin_mask, cv2.MORPH_OPEN, kernel)

    # 2. Find contours inside/near hands
    # We dilate the mask slightly to include objects held IN the hand
    skin_dilated = cv2.dilate(skin_mask, kernel, iterations=3)

    # Find contours in the full image
    contours, _ = cv2.findContours(
        skin_dilated, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE
    )

    print(f"\n🎞️ Scanning {frame_name}...")
    found_count = 0

    for cnt in contours:
        x, y, w, h = cv2.boundingRect(cnt)
        area = cv2.contourArea(cnt)

        # Object size filter:
        # Too small (< 100px) = noise
        # Too big (> 15000px) = likely the face or body part itself
        if 100 < area < 15000:
            # Shape filter: Rectangle-like (Aspect ratio 0.5 to 2.0)
            aspect_ratio = float(w) / h

            # Check for rectangularity (Extent)
            rect_area = w * h
            if rect_area > 0:
                extent = float(area) / rect_area
                # If extent > 0.4, it's somewhat rectangular/filled
                if 0.5 < aspect_ratio < 2.5 and extent > 0.4:
                    found_count += 1
                    print(
                        f"   ✅ Candidate Object: Area={int(area)}, Pos=({x},{y}), Size={w}x{h}"
                    )

                    # Crop with padding
                    pad = 10
                    crop = img[
                        max(0, y - pad) : min(img.shape[0], y + h + pad),
                        max(0, x - pad) : min(img.shape[1], x + w + pad),
                    ]
                    crop_path = os.path.join(
                        BASE_DIR, f"object_in_hand_{frame_name}_{x}_{y}.jpg"
                    )
                    cv2.imwrite(crop_path, crop)

                    # Draw on main image
                    cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 3)
                    cv2.putText(
                        img,
                        f"OBJ? ({int(area)})",
                        (x, y - 10),
                        cv2.FONT_HERSHEY_SIMPLEX,
                        0.6,
                        (0, 255, 0),
                        2,
                    )

    if found_count == 0:
        print("   ❌ No small objects found in hands.")
    else:
        res_path = os.path.join(BASE_DIR, f"result_objects_{frame_name}")
        cv2.imwrite(res_path, img)
        print(f"   🎨 Result saved to {res_path}")
