import os import sys from pprint import pprint # Ensure project root is on sys.path ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), "..")) if ROOT not in sys.path: sys.path.insert(0, ROOT) from path_config import ( get_screenshot_path, get_like_template_path, get_fav_template_path, ) try: from douyin.core.douyin_fudai import fudai_analyse except Exception as e: print("Failed to import fudai_analyse:", e) sys.exit(2) def test_detection_for_polygon(poly, tpl_path, label): a = fudai_analyse() # ensure template path if not tpl_path or not os.path.exists(tpl_path): print(f"Template for {label} not found: {tpl_path}") return # compute bbox xs = [int(p[0]) for p in poly] ys = [int(p[1]) for p in poly] x1, x2 = min(xs), max(xs) y1, y2 = min(ys), max(ys) cx = (x1 + x2) // 2 cy = (y1 + y2) // 2 size = max(1, max(x2 - x1, y2 - y1)) print(f"{label} bbox: ({x1},{y1})-({x2},{y2}), center=({cx},{cy}), size={size}") sx, sy = a._scale_from_base(int(cx), int(cy)) print("scaled center:", sx, sy) # compute pixel ROI size similar to yanghao logic try: scale_x = float(getattr(a, "resolution_ratio_x", 1080)) / 1080.0 scale_y = float(getattr(a, "resolution_ratio_y", 2400)) / 2400.0 except Exception: scale_x = scale_y = 1.0 try: if label == "like": action_scale = float(getattr(a, "yanghao_like_roi_scale", 1.5)) else: action_scale = float(getattr(a, "yanghao_fav_roi_scale", 1.5)) except Exception: action_scale = 1.5 size_px = max(1, int(size * max(scale_x, scale_y) * float(action_scale))) print( f"computed size_px={size_px} (base={size} scale_x={scale_x:.3f} scale_y={scale_y:.3f} action_scale={action_scale})" ) roi = a._capture_roi_screenshot(f"test_{label}", sx, sy, int(size_px)) print("roi saved to:", roi) if not roi or not os.path.exists(roi): print("ROI not available, abort") return detected, score = a._match_icon_template(roi, tpl_path) print(f"Result for {label}: detected={detected}, score={score}") def main(): ss = get_screenshot_path() print("Using screenshot:", ss) if not os.path.exists(ss): print( "Screenshot not found at pic/screenshot.png; place a device screenshot there and retry." ) like_tpl = get_like_template_path() fav_tpl = get_fav_template_path() # polygon from config like_poly = [[927, 1161], [1039, 1164], [1049, 1265], [940, 1263]] fav_poly = [ [938, 1601], [972, 1597], [987, 1566], [1006, 1596], [1037, 1604], [1015, 1628], [1019, 1660], [989, 1648], [959, 1661], [963, 1628], ] print("\nTesting like polygon:") test_detection_for_polygon(like_poly, like_tpl, "like") print("\nTesting fav polygon:") test_detection_for_polygon(fav_poly, fav_tpl, "fav") if __name__ == "__main__": main()