'commit'
This commit is contained in:
88
WeiXin/T3_MarkInputBox.py
Normal file
88
WeiXin/T3_MarkInputBox.py
Normal file
@@ -0,0 +1,88 @@
|
||||
# coding=utf-8
|
||||
import uiautomator2 as u2
|
||||
import time
|
||||
import logging
|
||||
import sys
|
||||
import os
|
||||
import cv2
|
||||
import numpy as np
|
||||
|
||||
project_root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
||||
if project_root not in sys.path:
|
||||
sys.path.append(project_root)
|
||||
|
||||
from WeiXin.WxUtil import find_input_box_center
|
||||
|
||||
# 配置日志
|
||||
log_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), "Logs")
|
||||
if not os.path.exists(log_dir):
|
||||
os.makedirs(log_dir)
|
||||
|
||||
logging.basicConfig(
|
||||
level=logging.INFO,
|
||||
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
|
||||
handlers=[
|
||||
logging.FileHandler(os.path.join(log_dir, "T3_MarkInputBox.log"), encoding='utf-8'),
|
||||
logging.StreamHandler()
|
||||
]
|
||||
)
|
||||
logger = logging.getLogger("T3_MarkInputBox")
|
||||
|
||||
def mark_input_box():
|
||||
logger.info("开始执行 T3: 标识输入框位置...")
|
||||
|
||||
try:
|
||||
d = u2.connect()
|
||||
logger.info(f"设备连接成功: {d.info.get('serial')}")
|
||||
except Exception as e:
|
||||
logger.error(f"设备连接失败: {e}")
|
||||
return
|
||||
|
||||
screenshot_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), "Screenshots")
|
||||
if not os.path.exists(screenshot_dir):
|
||||
os.makedirs(screenshot_dir)
|
||||
|
||||
timestamp = time.strftime("%Y%m%d_%H%M%S")
|
||||
filename = f"T3_input_{timestamp}.jpg"
|
||||
save_path = os.path.join(screenshot_dir, filename)
|
||||
|
||||
try:
|
||||
d.screenshot(save_path)
|
||||
logger.info(f"截图已保存: {save_path}")
|
||||
|
||||
# 调用 WxUtil 识别输入框
|
||||
center_point, rect_box = find_input_box_center(save_path)
|
||||
|
||||
if center_point and rect_box:
|
||||
# 读取图片进行绘制
|
||||
img_data = np.fromfile(save_path, dtype=np.uint8)
|
||||
img = cv2.imdecode(img_data, cv2.IMREAD_COLOR)
|
||||
|
||||
x, y, w, h = rect_box
|
||||
cx, cy = center_point
|
||||
|
||||
# 要求:绿色框,模块点击点处标识小红点
|
||||
# 绿色框 (Green): BGR (0, 255, 0)
|
||||
cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 5)
|
||||
|
||||
# 小红点 (Red): BGR (0, 0, 255)
|
||||
# 半径 10, 实心
|
||||
cv2.circle(img, (cx, cy), 10, (0, 0, 255), -1)
|
||||
|
||||
output_filename = f"T3_input_{timestamp}_marked.jpg"
|
||||
output_path = os.path.join(screenshot_dir, output_filename)
|
||||
|
||||
# 保存
|
||||
ext = os.path.splitext(output_path)[1]
|
||||
cv2.imencode(ext, img)[1].tofile(output_path)
|
||||
logger.info(f"✅ T3 执行完成: 标记图片已保存至 {output_path}")
|
||||
logger.info(f"输入框区域: {rect_box}, 点击中心点: {center_point}")
|
||||
|
||||
else:
|
||||
logger.warning("❌ T3 执行结果: 未能识别到输入框。")
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"❌ T3 执行失败: {e}")
|
||||
|
||||
if __name__ == "__main__":
|
||||
mark_input_box()
|
||||
Reference in New Issue
Block a user