Files
douyin_guaji-main/test_shopping_redpacket_detection.py
T

180 lines
4.8 KiB
Python

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""购物红包识别功能测试脚本"""
import sys
import os
from PIL import Image
import numpy as np
# 添加项目根目录到路径
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,
)
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))
pixels = test_img.load()
# 在中央区域添加红色像素(模拟购物红包)
red_color = (230, 80, 80) # 红色
for y in range(400, 1200):
for x in range(300, 780):
# 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
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()
def test_ocr_keywords():
"""测试OCR关键词"""
print("=" * 60)
print("测试3: OCR关键词检查")
print("=" * 60)
# 测试文本
test_texts = [
"邀你搶購物紅包",
"搶購物紅包",
"購物紅包",
"邀你搶",
"搶紅包",
"搶",
"最高領取",
"邀你抢",
"购物红包",
"抢红包",
"最高领取",
"抢",
]
# 关键词列表(与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()
def create_redpacket_test_image():
"""创建一个测试用的购物红包图像"""
print("=" * 60)
print("创建测试图像")
print("=" * 60)
# 创建1080x2400的图像
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")
img.save(test_img_path)
print(f"✓ 测试图像已保存: {test_img_path}")
print()
return test_img_path
if __name__ == "__main__":
print("\n")
print("╔" + "=" * 58 + "╗")
print("║" + " " * 58 + "║")
print("║" + "购物红包识别功能测试".center(58) + "║")
print("║" + " " * 58 + "║")
print("╚" + "=" * 58 + "╝")
print()
# 运行所有测试
test_coordinates()
test_ocr_keywords()
result = test_pixel_detection()
test_img_path = create_redpacket_test_image()
# 总结
print("=" * 60)
print("测试总结")
print("=" * 60)
print(f"✓ 像素检测函数: {'可用' if result else '无法检测'}")
print(f"✓ 坐标定义: 已更新到中央区域")
print(f"✓ OCR关键词: 12个关键词已定义(传统+简体中文)")
print(f"✓ 测试图像: {test_img_path}")
print()
print("下一步:")
print(" 1. 在实际应用中测试购物红包识别")
print(" 2. 查看 logs/actions.log 中的调试信息")
print(" 3. 验证像素检测和OCR识别是否都能触发")
print()