'commit'
This commit is contained in:
@@ -15,5 +15,6 @@ WAIT_BACK_TO_LIST = 1.0
|
|||||||
WAIT_AFTER_SCROLL = 2.5
|
WAIT_AFTER_SCROLL = 2.5
|
||||||
|
|
||||||
# 坐标计算与安全防护
|
# 坐标计算与安全防护
|
||||||
SAFE_EXCLUDE_RATIO = 0.15
|
SAFE_EXCLUDE_RATIO = 0.35 # 调整顶部排除比例,避开搜索、活动 Banner、快捷入口和广告位
|
||||||
BOTTOM_SAFE_EXCLUDE_RATIO = 0.10
|
BOTTOM_SAFE_EXCLUDE_RATIO = 0.12 # 略微增加底部排除比例,避开底部导航栏
|
||||||
|
MIN_CARD_HEIGHT = 150 # 场站卡片最小高度,防止识别不完整的卡片
|
||||||
|
|||||||
@@ -93,56 +93,72 @@ def save_image(path, img):
|
|||||||
logger.error(f"Error saving image {path}: {e}")
|
logger.error(f"Error saving image {path}: {e}")
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def detect_cards_cv(image_path, top_ratio=0.15, bottom_ratio=0.1):
|
def detect_cards_cv(image_path, top_ratio=0.40, bottom_ratio=0.12):
|
||||||
"""
|
"""
|
||||||
使用计算机图形学 (OpenCV) 检测列表中的场站卡片
|
使用计算机图形学 (OpenCV) 检测列表中的场站卡片。
|
||||||
"""
|
"""
|
||||||
|
from Apps.TeLaiDian.Config.Setting import MIN_CARD_HEIGHT
|
||||||
|
|
||||||
img = read_image(image_path)
|
img = read_image(image_path)
|
||||||
if img is None:
|
if img is None:
|
||||||
return []
|
return []
|
||||||
|
|
||||||
h, w = img.shape[:2]
|
h, w = img.shape[:2]
|
||||||
|
|
||||||
|
# 转换为灰度图
|
||||||
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
|
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
|
||||||
blurred = cv2.GaussianBlur(gray, (5, 5), 0)
|
|
||||||
edges = cv2.Canny(blurred, 10, 50)
|
|
||||||
|
|
||||||
contours, _ = cv2.findContours(edges, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
|
# 限制检测范围
|
||||||
|
|
||||||
cards = []
|
|
||||||
min_card_width = int(w * 0.8)
|
|
||||||
min_card_height = 150
|
|
||||||
top_limit = int(h * top_ratio)
|
top_limit = int(h * top_ratio)
|
||||||
bottom_limit = int(h * (1 - bottom_ratio))
|
bottom_limit = int(h * (1 - bottom_ratio))
|
||||||
|
|
||||||
valid_contours = []
|
# 使用自适应阈值
|
||||||
|
thresh = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV, 11, 2)
|
||||||
|
|
||||||
|
# 闭运算:连接断开的边缘
|
||||||
|
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (w // 2, 1))
|
||||||
|
closed = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, kernel)
|
||||||
|
|
||||||
|
# 寻找轮廓
|
||||||
|
contours, _ = cv2.findContours(closed, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
|
||||||
|
|
||||||
|
cards = []
|
||||||
|
min_card_width = int(w * 0.7)
|
||||||
|
|
||||||
|
temp_boxes = []
|
||||||
for cnt in contours:
|
for cnt in contours:
|
||||||
x, y, cw, ch = cv2.boundingRect(cnt)
|
x, y, cw, ch = cv2.boundingRect(cnt)
|
||||||
|
|
||||||
|
# 增加最小高度验证 MIN_CARD_HEIGHT
|
||||||
if (cw > min_card_width and
|
if (cw > min_card_width and
|
||||||
ch > min_card_height and
|
ch > MIN_CARD_HEIGHT and
|
||||||
y > top_limit and
|
y > top_limit and
|
||||||
y + ch < bottom_limit):
|
y + ch < bottom_limit):
|
||||||
|
|
||||||
center_y = y + ch // 2
|
temp_boxes.append((x, y, cw, ch))
|
||||||
is_duplicate = False
|
|
||||||
for vx, vy, vcw, vch in valid_contours:
|
|
||||||
v_center_y = vy + vch // 2
|
|
||||||
if abs(center_y - v_center_y) < 50:
|
|
||||||
is_duplicate = True
|
|
||||||
break
|
|
||||||
|
|
||||||
if not is_duplicate:
|
# 按 Y 轴排序
|
||||||
valid_contours.append((x, y, cw, ch))
|
temp_boxes.sort(key=lambda b: b[1])
|
||||||
padding = 5
|
|
||||||
cards.append([
|
# 再次过滤和去重
|
||||||
max(0, x + padding),
|
for i, box in enumerate(temp_boxes):
|
||||||
max(0, y + padding),
|
x, y, cw, ch = box
|
||||||
max(0, x + cw - padding),
|
|
||||||
max(0, y + ch - padding)
|
# 检查是否与已有的框重叠
|
||||||
])
|
is_duplicate = False
|
||||||
|
for v in cards:
|
||||||
|
if abs(y - v[1]) < 100: # 增加去重间距
|
||||||
|
is_duplicate = True
|
||||||
|
break
|
||||||
|
if not is_duplicate:
|
||||||
|
padding = 2
|
||||||
|
cards.append([
|
||||||
|
max(0, x + padding),
|
||||||
|
max(top_limit, y + padding),
|
||||||
|
min(w, x + cw - padding),
|
||||||
|
min(bottom_limit, y + ch - padding)
|
||||||
|
])
|
||||||
|
|
||||||
cards.sort(key=lambda c: c[1])
|
|
||||||
return cards
|
return cards
|
||||||
|
|
||||||
def draw_rectangles(image_path, points, output_path=None):
|
def draw_rectangles(image_path, points, output_path=None):
|
||||||
|
|||||||
63
Apps/TeLaiDian/TestCV.py
Normal file
63
Apps/TeLaiDian/TestCV.py
Normal file
@@ -0,0 +1,63 @@
|
|||||||
|
# coding=utf-8
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
|
import cv2
|
||||||
|
import numpy as np
|
||||||
|
|
||||||
|
# 确保项目根目录在 sys.path 中
|
||||||
|
project_root = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||||||
|
if project_root not in sys.path:
|
||||||
|
sys.path.append(project_root)
|
||||||
|
|
||||||
|
from Apps.TeLaiDian import Kit
|
||||||
|
from Apps.TeLaiDian.Config.Setting import SAFE_EXCLUDE_RATIO, BOTTOM_SAFE_EXCLUDE_RATIO
|
||||||
|
|
||||||
|
def test_cv_detection(image_path):
|
||||||
|
print(f"开始测试 CV 检测: {image_path}")
|
||||||
|
|
||||||
|
if not os.path.exists(image_path):
|
||||||
|
print(f"错误: 文件不存在 {image_path}")
|
||||||
|
return
|
||||||
|
|
||||||
|
img = Kit.read_image(image_path)
|
||||||
|
h, w = img.shape[:2]
|
||||||
|
print(f"图片尺寸: {w}x{h}")
|
||||||
|
print(f"当前过滤阈值: top_limit={int(h*SAFE_EXCLUDE_RATIO)}, bottom_limit={int(h*(1-BOTTOM_SAFE_EXCLUDE_RATIO))}")
|
||||||
|
|
||||||
|
# 1. 检测卡片
|
||||||
|
bboxes = Kit.detect_cards_cv(image_path, top_ratio=SAFE_EXCLUDE_RATIO, bottom_ratio=BOTTOM_SAFE_EXCLUDE_RATIO)
|
||||||
|
print(f"检测到 {len(bboxes)} 个卡片区域")
|
||||||
|
for i, box in enumerate(bboxes):
|
||||||
|
print(f" 卡片 {i+1}: {box}")
|
||||||
|
|
||||||
|
# 2. 生成 _vl.jpg (仅绿框)
|
||||||
|
vl_path = image_path.replace(".jpg", "_vl.jpg")
|
||||||
|
img_vl = Kit.read_image(image_path)
|
||||||
|
for box in bboxes:
|
||||||
|
cv2.rectangle(img_vl, (box[0], box[1]), (box[2], box[3]), (0, 255, 0), 3)
|
||||||
|
Kit.save_image(vl_path, img_vl)
|
||||||
|
print(f"已生成 VLM 标注图: {vl_path}")
|
||||||
|
|
||||||
|
# 3. 生成 _flag.jpg (绿框 + 点击点)
|
||||||
|
flag_path = image_path.replace(".jpg", "_flag.jpg")
|
||||||
|
img_flag = img_vl.copy()
|
||||||
|
for box in bboxes:
|
||||||
|
# 计算点击点:卡片中心
|
||||||
|
center_x = (box[0] + box[2]) // 2
|
||||||
|
center_y = (box[1] + box[3]) // 2
|
||||||
|
|
||||||
|
# 绘制红色点击点 (实心圆 + 十字)
|
||||||
|
cv2.circle(img_flag, (center_x, center_y), 15, (0, 0, 255), -1)
|
||||||
|
cv2.line(img_flag, (center_x - 30, center_y), (center_x + 30, center_y), (255, 255, 255), 3)
|
||||||
|
cv2.line(img_flag, (center_x, center_y - 30), (center_x, center_y + 30), (255, 255, 255), 3)
|
||||||
|
|
||||||
|
# 标注序号
|
||||||
|
idx = bboxes.index(box) + 1
|
||||||
|
cv2.putText(img_flag, str(idx), (box[0] + 10, box[1] + 40), cv2.FONT_HERSHEY_SIMPLEX, 1.5, (0, 0, 255), 3)
|
||||||
|
|
||||||
|
Kit.save_image(flag_path, img_flag)
|
||||||
|
print(f"已生成人工核对图: {flag_path}")
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
target_image = r"d:\dsWork\aiData\Output\tld_list_1768347471.jpg"
|
||||||
|
test_cv_detection(target_image)
|
||||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -1 +0,0 @@
|
|||||||
D:\anaconda3\envs\py310\python.exe
|
|
||||||
Reference in New Issue
Block a user