refactor: 优化代码格式,提升可读性
This commit is contained in:
1 parent
fb0c07c91a
commit
6ecd9fa1ae
7 files changed
+125
-61
No files matched your search
@@ -12,7 +12,11 @@ sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
||||
|
||||
from douyin.detection.douyin_pixel import detect_redpacket_popup_background
|
||||
from douyin.other.douyin_popups import check_shopping_redpacket_popup
|
||||
from douyin.fudai.core.coords import SHOPPING_REDPACKET_AREA, STAGE_RED_PACKET_OCR, DIAG_SHOPPING_REDPACKET
|
||||
from douyin.fudai.core.coords import (
|
||||
SHOPPING_REDPACKET_AREA,
|
||||
STAGE_RED_PACKET_OCR,
|
||||
DIAG_SHOPPING_REDPACKET,
|
||||
)
|
||||
|
||||
|
||||
def test_pixel_detection():
|
||||
@@ -20,12 +24,12 @@ def test_pixel_detection():
|
||||
print("=" * 60)
|
||||
print("测试1: 像素检测函数")
|
||||
print("=" * 60)
|
||||
|
||||
|
||||
# 创建测试图像:模拟购物红包(红色背景)
|
||||
width, height = 1080, 2400
|
||||
test_img = Image.new('RGB', (width, height), color=(255, 255, 255))
|
||||
test_img = Image.new("RGB", (width, height), color=(255, 255, 255))
|
||||
pixels = test_img.load()
|
||||
|
||||
|
||||
# 在中央区域添加红色像素(模拟购物红包)
|
||||
red_color = (230, 80, 80) # 红色
|
||||
for y in range(400, 1200):
|
||||
@@ -33,12 +37,12 @@ def test_pixel_detection():
|
||||
# 80%的像素为红色,20%为其他颜色
|
||||
if (x + y) % 5 != 0:
|
||||
pixels[x, y] = red_color
|
||||
|
||||
|
||||
result = detect_redpacket_popup_background(test_img)
|
||||
print(f"红色像素检测结果: {result}")
|
||||
print(f"预期: True (因为有超过15%的红色像素)")
|
||||
print()
|
||||
|
||||
|
||||
return result
|
||||
|
||||
|
||||
@@ -47,23 +51,23 @@ def test_coordinates():
|
||||
print("=" * 60)
|
||||
print("测试2: 坐标定义")
|
||||
print("=" * 60)
|
||||
|
||||
|
||||
print(f"SHOPPING_REDPACKET_AREA: {SHOPPING_REDPACKET_AREA}")
|
||||
print(f"STAGE_RED_PACKET_OCR: {STAGE_RED_PACKET_OCR}")
|
||||
print(f"DIAG_SHOPPING_REDPACKET: {DIAG_SHOPPING_REDPACKET}")
|
||||
|
||||
|
||||
# 验证坐标范围
|
||||
x1, y1, x2, y2 = DIAG_SHOPPING_REDPACKET
|
||||
print(f"\n诊断区域范围:")
|
||||
print(f" X: {x1} - {x2} (宽度: {x2-x1})")
|
||||
print(f" Y: {y1} - {y2} (高度: {y2-y1})")
|
||||
|
||||
|
||||
# 检查坐标是否有效
|
||||
if x1 < x2 and y1 < y2:
|
||||
print("✓ 坐标范围有效")
|
||||
else:
|
||||
print("✗ 坐标范围无效")
|
||||
|
||||
|
||||
print()
|
||||
|
||||
|
||||
@@ -72,7 +76,7 @@ def test_ocr_keywords():
|
||||
print("=" * 60)
|
||||
print("测试3: OCR关键词检查")
|
||||
print("=" * 60)
|
||||
|
||||
|
||||
# 测试文本
|
||||
test_texts = [
|
||||
"邀你搶購物紅包",
|
||||
@@ -88,25 +92,34 @@ def test_ocr_keywords():
|
||||
"最高领取",
|
||||
"抢",
|
||||
]
|
||||
|
||||
|
||||
# 关键词列表(与douyin_popups.py中的保持一致)
|
||||
keywords = [
|
||||
'邀你搶購物紅包', '搶購物紅包', '購物紅包', '邀你搶',
|
||||
'搶紅包', '搶', '最高領取',
|
||||
'邀你抢', '购物红包', '抢红包', '抢', '最高领取'
|
||||
"邀你搶購物紅包",
|
||||
"搶購物紅包",
|
||||
"購物紅包",
|
||||
"邀你搶",
|
||||
"搶紅包",
|
||||
"搶",
|
||||
"最高領取",
|
||||
"邀你抢",
|
||||
"购物红包",
|
||||
"抢红包",
|
||||
"抢",
|
||||
"最高领取",
|
||||
]
|
||||
|
||||
|
||||
print(f"定义的关键词数量: {len(keywords)}")
|
||||
print("关键词列表:")
|
||||
for kw in keywords:
|
||||
print(f" - {kw}")
|
||||
|
||||
|
||||
print("\n测试文本匹配:")
|
||||
for text in test_texts:
|
||||
matched = any(kw in text for kw in keywords)
|
||||
status = "✓" if matched else "✗"
|
||||
print(f" {status} '{text}'")
|
||||
|
||||
|
||||
print()
|
||||
|
||||
|
||||
@@ -115,27 +128,27 @@ def create_redpacket_test_image():
|
||||
print("=" * 60)
|
||||
print("创建测试图像")
|
||||
print("=" * 60)
|
||||
|
||||
|
||||
# 创建1080x2400的图像
|
||||
img = Image.new('RGB', (1080, 2400), color=(255, 255, 255))
|
||||
img = Image.new("RGB", (1080, 2400), color=(255, 255, 255))
|
||||
pixels = img.load()
|
||||
|
||||
|
||||
# 在中央画一个红色矩形(模拟购物红包)
|
||||
red_color = (230, 80, 80)
|
||||
for y in range(400, 1400):
|
||||
for x in range(300, 780):
|
||||
pixels[x, y] = red_color
|
||||
|
||||
|
||||
# 保存图像
|
||||
test_img_path = os.path.join(os.path.dirname(__file__), 'test_redpacket.png')
|
||||
test_img_path = os.path.join(os.path.dirname(__file__), "test_redpacket.png")
|
||||
img.save(test_img_path)
|
||||
print(f"✓ 测试图像已保存: {test_img_path}")
|
||||
print()
|
||||
|
||||
|
||||
return test_img_path
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
if __name__ == "__main__":
|
||||
print("\n")
|
||||
print("╔" + "=" * 58 + "╗")
|
||||
print("║" + " " * 58 + "║")
|
||||
@@ -143,13 +156,13 @@ if __name__ == '__main__':
|
||||
print("║" + " " * 58 + "║")
|
||||
print("╚" + "=" * 58 + "╝")
|
||||
print()
|
||||
|
||||
|
||||
# 运行所有测试
|
||||
test_coordinates()
|
||||
test_ocr_keywords()
|
||||
result = test_pixel_detection()
|
||||
test_img_path = create_redpacket_test_image()
|
||||
|
||||
|
||||
# 总结
|
||||
print("=" * 60)
|
||||
print("测试总结")
|
||||
|
||||
Reference in new issue
Block a user