import argparse import os import sys import cv2 from src.data.renderer import generate_high_fidelity_spinda from src.models.inference import SpindaInference from src.registry.database import SpindaRegistry from src.utils.detector import SpindaDetector from src.utils.resolver import SpindaResolver def identify_spinda( image_path: str, model_path: str = "models/best_resnet34_model.pth", backbone: str = "resnet34", ) -> None: if not os.path.exists(image_path): print(f"Error: File {image_path} not found.") return print(f"--- Identifying Spinda in {image_path} ---") # 1. Detect and crop detector = SpindaDetector() cropped_img = detector.detect_and_crop(image_path) if cropped_img is None: print("Error: Could not detect Spinda in the image.") return cv2.imwrite("detected_spinda_crop.png", cropped_img) print("Detected Spinda saved to detected_spinda_crop.png") # 2. Inference — pass the BGR array directly, no temp file needed inf = SpindaInference(model_path=model_path, backbone=backbone) coords, fingerprint = inf.predict(cropped_img) print(f"Visual Fingerprint: {fingerprint}") print(f"Predicted Grid Coordinates: {coords}") # 3. Resolve to PIDs resolved = SpindaResolver.resolve_fingerprint(fingerprint) print("\nPossible PIDs:") print(f" Standard (Gen 3-8, HOME): 0x{resolved['standard']}") print(f" BDSP (Big-Endian Flip): 0x{resolved['bdsp']}") # 4. Visual verification print("\nGenerating visual verification image...") verify_img = generate_high_fidelity_spinda(int(resolved['standard'], 16)) cv2.imwrite("prediction_verify.png", verify_img) print("Verification image saved to: prediction_verify.png") # 5. Registry lookup reg = SpindaRegistry() matches = reg.lookup_by_fingerprint(fingerprint) if matches: print("\nMatches found in Global Registry:") for pid in matches: print(f" - Registered PID: 0x{pid}") else: print("\nNo matching entries in Global Registry.") if __name__ == "__main__": parser = argparse.ArgumentParser() parser.add_argument("image_path") parser.add_argument("--backbone", type=str, default="resnet34", choices=["resnet18", "resnet34", "convnext_tiny"]) parser.add_argument("--model_path", type=str, default="models/best_resnet34_model.pth") args = parser.parse_args() identify_spinda(args.image_path, model_path=args.model_path, backbone=args.backbone)