This commit is contained in:
HuangHai
2026-01-15 09:51:16 +08:00
parent fe27bc6884
commit 98e890a693
8 changed files with 3 additions and 449 deletions

View File

@@ -93,270 +93,6 @@ def save_image(path, img):
logger.error(f"Error saving image {path}: {e}")
return False
def detect_list_price_blocks_cv(image_path, top_ratio=0.40, bottom_ratio=0.12):
img = read_image(image_path)
if img is None:
return []
h, w = img.shape[:2]
top_limit = int(h * top_ratio)
bottom_limit = int(h * (1 - bottom_ratio))
roi_bgr = img[top_limit:bottom_limit, :, :]
if roi_bgr.size == 0:
return []
b, g, r = cv2.split(roi_bgr)
mask_bgr = (r > 190) & (g > 80) & (r - g > 25) & (r - b > 25)
mask_bgr = (mask_bgr.astype(np.uint8)) * 255
if cv2.countNonZero(mask_bgr) < 50:
hsv = cv2.cvtColor(roi_bgr, cv2.COLOR_BGR2HSV)
lower_red1 = np.array([0, 100, 120])
upper_red1 = np.array([20, 255, 255])
lower_red2 = np.array([160, 100, 120])
upper_red2 = np.array([180, 255, 255])
mask1 = cv2.inRange(hsv, lower_red1, upper_red1)
mask2 = cv2.inRange(hsv, lower_red2, upper_red2)
mask = cv2.bitwise_or(mask1, mask2)
else:
mask = mask_bgr
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (30, 10))
closed = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, kernel)
contours, _ = cv2.findContours(closed, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
blocks = []
for cnt in contours:
x, y, cw, ch = cv2.boundingRect(cnt)
y_global = y + top_limit
if ch < 30 or ch > 140:
continue
if cw < 80 or cw > int(w * 0.8):
continue
center_y = y_global + ch // 2
if center_y < int(h * 0.6) or center_y > bottom_limit:
continue
blocks.append([x, y_global, x + cw, y_global + ch])
blocks.sort(key=lambda b: b[1])
return blocks
def detect_price_click_point_cv(image_path):
"""
使用 HSV 颜色过滤定位详情页的橘红色价格区域,返回最左侧区域的中心点击点
"""
img = read_image(image_path)
if img is None:
return None
h, w = img.shape[:2]
# 1. 转换为 HSV 空间
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
# 2. 橘红色的 HSV 范围 (适配特来电价格颜色)
lower_orange = np.array([0, 150, 150])
upper_orange = np.array([20, 255, 255])
mask = cv2.inRange(hsv, lower_orange, upper_orange)
# 3. 对掩码进行膨胀,连接数字
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (50, 20))
dilated = cv2.dilate(mask, kernel)
# 4. 寻找轮廓
contours, _ = cv2.findContours(dilated, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
detected_areas = []
for cnt in contours:
x, y, cw, ch = cv2.boundingRect(cnt)
# 1. 过滤掉宽度过大(可能是横幅广告)或过小(可能是杂点)的区域
# 2. 价格区域 P0 通常在屏幕的中部,且宽度约为屏幕的一半
if 200 < y < h * 0.8 and 100 < cw < w * 0.6 and ch > 30:
detected_areas.append([x, y, x + cw, y + ch])
if not detected_areas:
# 备选:如果 HSV 失败,尝试通过轮廓大小寻找
# 1.1556 这种大数字通常会有很明显的轮廓
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
thresh = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV, 11, 2)
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (30, 10))
closed = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, kernel)
contours, _ = cv2.findContours(closed, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
for cnt in contours:
x, y, cw, ch = cv2.boundingRect(cnt)
if 200 < y < h * 0.6 and 150 < cw < 300 and 50 < ch < 150:
detected_areas.append([x, y, x + cw, y + ch])
if not detected_areas:
return None
# 5. 按 X 轴排序,取最左边的区域 (即用户确认的 P0)
# 但要排除掉可能在最左侧的导航栏返回按钮等小元素,所以前面加了宽度限制
detected_areas.sort(key=lambda b: b[0])
target = detected_areas[0]
center_x = (target[0] + target[2]) // 2
center_y = (target[1] + target[3]) // 2
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
img = read_image(image_path)
if img is None:
return []
h, w = img.shape[:2]
price_blocks = detect_list_price_blocks_cv(image_path, top_ratio=top_ratio, bottom_ratio=bottom_ratio)
cards = []
if price_blocks:
from Apps.TeLaiDian.Config.Setting import SAFE_EXCLUDE_RATIO, BOTTOM_SAFE_EXCLUDE_RATIO
top_limit = int(h * SAFE_EXCLUDE_RATIO)
bottom_limit = int(h * (1 - BOTTOM_SAFE_EXCLUDE_RATIO))
for b in price_blocks:
px1, py1, px2, py2 = b
center_y = (py1 + py2) // 2
card_height = MIN_CARD_HEIGHT
card_top = max(top_limit, center_y - int(card_height * 0.7))
card_bottom = card_top + card_height
if card_bottom > bottom_limit:
card_bottom = bottom_limit
card_top = max(top_limit, card_bottom - card_height)
card_left = 0
card_right = w
cards.append([card_left, card_top, card_right, card_bottom])
cards.sort(key=lambda b: b[1])
return cards
# 转换为灰度图
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# 限制检测范围
top_limit = int(h * top_ratio)
bottom_limit = int(h * (1 - bottom_ratio))
logger.info(f"CV检测卡片: h={h}, w={w}, top_limit={top_limit}, bottom_limit={bottom_limit}")
# 使用自适应阈值
thresh = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV, 11, 2)
# 闭运算:连接断开的边缘
# 减小水平核宽度,增加一点垂直连接,以适应卡片边缘不连续的情况
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (w // 4, 3))
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.8)
temp_boxes = []
for cnt in contours:
x, y, cw, ch = cv2.boundingRect(cnt)
center_y = y + ch // 2
# 绿色营销券区域排除
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
roi = hsv[max(0,y):min(h,y+ch), max(0,x):min(w,x+cw)]
green_mask = cv2.inRange(roi, np.array([35, 80, 80]), np.array([85, 255, 255]))
green_ratio = float(cv2.countNonZero(green_mask)) / (roi.shape[0]*roi.shape[1]) if roi.size > 0 else 0.0
# 圆角矩形判断:面积占比与顶点数量
rect_area = cw * ch
cnt_area = cv2.contourArea(cnt)
extent = cnt_area / rect_area if rect_area > 0 else 0
approx = cv2.approxPolyDP(cnt, 0.02 * cv2.arcLength(cnt, True), True)
ok_width = cw >= min_card_width
ok_height = ch > MIN_CARD_HEIGHT * 0.8
ok_vertical = center_y >= int(h * 0.58) and y > top_limit and center_y < bottom_limit
ok_shape = (len(approx) >= 6 or extent > 0.85)
ok_color = green_ratio < 0.25
if ok_width and ok_height and ok_vertical and ok_shape and ok_color:
logger.info(f"✅ 找到候选卡片: y={y}, h={ch}, w={cw}, extent={extent:.2f}, verts={len(approx)}, green={green_ratio:.2f}")
temp_boxes.append((x, y, cw, ch))
else:
if ok_width:
logger.debug(f"❌ 排除候选: y={y}, h={ch}, w={cw}, extent={extent:.2f}, verts={len(approx)}, green={green_ratio:.2f} (limit: {top_limit}-{bottom_limit})")
if not temp_boxes:
logger.warning(f"⚠️ CV 未能在指定范围 ({top_limit}-{bottom_limit}) 内检测到任何场站卡片")
# 按 Y 轴排序
temp_boxes.sort(key=lambda b: b[1])
# 再次过滤和去重,并根据高度拆分为多张卡片
for i, box in enumerate(temp_boxes):
x, y, cw, ch = box
# 检查是否与已有的框重叠(按顶部 y 去重)
is_duplicate = False
for v in cards:
if abs(y - v[1]) < 100:
is_duplicate = True
break
if is_duplicate:
continue
padding = 2
x1 = max(0, x + padding)
x2 = min(w, x + cw - padding)
y1 = max(top_limit, y + padding)
y2 = min(bottom_limit, y + ch - padding)
if y2 <= y1:
continue
effective_h = y2 - y1
estimated_count = max(1, int(round(effective_h / float(MIN_CARD_HEIGHT))))
if estimated_count <= 1:
cards.append([x1, y1, x2, y2])
else:
seg_h = effective_h / float(estimated_count)
for k in range(estimated_count):
sy1 = int(round(y1 + k * seg_h))
sy2 = int(round(y1 + (k + 1) * seg_h))
if sy2 <= sy1:
continue
if sy2 - sy1 < MIN_CARD_HEIGHT * 0.6:
continue
cards.append([x1, sy1, x2, sy2])
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