'commit'
This commit is contained in:
104
WeiXin/T4_Text_StaticTest.py
Normal file
104
WeiXin/T4_Text_StaticTest.py
Normal file
@@ -0,0 +1,104 @@
|
||||
# coding=utf-8
|
||||
import os
|
||||
import sys
|
||||
import logging
|
||||
import cv2
|
||||
|
||||
# Add project root
|
||||
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 import WxUtil
|
||||
|
||||
# Setup Logger
|
||||
logger = logging.getLogger("T4_Text")
|
||||
|
||||
# Configure Root Logger to ensure all logs (including WxUtil) go to file
|
||||
root_logger = logging.getLogger()
|
||||
root_logger.setLevel(logging.INFO)
|
||||
|
||||
# Ensure Log Directory exists
|
||||
if hasattr(WxUtil, 'LOG_DIR'):
|
||||
log_dir = WxUtil.LOG_DIR
|
||||
else:
|
||||
log_dir = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "Logs")
|
||||
|
||||
if not os.path.exists(log_dir):
|
||||
os.makedirs(log_dir)
|
||||
|
||||
# File Handler
|
||||
log_file = os.path.join(log_dir, "T4_Text.log")
|
||||
fh = logging.FileHandler(log_file, mode='w', encoding='utf-8')
|
||||
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
|
||||
fh.setFormatter(formatter)
|
||||
root_logger.addHandler(fh)
|
||||
|
||||
# Console Handler (if not already added by WxUtil)
|
||||
if not any(isinstance(h, logging.StreamHandler) for h in root_logger.handlers):
|
||||
ch = logging.StreamHandler()
|
||||
ch.setFormatter(formatter)
|
||||
root_logger.addHandler(ch)
|
||||
|
||||
def main():
|
||||
logger.info("--- T4: Text Processing Test ---")
|
||||
|
||||
# 1. Connect
|
||||
d = WxUtil.connect_device()
|
||||
if not d:
|
||||
logger.error("Device connection failed.")
|
||||
return
|
||||
|
||||
# 2. Screenshot
|
||||
screenshot_path = os.path.join(WxUtil.OUTPUT_DIR, "T4_Screenshot.jpg")
|
||||
logger.info("Taking screenshot...")
|
||||
d.screenshot(screenshot_path)
|
||||
|
||||
# 3. Scan
|
||||
logger.info("Scanning image...")
|
||||
messages, debug_img, _ = WxUtil._scan_chat_messages(screenshot_path)
|
||||
|
||||
# 4. Filter and Output
|
||||
logger.info("--- Raw Text Messages ---")
|
||||
text_msgs = [m for m in messages if m['type'] == 'text']
|
||||
|
||||
if not text_msgs:
|
||||
logger.info("No text messages found.")
|
||||
else:
|
||||
for i, msg in enumerate(text_msgs):
|
||||
logger.info(f"Text [{i}] Sender: {msg['sender']} | y={msg['y']} | Content: {msg['content']}")
|
||||
|
||||
# 5. Merging Logic (Same as T2)
|
||||
logger.info("--- Merged Text Dialogues ---")
|
||||
merged_msgs = []
|
||||
if text_msgs:
|
||||
current_msg = text_msgs[0].copy()
|
||||
last_y = current_msg['y']
|
||||
|
||||
for i in range(1, len(text_msgs)):
|
||||
next_msg = text_msgs[i]
|
||||
|
||||
is_same_sender = (current_msg['sender'] == next_msg['sender'])
|
||||
y_diff = next_msg['y'] - last_y
|
||||
is_close = (y_diff < 120)
|
||||
|
||||
if is_same_sender and is_close:
|
||||
current_msg['content'] += " " + next_msg['content']
|
||||
last_y = next_msg['y']
|
||||
else:
|
||||
merged_msgs.append(current_msg)
|
||||
current_msg = next_msg.copy()
|
||||
last_y = current_msg['y']
|
||||
|
||||
merged_msgs.append(current_msg)
|
||||
|
||||
for i, msg in enumerate(merged_msgs):
|
||||
logger.info(f"[{i}] {msg['sender']}: {msg['content']}")
|
||||
|
||||
# Save debug
|
||||
debug_path = os.path.join(WxUtil.OUTPUT_DIR, "T4_Debug.jpg")
|
||||
cv2.imwrite(debug_path, debug_img)
|
||||
logger.info(f"Debug image saved to: {debug_path}")
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user