This commit is contained in:
HuangHai
2026-01-14 10:50:11 +08:00
parent bf553ce232
commit 7549a5f038
6 changed files with 59 additions and 16 deletions

View File

@@ -151,11 +151,11 @@ def detect_price_click_point_cv(image_path):
return [center_x, center_y]
def detect_cards_cv(image_path, top_ratio=0.40, bottom_ratio=0.12):
"""
使用计算机图形学 (OpenCV) 检测列表中的场站卡片。
"""
from Apps.TeLaiDian.Config.Setting import MIN_CARD_HEIGHT
def detect_cards_cv(image_path, top_ratio=0.40, bottom_ratio=0.12):
"""
使用计算机图形学 (OpenCV) 检测列表中的场站卡片。
"""
from Apps.TeLaiDian.Config.Setting import MIN_CARD_HEIGHT
img = read_image(image_path)
if img is None:
@@ -226,14 +226,46 @@ def detect_cards_cv(image_path, top_ratio=0.40, bottom_ratio=0.12):
min(w, x + cw - padding),
min(bottom_limit, y + ch - padding)
])
return cards
def draw_rectangles(image_path, bboxes=None, click_points=None):
"""
使用 OpenCV 在图片上绘制矩形框和点击点,生成 _vl.jpg 和 _flag.jpg
- _vl.jpg: 仅包含矩形框,供视觉模型参考
- _flag.jpg: 包含矩形框和点击点,供人工调试
return cards
def detect_wide_rounded_card_cv(image_path, min_width_ratio=0.8, min_y_ratio=0.5):
img = read_image(image_path)
if img is None:
return []
h, w = img.shape[:2]
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray, (7, 7), 0)
edges = cv2.Canny(blur, 60, 180)
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (15, 15))
closed = cv2.morphologyEx(edges, cv2.MORPH_CLOSE, kernel)
contours, _ = cv2.findContours(closed, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
results = []
for cnt in contours:
x, y, cw, ch = cv2.boundingRect(cnt)
center_y = y + ch // 2
if cw < int(w * min_width_ratio):
continue
if center_y < int(h * min_y_ratio):
continue
rect_area = cw * ch
cnt_area = cv2.contourArea(cnt)
if rect_area <= 0:
continue
extent = cnt_area / rect_area
approx = cv2.approxPolyDP(cnt, 0.02 * cv2.arcLength(cnt, True), True)
if extent < 0.7:
continue
if len(approx) >= 6 or (len(approx) == 4 and extent > 0.85):
results.append([x, y, x + cw, y + ch])
results.sort(key=lambda b: b[1])
return results
def draw_rectangles(image_path, bboxes=None, click_points=None):
"""
使用 OpenCV 在图片上绘制矩形框和点击点,生成 _vl.jpg 和 _flag.jpg
- _vl.jpg: 仅包含矩形框,供视觉模型参考
- _flag.jpg: 包含矩形框和点击点,供人工调试
"""
try:
DEBUG_BOX_COLOR = (0, 255, 0) # 绿色矩形