- Removed obsolete command-line test parameters from `run_cli.py` to streamline functionality. - Introduced a new `ocr_preprocess.py` module for unified OCR image preprocessing, enhancing recognition stability. - Updated various components to utilize the new preprocessing function, improving code organization and readability. - Adjusted configuration values in `config.json` for better performance and consistency. - Added comprehensive technical documentation for the Douyin project, detailing code modification standards and operational guidelines. - Cleaned up excessive blank lines across multiple files to enhance overall readability.
147 lines
4.5 KiB
Python
147 lines
4.5 KiB
Python
#!/usr/bin/env python3
|
|
# -*- coding: utf-8 -*-
|
|
"""
|
|
在两个指定小区域内统计白色像素比例,用于辅助判断右上角是否有关闭 X 图标。
|
|
|
|
用法示例:
|
|
python tools/test_white_regions_x.py pic/screenshot_recommend.png pic/screenshot_live.png
|
|
|
|
思路:
|
|
- 假设原始截图分辨率约为 1080x2400(宽x高),并使用与代码中像素检测相同的坐标系。
|
|
- X 图标所在的大 ROI 为 (972,147)-(1047,216)
|
|
- 你在 pick_coords 中点出的两个白色点大致为:
|
|
P1 = (999,174)
|
|
P2 = (1025,201)
|
|
- 本脚本在这两个点附近取一个小方框(默认 7x7),统计白色像素比例。
|
|
"""
|
|
|
|
import os
|
|
import sys
|
|
from typing import Tuple, List
|
|
|
|
import cv2
|
|
import numpy as np
|
|
|
|
# 修正 Windows 控制台编码问题
|
|
if sys.platform == "win32":
|
|
try:
|
|
sys.stdout.reconfigure(encoding="utf-8")
|
|
except Exception:
|
|
pass
|
|
|
|
|
|
def load_image(path: str) -> np.ndarray:
|
|
"""加载图片(支持中文路径),返回 BGR 图像。"""
|
|
if not os.path.exists(path):
|
|
raise FileNotFoundError(path)
|
|
data = np.fromfile(path, dtype=np.uint8)
|
|
img = cv2.imdecode(data, cv2.IMREAD_COLOR)
|
|
if img is None:
|
|
raise RuntimeError(f"无法读取图片: {path}")
|
|
return img
|
|
|
|
|
|
def crop_region(
|
|
img: np.ndarray, x1: int, y1: int, x2: int, y2: int
|
|
) -> np.ndarray:
|
|
h, w = img.shape[:2]
|
|
x1 = max(0, min(w - 1, x1))
|
|
x2 = max(0, min(w, x2))
|
|
y1 = max(0, min(h - 1, y1))
|
|
y2 = max(0, min(h, y2))
|
|
if x2 <= x1 or y2 <= y1:
|
|
return img[0:0, 0:0]
|
|
return img[y1:y2, x1:x2]
|
|
|
|
|
|
def count_white_pixels(region: np.ndarray, gray_threshold: int = 245, diff_threshold: int = 10) -> Tuple[int, int, float]:
|
|
"""
|
|
统计区域内“白色像素”数量。
|
|
|
|
- gray_threshold: 灰度阈值(越大越严格,默认 245)
|
|
- diff_threshold: RGB 分量差异阈值(越小越接近纯白,默认 10)
|
|
"""
|
|
if region.size == 0:
|
|
return 0, 0, 0.0
|
|
|
|
b, g, r = cv2.split(region)
|
|
gray = cv2.cvtColor(region, cv2.COLOR_BGR2GRAY)
|
|
|
|
# 灰度足够亮
|
|
bright = gray >= gray_threshold
|
|
# RGB 三通道差异不大(接近无色)
|
|
diff_rg = np.abs(r.astype(np.int16) - g.astype(np.int16)) <= diff_threshold
|
|
diff_rb = np.abs(r.astype(np.int16) - b.astype(np.int16)) <= diff_threshold
|
|
diff_gb = np.abs(g.astype(np.int16) - b.astype(np.int16)) <= diff_threshold
|
|
|
|
white_mask = bright & diff_rg & diff_rb & diff_gb
|
|
white_count = int(white_mask.sum())
|
|
total = int(region.shape[0] * region.shape[1])
|
|
ratio = white_count / total if total > 0 else 0.0
|
|
return white_count, total, ratio
|
|
|
|
|
|
def analyse_image(
|
|
path: str,
|
|
p1: Tuple[int, int],
|
|
p2: Tuple[int, int],
|
|
) -> None:
|
|
"""对单张截图进行分析并打印结果(使用两点组成的对角矩形区域)。"""
|
|
img = load_image(path)
|
|
h, w = img.shape[:2]
|
|
print("=" * 80)
|
|
print(f"图片: {path}")
|
|
print(f"尺寸: {w}x{h}")
|
|
|
|
# 如果不是接近 1080x2400,可以按宽高比例缩放坐标
|
|
base_w, base_h = 1080, 2400
|
|
scale_x = w / base_w
|
|
scale_y = h / base_h
|
|
|
|
def scaled_point(p: Tuple[int, int]) -> Tuple[int, int]:
|
|
return int(round(p[0] * scale_x)), int(round(p[1] * scale_y))
|
|
|
|
sp1 = scaled_point(p1)
|
|
sp2 = scaled_point(p2)
|
|
|
|
# 使用两点组成对角矩形区域
|
|
x1, y1 = sp1
|
|
x2, y2 = sp2
|
|
rx1, ry1 = min(x1, x2), min(y1, y2)
|
|
rx2, ry2 = max(x1, x2), max(y1, y2)
|
|
|
|
region = crop_region(img, rx1, ry1, rx2, ry2)
|
|
|
|
# 为了更接近“纯白”,在这个测试里稍微提高阈值要求
|
|
white_count, total, ratio = count_white_pixels(region, gray_threshold=250, diff_threshold=5)
|
|
|
|
print(f"矩形区域(基于原坐标): {p1} - {p2}")
|
|
print(f"实际像素区域: ({rx1},{ry1})-({rx2},{ry2}), 尺寸={region.shape[1]}x{region.shape[0]}")
|
|
print(f"纯白像素: {white_count}/{total}, 比例={ratio:.4f}")
|
|
|
|
|
|
def main(argv: List[str]) -> int:
|
|
if len(argv) < 2:
|
|
print(
|
|
"用法: python tools/test_white_regions_x.py <图片1> [图片2 ...]\n"
|
|
"建议:分别传入推荐页截图和直播间截图,对比输出的白色像素比例。"
|
|
)
|
|
return 1
|
|
|
|
# 你在 pick_coords 中点出的两个点
|
|
p1 = (999, 174)
|
|
p2 = (1025, 201)
|
|
|
|
for path in argv[1:]:
|
|
try:
|
|
analyse_image(path, p1, p2)
|
|
except Exception as e:
|
|
print(f"分析失败: {path} -> {e}")
|
|
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
raise SystemExit(main(sys.argv))
|
|
|