import sys import os import cv2 import numpy as np # Add project root to path project_root = os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) sys.path.append(project_root) from Apps.YeLiTe.Kit import detect_cards_cv, read_image def test_detection(): image_path = r"d:\dsWork\aiData\Output\list_1768309287.jpg" if not os.path.exists(image_path): print(f"Error: Image not found at {image_path}") # Try to list files in Output to see what's available output_dir = os.path.dirname(image_path) if os.path.exists(output_dir): print(f"Files in {output_dir}:") for f in os.listdir(output_dir): print(f) return print(f"Testing detection on {image_path}") # Debug mode: copy logic from Kit.py but add debug prints and saves img = read_image(image_path) if img is None: return h, w = img.shape[:2] print(f"Image size: {w}x{h}") gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) blurred = cv2.GaussianBlur(gray, (5, 5), 0) # Try different thresholds edges = cv2.Canny(blurred, 10, 50) # Lower thresholds # Save edges debug_edges_path = os.path.join(os.path.dirname(__file__), "debug_edges.jpg") cv2.imencode('.jpg', edges)[1].tofile(debug_edges_path) print(f"Saved edges to {debug_edges_path}") contours, _ = cv2.findContours(edges, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) print(f"Found {len(contours)} contours") # Draw all contours on a debug image debug_contours_img = img.copy() cv2.drawContours(debug_contours_img, contours, -1, (0, 0, 255), 1) debug_contours_path = os.path.join(os.path.dirname(__file__), "debug_contours.jpg") cv2.imencode('.jpg', debug_contours_img)[1].tofile(debug_contours_path) print(f"Saved all contours to {debug_contours_path}") min_card_width = int(w * 0.8) print(f"Min card width (strict): {min_card_width}") for i, cnt in enumerate(contours): x, y, cw, ch = cv2.boundingRect(cnt) if cw > 100 and ch > 50: # Only print somewhat large ones print(f"Contour {i}: pos=({x},{y}), size={cw}x{ch}, aspect_ratio={cw/ch:.2f}") # Run actual detection try: cards = detect_cards_cv(image_path) print(f"Detected {len(cards)} cards") except Exception as e: print(f"Error during detection: {e}") import traceback traceback.print_exc() return # Read image for drawing img_lv = read_image(image_path) img_flag = read_image(image_path) if img_lv is None: print("Failed to read image") return for i, card in enumerate(cards): x1, y1, x2, y2 = card print(f"Card {i+1}: [{x1}, {y1}, {x2}, {y2}]") # Draw green rectangle on img_lv cv2.rectangle(img_lv, (x1, y1), (x2, y2), (0, 255, 0), 2) cv2.putText(img_lv, f"Card {i+1}", (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2) # Calculate click point (center) center_x = (x1 + x2) // 2 center_y = (y1 + y2) // 2 # Draw red click point on img_flag cv2.circle(img_flag, (center_x, center_y), 5, (0, 0, 255), -1) # Also draw a crosshair for better visibility cv2.line(img_flag, (center_x - 10, center_y), (center_x + 10, center_y), (0, 0, 255), 1) cv2.line(img_flag, (center_x, center_y - 10), (center_x, center_y + 10), (0, 0, 255), 1) cv2.putText(img_flag, f"Click {i+1}", (center_x + 10, center_y), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 1) print(f"Click Point {i+1}: ({center_x}, {center_y})") # Save _lv.jpg base_name = os.path.splitext(os.path.basename(image_path))[0] output_lv = os.path.join(os.path.dirname(__file__), f"{base_name}_lv.jpg") cv2.imencode('.jpg', img_lv)[1].tofile(output_lv) print(f"Green box image saved to {output_lv}") # Save _flag.jpg output_flag = os.path.join(os.path.dirname(__file__), f"{base_name}_flag.jpg") cv2.imencode('.jpg', img_flag)[1].tofile(output_flag) print(f"Click point image saved to {output_flag}") if __name__ == "__main__": test_detection()