This commit is contained in:
HuangHai
2026-01-12 15:22:50 +08:00
parent cc5547f37c
commit 2339558596
10 changed files with 468 additions and 6 deletions

View File

@@ -4,6 +4,7 @@ import cv2
import numpy as np
import time
import json
import hashlib
from Apps.AiTeJiYiChong.Config.Setting import BOTTOM_SAFE_EXCLUDE_RATIO
from Config.Config import TEMP_IMAGE_DIR
@@ -11,6 +12,17 @@ logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(name)s - %(level
logger = logging.getLogger(__name__)
def get_file_md5(file_path):
"""计算文件的 MD5 值"""
if not os.path.exists(file_path):
return None
hash_md5 = hashlib.md5()
with open(file_path, "rb") as f:
for chunk in iter(lambda: f.read(4096), b""):
hash_md5.update(chunk)
return hash_md5.hexdigest()
def read_image(path):
"""读取图片,支持中文路径"""
try:
@@ -105,6 +117,40 @@ def click_image_template(d, template_path, timeout=5.0, threshold=0.8):
return False
def find_template_coords(img_path, template_path, threshold=0.8):
"""
在图片中查找模板并返回中心坐标
"""
if not os.path.exists(img_path) or not os.path.exists(template_path):
return None
img = read_image(img_path)
template = read_image(template_path)
if img is None or template is None:
return None
t_h, t_w = template.shape[:2]
# 多尺度匹配
best_match = None
for scale in np.linspace(0.8, 1.2, 5):
resized = cv2.resize(template, (int(t_w * scale), int(t_h * scale)))
if resized.shape[0] > img.shape[0] or resized.shape[1] > img.shape[1]:
continue
res = cv2.matchTemplate(img, resized, cv2.TM_CCOEFF_NORMED)
_, max_val, _, max_loc = cv2.minMaxLoc(res)
if best_match is None or max_val > best_match[0]:
best_match = (max_val, max_loc, resized.shape[1], resized.shape[0])
if best_match and best_match[0] >= threshold:
max_val, max_loc, r_w, r_h = best_match
center_x = max_loc[0] + r_w // 2
center_y = max_loc[1] + r_h // 2
return (center_x, center_y, max_val)
return None
def crop_cards_from_image(img_path, output_dir=None, save_debug=True):
"""
从图片中裁剪场站卡片并生成 _flag.jpg 和 _vl.jpg