'commit'
@@ -13,7 +13,10 @@ WAIT_AFTER_SCROLL = 1.0
|
||||
|
||||
# 坐标计算与安全防护
|
||||
SAFE_EXCLUDE_RATIO = 0.30
|
||||
BOTTOM_SAFE_EXCLUDE_RATIO = 0.12
|
||||
BOTTOM_SAFE_EXCLUDE_RATIO = 0.08
|
||||
MIN_CARD_HEIGHT_RATIO = 0.12
|
||||
STATION_BOX_WIDTH_RATIO = 0.95
|
||||
STATION_BOX_HEIGHT_RATIO = 0.18
|
||||
FALLBACK_WIDTH = 1080
|
||||
FALLBACK_HEIGHT = 2400
|
||||
|
||||
|
||||
483
Apps/XinDianTu/FirstPageKit.py
Normal file
@@ -0,0 +1,483 @@
|
||||
import asyncio
|
||||
import json
|
||||
import os
|
||||
import sys
|
||||
|
||||
import cv2
|
||||
import numpy as np
|
||||
|
||||
project_root = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||||
if project_root not in sys.path:
|
||||
sys.path.append(project_root)
|
||||
|
||||
from Apps.XinDianTu.Kit import setup_logger, get_ocr_reader
|
||||
from Apps.XinDianTu.ReadImageKit import ReadImageKit
|
||||
from Apps.XinDianTu.Config.Setting import SAFE_EXCLUDE_RATIO, BOTTOM_SAFE_EXCLUDE_RATIO, STATION_BOX_WIDTH_RATIO, STATION_BOX_HEIGHT_RATIO
|
||||
from Util.LlmUtil import get_llm_response
|
||||
|
||||
|
||||
logger = setup_logger("XinDianTu.FirstPageKit")
|
||||
NON_STATION_KEYWORDS = [
|
||||
"首页",
|
||||
"找桩",
|
||||
"会员",
|
||||
"我的",
|
||||
"扫码充电",
|
||||
"扫码",
|
||||
"新电途 到哪都能充",
|
||||
"搜索场站或目的地",
|
||||
"签到",
|
||||
"畅充会员",
|
||||
"组团充电",
|
||||
"微信社群",
|
||||
"申卡有礼",
|
||||
"新电途招募城市合伙人",
|
||||
"去赚钱",
|
||||
"综合排序",
|
||||
"停车费",
|
||||
"充电速度",
|
||||
"筛选",
|
||||
"不限车长",
|
||||
"不限车高",
|
||||
"夜间免停",
|
||||
"洗手间",
|
||||
]
|
||||
STATION_TITLE_KEYWORDS = [
|
||||
"充电站",
|
||||
"超快充",
|
||||
"快充",
|
||||
"换电站",
|
||||
"加电站",
|
||||
"超级充电",
|
||||
"超充",
|
||||
"充电",
|
||||
]
|
||||
TOP_ZONE_STATION_HINT_KEYWORDS = [
|
||||
"充电站",
|
||||
"公共充电",
|
||||
"超级充电",
|
||||
"慢充站",
|
||||
"慢充",
|
||||
"超快充",
|
||||
"快充",
|
||||
"充电",
|
||||
"超充",
|
||||
]
|
||||
|
||||
|
||||
def _load_image(path):
|
||||
if not os.path.exists(path):
|
||||
raise FileNotFoundError(path)
|
||||
img = cv2.imread(path)
|
||||
if img is None:
|
||||
raise RuntimeError(f"无法读取图片: {path}")
|
||||
h, w = img.shape[:2]
|
||||
return img, w, h
|
||||
|
||||
|
||||
async def run_ocr_rect(image_path, log_path=None):
|
||||
log_lines = []
|
||||
|
||||
def log_detail(msg):
|
||||
logger.info(msg)
|
||||
log_lines.append(msg)
|
||||
|
||||
img, w, h = _load_image(image_path)
|
||||
log_detail(f"开始处理图片: {image_path}, 宽={w}, 高={h}")
|
||||
|
||||
reader = get_ocr_reader()
|
||||
ocr_results = reader.read_text(img)
|
||||
log_detail(f"OCR 原始结果数量: {len(ocr_results)}")
|
||||
|
||||
entries = []
|
||||
for idx, (quad, text, prob) in enumerate(ocr_results):
|
||||
pts = np.array(quad).astype(int)
|
||||
x_min = int(np.min(pts[:, 0]))
|
||||
y_min = int(np.min(pts[:, 1]))
|
||||
x_max = int(np.max(pts[:, 0]))
|
||||
y_max = int(np.max(pts[:, 1]))
|
||||
cx = (x_min + x_max) / 2.0
|
||||
cy = (y_min + y_max) / 2.0
|
||||
cx_norm = cx / w
|
||||
cy_norm = cy / h
|
||||
|
||||
status = "keep"
|
||||
reasons = []
|
||||
if prob < 0.4:
|
||||
status = "drop"
|
||||
reasons.append("prob<0.4")
|
||||
if not text:
|
||||
status = "drop"
|
||||
reasons.append("empty_text")
|
||||
if cy_norm < SAFE_EXCLUDE_RATIO:
|
||||
status = "drop"
|
||||
reasons.append("top_safe_zone")
|
||||
if cy_norm > (1 - BOTTOM_SAFE_EXCLUDE_RATIO):
|
||||
status = "drop"
|
||||
reasons.append("bottom_safe_zone")
|
||||
if status == "keep":
|
||||
for kw in NON_STATION_KEYWORDS:
|
||||
if kw and kw in text:
|
||||
status = "drop"
|
||||
reasons.append("non_station_keyword")
|
||||
break
|
||||
if status == "drop" and "prob<0.4" in reasons and text:
|
||||
for kw in STATION_TITLE_KEYWORDS:
|
||||
if kw and kw in text:
|
||||
status = "keep"
|
||||
reasons.append("force_keep_station_title")
|
||||
break
|
||||
if status == "drop" and "top_safe_zone" in reasons and text:
|
||||
if SAFE_EXCLUDE_RATIO - 0.06 <= cy_norm < SAFE_EXCLUDE_RATIO:
|
||||
for kw in TOP_ZONE_STATION_HINT_KEYWORDS:
|
||||
if kw and kw in text:
|
||||
status = "keep"
|
||||
reasons.append("force_keep_top_station_title")
|
||||
break
|
||||
|
||||
log_detail(
|
||||
f"OCR[{idx + 1}] text={repr(text)} prob={prob:.3f} "
|
||||
f"cx_norm={cx_norm:.4f} cy_norm={cy_norm:.4f} "
|
||||
f"status={status} reasons={','.join(reasons) if reasons else '-'}"
|
||||
)
|
||||
|
||||
if status != "keep":
|
||||
continue
|
||||
|
||||
entries.append(
|
||||
{
|
||||
"text": text,
|
||||
"prob": float(prob),
|
||||
"cx_norm": cx_norm,
|
||||
"cy_norm": cy_norm,
|
||||
}
|
||||
)
|
||||
|
||||
log_detail(f"OCR 通过过滤的有效文本数量: {len(entries)}")
|
||||
|
||||
if not entries:
|
||||
log_detail("无有效 OCR 文本, 结束当前图片处理")
|
||||
return
|
||||
|
||||
indexed_entries = []
|
||||
for idx, e in enumerate(entries):
|
||||
indexed_entries.append(
|
||||
{
|
||||
"id": idx + 1,
|
||||
"text": e["text"],
|
||||
"prob": e["prob"],
|
||||
"cx_norm": round(e["cx_norm"], 4),
|
||||
"cy_norm": round(e["cy_norm"], 4),
|
||||
}
|
||||
)
|
||||
|
||||
payload_json = json.dumps(indexed_entries, ensure_ascii=False)
|
||||
log_detail(f"传给 LLM 的 OCR 条目数: {len(indexed_entries)}")
|
||||
|
||||
query_text = (
|
||||
"下面是新电途列表页整张截图的 OCR 结果,每一项代表一行文字,包含其中心点的归一化坐标:\n"
|
||||
"ocr_items = " + payload_json + "\n"
|
||||
"请你根据这些文本,将它们聚合成若干个“充电场站卡片”。输出一个 JSON 数组,每个元素必须包含:\n"
|
||||
"1) station_name: 场站名称,只能是卡片标题中的名称,不允许是筛选标签、导航按钮、底部功能区等。\n"
|
||||
"2) anchor_point_norm: 一个对象 {\"x\": number, \"y\": number},表示该场站名称文字所在行的中心点坐标,取值范围 0-1。\n"
|
||||
"并且尽量补充以下可选字段(找不到时可以省略或设为 null):\n"
|
||||
"3) distance_text: 距离字符串,例如 \"6.9km\"、\"500m\",从对应卡片中的距离行提取。\n"
|
||||
"4) busy_info: 忙闲信息对象,格式为 {\"mode\": \"快|慢|超|普通\", \"idle\": number, \"total\": number}。\n"
|
||||
" 例如:\"快 闲24/32\" => {\"mode\": \"快\", \"idle\": 24, \"total\": 32};\n"
|
||||
" \"慢 闲0/10\" => {\"mode\": \"慢\", \"idle\": 0, \"total\": 10};\n"
|
||||
" \"超 闲1/3\" => {\"mode\": \"超\", \"idle\": 1, \"total\": 3};\n"
|
||||
" \"闲5/10\" => {\"mode\": \"慢\", \"idle\": 5, \"total\": 10}。\n"
|
||||
"额外提示:\n"
|
||||
"- 每个场站卡片通常包含一行类似 \"1.4km\"、\"3.6km\" 的距离文本;\n"
|
||||
"- 该距离文本所在行的左侧、且在同一卡片中的那一行文字,就是对应的场站标题 station_name;\n"
|
||||
"- 忙闲信息通常出现在卡片右侧的红色或绿色小块中,例如 \"快 闲24/32\"、\"慢 闲0/10\"、\"超 闲1/3\"、\"闲5/10\";\n"
|
||||
"- 即使 station_name 中不包含“充电站”“超快充”等字样,但只要与某个 \"x.xkm\" 行在同一卡片区域内,也应视为一个完整的场站名称。\n"
|
||||
"要求:\n"
|
||||
"- 场站按从上到下排序;\n"
|
||||
"- station_name 不能取距离行本身(如 \"1.4km\"),而是要取与之成一对的标题行;\n"
|
||||
"- 如果某些 OCR 文本显然不属于任何场站卡片,可以忽略;\n"
|
||||
"- 只输出 JSON 数组,不要输出其它任何文字。"
|
||||
)
|
||||
|
||||
chunks = []
|
||||
async for part in get_llm_response(
|
||||
query_text=query_text,
|
||||
stream=False,
|
||||
system_prompt="你是一个帮助整理 OCR 文本的助手,只输出 JSON。",
|
||||
chat_history=None,
|
||||
temperature=0,
|
||||
):
|
||||
chunks.append(part)
|
||||
|
||||
full_text = "".join(chunks)
|
||||
log_detail("LLM 原始返回内容开始")
|
||||
log_lines.append(full_text)
|
||||
log_detail("LLM 原始返回内容结束")
|
||||
|
||||
raw = ReadImageKit._extract_json(full_text)
|
||||
log_detail(f"从 LLM 返回内容中抽取出的 JSON 片段: {raw}")
|
||||
|
||||
try:
|
||||
data = json.loads(raw)
|
||||
except Exception as e:
|
||||
log_detail(f"解析 LLM 返回 JSON 失败: {e}")
|
||||
logs_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), "Logs")
|
||||
os.makedirs(logs_dir, exist_ok=True)
|
||||
if log_path is None:
|
||||
final_log_path = os.path.join(
|
||||
logs_dir, os.path.basename(image_path).replace(".jpg", ".log")
|
||||
)
|
||||
mode = "w"
|
||||
else:
|
||||
final_log_path = log_path
|
||||
mode = "a"
|
||||
with open(final_log_path, mode, encoding="utf-8") as f:
|
||||
for line in log_lines:
|
||||
f.write(line + "\n")
|
||||
log_detail(f"已写入详细日志到: {final_log_path}")
|
||||
return
|
||||
|
||||
if not isinstance(data, list):
|
||||
log_detail(f"期望 LLM 返回 JSON 数组, 实际类型: {type(data)}")
|
||||
return
|
||||
|
||||
if not data:
|
||||
log_detail("LLM 返回空数组, 结束当前图片处理")
|
||||
return
|
||||
|
||||
stations = []
|
||||
for idx, item in enumerate(data):
|
||||
if not isinstance(item, dict):
|
||||
log_detail(f"LLM item[{idx}] 不是对象类型, 跳过")
|
||||
continue
|
||||
name = item.get("station_name") or item.get("name")
|
||||
anchor = item.get("anchor_point_norm") or item.get("anchor") or item.get("center_norm")
|
||||
if not anchor:
|
||||
log_detail(f"LLM item[{idx}] 缺少 anchor 信息, 跳过, content={item}")
|
||||
continue
|
||||
try:
|
||||
ax = float(anchor.get("x"))
|
||||
ay = float(anchor.get("y"))
|
||||
except Exception:
|
||||
log_detail(f"LLM item[{idx}] anchor 不可解析, content={anchor}")
|
||||
continue
|
||||
if not (0 <= ax <= 1 and 0 <= ay <= 1):
|
||||
log_detail(
|
||||
f"LLM item[{idx}] anchor 超出范围, x={ax}, y={ay}, 跳过"
|
||||
)
|
||||
continue
|
||||
px = int(ax * w)
|
||||
py = int(ay * h) - 20
|
||||
if py < 0:
|
||||
py = 0
|
||||
|
||||
distance_text = item.get("distance_text") or item.get("distance")
|
||||
busy_raw = item.get("busy_info") or item.get("busy")
|
||||
busy_mode = None
|
||||
busy_idle = None
|
||||
busy_total = None
|
||||
if isinstance(busy_raw, dict):
|
||||
busy_mode = busy_raw.get("mode") or busy_raw.get("type")
|
||||
try:
|
||||
if busy_raw.get("idle") is not None:
|
||||
busy_idle = int(busy_raw.get("idle"))
|
||||
except Exception:
|
||||
busy_idle = None
|
||||
try:
|
||||
if busy_raw.get("total") is not None:
|
||||
busy_total = int(busy_raw.get("total"))
|
||||
except Exception:
|
||||
busy_total = None
|
||||
|
||||
stations.append(
|
||||
{
|
||||
"name": name,
|
||||
"ax": ax,
|
||||
"ay": ay,
|
||||
"px": px,
|
||||
"py": py,
|
||||
"distance_text": distance_text,
|
||||
"busy_mode": busy_mode,
|
||||
"busy_idle": busy_idle,
|
||||
"busy_total": busy_total,
|
||||
}
|
||||
)
|
||||
log_detail(
|
||||
f"LLM anchor 规范化[{len(stations)}] name={name} ax={ax:.4f} ay={ay:.4f} py={py} "
|
||||
f"distance={distance_text} busy=({busy_mode},{busy_idle},{busy_total})"
|
||||
)
|
||||
|
||||
if not stations:
|
||||
log_detail("LLM 解析后没有可用的场站锚点, 结束当前图片处理")
|
||||
return
|
||||
|
||||
stations.sort(key=lambda s: s["py"])
|
||||
|
||||
overlay = img.copy()
|
||||
results = []
|
||||
|
||||
box_w = int(w * STATION_BOX_WIDTH_RATIO)
|
||||
box_h_conf = int(h * STATION_BOX_HEIGHT_RATIO)
|
||||
box_h = box_h_conf
|
||||
|
||||
if len(stations) >= 2:
|
||||
gaps = []
|
||||
for i in range(len(stations) - 1):
|
||||
dy = stations[i + 1]["py"] - stations[i]["py"]
|
||||
if dy > 0:
|
||||
gaps.append(dy)
|
||||
if gaps:
|
||||
min_gap = min(gaps)
|
||||
max_no_overlap = max(min_gap - 4, 40)
|
||||
if max_no_overlap < box_h_conf * 0.6:
|
||||
box_h = box_h_conf
|
||||
log_detail(
|
||||
f"场站间距过小(min_gap={min_gap}), 使用配置高度 box_h={box_h_conf}"
|
||||
)
|
||||
else:
|
||||
box_h = min(box_h_conf, max_no_overlap)
|
||||
log_detail(
|
||||
f"根据最小锚点间距调整 box_h: conf={box_h_conf}, "
|
||||
f"min_gap={min_gap}, final={box_h}"
|
||||
)
|
||||
else:
|
||||
box_h = box_h_conf
|
||||
log_detail("未找到有效间距, 使用配置高度 box_h={box_h_conf}")
|
||||
else:
|
||||
box_h = box_h_conf
|
||||
log_detail(f"仅一个场站, 使用配置高度 box_h={box_h_conf}")
|
||||
|
||||
x1_fixed = max(0, (w - box_w) // 2)
|
||||
x2_fixed = min(w, x1_fixed + box_w)
|
||||
effective_top = int(h * SAFE_EXCLUDE_RATIO)
|
||||
effective_bottom = int(h * (1 - BOTTOM_SAFE_EXCLUDE_RATIO))
|
||||
|
||||
log_detail(
|
||||
f"固定绿框参数: box_w={box_w}, box_h={box_h}, "
|
||||
f"x1_fixed={x1_fixed}, x2_fixed={x2_fixed}, "
|
||||
f"effective_top={effective_top}, effective_bottom={effective_bottom}"
|
||||
)
|
||||
|
||||
prev_y2 = None
|
||||
|
||||
for idx, s in enumerate(stations):
|
||||
name = s["name"]
|
||||
ax = s["ax"]
|
||||
ay = s["ay"]
|
||||
px = s["px"]
|
||||
py = s["py"]
|
||||
|
||||
y1 = py - int(box_h * 0.35)
|
||||
y2 = y1 + box_h
|
||||
orig_y1 = y1
|
||||
orig_y2 = y2
|
||||
if y1 < 0:
|
||||
y1 = 0
|
||||
y2 = box_h
|
||||
if y2 > h:
|
||||
y2 = h
|
||||
y1 = h - box_h
|
||||
|
||||
if ay >= SAFE_EXCLUDE_RATIO and y1 < effective_top:
|
||||
y1 = effective_top
|
||||
y2 = y1 + box_h
|
||||
if y2 > h:
|
||||
y2 = h
|
||||
y1 = h - box_h
|
||||
if y2 > effective_bottom:
|
||||
y2 = effective_bottom
|
||||
y1 = y2 - box_h
|
||||
if y1 < 0:
|
||||
y1 = 0
|
||||
y2 = box_h
|
||||
|
||||
if prev_y2 is not None and y1 <= prev_y2:
|
||||
shift = prev_y2 - y1 + 1
|
||||
y1 += shift
|
||||
y2 += shift
|
||||
if y2 > effective_bottom:
|
||||
log_detail(
|
||||
f"Station[{idx + 1}] {name} 因避免重叠无法放入有效区域, 被丢弃"
|
||||
)
|
||||
continue
|
||||
|
||||
prev_y2 = y2
|
||||
|
||||
click_x = int((x1_fixed + x2_fixed) / 2)
|
||||
click_y = int((y1 + y2) / 2)
|
||||
|
||||
log_detail(
|
||||
f"Station[{idx + 1}] name={name} anchor=({ax:.4f},{ay:.4f}) "
|
||||
f"px={px}, py={py}, box_orig=({orig_y1},{orig_y2}), "
|
||||
f"box_adj=({y1},{y2}), click=({click_x},{click_y})"
|
||||
)
|
||||
|
||||
cv2.rectangle(overlay, (x1_fixed, y1), (x2_fixed, y2), (0, 255, 0), 2)
|
||||
cv2.circle(overlay, (click_x, click_y), 8, (0, 0, 255), -1)
|
||||
|
||||
results.append(
|
||||
{
|
||||
"index": idx + 1,
|
||||
"station_name": name,
|
||||
"rect": [x1_fixed, y1, x2_fixed, y2],
|
||||
"click_point": [click_x, click_y],
|
||||
"distance_text": s.get("distance_text"),
|
||||
"busy_mode": s.get("busy_mode"),
|
||||
"busy_idle": s.get("busy_idle"),
|
||||
"busy_total": s.get("busy_total"),
|
||||
}
|
||||
)
|
||||
|
||||
stem, ext = os.path.splitext(image_path)
|
||||
out_path = f"{stem}_ocr_rect{ext}"
|
||||
cv2.imwrite(out_path, overlay)
|
||||
|
||||
log_detail(f"输入图片: {image_path}")
|
||||
log_detail(f"调试输出图片: {out_path}")
|
||||
log_detail("识别到的场站及矩形坐标如下:")
|
||||
for item in results:
|
||||
log_detail(json.dumps(item, ensure_ascii=False))
|
||||
|
||||
logs_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), "Logs")
|
||||
os.makedirs(logs_dir, exist_ok=True)
|
||||
if log_path is None:
|
||||
final_log_path = os.path.join(
|
||||
logs_dir, os.path.basename(image_path).replace(".jpg", ".log")
|
||||
)
|
||||
mode = "w"
|
||||
else:
|
||||
final_log_path = log_path
|
||||
mode = "a"
|
||||
with open(final_log_path, mode, encoding="utf-8") as f:
|
||||
for line in log_lines:
|
||||
f.write(line + "\n")
|
||||
log_detail(f"已写入详细日志到: {final_log_path}")
|
||||
|
||||
|
||||
async def run_batch_in_dir(base_dir, log_path=None):
|
||||
files = sorted(os.listdir(base_dir))
|
||||
jpg_files = []
|
||||
for name in files:
|
||||
lower = name.lower()
|
||||
if not lower.endswith(".jpg"):
|
||||
continue
|
||||
if lower.endswith("_ocr_rect.jpg"):
|
||||
continue
|
||||
jpg_files.append(os.path.join(base_dir, name))
|
||||
|
||||
if not jpg_files:
|
||||
logger.info(f"目录中未找到待处理的 JPG 文件: {base_dir}")
|
||||
return
|
||||
|
||||
logger.info(f"即将批量处理 {len(jpg_files)} 张图片:")
|
||||
for p in jpg_files:
|
||||
logger.info(f" - {p}")
|
||||
|
||||
for p in jpg_files:
|
||||
await run_ocr_rect(p, log_path=log_path)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
base_dir = os.path.dirname(os.path.abspath(__file__))
|
||||
asyncio.run(run_batch_in_dir(base_dir))
|
||||
|
||||
@@ -4,7 +4,7 @@ import cv2
|
||||
import numpy as np
|
||||
import time
|
||||
import hashlib
|
||||
from Apps.XinDianTu.Config.Setting import SAFE_EXCLUDE_RATIO, BOTTOM_SAFE_EXCLUDE_RATIO
|
||||
from Apps.XinDianTu.Config.Setting import SAFE_EXCLUDE_RATIO, BOTTOM_SAFE_EXCLUDE_RATIO, MIN_CARD_HEIGHT_RATIO
|
||||
from Config.Config import TEMP_IMAGE_DIR
|
||||
|
||||
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s")
|
||||
@@ -862,12 +862,21 @@ def crop_cards_from_image(img_path, output_dir=None, save_debug=True):
|
||||
max_h = max(heights)
|
||||
median_h = np.median(heights)
|
||||
|
||||
threshold_h = median_h * 0.80
|
||||
min_abs_h = h * MIN_CARD_HEIGHT_RATIO
|
||||
base_threshold = max(median_h * 0.90, min_abs_h)
|
||||
|
||||
for (y1, y2), card_h in zip(temp_valid_segments, heights):
|
||||
if card_h < threshold_h:
|
||||
center_y = (y1 + y2) / 2.0
|
||||
|
||||
local_threshold = base_threshold
|
||||
|
||||
if center_y > h * 0.70:
|
||||
local_threshold = median_h * 0.70
|
||||
|
||||
if card_h < local_threshold:
|
||||
logger.info(
|
||||
f" Filtering out segment Y={y1}-{y2} (H={card_h}) because it's too short (Threshold={threshold_h:.1f}).")
|
||||
f" Filtering out segment Y={y1}-{y2} (H={card_h}) because it's too short "
|
||||
f"(Threshold={local_threshold:.1f}, center_y={center_y:.1f}).")
|
||||
else:
|
||||
final_cards.append((y1, y2, final_x1, final_x2))
|
||||
logger.info(f" Card: Y={y1}-{y2}, X={final_x1}-{final_x2}, H={card_h}")
|
||||
@@ -911,8 +920,14 @@ def crop_cards_from_image(img_path, output_dir=None, save_debug=True):
|
||||
logger.info(f" Step [2.1/VL] 准备在 VL 图片上绘制 {len(final_cards)} 个场站的绿色方框...")
|
||||
|
||||
for idx, (y1, y2, x1, x2) in enumerate(final_cards):
|
||||
PAD_TOP = 5
|
||||
draw_y1 = max(0, y1 - PAD_TOP)
|
||||
segment_height = y2 - y1
|
||||
pad_top = int(segment_height * 0.20)
|
||||
if pad_top < 10:
|
||||
pad_top = 10
|
||||
if pad_top > 60:
|
||||
pad_top = 60
|
||||
|
||||
draw_y1 = max(0, y1 - pad_top)
|
||||
draw_y2 = y2
|
||||
|
||||
w_card = x2 - x1
|
||||
|
||||
BIN
Test/TestFirstPage/Output/xdt_firstpage_10_1768695739.jpg
Normal file
|
After Width: | Height: | Size: 125 KiB |
|
After Width: | Height: | Size: 247 KiB |
BIN
Test/TestFirstPage/Output/xdt_firstpage_11_1768695746.jpg
Normal file
|
After Width: | Height: | Size: 121 KiB |
|
After Width: | Height: | Size: 236 KiB |
BIN
Test/TestFirstPage/Output/xdt_firstpage_12_1768695755.jpg
Normal file
|
After Width: | Height: | Size: 130 KiB |
|
After Width: | Height: | Size: 253 KiB |
BIN
Test/TestFirstPage/Output/xdt_firstpage_13_1768695765.jpg
Normal file
|
After Width: | Height: | Size: 116 KiB |
|
After Width: | Height: | Size: 232 KiB |
BIN
Test/TestFirstPage/Output/xdt_firstpage_14_1768695772.jpg
Normal file
|
After Width: | Height: | Size: 119 KiB |
|
After Width: | Height: | Size: 227 KiB |
BIN
Test/TestFirstPage/Output/xdt_firstpage_15_1768695782.jpg
Normal file
|
After Width: | Height: | Size: 124 KiB |
|
After Width: | Height: | Size: 243 KiB |
BIN
Test/TestFirstPage/Output/xdt_firstpage_16_1768695790.jpg
Normal file
|
After Width: | Height: | Size: 129 KiB |
|
After Width: | Height: | Size: 249 KiB |
BIN
Test/TestFirstPage/Output/xdt_firstpage_17_1768695798.jpg
Normal file
|
After Width: | Height: | Size: 131 KiB |
|
After Width: | Height: | Size: 251 KiB |
BIN
Test/TestFirstPage/Output/xdt_firstpage_18_1768695806.jpg
Normal file
|
After Width: | Height: | Size: 118 KiB |
|
After Width: | Height: | Size: 230 KiB |
BIN
Test/TestFirstPage/Output/xdt_firstpage_19_1768695814.jpg
Normal file
|
After Width: | Height: | Size: 124 KiB |
|
After Width: | Height: | Size: 240 KiB |
BIN
Test/TestFirstPage/Output/xdt_firstpage_1_1768695656.jpg
Normal file
|
After Width: | Height: | Size: 138 KiB |
|
After Width: | Height: | Size: 259 KiB |
BIN
Test/TestFirstPage/Output/xdt_firstpage_20_1768695823.jpg
Normal file
|
After Width: | Height: | Size: 125 KiB |
|
After Width: | Height: | Size: 244 KiB |
BIN
Test/TestFirstPage/Output/xdt_firstpage_21_1768695831.jpg
Normal file
|
After Width: | Height: | Size: 118 KiB |
|
After Width: | Height: | Size: 238 KiB |
BIN
Test/TestFirstPage/Output/xdt_firstpage_22_1768695851.jpg
Normal file
|
After Width: | Height: | Size: 121 KiB |
|
After Width: | Height: | Size: 245 KiB |
BIN
Test/TestFirstPage/Output/xdt_firstpage_23_1768695860.jpg
Normal file
|
After Width: | Height: | Size: 123 KiB |
|
After Width: | Height: | Size: 243 KiB |
BIN
Test/TestFirstPage/Output/xdt_firstpage_24_1768695867.jpg
Normal file
|
After Width: | Height: | Size: 125 KiB |
|
After Width: | Height: | Size: 242 KiB |
BIN
Test/TestFirstPage/Output/xdt_firstpage_25_1768695875.jpg
Normal file
|
After Width: | Height: | Size: 126 KiB |
|
After Width: | Height: | Size: 244 KiB |
BIN
Test/TestFirstPage/Output/xdt_firstpage_26_1768695884.jpg
Normal file
|
After Width: | Height: | Size: 132 KiB |
|
After Width: | Height: | Size: 257 KiB |
BIN
Test/TestFirstPage/Output/xdt_firstpage_27_1768695892.jpg
Normal file
|
After Width: | Height: | Size: 130 KiB |
|
After Width: | Height: | Size: 249 KiB |
BIN
Test/TestFirstPage/Output/xdt_firstpage_28_1768695901.jpg
Normal file
|
After Width: | Height: | Size: 128 KiB |
|
After Width: | Height: | Size: 250 KiB |
BIN
Test/TestFirstPage/Output/xdt_firstpage_29_1768695910.jpg
Normal file
|
After Width: | Height: | Size: 127 KiB |
|
After Width: | Height: | Size: 252 KiB |
BIN
Test/TestFirstPage/Output/xdt_firstpage_2_1768695666.jpg
Normal file
|
After Width: | Height: | Size: 123 KiB |
|
After Width: | Height: | Size: 242 KiB |
BIN
Test/TestFirstPage/Output/xdt_firstpage_30_1768695918.jpg
Normal file
|
After Width: | Height: | Size: 123 KiB |
|
After Width: | Height: | Size: 242 KiB |
BIN
Test/TestFirstPage/Output/xdt_firstpage_3_1768695674.jpg
Normal file
|
After Width: | Height: | Size: 125 KiB |
|
After Width: | Height: | Size: 244 KiB |
BIN
Test/TestFirstPage/Output/xdt_firstpage_4_1768695683.jpg
Normal file
|
After Width: | Height: | Size: 120 KiB |
|
After Width: | Height: | Size: 236 KiB |
BIN
Test/TestFirstPage/Output/xdt_firstpage_5_1768695693.jpg
Normal file
|
After Width: | Height: | Size: 121 KiB |
|
After Width: | Height: | Size: 242 KiB |
BIN
Test/TestFirstPage/Output/xdt_firstpage_6_1768695702.jpg
Normal file
|
After Width: | Height: | Size: 123 KiB |
|
After Width: | Height: | Size: 237 KiB |
BIN
Test/TestFirstPage/Output/xdt_firstpage_7_1768695710.jpg
Normal file
|
After Width: | Height: | Size: 121 KiB |
|
After Width: | Height: | Size: 240 KiB |
BIN
Test/TestFirstPage/Output/xdt_firstpage_8_1768695718.jpg
Normal file
|
After Width: | Height: | Size: 120 KiB |
|
After Width: | Height: | Size: 232 KiB |
BIN
Test/TestFirstPage/Output/xdt_firstpage_9_1768695730.jpg
Normal file
|
After Width: | Height: | Size: 124 KiB |
|
After Width: | Height: | Size: 242 KiB |
|
Before Width: | Height: | Size: 130 KiB |
|
Before Width: | Height: | Size: 280 KiB |
|
Before Width: | Height: | Size: 130 KiB |
|
Before Width: | Height: | Size: 297 KiB |
|
Before Width: | Height: | Size: 122 KiB |
|
Before Width: | Height: | Size: 261 KiB |
|
Before Width: | Height: | Size: 131 KiB |
|
Before Width: | Height: | Size: 289 KiB |
77
Test/TestFirstPage/T_XinDianTu_FirstPageScrollTest.py
Normal file
@@ -0,0 +1,77 @@
|
||||
import asyncio
|
||||
import os
|
||||
import sys
|
||||
import time
|
||||
import shutil
|
||||
|
||||
import uiautomator2 as u2
|
||||
|
||||
|
||||
project_root = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||||
if project_root not in sys.path:
|
||||
sys.path.append(project_root)
|
||||
|
||||
|
||||
from Apps.XinDianTu.Kit import setup_logger, take_screenshot
|
||||
from Apps.XinDianTu.Config.Setting import SCROLL_DISTANCE_RATIO, WAIT_AFTER_SCROLL
|
||||
from Apps.XinDianTu.FirstPageKit import run_ocr_rect
|
||||
|
||||
|
||||
LOGS_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), "Logs")
|
||||
os.makedirs(LOGS_DIR, exist_ok=True)
|
||||
LOG_FILE_PATH = os.path.join(LOGS_DIR, "T_XinDianTu_FirstPageScrollTest.log")
|
||||
|
||||
logger = setup_logger(
|
||||
"XinDianTu.FirstPageScrollTest", log_file=LOG_FILE_PATH, clear_old_log=True
|
||||
)
|
||||
|
||||
|
||||
async def capture_list_page(d, base_dir: str, index: int):
|
||||
ts = int(time.time())
|
||||
image_uuid = f"xdt_firstpage_{index}_{ts}"
|
||||
list_path = take_screenshot(d, image_uuid, save_dir=base_dir)
|
||||
logger.info(f"[{index}] 列表截图路径: {list_path}")
|
||||
|
||||
try:
|
||||
await run_ocr_rect(list_path, log_path=LOG_FILE_PATH)
|
||||
logger.info(f"[{index}] 已根据 OCR+LLM 算法生成首屏绿框调试图。")
|
||||
except Exception as e:
|
||||
logger.exception(f"[{index}] 运行 OCR 绿框测试失败: {e}")
|
||||
|
||||
|
||||
async def run():
|
||||
logger.info("请先手动打开微信并进入新电途小程序的场站列表第一页。")
|
||||
|
||||
d = u2.connect()
|
||||
w, h = d.window_size()
|
||||
device_info = d.info or {}
|
||||
device_info["width"] = w
|
||||
device_info["height"] = h
|
||||
|
||||
logger.info(f"当前设备: {device_info.get('productName')} | 分辨率: {w}x{h}")
|
||||
|
||||
base_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), "Output")
|
||||
if os.path.exists(base_dir):
|
||||
for name in os.listdir(base_dir):
|
||||
path = os.path.join(base_dir, name)
|
||||
if os.path.isfile(path) or os.path.islink(path):
|
||||
os.remove(path)
|
||||
elif os.path.isdir(path):
|
||||
shutil.rmtree(path)
|
||||
os.makedirs(base_dir, exist_ok=True)
|
||||
|
||||
for i in range(1, 11):
|
||||
await capture_list_page(d, base_dir, i)
|
||||
if i < 10:
|
||||
logger.info(f"[{i}] 向上滑动,scale={SCROLL_DISTANCE_RATIO},等待 {WAIT_AFTER_SCROLL} 秒后继续截图。")
|
||||
d.swipe_ext("up", scale=SCROLL_DISTANCE_RATIO)
|
||||
await asyncio.sleep(WAIT_AFTER_SCROLL)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
try:
|
||||
asyncio.run(run())
|
||||
except KeyboardInterrupt:
|
||||
logger.info("用户手动中断测试脚本。")
|
||||
except Exception as e:
|
||||
logger.exception(f"测试脚本运行异常: {e}")
|
||||
@@ -1,171 +1,16 @@
|
||||
import asyncio
|
||||
import base64
|
||||
import json
|
||||
import os
|
||||
import sys
|
||||
|
||||
import cv2
|
||||
import numpy as np
|
||||
|
||||
project_root = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||||
if project_root not in sys.path:
|
||||
sys.path.append(project_root)
|
||||
|
||||
from Apps.XinDianTu.Kit import setup_logger
|
||||
from Apps.XinDianTu.ReadImageKit import ReadImageKit
|
||||
from Apps.XinDianTu.Config.Setting import SAFE_EXCLUDE_RATIO, BOTTOM_SAFE_EXCLUDE_RATIO
|
||||
from Config.Config import VL_MODEL_NAME
|
||||
from Apps.XinDianTu.FirstPageKit import run_ocr_rect
|
||||
|
||||
|
||||
logger = setup_logger("XinDianTu.VLRectTest")
|
||||
|
||||
|
||||
def _load_image(path):
|
||||
if not os.path.exists(path):
|
||||
raise FileNotFoundError(path)
|
||||
img = cv2.imread(path)
|
||||
if img is None:
|
||||
raise RuntimeError(f"无法读取图片: {path}")
|
||||
h, w = img.shape[:2]
|
||||
return img, w, h
|
||||
|
||||
|
||||
def _encode_image_to_data_url(path):
|
||||
with open(path, "rb") as f:
|
||||
encoded = base64.b64encode(f.read()).decode("utf-8")
|
||||
return {"url": f"data:image/jpeg;base64,{encoded}"}
|
||||
|
||||
|
||||
async def run_test(image_path):
|
||||
img, w, h = _load_image(image_path)
|
||||
img_input = _encode_image_to_data_url(image_path)
|
||||
|
||||
prompt = (
|
||||
"这是一张名为“新电途 到哪都能充”的充电站列表页面完整截图。\n"
|
||||
"请你从整张图中找出所有“单个充电站卡片”的区域,并按从上到下的顺序输出一个 JSON 数组。\n"
|
||||
"每个数组元素是一个对象,必须包含:\n"
|
||||
"1. station_name: 该卡片内的充电站名称字符串。\n"
|
||||
"2. bounds_norm: 该卡片在整张图片中的相对矩形坐标,字段为 left, top, right, bottom,"
|
||||
"取值范围均为 0 到 1,表示相对于整张图片宽高的比例。\n"
|
||||
"矩形应尽量完整覆盖卡片,包括名称、价格、桩数、距离等内容。\n"
|
||||
"如果你不完全确定精确边界,也要给出你认为最接近的矩形位置,不要省略。\n"
|
||||
"返回示例:\n"
|
||||
"[\n"
|
||||
" {\"station_name\": \"长春市南关区南溪湿地公园公共充电站\", \"bounds_norm\": {\"left\": 0.03, \"top\": 0.32, \"right\": 0.97, \"bottom\": 0.43}},\n"
|
||||
" {\"station_name\": \"长春环球贸易中心极东站\", \"bounds_norm\": {\"left\": 0.03, \"top\": 0.45, \"right\": 0.97, \"bottom\": 0.56}}\n"
|
||||
"]\n"
|
||||
"只允许输出一个 JSON 数组,不要包含任何解释性文字。"
|
||||
)
|
||||
|
||||
resp = await ReadImageKit._client.chat.completions.create(
|
||||
model=VL_MODEL_NAME,
|
||||
messages=[
|
||||
{
|
||||
"role": "user",
|
||||
"content": [
|
||||
{"type": "image_url", "image_url": img_input},
|
||||
{"type": "text", "text": prompt},
|
||||
],
|
||||
},
|
||||
],
|
||||
)
|
||||
|
||||
content = resp.choices[0].message.content or ""
|
||||
raw = ReadImageKit._extract_json(content)
|
||||
|
||||
try:
|
||||
data = json.loads(raw)
|
||||
except Exception as e:
|
||||
logger.error(f"解析模型返回JSON失败: {e}")
|
||||
logger.error(f"原始内容: {content}")
|
||||
return
|
||||
|
||||
if not isinstance(data, list):
|
||||
logger.error(f"期望返回JSON数组,但得到: {type(data)}")
|
||||
logger.error(f"解析结果: {data}")
|
||||
return
|
||||
|
||||
if not data:
|
||||
logger.info("模型返回空数组。原始内容如下:")
|
||||
logger.info(content)
|
||||
return
|
||||
|
||||
overlay = img.copy()
|
||||
results = []
|
||||
|
||||
for idx, item in enumerate(data):
|
||||
if not isinstance(item, dict):
|
||||
continue
|
||||
name = item.get("station_name")
|
||||
bounds_norm = item.get("bounds_norm") or {}
|
||||
try:
|
||||
l = float(bounds_norm.get("left"))
|
||||
t = float(bounds_norm.get("top"))
|
||||
r = float(bounds_norm.get("right"))
|
||||
b = float(bounds_norm.get("bottom"))
|
||||
except Exception:
|
||||
continue
|
||||
|
||||
if not (0 <= l < r <= 1 and 0 <= t < b <= 1):
|
||||
continue
|
||||
|
||||
x1 = int(l * w)
|
||||
y1 = int(t * h)
|
||||
x2 = int(r * w)
|
||||
y2 = int(b * h)
|
||||
|
||||
center_y = (y1 + y2) / 2.0
|
||||
top_safe_ratio = 0.30
|
||||
bottom_safe_ratio = BOTTOM_SAFE_EXCLUDE_RATIO
|
||||
in_top_safe = center_y < h * top_safe_ratio
|
||||
in_bottom_safe = center_y > h * (1 - bottom_safe_ratio)
|
||||
|
||||
color = (0, 255, 0)
|
||||
click_x = None
|
||||
click_y = None
|
||||
if in_top_safe or in_bottom_safe:
|
||||
color = (0, 0, 255)
|
||||
|
||||
cv2.rectangle(overlay, (x1, y1), (x2, y2), color, 2)
|
||||
|
||||
if not in_top_safe and not in_bottom_safe:
|
||||
click_x = int((x1 + x2) / 2)
|
||||
click_y = int((y1 + y2) / 2)
|
||||
cv2.circle(overlay, (click_x, click_y), 8, (0, 0, 255), -1)
|
||||
|
||||
if name:
|
||||
label = f"{idx + 1}:{name}"
|
||||
cv2.putText(
|
||||
overlay,
|
||||
label[:20],
|
||||
(x1 + 5, max(y1 - 10, 20)),
|
||||
cv2.FONT_HERSHEY_SIMPLEX,
|
||||
0.6,
|
||||
color,
|
||||
2,
|
||||
cv2.LINE_AA,
|
||||
)
|
||||
|
||||
results.append(
|
||||
{
|
||||
"index": idx + 1,
|
||||
"station_name": name,
|
||||
"rect": [x1, y1, x2, y2],
|
||||
"center_y": center_y,
|
||||
"in_top_safe": in_top_safe,
|
||||
"in_bottom_safe": in_bottom_safe,
|
||||
"click_point": [click_x, click_y],
|
||||
}
|
||||
)
|
||||
|
||||
out_path = image_path.replace(".jpg", "_vlm_rect.jpg")
|
||||
cv2.imwrite(out_path, overlay)
|
||||
|
||||
logger.info(f"输入图片: {image_path}")
|
||||
logger.info(f"调试输出图片: {out_path}")
|
||||
logger.info("识别到的场站及矩形坐标如下:")
|
||||
for item in results:
|
||||
logger.info(json.dumps(item, ensure_ascii=False))
|
||||
async def run_test(image_path, log_path=None):
|
||||
await run_ocr_rect(image_path, log_path=log_path)
|
||||
|
||||
|
||||
async def run_batch():
|
||||
@@ -174,28 +19,24 @@ async def run_batch():
|
||||
|
||||
jpg_files = []
|
||||
for name in files:
|
||||
if not name.lower().endswith(".jpg"):
|
||||
lower = name.lower()
|
||||
if not lower.endswith(".jpg"):
|
||||
continue
|
||||
if name.lower().endswith("_vlm_rect.jpg"):
|
||||
if lower.endswith("_ocr_rect.jpg"):
|
||||
continue
|
||||
jpg_files.append(os.path.join(base_dir, name))
|
||||
|
||||
if not jpg_files:
|
||||
logger.info(f"目录中未找到待处理的 JPG 文件: {base_dir}")
|
||||
print(f"目录中未找到待处理的 JPG 文件: {base_dir}")
|
||||
return
|
||||
|
||||
logger.info(f"即将批量处理 {len(jpg_files)} 张图片:")
|
||||
print(f"即将批量处理 {len(jpg_files)} 张图片:")
|
||||
for p in jpg_files:
|
||||
logger.info(f" - {p}")
|
||||
print(f" - {p}")
|
||||
|
||||
for p in jpg_files:
|
||||
await run_test(p)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
# 单张测试示例:
|
||||
# test_image = r"d:\dsWork\aiData\Test\TestFirstPage\Screenshot_20260117_161824.jpg"
|
||||
# asyncio.run(run_test(test_image))
|
||||
|
||||
asyncio.run(run_batch())
|
||||
|
||||
|
||||