Repository navigation
Expand file tree
/
Copy pathdebug_window.py
More file actions
511 lines (458 loc) · 21.1 KB
/
Copy pathdebug_window.py
File metadata and controls
511 lines (458 loc) · 21.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
from __future__ import annotations
import os
import subprocess
import tkinter as tk
from tkinter import messagebox, ttk
import pyautogui
from PIL import Image, ImageDraw, ImageTk
from automation import activate_window, find_game_window, get_client_region, move_relative
from config import (
ANOMALY_LOG_FILE,
DEBUG_DIR,
DUPLICATE_TEXT_HINTS,
FAILED_TEXT_HINTS,
WAIT_TEXT_HINTS,
)
from layout_store import (
LAYOUT_OVERRIDE_FILE,
get_click_points,
get_ocr_rois,
load_override,
reset_to_defaults,
save_override,
set_click_point,
set_ocr_roi,
)
from vision.layout import point_from_relative, roi_from_relative
from vision.ocr import ocr_region_text
from vision.status_text import classify_status_text
class DebugWindow:
def __init__(self, master, log_callback, window_keyword_getter):
self.master = master
self.log = log_callback
self.window_keyword_getter = window_keyword_getter
self.top = tk.Toplevel(master)
self.top.title("坐标与 OCR 调试")
self.top.geometry("1360x820")
self.region: tuple[int, int, int, int] | None = None
self.base_image: Image.Image | None = None
self.photo: ImageTk.PhotoImage | None = None
self.selected_click: str | None = None
self.selected_roi: str | None = None
self.display_scale = 1.0
self.drag_mode: str | None = None # click | roi_move | roi_resize
self.drag_corner: str | None = None
self.drag_origin_img: tuple[float, float] | None = None
self.drag_roi_start: tuple[float, float, float, float] | None = None
self._build_ui()
self.refresh_lists()
def _build_ui(self) -> None:
toolbar = ttk.Frame(self.top, padding=10)
toolbar.pack(fill="x")
ttk.Label(toolbar, text="窗口标题关键字:").pack(side="left")
self.keyword_var = tk.StringVar(value=self.window_keyword_getter())
ttk.Entry(toolbar, textvariable=self.keyword_var, width=28).pack(side="left", padx=(6, 8))
ttk.Button(toolbar, text="抓取客户区", command=self.capture_client).pack(side="left", padx=3)
ttk.Button(toolbar, text="移动到选中点", command=self.move_to_selected).pack(side="left", padx=3)
ttk.Button(toolbar, text="点击选中点", command=self.click_selected).pack(side="left", padx=3)
ttk.Button(toolbar, text="OCR 选中 ROI", command=self.ocr_selected).pack(side="left", padx=3)
ttk.Button(toolbar, text="保存点位", command=self.save_layout).pack(side="left", padx=3)
ttk.Button(toolbar, text="重新加载", command=self.reload_layout).pack(side="left", padx=3)
ttk.Button(toolbar, text="恢复默认", command=self.restore_defaults).pack(side="left", padx=3)
ttk.Button(toolbar, text="打开截图目录", command=self.open_debug_dir).pack(side="left", padx=3)
hint = ttk.Label(
self.top,
text=(
"编辑:左侧选中点击点后,在预览上单击/拖拽十字;选中 ROI 后拖拽矩形主体移动,"
"拖拽角点缩放。改完点「保存点位」写入 layout_override.json。"
),
padding=(10, 0, 10, 6),
wraplength=1280,
)
hint.pack(fill="x")
main = ttk.Panedwindow(self.top, orient=tk.HORIZONTAL)
main.pack(fill="both", expand=True, padx=10, pady=(0, 10))
left = ttk.Frame(main)
right = ttk.Frame(main)
main.add(left, weight=1)
main.add(right, weight=3)
click_frame = ttk.LabelFrame(left, text="点击点", padding=6)
click_frame.pack(fill="both", expand=True, pady=(0, 6))
self.click_list = tk.Listbox(click_frame, height=7, exportselection=False)
self.click_list.pack(fill="both", expand=True)
self.click_list.bind("<<ListboxSelect>>", self.on_click_select)
roi_frame = ttk.LabelFrame(left, text="OCR ROI", padding=6)
roi_frame.pack(fill="both", expand=True, pady=(0, 6))
self.roi_list = tk.Listbox(roi_frame, height=5, exportselection=False)
self.roi_list.pack(fill="both", expand=True)
self.roi_list.bind("<<ListboxSelect>>", self.on_roi_select)
edit_frame = ttk.LabelFrame(left, text="数值编辑", padding=6)
edit_frame.pack(fill="x", pady=(0, 6))
self.edit_vars = {
"a": tk.StringVar(),
"b": tk.StringVar(),
"c": tk.StringVar(),
"d": tk.StringVar(),
}
self.edit_labels = []
for i, key in enumerate(("a", "b", "c", "d")):
row = ttk.Frame(edit_frame)
row.pack(fill="x", pady=2)
lab = ttk.Label(row, text=key, width=8)
lab.pack(side="left")
self.edit_labels.append(lab)
ttk.Entry(row, textvariable=self.edit_vars[key], width=14).pack(side="left")
ttk.Button(edit_frame, text="应用数值", command=self.apply_numeric).pack(anchor="e", pady=(4, 0))
info_frame = ttk.LabelFrame(left, text="选中信息", padding=6)
info_frame.pack(fill="x")
self.info_var = tk.StringVar(value="尚未抓取")
ttk.Label(info_frame, textvariable=self.info_var, wraplength=280, justify="left").pack(anchor="w")
ocr_frame = ttk.LabelFrame(left, text="OCR 结果", padding=6)
ocr_frame.pack(fill="both", expand=True, pady=(6, 0))
self.ocr_text = tk.Text(ocr_frame, height=7, wrap="word")
self.ocr_text.pack(fill="both", expand=True)
preview_frame = ttk.LabelFrame(right, text="预览(可拖拽编辑)", padding=6)
preview_frame.pack(fill="both", expand=True)
self.canvas = tk.Canvas(preview_frame, background="#222", cursor="crosshair")
self.canvas.pack(fill="both", expand=True)
self.canvas.bind("<ButtonPress-1>", self.on_canvas_press)
self.canvas.bind("<B1-Motion>", self.on_canvas_drag)
self.canvas.bind("<ButtonRelease-1>", self.on_canvas_release)
self.canvas.bind("<Configure>", lambda _e: self._redraw())
def refresh_lists(self) -> None:
click_sel = self.click_list.curselection()
roi_sel = self.roi_list.curselection()
self.click_list.delete(0, tk.END)
for name, (rx, ry) in get_click_points().items():
self.click_list.insert(tk.END, f"{name} ({rx:.3f}, {ry:.3f})")
self.roi_list.delete(0, tk.END)
for name, box in get_ocr_rois().items():
self.roi_list.insert(tk.END, f"{name} ({box[0]:.3f},{box[1]:.3f},{box[2]:.3f},{box[3]:.3f})")
if click_sel:
self.click_list.selection_set(click_sel[0])
if roi_sel:
self.roi_list.selection_set(roi_sel[0])
self._sync_edit_fields()
self._update_info()
self._redraw()
def on_click_select(self, _event=None) -> None:
sel = self.click_list.curselection()
if not sel:
return
self.selected_click = list(get_click_points().keys())[sel[0]]
self.selected_roi = None
self.roi_list.selection_clear(0, tk.END)
self._sync_edit_fields()
self._update_info()
self._redraw()
def on_roi_select(self, _event=None) -> None:
sel = self.roi_list.curselection()
if not sel:
return
self.selected_roi = list(get_ocr_rois().keys())[sel[0]]
self.selected_click = None
self.click_list.selection_clear(0, tk.END)
self._sync_edit_fields()
self._update_info()
self._redraw()
def _sync_edit_fields(self) -> None:
if self.selected_click:
rx, ry = get_click_points()[self.selected_click]
for lab, text in zip(self.edit_labels, ("rel_x", "rel_y", "-", "-")):
lab.configure(text=text)
self.edit_vars["a"].set(f"{rx:.4f}")
self.edit_vars["b"].set(f"{ry:.4f}")
self.edit_vars["c"].set("")
self.edit_vars["d"].set("")
elif self.selected_roi:
l, t, r, b = get_ocr_rois()[self.selected_roi]
for lab, text in zip(self.edit_labels, ("left", "top", "right", "bottom")):
lab.configure(text=text)
self.edit_vars["a"].set(f"{l:.4f}")
self.edit_vars["b"].set(f"{t:.4f}")
self.edit_vars["c"].set(f"{r:.4f}")
self.edit_vars["d"].set(f"{b:.4f}")
else:
for lab, text in zip(self.edit_labels, ("a", "b", "c", "d")):
lab.configure(text=text)
for v in self.edit_vars.values():
v.set("")
def apply_numeric(self) -> None:
try:
if self.selected_click:
rx = float(self.edit_vars["a"].get())
ry = float(self.edit_vars["b"].get())
set_click_point(self.selected_click, max(0.0, min(rx, 1.0)), max(0.0, min(ry, 1.0)))
elif self.selected_roi:
box = (
float(self.edit_vars["a"].get()),
float(self.edit_vars["b"].get()),
float(self.edit_vars["c"].get()),
float(self.edit_vars["d"].get()),
)
set_ocr_roi(self.selected_roi, box)
else:
messagebox.showwarning("提示", "请先选择点击点或 ROI。", parent=self.top)
return
except ValueError:
messagebox.showerror("错误", "请输入有效数字。", parent=self.top)
return
self.refresh_lists()
def _update_info(self) -> None:
if self.region is None:
self.info_var.set("尚未抓取客户区")
return
lines = [f"region={self.region} (Win32 client)"]
if self.selected_click:
rel = get_click_points()[self.selected_click]
pos = point_from_relative(self.region, rel[0], rel[1])
lines.append(f"click {self.selected_click}: rel={rel} screen={pos}")
if self.selected_roi:
box = get_ocr_rois()[self.selected_roi]
abs_roi = roi_from_relative(self.region, box)
lines.append(f"roi {self.selected_roi}: rel={box} abs={abs_roi}")
lines.append(f"override: {LAYOUT_OVERRIDE_FILE.resolve()}")
self.info_var.set("\n".join(lines))
def capture_client(self) -> None:
keyword = self.keyword_var.get().strip()
if not keyword:
messagebox.showwarning("提示", "请输入窗口标题关键字。", parent=self.top)
return
try:
window = find_game_window(keyword)
activate_window(window)
self.region = get_client_region(window)
shot = pyautogui.screenshot(region=self.region)
self.base_image = shot.convert("RGB")
self._update_info()
self._redraw()
outer = (window.left, window.top, window.width, window.height)
self.log(f"调试:客户区 {self.region} | 外框 {outer} | {window.title}")
except Exception as e:
messagebox.showerror("抓取失败", str(e), parent=self.top)
def _annotated_image(self) -> Image.Image | None:
if self.base_image is None or self.region is None:
return None
img = self.base_image.copy()
draw = ImageDraw.Draw(img)
left, top, _, _ = self.region
for name, (rx, ry) in get_click_points().items():
sx, sy = point_from_relative(self.region, rx, ry)
x, y = sx - left, sy - top
color = "#00ff66" if name == self.selected_click else "#ffcc00"
draw.line((x - 12, y, x + 12, y), fill=color, width=2)
draw.line((x, y - 12, x, y + 12), fill=color, width=2)
draw.ellipse((x - 4, y - 4, x + 4, y + 4), outline=color, width=2)
draw.text((x + 12, y - 8), name, fill=color)
for name, box in get_ocr_rois().items():
abs_roi = roi_from_relative(self.region, box)
x, y, w, h = abs_roi[0] - left, abs_roi[1] - top, abs_roi[2], abs_roi[3]
color = "#66ccff" if name == self.selected_roi else "#ff6699"
draw.rectangle((x, y, x + w, y + h), outline=color, width=2)
draw.text((x + 4, y + 4), name, fill=color)
if name == self.selected_roi:
for cx, cy in ((x, y), (x + w, y), (x, y + h), (x + w, y + h)):
draw.rectangle((cx - 4, cy - 4, cx + 4, cy + 4), fill=color)
return img
def _redraw(self) -> None:
img = self._annotated_image()
if img is None:
return
canvas_w = max(self.canvas.winfo_width(), 400)
canvas_h = max(self.canvas.winfo_height(), 300)
self.display_scale = min(canvas_w / img.width, canvas_h / img.height, 1.0)
show_w = max(int(img.width * self.display_scale), 1)
show_h = max(int(img.height * self.display_scale), 1)
show = img if self.display_scale >= 0.999 else img.resize(
(show_w, show_h),
Image.Resampling.LANCZOS,
)
self.photo = ImageTk.PhotoImage(show)
self.canvas.delete("all")
self.canvas.create_image(0, 0, anchor="nw", image=self.photo)
def _canvas_to_image(self, event_x: int, event_y: int) -> tuple[float, float] | None:
if self.base_image is None or self.display_scale <= 0:
return None
ix = event_x / self.display_scale
iy = event_y / self.display_scale
if ix < 0 or iy < 0 or ix > self.base_image.width or iy > self.base_image.height:
return None
return (ix, iy)
def _image_to_rel(self, ix: float, iy: float) -> tuple[float, float]:
assert self.base_image is not None
rx = max(0.0, min(ix / self.base_image.width, 1.0))
ry = max(0.0, min(iy / self.base_image.height, 1.0))
return (rx, ry)
def _hit_roi_corner(
self, ix: float, iy: float, box: tuple[float, float, float, float]
) -> str | None:
assert self.base_image is not None
w, h = self.base_image.width, self.base_image.height
corners = {
"tl": (box[0] * w, box[1] * h),
"tr": (box[2] * w, box[1] * h),
"bl": (box[0] * w, box[3] * h),
"br": (box[2] * w, box[3] * h),
}
for name, (cx, cy) in corners.items():
if abs(ix - cx) <= 10 and abs(iy - cy) <= 10:
return name
return None
def _hit_roi_body(
self, ix: float, iy: float, box: tuple[float, float, float, float]
) -> bool:
assert self.base_image is not None
w, h = self.base_image.width, self.base_image.height
x1, y1, x2, y2 = box[0] * w, box[1] * h, box[2] * w, box[3] * h
return x1 <= ix <= x2 and y1 <= iy <= y2
def on_canvas_press(self, event) -> None:
img_pt = self._canvas_to_image(event.x, event.y)
if img_pt is None or self.region is None:
return
ix, iy = img_pt
if self.selected_roi:
box = get_ocr_rois()[self.selected_roi]
corner = self._hit_roi_corner(ix, iy, box)
if corner:
self.drag_mode = "roi_resize"
self.drag_corner = corner
self.drag_roi_start = box
self.drag_origin_img = (ix, iy)
return
if self._hit_roi_body(ix, iy, box):
self.drag_mode = "roi_move"
self.drag_corner = None
self.drag_roi_start = box
self.drag_origin_img = (ix, iy)
return
# Click outside: set top-left and start resize to br
rx, ry = self._image_to_rel(ix, iy)
set_ocr_roi(self.selected_roi, (rx, ry, min(rx + 0.05, 1.0), min(ry + 0.05, 1.0)))
self.drag_mode = "roi_resize"
self.drag_corner = "br"
self.drag_roi_start = get_ocr_rois()[self.selected_roi]
self.drag_origin_img = (ix, iy)
self.refresh_lists()
return
if self.selected_click:
rx, ry = self._image_to_rel(ix, iy)
set_click_point(self.selected_click, rx, ry)
self.drag_mode = "click"
self.refresh_lists()
return
messagebox.showinfo("提示", "请先在左侧选择要点击点或 ROI,再在预览上编辑。", parent=self.top)
def on_canvas_drag(self, event) -> None:
if not self.drag_mode or self.base_image is None:
return
img_pt = self._canvas_to_image(event.x, event.y)
if img_pt is None:
return
ix, iy = img_pt
if self.drag_mode == "click" and self.selected_click:
rx, ry = self._image_to_rel(ix, iy)
set_click_point(self.selected_click, rx, ry)
self.refresh_lists()
return
if self.selected_roi and self.drag_roi_start and self.drag_origin_img:
w, h = self.base_image.width, self.base_image.height
l, t, r, b = self.drag_roi_start
if self.drag_mode == "roi_move":
dx = (ix - self.drag_origin_img[0]) / w
dy = (iy - self.drag_origin_img[1]) / h
width = r - l
height = b - t
nl = max(0.0, min(l + dx, 1.0 - width))
nt = max(0.0, min(t + dy, 1.0 - height))
set_ocr_roi(self.selected_roi, (nl, nt, nl + width, nt + height))
elif self.drag_mode == "roi_resize" and self.drag_corner:
rx, ry = self._image_to_rel(ix, iy)
nl, nt, nr, nb = l, t, r, b
if "l" in self.drag_corner:
nl = rx
if "r" in self.drag_corner:
nr = rx
if "t" in self.drag_corner:
nt = ry
if "b" in self.drag_corner:
nb = ry
set_ocr_roi(self.selected_roi, (nl, nt, nr, nb))
self.refresh_lists()
def on_canvas_release(self, _event) -> None:
self.drag_mode = None
self.drag_corner = None
self.drag_origin_img = None
self.drag_roi_start = None
def save_layout(self) -> None:
path = save_override()
self.log(f"调试:已保存点位到 {path.resolve()}")
messagebox.showinfo("已保存", f"已写入\n{path.resolve()}", parent=self.top)
self._update_info()
def reload_layout(self) -> None:
reset_to_defaults()
loaded = load_override()
self.refresh_lists()
self.log("调试:已重新加载 " + ("override" if loaded else "默认点位"))
def restore_defaults(self) -> None:
if not messagebox.askyesno("恢复默认", "丢弃当前编辑并恢复 config 默认点位?", parent=self.top):
return
reset_to_defaults()
if LAYOUT_OVERRIDE_FILE.exists():
LAYOUT_OVERRIDE_FILE.unlink()
self.refresh_lists()
self.log("调试:已恢复默认点位并删除 layout_override.json")
def _require_region_and_click(self) -> tuple[str, tuple[float, float]] | None:
if self.region is None:
messagebox.showwarning("提示", "请先抓取客户区。", parent=self.top)
return None
if not self.selected_click:
messagebox.showwarning("提示", "请先选择点击点。", parent=self.top)
return None
return self.selected_click, get_click_points()[self.selected_click]
def move_to_selected(self) -> None:
selected = self._require_region_and_click()
if not selected:
return
name, (rx, ry) = selected
pos = move_relative(self.region, rx, ry)
self.log(f"调试:移动到 {name} {pos}")
def click_selected(self) -> None:
selected = self._require_region_and_click()
if not selected:
return
name, (rx, ry) = selected
pos = point_from_relative(self.region, rx, ry)
pyautogui.click(pos[0], pos[1])
self.log(f"调试:点击 {name} {pos}")
def ocr_selected(self) -> None:
if self.region is None:
messagebox.showwarning("提示", "请先抓取客户区。", parent=self.top)
return
if not self.selected_roi:
messagebox.showwarning("提示", "请先选择 OCR ROI。", parent=self.top)
return
abs_roi = roi_from_relative(self.region, get_ocr_rois()[self.selected_roi])
text = ocr_region_text(abs_roi)
classified = classify_status_text(
text,
FAILED_TEXT_HINTS,
DUPLICATE_TEXT_HINTS,
WAIT_TEXT_HINTS,
)
compact = " ".join(text.split())
self.ocr_text.delete("1.0", tk.END)
self.ocr_text.insert(
"1.0",
f"ROI: {self.selected_roi}\nabs: {abs_roi}\nclassify: {classified or 'NONE'}\n\n{compact}",
)
self.log(f"调试 OCR {self.selected_roi}: {classified or 'NONE'} / {compact[:80]!r}")
def open_debug_dir(self) -> None:
DEBUG_DIR.mkdir(parents=True, exist_ok=True)
self._open_path(DEBUG_DIR.resolve())
def _open_path(self, path) -> None:
try:
if os.name == "nt":
os.startfile(path) # type: ignore[attr-defined]
else:
subprocess.Popen(["xdg-open", str(path)])
except Exception as e:
messagebox.showerror("打开失败", str(e), parent=self.top)