Files
aiData/Apps/YeLiTe/Test/test_card_detection.py
HuangHai 46f17aed11 'commit'
2026-01-13 21:37:14 +08:00

83 lines
2.6 KiB
Python

# coding=utf-8
import sys
import os
import cv2
import numpy as np
import time
# Add project root to sys.path
project_root = os.path.abspath(os.path.join(os.path.dirname(__file__), "../../../"))
sys.path.append(project_root)
try:
from Apps.YeLiTe.Kit import detect_cards_cv
from Apps.YeLiTe.Config import Setting
except ImportError as e:
print(f"Import Error: {e}")
print(f"Current sys.path: {sys.path}")
sys.exit(1)
def test_detection_and_click_simulation(image_path):
print(f"Loading image: {image_path}")
if not os.path.exists(image_path):
print(f"Error: File not found: {image_path}")
return
# 1. Run detection
print("Running detect_cards_cv...")
cards = detect_cards_cv(image_path, top_ratio=Setting.SAFE_EXCLUDE_RATIO, bottom_ratio=Setting.BOTTOM_SAFE_EXCLUDE_RATIO)
if not cards:
print("No cards detected!")
return
print(f"Detected {len(cards)} cards.")
# 2. Draw results
img = cv2.imread(image_path)
for i, box in enumerate(cards):
x1, y1, x2, y2 = box
# Draw green box
cv2.rectangle(img, (x1, y1), (x2, y2), (0, 255, 0), 2)
# Calculate click point (center)
center_x = int((x1 + x2) / 2)
center_y = int((y1 + y2) / 2)
# Draw click point (Red Circle)
cv2.circle(img, (center_x, center_y), 10, (0, 0, 255), -1)
# Add label
label = f"#{i+1} ({center_x}, {center_y})"
cv2.putText(img, label, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 0, 255), 2)
print(f"Card #{i+1}: Box=[{x1}, {y1}, {x2}, {y2}], Click Point=({center_x}, {center_y})")
# 3. Save result
output_filename = f"result_{os.path.basename(image_path)}"
output_path = os.path.join(os.path.dirname(__file__), output_filename)
cv2.imwrite(output_path, img)
print(f"Result saved to: {output_path}")
if __name__ == "__main__":
# Default to the latest list_*.jpg in Output if no argument provided
target_image = ""
if len(sys.argv) > 1:
target_image = sys.argv[1]
else:
output_dir = os.path.join(project_root, "Output")
if os.path.exists(output_dir):
files = [f for f in os.listdir(output_dir) if f.startswith("list_") and f.endswith(".jpg")]
if files:
# Sort by time (filename usually contains timestamp)
files.sort(reverse=True)
target_image = os.path.join(output_dir, files[0])
if target_image:
test_detection_and_click_simulation(target_image)
else:
print("No target image found. Please provide an image path as an argument.")