This commit is contained in:
HuangHai
2026-01-14 13:14:05 +08:00
parent 11e1091c6a
commit 9e610b1906
6 changed files with 300 additions and 120 deletions

View File

@@ -93,6 +93,48 @@ 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 颜色过滤定位详情页的橘红色价格区域,返回最左侧区域的中心点击点
@@ -162,7 +204,28 @@ def detect_cards_cv(image_path, top_ratio=0.40, bottom_ratio=0.12):
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)
@@ -205,7 +268,7 @@ def detect_cards_cv(image_path, top_ratio=0.40, bottom_ratio=0.12):
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 y + ch < bottom_limit
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
@@ -222,24 +285,43 @@ def detect_cards_cv(image_path, top_ratio=0.40, bottom_ratio=0.12):
# 按 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: # 增加去重间距
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)
])
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