tool.py 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435
  1. # This Python file uses the following encoding: utf-8
  2. # @author runhey
  3. # github https://github.com/runhey
  4. import json
  5. import re
  6. import shutil
  7. import threading
  8. import time
  9. import uuid
  10. import base64
  11. from dataclasses import dataclass
  12. from pathlib import Path
  13. from typing import Any
  14. import cv2
  15. import numpy as np
  16. from filelock import FileLock
  17. from dev_tools.assets_extract import AssetsExtractor
  18. from dev_tools.assets_test import detect_image_detail, detect_ocr_detail
  19. from module.config.atomicwrites import atomic_write
  20. from module.config.config import Config
  21. from module.device.device import Device
  22. from module.logger import logger
  23. from module.server.config_manager import ConfigManager
  24. logger.set_file_logger('tool', do_cleanup=True)
  25. PROJECT_ROOT = Path.cwd().resolve()
  26. TASKS_ROOT = (PROJECT_ROOT / "tasks").resolve()
  27. ANNOTATOR_ROOT = (PROJECT_ROOT / "log" / "annotator").resolve()
  28. ALLOWED_IMAGE_EXT = {".png", ".jpg", ".jpeg", ".bmp", ".webp"}
  29. ALLOWED_OCR_MODE = {"Single", "Full", "Digit", "DigitCounter", "Duration"}
  30. ALLOWED_LIST_DIRECTION = {"vertical", "horizontal"}
  31. ALLOWED_LIST_MODE = {"image", "ocr"}
  32. ALLOWED_SWIPE_MODE = {"default", "vector"}
  33. SESSION_IDLE_TIMEOUT_SECONDS = 10 * 60
  34. SESSION_SWEEP_INTERVAL_SECONDS = 30
  35. EMULATOR_CAPTURE_MAX_RETRIES = 3
  36. EMULATOR_CAPTURE_RETRY_BACKOFF_SECONDS = 1.0
  37. class AnnotatorError(Exception):
  38. def __init__(self, code: str, message: str, status_code: int = 400) -> None:
  39. super().__init__(message)
  40. self.code = code
  41. self.message = message
  42. self.status_code = status_code
  43. @dataclass
  44. class SessionImage:
  45. image_id: str
  46. path: Path
  47. source: str
  48. original_name: str
  49. created_at: float
  50. def to_dict(self, session_id: str) -> dict[str, Any]:
  51. url = f"/tool/annotator/api/images/{session_id}/{self.image_id}"
  52. return {
  53. "id": self.image_id,
  54. "name": self.original_name,
  55. "source": self.source,
  56. "created_at": self.created_at,
  57. "url": url,
  58. "thumb_url": url,
  59. }
  60. class EmulatorCaptureSession:
  61. def __init__(self, session_id: str) -> None:
  62. self.session_id = session_id
  63. self.state = "stopped"
  64. self.config_name = ""
  65. self.frame_rate = 2
  66. self.error = ""
  67. self._thread: threading.Thread | None = None
  68. self._stop_event = threading.Event()
  69. self._frame_lock = threading.Lock()
  70. self._latest_frame = None
  71. self._latest_jpeg: bytes | None = None
  72. self._updated_at = 0.0
  73. self._retry_count = 0
  74. self._max_retries = EMULATOR_CAPTURE_MAX_RETRIES
  75. self._last_error_at = 0.0
  76. def status(self) -> dict[str, Any]:
  77. with self._frame_lock:
  78. return {
  79. "state": self.state,
  80. "config_name": self.config_name,
  81. "frame_rate": self.frame_rate,
  82. "error": self.error,
  83. "updated_at": self._updated_at,
  84. "last_frame_at": self._updated_at,
  85. "retry_count": self._retry_count,
  86. "max_retries": self._max_retries,
  87. "last_error_at": self._last_error_at,
  88. }
  89. def start(self, config_name: str, frame_rate: int) -> int:
  90. self.stop(clear_error=True)
  91. with self._frame_lock:
  92. self.config_name = config_name
  93. self.frame_rate = max(1, min(int(frame_rate), 10))
  94. self.state = "starting"
  95. self.error = ""
  96. self._updated_at = 0.0
  97. self._retry_count = 0
  98. self._last_error_at = 0.0
  99. self._latest_frame = None
  100. self._latest_jpeg = None
  101. self._stop_event.clear()
  102. self._thread = threading.Thread(target=self._run, daemon=True, name=f"annotator_capture_{self.session_id}")
  103. self._thread.start()
  104. return self.frame_rate
  105. def stop(self, clear_error: bool = False) -> None:
  106. self._stop_event.set()
  107. thread = self._thread
  108. if thread and thread.is_alive():
  109. thread.join(timeout=2.5)
  110. self._thread = None
  111. with self._frame_lock:
  112. self.state = "stopped"
  113. self._retry_count = 0
  114. if clear_error:
  115. self.error = ""
  116. self._last_error_at = 0.0
  117. def _build_device(self, config: Config, interval: float) -> Device:
  118. device = Device(config=config)
  119. device.disable_stuck_detection()
  120. device.screenshot_interval_set(interval)
  121. logger.info(
  122. f"[annotator] emulator device ready, session={self.session_id}, "
  123. f"config={self.config_name}, interval={interval:.3f}"
  124. )
  125. return device
  126. @staticmethod
  127. def _release_device(device: Device | None) -> None:
  128. if device is None:
  129. return
  130. try:
  131. device.release_during_wait()
  132. except Exception:
  133. pass
  134. @staticmethod
  135. def _format_error_message(error: Exception) -> str:
  136. text = str(error).strip()
  137. if text:
  138. return text
  139. return error.__class__.__name__
  140. def _mark_capture_failure(self, stage: str, error: Exception) -> tuple[bool, int, str]:
  141. message = self._format_error_message(error)
  142. with self._frame_lock:
  143. self.error = message
  144. self._last_error_at = time.time()
  145. self._retry_count += 1
  146. attempt = self._retry_count
  147. should_retry = attempt <= self._max_retries and not self._stop_event.is_set()
  148. self.state = "starting" if should_retry else "error"
  149. if should_retry:
  150. logger.warning(
  151. f"[annotator] emulator capture retry, session={self.session_id}, config={self.config_name}, "
  152. f"stage={stage}, attempt={attempt}/{self._max_retries}, error={message}"
  153. )
  154. else:
  155. logger.error(
  156. f"[annotator] emulator capture failed, session={self.session_id}, config={self.config_name}, "
  157. f"stage={stage}, attempt={attempt}/{self._max_retries}, error={message}"
  158. )
  159. return should_retry, attempt, message
  160. def _run(self) -> None:
  161. device: Device | None = None
  162. interval = max(0.1, 1.0 / float(self.frame_rate))
  163. try:
  164. config = Config(config_name=self.config_name)
  165. while not self._stop_event.is_set():
  166. if device is None:
  167. try:
  168. device = self._build_device(config, interval)
  169. with self._frame_lock:
  170. self.state = "running"
  171. except Exception as e:
  172. should_retry, attempt, _ = self._mark_capture_failure("connect", e)
  173. if not should_retry:
  174. break
  175. if self._stop_event.wait(EMULATOR_CAPTURE_RETRY_BACKOFF_SECONDS * attempt):
  176. break
  177. continue
  178. try:
  179. frame = device.screenshot()
  180. except Exception as e:
  181. self._release_device(device)
  182. device = None
  183. should_retry, attempt, _ = self._mark_capture_failure("capture", e)
  184. if not should_retry:
  185. break
  186. if self._stop_event.wait(EMULATOR_CAPTURE_RETRY_BACKOFF_SECONDS * attempt):
  187. break
  188. continue
  189. frame_bgr = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
  190. ok, buf = cv2.imencode(".jpg", frame_bgr)
  191. if ok:
  192. with self._frame_lock:
  193. self._latest_frame = frame_bgr.copy()
  194. self._latest_jpeg = buf.tobytes()
  195. self._updated_at = time.time()
  196. self._retry_count = 0
  197. self.error = ""
  198. self.state = "running"
  199. if self._stop_event.wait(interval):
  200. break
  201. except Exception:
  202. with self._frame_lock:
  203. self.state = "error"
  204. self._last_error_at = time.time()
  205. if not self.error:
  206. self.error = "模拟器采集线程异常退出"
  207. logger.error(f"[annotator] emulator capture loop crashed, session={self.session_id}, config={self.config_name}")
  208. finally:
  209. self._release_device(device)
  210. if self.state != "error":
  211. with self._frame_lock:
  212. self.state = "stopped"
  213. def latest_jpeg(self) -> bytes | None:
  214. with self._frame_lock:
  215. if self.state != "running" or self._latest_jpeg is None:
  216. return None
  217. return bytes(self._latest_jpeg)
  218. def capture_latest_frame(self, output_file: Path) -> None:
  219. with self._frame_lock:
  220. state = self.state
  221. error = self.error
  222. frame = None if self._latest_frame is None else self._latest_frame.copy()
  223. if state != "running":
  224. if state == "error":
  225. raise AnnotatorError("emulator_error", f"模拟器画面不可用: {error or 'unknown'}", 409)
  226. if state == "starting":
  227. raise AnnotatorError("emulator_starting", "模拟器画面尚未就绪,请稍后重试", 409)
  228. raise AnnotatorError("emulator_not_running", "模拟器画面未启动", 409)
  229. if frame is None:
  230. raise AnnotatorError("no_frame", "当前没有可用帧,无法截图", 400)
  231. ok = cv2.imwrite(str(output_file), frame)
  232. if not ok:
  233. raise AnnotatorError("capture_failed", "保存截图失败", 500)
  234. class AnnotatorSession:
  235. def __init__(self, session_id: str) -> None:
  236. self.session_id = session_id
  237. self.root = ANNOTATOR_ROOT / session_id
  238. self.upload_dir = self.root / "uploads"
  239. self.capture_dir = self.root / "captures"
  240. self.root.mkdir(parents=True, exist_ok=True)
  241. self.upload_dir.mkdir(parents=True, exist_ok=True)
  242. self.capture_dir.mkdir(parents=True, exist_ok=True)
  243. self.images: dict[str, SessionImage] = {}
  244. self.image_order: list[str] = []
  245. self.current_image_id: str | None = None
  246. self.lock = threading.Lock()
  247. self.capture_session = EmulatorCaptureSession(session_id)
  248. self.last_active_at = time.time()
  249. def touch(self) -> float:
  250. now = time.time()
  251. with self.lock:
  252. self.last_active_at = now
  253. return now
  254. def get_last_active_at(self) -> float:
  255. with self.lock:
  256. return self.last_active_at
  257. def shutdown(self) -> None:
  258. self.capture_session.stop(clear_error=True)
  259. def add_image(self, image_file: Path, source: str, original_name: str) -> SessionImage:
  260. image_id = uuid.uuid4().hex
  261. image = SessionImage(
  262. image_id=image_id,
  263. path=image_file,
  264. source=source,
  265. original_name=original_name,
  266. created_at=time.time(),
  267. )
  268. with self.lock:
  269. self.images[image_id] = image
  270. self.image_order.append(image_id)
  271. self.current_image_id = image_id
  272. self.last_active_at = time.time()
  273. return image
  274. def remove_image(self, image_id: str) -> bool:
  275. with self.lock:
  276. image = self.images.pop(image_id, None)
  277. if image is None:
  278. return False
  279. self.image_order = [x for x in self.image_order if x != image_id]
  280. if self.current_image_id == image_id:
  281. self.current_image_id = self.image_order[-1] if self.image_order else None
  282. self.last_active_at = time.time()
  283. try:
  284. if image.path.exists():
  285. image.path.unlink()
  286. except Exception:
  287. logger.warning(f"[annotator] remove image file failed: {image.path}")
  288. return True
  289. def remove_images(self, image_ids: list[str]) -> int:
  290. removed = 0
  291. for image_id in image_ids:
  292. if self.remove_image(image_id):
  293. removed += 1
  294. return removed
  295. def clear_images(self) -> int:
  296. with self.lock:
  297. image_ids = list(self.image_order)
  298. return self.remove_images(image_ids)
  299. def snapshot(self) -> dict[str, Any]:
  300. with self.lock:
  301. images = [self.images[i].to_dict(self.session_id) for i in self.image_order if i in self.images]
  302. return {
  303. "session_id": self.session_id,
  304. "current_image_id": self.current_image_id,
  305. "updated_at": self.last_active_at,
  306. "images": images,
  307. "emulator": self.capture_session.status(),
  308. }
  309. class AnnotatorManager:
  310. def __init__(self) -> None:
  311. self._sessions: dict[str, AnnotatorSession] = {}
  312. self._lock = threading.Lock()
  313. self._cleanup_lock = threading.Lock()
  314. self._last_cleanup_at = 0.0
  315. self._session_idle_timeout = SESSION_IDLE_TIMEOUT_SECONDS
  316. self._session_sweep_interval = SESSION_SWEEP_INTERVAL_SECONDS
  317. self._cleanup_stop = threading.Event()
  318. self._cleanup_thread = threading.Thread(
  319. target=self._session_cleanup_loop,
  320. daemon=True,
  321. name="annotator_session_cleanup",
  322. )
  323. ANNOTATOR_ROOT.mkdir(parents=True, exist_ok=True)
  324. self._cleanup_thread.start()
  325. def _session_cleanup_loop(self) -> None:
  326. while not self._cleanup_stop.wait(self._session_sweep_interval):
  327. try:
  328. self._cleanup_expired_sessions(force=True)
  329. except Exception:
  330. logger.exception("[annotator] cleanup loop failed")
  331. def _cleanup_expired_sessions(self, force: bool = False) -> None:
  332. now = time.time()
  333. if not force and now - self._last_cleanup_at < self._session_sweep_interval:
  334. return
  335. with self._cleanup_lock:
  336. now = time.time()
  337. if not force and now - self._last_cleanup_at < self._session_sweep_interval:
  338. return
  339. self._last_cleanup_at = now
  340. expired: list[str] = []
  341. with self._lock:
  342. for session_id, session in self._sessions.items():
  343. idle_seconds = now - session.get_last_active_at()
  344. if idle_seconds >= self._session_idle_timeout:
  345. expired.append(session_id)
  346. for session_id in expired:
  347. self.close_session(session_id, reason="timeout")
  348. @staticmethod
  349. def index_file() -> Path:
  350. return Path(__file__).resolve().parent / "web" / "annotator" / "index.html"
  351. @staticmethod
  352. def static_dir() -> Path:
  353. return Path(__file__).resolve().parent / "web" / "annotator"
  354. def _cleanup_session_dir_path(self, session_id: str, target: Path, reason: str) -> bool:
  355. target = target.resolve()
  356. self._ensure_within_root(target, ANNOTATOR_ROOT)
  357. if target == ANNOTATOR_ROOT:
  358. raise AnnotatorError("invalid_path", "会话目录无效", 500)
  359. if not target.exists():
  360. return False
  361. if not target.is_dir():
  362. logger.warning(
  363. f"[annotator] skip non-dir session cleanup, session={session_id}, reason={reason}, target={target}"
  364. )
  365. return False
  366. shutil.rmtree(target)
  367. logger.info(f"[annotator] session dir removed, session={session_id}, reason={reason}, dir={target}")
  368. return True
  369. def _cleanup_session_dir(self, session: AnnotatorSession, reason: str) -> bool:
  370. return self._cleanup_session_dir_path(session.session_id, session.root, reason)
  371. def _cleanup_foreign_session_dirs(self, keep_session_id: str, reason: str) -> int:
  372. removed = 0
  373. if not ANNOTATOR_ROOT.exists():
  374. return removed
  375. for target in ANNOTATOR_ROOT.iterdir():
  376. if target.name == keep_session_id:
  377. continue
  378. try:
  379. if self._cleanup_session_dir_path(target.name, target, reason):
  380. removed += 1
  381. except Exception:
  382. logger.exception(
  383. f"[annotator] foreign session dir cleanup failed, session={target.name}, reason={reason}"
  384. )
  385. return removed
  386. def _replace_active_sessions(self, session: AnnotatorSession, reason: str) -> int:
  387. with self._lock:
  388. replaced_sessions = [
  389. (session_id, existing)
  390. for session_id, existing in self._sessions.items()
  391. if session_id != session.session_id
  392. ]
  393. self._sessions = {session.session_id: session}
  394. for session_id, existing in replaced_sessions:
  395. try:
  396. existing.shutdown()
  397. except Exception:
  398. logger.exception(
  399. f"[annotator] old session shutdown failed, session={session_id}, reason={reason}"
  400. )
  401. try:
  402. self._cleanup_session_dir(existing, reason)
  403. except Exception:
  404. logger.exception(
  405. f"[annotator] old session dir cleanup failed, session={session_id}, reason={reason}"
  406. )
  407. return len(replaced_sessions)
  408. def close_session(self, session_id: str, reason: str = "manual", raise_if_missing: bool = False) -> dict[str, Any]:
  409. with self._lock:
  410. session = self._sessions.pop(session_id, None)
  411. if session is None:
  412. if raise_if_missing:
  413. raise AnnotatorError("invalid_session", f"会话不存在: {session_id}", 404)
  414. return {
  415. "session_id": session_id,
  416. "closed": False,
  417. "reason": reason,
  418. "dir_removed": False,
  419. }
  420. session.shutdown()
  421. dir_removed = False
  422. try:
  423. dir_removed = self._cleanup_session_dir(session, reason)
  424. except Exception:
  425. logger.exception(f"[annotator] session dir cleanup failed, session={session_id}, reason={reason}")
  426. logger.info(
  427. f"[annotator] session closed, session={session_id}, reason={reason}, dir_removed={dir_removed}"
  428. )
  429. return {
  430. "session_id": session_id,
  431. "closed": True,
  432. "reason": reason,
  433. "dir_removed": dir_removed,
  434. }
  435. def create_session(self) -> dict[str, Any]:
  436. self._cleanup_expired_sessions()
  437. with self._cleanup_lock:
  438. session_id = uuid.uuid4().hex
  439. session = AnnotatorSession(session_id)
  440. replace_reason = f"replaced_by:{session_id}"
  441. replaced_count = self._replace_active_sessions(session, replace_reason)
  442. orphan_removed = self._cleanup_foreign_session_dirs(
  443. keep_session_id=session_id,
  444. reason=f"create_session:{session_id}",
  445. )
  446. logger.info(
  447. f"[annotator] session created, session={session_id}, "
  448. f"replaced_sessions={replaced_count}, removed_other_dirs={orphan_removed}"
  449. )
  450. return session.snapshot()
  451. def _get_session(self, session_id: str) -> AnnotatorSession:
  452. self._cleanup_expired_sessions()
  453. with self._lock:
  454. session = self._sessions.get(session_id)
  455. if session is None:
  456. raise AnnotatorError("invalid_session", f"会话不存在: {session_id}", 404)
  457. session.touch()
  458. return session
  459. def get_session_snapshot(self, session_id: str) -> dict[str, Any]:
  460. return self._get_session(session_id).snapshot()
  461. @staticmethod
  462. def _safe_stem(filename: str) -> str:
  463. stem = Path(filename).stem.strip()
  464. stem = re.sub(r"[^a-zA-Z0-9_\-]", "_", stem)
  465. return stem or "image"
  466. def save_uploaded_images_base64(self, session_id: str, images_payload: list[dict[str, Any]]) -> list[dict[str, Any]]:
  467. session = self._get_session(session_id)
  468. results: list[dict[str, Any]] = []
  469. for payload in images_payload:
  470. filename = str(payload.get("name", "")).strip()
  471. content_base64 = str(payload.get("content_base64", "")).strip()
  472. if not filename:
  473. continue
  474. if not content_base64:
  475. continue
  476. suffix = Path(filename).suffix.lower()
  477. if not suffix:
  478. suffix = ".png"
  479. if suffix not in ALLOWED_IMAGE_EXT:
  480. raise AnnotatorError("invalid_image_ext", f"不支持的图片类型: {suffix}", 400)
  481. image_name = f"{uuid.uuid4().hex}_{self._safe_stem(filename)}{suffix}"
  482. target = session.upload_dir / image_name
  483. if "," in content_base64:
  484. content_base64 = content_base64.split(",", 1)[1]
  485. try:
  486. content = base64.b64decode(content_base64)
  487. except Exception as e:
  488. raise AnnotatorError("invalid_image_data", f"图片编码解析失败: {filename}", 400) from e
  489. target.write_bytes(content)
  490. image = session.add_image(target, "upload", filename)
  491. results.append(image.to_dict(session_id))
  492. if not results:
  493. raise AnnotatorError("empty_upload", "未上传有效图片", 400)
  494. logger.info(f"[annotator] upload images, session={session_id}, count={len(results)}")
  495. return results
  496. def list_images(self, session_id: str) -> list[dict[str, Any]]:
  497. session = self._get_session(session_id)
  498. return session.snapshot()["images"]
  499. def get_image_file(self, session_id: str, image_id: str) -> Path:
  500. session = self._get_session(session_id)
  501. image = session.images.get(image_id)
  502. if image is None:
  503. raise AnnotatorError("image_not_found", f"图片不存在: {image_id}", 404)
  504. return image.path
  505. def delete_image(self, session_id: str, image_id: str) -> dict[str, Any]:
  506. session = self._get_session(session_id)
  507. removed = session.remove_image(image_id)
  508. if not removed:
  509. raise AnnotatorError("image_not_found", f"图片不存在: {image_id}", 404)
  510. logger.info(f"[annotator] image removed, session={session_id}, image={image_id}")
  511. return session.snapshot()
  512. def delete_images(self, session_id: str, image_ids: list[str]) -> dict[str, Any]:
  513. if not image_ids:
  514. raise AnnotatorError("empty_image_ids", "image_ids 不能为空", 400)
  515. session = self._get_session(session_id)
  516. removed = session.remove_images(image_ids)
  517. logger.info(f"[annotator] images removed, session={session_id}, removed={removed}")
  518. return {"removed_count": removed, "session": session.snapshot()}
  519. def clear_images(self, session_id: str) -> dict[str, Any]:
  520. session = self._get_session(session_id)
  521. removed = session.clear_images()
  522. logger.info(f"[annotator] images cleared, session={session_id}, removed={removed}")
  523. return {"removed_count": removed, "session": session.snapshot()}
  524. @staticmethod
  525. def list_task_names() -> list[str]:
  526. tasks = []
  527. for d in TASKS_ROOT.iterdir():
  528. if not d.is_dir():
  529. continue
  530. if d.name.startswith("__"):
  531. continue
  532. tasks.append(d.name)
  533. return sorted(tasks)
  534. def list_configs(self) -> list[str]:
  535. return ConfigManager.all_script_files()
  536. def start_emulator(self, session_id: str, config_name: str, frame_rate: int) -> dict[str, Any]:
  537. session = self._get_session(session_id)
  538. if config_name not in self.list_configs():
  539. raise AnnotatorError("invalid_config", f"配置不存在: {config_name}", 400)
  540. applied_rate = session.capture_session.start(config_name, frame_rate)
  541. logger.info(
  542. f"[annotator] emulator start, session={session_id}, config={config_name}, frame_rate={applied_rate}"
  543. )
  544. return session.capture_session.status()
  545. def stop_emulator(self, session_id: str) -> dict[str, Any]:
  546. session = self._get_session(session_id)
  547. session.capture_session.stop(clear_error=True)
  548. logger.info(f"[annotator] emulator stop, session={session_id}")
  549. return session.capture_session.status()
  550. def emulator_status(self, session_id: str) -> dict[str, Any]:
  551. session = self._get_session(session_id)
  552. return session.capture_session.status()
  553. def latest_emulator_frame(self, session_id: str) -> bytes | None:
  554. session = self._get_session(session_id)
  555. return session.capture_session.latest_jpeg()
  556. def capture_from_emulator(self, session_id: str) -> dict[str, Any]:
  557. session = self._get_session(session_id)
  558. target = session.capture_dir / f"capture_{int(time.time() * 1000)}.png"
  559. session.capture_session.capture_latest_frame(target)
  560. image = session.add_image(target, "capture", target.name)
  561. logger.info(f"[annotator] capture frame, session={session_id}, target={target}")
  562. return image.to_dict(session_id)
  563. @staticmethod
  564. def _format_roi_number(value: float) -> str:
  565. rounded = round(value)
  566. if abs(value - rounded) < 1e-6:
  567. return str(int(rounded))
  568. text = f"{value:.4f}".rstrip("0").rstrip(".")
  569. return text or "0"
  570. @staticmethod
  571. def _parse_roi(value: str) -> str:
  572. if not isinstance(value, str):
  573. raise AnnotatorError("invalid_roi", "ROI 必须是字符串", 400)
  574. parts = [p.strip() for p in value.split(",")]
  575. if len(parts) != 4:
  576. raise AnnotatorError("invalid_roi", "ROI 必须是 x,y,w,h", 400)
  577. try:
  578. x, y, w, h = [float(p) for p in parts]
  579. except ValueError as e:
  580. raise AnnotatorError("invalid_roi", "ROI 必须是数字", 400) from e
  581. if w <= 0 or h <= 0:
  582. raise AnnotatorError("invalid_roi", "ROI 的宽高必须大于 0", 400)
  583. return ",".join(
  584. [
  585. AnnotatorManager._format_roi_number(x),
  586. AnnotatorManager._format_roi_number(y),
  587. AnnotatorManager._format_roi_number(w),
  588. AnnotatorManager._format_roi_number(h),
  589. ]
  590. )
  591. def _normalize_image_rules(self, rules: list[dict[str, Any]]) -> list[dict[str, Any]]:
  592. normalized: list[dict[str, Any]] = []
  593. for index, rule in enumerate(rules):
  594. item_name = str(rule.get("itemName", "")).strip()
  595. if not item_name:
  596. raise AnnotatorError("invalid_rule", f"第 {index + 1} 条规则缺少 itemName", 400)
  597. image_name = str(rule.get("imageName", "")).strip() or f"{item_name}.png"
  598. threshold_value = rule.get("threshold", 0.8)
  599. try:
  600. threshold = float(threshold_value)
  601. except (TypeError, ValueError) as e:
  602. raise AnnotatorError("invalid_rule", f"第 {index + 1} 条规则 threshold 非法", 400) from e
  603. if threshold < 0 or threshold > 1:
  604. raise AnnotatorError("invalid_rule", f"第 {index + 1} 条规则 threshold 必须在 0-1", 400)
  605. normalized.append(
  606. {
  607. "itemName": item_name,
  608. "imageName": image_name,
  609. "roiFront": self._parse_roi(str(rule.get("roiFront", ""))),
  610. "roiBack": self._parse_roi(str(rule.get("roiBack", ""))),
  611. "method": str(rule.get("method", "Template matching")).strip() or "Template matching",
  612. "threshold": threshold,
  613. "description": str(rule.get("description", "")).strip(),
  614. }
  615. )
  616. return normalized
  617. def _normalize_ocr_rules(self, rules: list[dict[str, Any]]) -> list[dict[str, Any]]:
  618. normalized: list[dict[str, Any]] = []
  619. for index, rule in enumerate(rules):
  620. item_name = str(rule.get("itemName", "")).strip()
  621. if not item_name:
  622. raise AnnotatorError("invalid_rule", f"第 {index + 1} 条规则缺少 itemName", 400)
  623. mode = str(rule.get("mode", "Single")).strip() or "Single"
  624. if mode not in ALLOWED_OCR_MODE:
  625. raise AnnotatorError("invalid_rule", f"第 {index + 1} 条规则 mode 不支持: {mode}", 400)
  626. normalized.append(
  627. {
  628. "itemName": item_name,
  629. "roiFront": self._parse_roi(str(rule.get("roiFront", ""))),
  630. "roiBack": self._parse_roi(str(rule.get("roiBack", ""))),
  631. "mode": mode,
  632. "method": str(rule.get("method", "Default")).strip() or "Default",
  633. "keyword": str(rule.get("keyword", "")).strip(),
  634. "description": str(rule.get("description", "")).strip(),
  635. }
  636. )
  637. return normalized
  638. def _normalize_click_rules(self, rules: list[dict[str, Any]]) -> list[dict[str, Any]]:
  639. normalized: list[dict[str, Any]] = []
  640. for index, rule in enumerate(rules):
  641. item_name = str(rule.get("itemName", "")).strip()
  642. if not item_name:
  643. raise AnnotatorError("invalid_rule", f"第 {index + 1} 条点击规则缺少 itemName", 400)
  644. normalized.append(
  645. {
  646. "itemName": item_name,
  647. "roiFront": self._parse_roi(str(rule.get("roiFront", ""))),
  648. "roiBack": self._parse_roi(str(rule.get("roiBack", ""))),
  649. "description": str(rule.get("description", "")).strip(),
  650. }
  651. )
  652. return normalized
  653. def _normalize_swipe_rules(self, rules: list[dict[str, Any]]) -> list[dict[str, Any]]:
  654. normalized: list[dict[str, Any]] = []
  655. for index, rule in enumerate(rules):
  656. item_name = str(rule.get("itemName", "")).strip()
  657. if not item_name:
  658. raise AnnotatorError("invalid_rule", f"第 {index + 1} 条滑动规则缺少 itemName", 400)
  659. mode = str(rule.get("mode", "default")).strip() or "default"
  660. if mode not in ALLOWED_SWIPE_MODE:
  661. raise AnnotatorError("invalid_rule", f"第 {index + 1} 条滑动规则 mode 不支持: {mode}", 400)
  662. normalized.append(
  663. {
  664. "itemName": item_name,
  665. "roiFront": self._parse_roi(str(rule.get("roiFront", ""))),
  666. "roiBack": self._parse_roi(str(rule.get("roiBack", ""))),
  667. "mode": mode,
  668. "description": str(rule.get("description", "")).strip(),
  669. }
  670. )
  671. return normalized
  672. def _normalize_long_click_rules(self, rules: list[dict[str, Any]]) -> list[dict[str, Any]]:
  673. normalized: list[dict[str, Any]] = []
  674. for index, rule in enumerate(rules):
  675. item_name = str(rule.get("itemName", "")).strip()
  676. if not item_name:
  677. raise AnnotatorError("invalid_rule", f"第 {index + 1} 条长按规则缺少 itemName", 400)
  678. try:
  679. duration = int(rule.get("duration", 1000))
  680. except (TypeError, ValueError) as e:
  681. raise AnnotatorError("invalid_rule", f"第 {index + 1} 条长按规则 duration 非法", 400) from e
  682. if duration <= 0:
  683. raise AnnotatorError("invalid_rule", f"第 {index + 1} 条长按规则 duration 必须大于 0", 400)
  684. normalized.append(
  685. {
  686. "itemName": item_name,
  687. "roiFront": self._parse_roi(str(rule.get("roiFront", ""))),
  688. "roiBack": self._parse_roi(str(rule.get("roiBack", ""))),
  689. "duration": duration,
  690. "description": str(rule.get("description", "")).strip(),
  691. }
  692. )
  693. return normalized
  694. def _normalize_list_rules(self, rules: list[dict[str, Any]], list_meta: dict[str, Any] | None) -> dict[str, Any]:
  695. if not list_meta:
  696. raise AnnotatorError("invalid_rule", "list 规则缺少 list_meta", 400)
  697. list_name = str(list_meta.get("name", "")).strip()
  698. if not list_name:
  699. raise AnnotatorError("invalid_rule", "list_meta.name 不能为空", 400)
  700. direction = str(list_meta.get("direction", "vertical")).strip() or "vertical"
  701. if direction not in ALLOWED_LIST_DIRECTION:
  702. raise AnnotatorError("invalid_rule", f"list_meta.direction 不支持: {direction}", 400)
  703. list_type = str(list_meta.get("type", "image")).strip() or "image"
  704. if list_type not in ALLOWED_LIST_MODE:
  705. raise AnnotatorError("invalid_rule", f"list_meta.type 不支持: {list_type}", 400)
  706. roi_back = self._parse_roi(str(list_meta.get("roiBack", "")))
  707. normalized_items = []
  708. for index, item in enumerate(rules):
  709. item_name = str(item.get("itemName", "")).strip()
  710. if not item_name:
  711. raise AnnotatorError("invalid_rule", f"第 {index + 1} 条 list 项缺少 itemName", 400)
  712. normalized_items.append(
  713. {
  714. "itemName": item_name,
  715. "roiFront": self._parse_roi(str(item.get("roiFront", ""))),
  716. }
  717. )
  718. return {
  719. "name": list_name,
  720. "direction": direction,
  721. "type": list_type,
  722. "roiBack": roi_back,
  723. "description": str(list_meta.get("description", "")).strip(),
  724. "list": normalized_items,
  725. }
  726. @staticmethod
  727. def _ensure_within_root(path: Path, root: Path) -> None:
  728. try:
  729. path.relative_to(root)
  730. except ValueError as e:
  731. raise AnnotatorError("invalid_path", "目标路径不在允许目录内", 400) from e
  732. @staticmethod
  733. def _resolve_task_root(task_name: str) -> Path:
  734. task_root = (TASKS_ROOT / task_name).resolve()
  735. AnnotatorManager._ensure_within_root(task_root, TASKS_ROOT)
  736. if not task_root.exists() or not task_root.is_dir():
  737. raise AnnotatorError("invalid_task", f"任务目录不存在: {task_name}", 404)
  738. return task_root
  739. @staticmethod
  740. def _resolve_tasks_subdir(rel_dir: str) -> Path:
  741. rel = Path(str(rel_dir or "").strip().replace("\\", "/"))
  742. if rel.is_absolute():
  743. raise AnnotatorError("invalid_path", "目录路径必须是相对路径", 400)
  744. base = (TASKS_ROOT / rel).resolve()
  745. AnnotatorManager._ensure_within_root(base, TASKS_ROOT)
  746. if not base.exists() or not base.is_dir():
  747. raise AnnotatorError("invalid_path", f"目录不存在: {rel.as_posix()}", 404)
  748. return base
  749. @staticmethod
  750. def _resolve_json_path(task_name: str, json_relpath: str) -> tuple[Path, Path]:
  751. task_root = AnnotatorManager._resolve_task_root(task_name)
  752. rel = Path(json_relpath)
  753. if rel.is_absolute():
  754. raise AnnotatorError("invalid_path", "json_relpath 必须是相对路径", 400)
  755. target_json = (task_root / rel).resolve()
  756. AnnotatorManager._ensure_within_root(target_json, task_root)
  757. if target_json.suffix.lower() != ".json":
  758. raise AnnotatorError("invalid_path", "目标文件必须是 .json", 400)
  759. return task_root, target_json
  760. @staticmethod
  761. def _resolve_assets_extract_target(task_root: Path, target_json: Path) -> tuple[Path, Path]:
  762. task_root = task_root.resolve()
  763. target_json = target_json.resolve()
  764. AnnotatorManager._ensure_within_root(target_json, task_root)
  765. if task_root.name != "Component":
  766. return task_root, task_root / "assets.py"
  767. rel_parts = target_json.relative_to(task_root).parts
  768. if len(rel_parts) < 2:
  769. raise AnnotatorError(
  770. "invalid_component_task",
  771. "Component 目录下的规则文件必须位于具体组件子任务目录中",
  772. 400,
  773. )
  774. component_name = rel_parts[0]
  775. extract_root = (task_root / component_name).resolve()
  776. AnnotatorManager._ensure_within_root(extract_root, task_root)
  777. if not extract_root.exists() or not extract_root.is_dir():
  778. raise AnnotatorError(
  779. "invalid_component_task",
  780. f"Component 子任务目录不存在: {component_name}",
  781. 404,
  782. )
  783. return extract_root, extract_root / "assets.py"
  784. @staticmethod
  785. def list_task_json_files(task_name: str) -> list[str]:
  786. task_root = AnnotatorManager._resolve_task_root(task_name)
  787. result = []
  788. for p in task_root.rglob("*.json"):
  789. if p.is_file():
  790. result.append(str(p.relative_to(task_root).as_posix()))
  791. return sorted(result)
  792. @staticmethod
  793. def list_rule_source(rel_dir: str = "") -> dict[str, Any]:
  794. base = AnnotatorManager._resolve_tasks_subdir(rel_dir)
  795. directories: list[str] = []
  796. json_files: list[str] = []
  797. for item in sorted(base.iterdir(), key=lambda x: x.name.lower()):
  798. if item.is_dir():
  799. if item.name.startswith("__"):
  800. continue
  801. directories.append(item.name)
  802. elif item.is_file() and item.suffix.lower() == ".json":
  803. json_files.append(item.name)
  804. base_rel = ""
  805. if base != TASKS_ROOT:
  806. base_rel = str(base.relative_to(TASKS_ROOT).as_posix())
  807. return {
  808. "base_dir": base_rel,
  809. "directories": directories,
  810. "json_files": json_files,
  811. }
  812. @staticmethod
  813. def create_rule_json(rel_dir: str, file_name: str) -> dict[str, Any]:
  814. base = AnnotatorManager._resolve_tasks_subdir(rel_dir)
  815. name = str(file_name or "").strip()
  816. if not name:
  817. raise AnnotatorError("invalid_name", "文件名不能为空", 400)
  818. if "/" in name or "\\" in name:
  819. raise AnnotatorError("invalid_name", "文件名不能包含路径分隔符", 400)
  820. if not name.lower().endswith(".json"):
  821. name = f"{name}.json"
  822. target = (base / name).resolve()
  823. AnnotatorManager._ensure_within_root(target, TASKS_ROOT)
  824. if target.exists():
  825. raise AnnotatorError("file_exists", f"文件已存在: {name}", 409)
  826. target.parent.mkdir(parents=True, exist_ok=True)
  827. with atomic_write(target, overwrite=True, encoding="utf-8") as f:
  828. json.dump([], f, ensure_ascii=False, indent=2)
  829. return {"file": str(target.relative_to(TASKS_ROOT).as_posix())}
  830. @staticmethod
  831. def delete_rule_json(rel_dir: str, file_name: str) -> dict[str, Any]:
  832. base = AnnotatorManager._resolve_tasks_subdir(rel_dir)
  833. name = str(file_name or "").strip()
  834. if not name:
  835. raise AnnotatorError("invalid_name", "文件名不能为空", 400)
  836. if "/" in name or "\\" in name:
  837. raise AnnotatorError("invalid_name", "文件名不能包含路径分隔符", 400)
  838. target = (base / name).resolve()
  839. AnnotatorManager._ensure_within_root(target, TASKS_ROOT)
  840. if target.suffix.lower() != ".json":
  841. raise AnnotatorError("invalid_name", "仅支持删除 .json 文件", 400)
  842. if not target.exists():
  843. raise AnnotatorError("json_not_found", f"JSON 文件不存在: {name}", 404)
  844. target.unlink()
  845. return {"file": str(target.relative_to(TASKS_ROOT).as_posix())}
  846. @staticmethod
  847. def _detect_rule_type(data: Any) -> str:
  848. if isinstance(data, dict) and "list" in data and "name" in data:
  849. return "list"
  850. if isinstance(data, list) and data:
  851. item = data[0]
  852. if not isinstance(item, dict):
  853. return "unknown"
  854. if "imageName" in item:
  855. return "image"
  856. if "keyword" in item:
  857. return "ocr"
  858. if "duration" in item:
  859. return "long_click"
  860. if "mode" in item:
  861. return "swipe"
  862. return "click"
  863. return "unknown"
  864. @staticmethod
  865. def load_rule_file(task_name: str, json_relpath: str) -> dict[str, Any]:
  866. task_root, target_json = AnnotatorManager._resolve_json_path(task_name, json_relpath)
  867. if not target_json.exists():
  868. raise AnnotatorError("json_not_found", f"JSON 文件不存在: {json_relpath}", 404)
  869. with open(target_json, "r", encoding="utf-8") as f:
  870. data = json.load(f)
  871. rule_type = AnnotatorManager._detect_rule_type(data)
  872. list_meta = None
  873. rules: list[dict[str, Any]] = []
  874. if rule_type == "list":
  875. list_meta = {
  876. "name": str(data.get("name", "list_name")),
  877. "direction": str(data.get("direction", "vertical")),
  878. "type": str(data.get("type", "image")),
  879. "roiBack": str(data.get("roiBack", "0,0,100,100")),
  880. "description": str(data.get("description", "")),
  881. }
  882. for item in data.get("list", []):
  883. if not isinstance(item, dict):
  884. continue
  885. rules.append(
  886. {
  887. "itemName": str(item.get("itemName", "")),
  888. "roiFront": str(item.get("roiFront", "0,0,100,100")),
  889. }
  890. )
  891. elif isinstance(data, list):
  892. for item in data:
  893. if not isinstance(item, dict):
  894. continue
  895. if rule_type == "image":
  896. rules.append(
  897. {
  898. "itemName": str(item.get("itemName", "")),
  899. "imageName": str(item.get("imageName", "")),
  900. "roiFront": str(item.get("roiFront", "0,0,100,100")),
  901. "roiBack": str(item.get("roiBack", "0,0,100,100")),
  902. "method": str(item.get("method", "Template matching")),
  903. "threshold": float(item.get("threshold", 0.8)),
  904. "description": str(item.get("description", "")),
  905. }
  906. )
  907. elif rule_type == "ocr":
  908. rules.append(
  909. {
  910. "itemName": str(item.get("itemName", "")),
  911. "roiFront": str(item.get("roiFront", "0,0,100,100")),
  912. "roiBack": str(item.get("roiBack", "0,0,100,100")),
  913. "mode": str(item.get("mode", "Single")),
  914. "method": str(item.get("method", "Default")),
  915. "keyword": str(item.get("keyword", "")),
  916. "description": str(item.get("description", "")),
  917. }
  918. )
  919. elif rule_type == "click":
  920. rules.append(
  921. {
  922. "itemName": str(item.get("itemName", "")),
  923. "roiFront": str(item.get("roiFront", "0,0,100,100")),
  924. "roiBack": str(item.get("roiBack", "0,0,100,100")),
  925. "description": str(item.get("description", "")),
  926. }
  927. )
  928. elif rule_type == "swipe":
  929. rules.append(
  930. {
  931. "itemName": str(item.get("itemName", "")),
  932. "roiFront": str(item.get("roiFront", "0,0,100,100")),
  933. "roiBack": str(item.get("roiBack", "0,0,100,100")),
  934. "mode": str(item.get("mode", "default")),
  935. "description": str(item.get("description", "")),
  936. }
  937. )
  938. elif rule_type == "long_click":
  939. rules.append(
  940. {
  941. "itemName": str(item.get("itemName", "")),
  942. "roiFront": str(item.get("roiFront", "0,0,100,100")),
  943. "roiBack": str(item.get("roiBack", "0,0,100,100")),
  944. "duration": int(item.get("duration", 1000)),
  945. "description": str(item.get("description", "")),
  946. }
  947. )
  948. rule_type_locked = rule_type in {"image", "ocr", "click", "swipe", "long_click", "list"} and len(rules) > 0
  949. return {
  950. "task_name": task_name,
  951. "json_relpath": str(target_json.relative_to(task_root).as_posix()),
  952. "rule_type": rule_type,
  953. "rule_type_locked": rule_type_locked,
  954. "rules": rules,
  955. "list_meta": list_meta,
  956. }
  957. @staticmethod
  958. def _resolve_rule_image_path(task_root: Path, target_json: Path, image_name: str) -> tuple[Path, str]:
  959. image_rel = str(image_name or "").strip()
  960. if not image_rel:
  961. raise AnnotatorError("invalid_path", "image_name 不能为空", 400)
  962. rel = Path(image_rel)
  963. if rel.is_absolute():
  964. raise AnnotatorError("invalid_path", "image_name 必须是相对路径", 400)
  965. target = (target_json.parent / rel).resolve()
  966. AnnotatorManager._ensure_within_root(target, task_root)
  967. if target.suffix.lower() not in ALLOWED_IMAGE_EXT:
  968. raise AnnotatorError("invalid_image_ext", "目标图片后缀不受支持", 400)
  969. return target, image_rel
  970. @staticmethod
  971. def get_rule_image_file(task_name: str, json_relpath: str, image_name: str) -> Path:
  972. task_root, target_json = AnnotatorManager._resolve_json_path(task_name, json_relpath)
  973. target, image_rel = AnnotatorManager._resolve_rule_image_path(task_root, target_json, image_name)
  974. if not target.exists() or not target.is_file():
  975. raise AnnotatorError("image_not_found", f"图片不存在: {image_rel}", 404)
  976. return target
  977. def delete_rule_image(self, task_name: str, json_relpath: str, image_name: str) -> dict[str, Any]:
  978. task_root, target_json = self._resolve_json_path(task_name, json_relpath)
  979. target, image_rel = self._resolve_rule_image_path(task_root, target_json, image_name)
  980. if target.exists() and not target.is_file():
  981. raise AnnotatorError("invalid_path", f"目标图片不是文件: {image_rel}", 400)
  982. removed = False
  983. if target.exists():
  984. try:
  985. target.unlink()
  986. except OSError as e:
  987. raise AnnotatorError("delete_image_failed", f"删除图片失败: {image_rel}", 500) from e
  988. removed = True
  989. logger.info(
  990. f"[annotator] delete rule image, task={task_name}, target={target}, removed={removed}"
  991. )
  992. return {
  993. "removed": removed,
  994. "image_name": image_rel,
  995. "target_image": str(target.relative_to(PROJECT_ROOT).as_posix()),
  996. "target_json": str(target_json.relative_to(task_root).as_posix()),
  997. }
  998. @staticmethod
  999. def _list_item_image_name(item_name: str) -> str:
  1000. name = str(item_name or "").strip()
  1001. if not name:
  1002. raise AnnotatorError("invalid_rule", "list 项缺少 itemName", 400)
  1003. return f"{name}.png"
  1004. @classmethod
  1005. def get_list_item_image_file(cls, task_name: str, json_relpath: str, item_name: str) -> Path:
  1006. return cls.get_rule_image_file(task_name, json_relpath, cls._list_item_image_name(item_name))
  1007. @staticmethod
  1008. def _parse_roi_tuple(value: str) -> tuple[int, int, int, int]:
  1009. roi = AnnotatorManager._parse_roi(value)
  1010. x, y, w, h = [float(v) for v in roi.split(",")]
  1011. return int(round(x)), int(round(y)), int(round(w)), int(round(h))
  1012. @staticmethod
  1013. def _load_session_bgr_image(session: AnnotatorSession, image_id: str) -> tuple[np.ndarray, Path]:
  1014. image = session.images.get(image_id)
  1015. if image is None:
  1016. raise AnnotatorError("image_not_found", f"图片不存在: {image_id}", 404)
  1017. if not image.path.exists():
  1018. raise AnnotatorError("image_not_found", "源图片文件不存在", 404)
  1019. raw = cv2.imread(str(image.path), cv2.IMREAD_COLOR)
  1020. if raw is None:
  1021. raise AnnotatorError("invalid_image_data", "源图片读取失败", 400)
  1022. return raw, image.path
  1023. def test_rule(
  1024. self,
  1025. session_id: str,
  1026. image_id: str,
  1027. task_name: str,
  1028. json_relpath: str,
  1029. rule_type: str,
  1030. rule: dict[str, Any],
  1031. list_meta: dict[str, Any] | None = None,
  1032. ) -> dict[str, Any]:
  1033. session = self._get_session(session_id)
  1034. _, source_path = self._load_session_bgr_image(session, image_id)
  1035. if rule_type == "image":
  1036. item_name = str(rule.get("itemName", "")).strip() or "unnamed"
  1037. image_name = str(rule.get("imageName", "")).strip()
  1038. if not image_name:
  1039. raise AnnotatorError("invalid_rule", "image 规则缺少 imageName", 400)
  1040. method = str(rule.get("method", "Template matching")).strip() or "Template matching"
  1041. try:
  1042. threshold = float(rule.get("threshold", 0.8))
  1043. except (TypeError, ValueError) as e:
  1044. raise AnnotatorError("invalid_rule", "threshold 非法", 400) from e
  1045. roi_back = self._parse_roi_tuple(str(rule.get("roiBack", "")))
  1046. task_root, target_json = self._resolve_json_path(task_name, json_relpath)
  1047. template = self.get_rule_image_file(task_name, json_relpath, image_name)
  1048. from module.atom.image import RuleImage
  1049. target = RuleImage(
  1050. roi_front=self._parse_roi_tuple(str(rule.get("roiFront", "0,0,100,100"))),
  1051. roi_back=roi_back,
  1052. method=method,
  1053. threshold=threshold,
  1054. file=str(template),
  1055. )
  1056. detail = detect_image_detail(str(source_path), target)
  1057. return {
  1058. "rule_type": "image",
  1059. "item_name": item_name,
  1060. "image_name": image_name,
  1061. "matched": bool(detail.get("matched", False)),
  1062. "similarity": float(detail.get("similarity", 0.0)),
  1063. "roiFront": self._parse_roi(str(detail.get("roiFront", rule.get("roiFront", "0,0,100,100")))),
  1064. "roiBack": self._parse_roi(str(detail.get("roiBack", rule.get("roiBack", "0,0,100,100")))),
  1065. "threshold": threshold,
  1066. "message": str(detail.get("message", "not_match")),
  1067. "target_json": str(target_json.relative_to(task_root).as_posix()),
  1068. }
  1069. if rule_type == "ocr":
  1070. item_name = str(rule.get("itemName", "")).strip() or "unnamed"
  1071. mode = str(rule.get("mode", "Single")).strip() or "Single"
  1072. method = str(rule.get("method", "Default")).strip() or "Default"
  1073. keyword = str(rule.get("keyword", "")).strip()
  1074. roi_front = self._parse_roi_tuple(str(rule.get("roiFront", "")))
  1075. roi_back = self._parse_roi_tuple(str(rule.get("roiBack", "")))
  1076. from module.atom.ocr import RuleOcr
  1077. target = RuleOcr(
  1078. name=item_name,
  1079. mode=mode,
  1080. method=method,
  1081. roi=roi_front,
  1082. area=roi_back,
  1083. keyword=keyword,
  1084. )
  1085. detail = detect_ocr_detail(str(source_path), target)
  1086. return {
  1087. "rule_type": "ocr",
  1088. "item_name": item_name,
  1089. "matched": bool(detail.get("matched", False)),
  1090. "similarity": float(detail.get("similarity", 0.0)),
  1091. "text": str(detail.get("text", "")),
  1092. "keyword": keyword,
  1093. "roiFront": self._parse_roi(str(detail.get("roiFront", rule.get("roiFront", "0,0,100,100")))),
  1094. "roiBack": self._parse_roi(str(detail.get("roiBack", rule.get("roiBack", "0,0,100,100")))),
  1095. "message": str(detail.get("message", "not_match")),
  1096. }
  1097. if rule_type == "list":
  1098. if not list_meta:
  1099. raise AnnotatorError("invalid_rule", "list 测试缺少 list_meta", 400)
  1100. item_name = str(rule.get("itemName", "")).strip()
  1101. if not item_name:
  1102. raise AnnotatorError("invalid_rule", "list 项缺少 itemName", 400)
  1103. list_type = str(list_meta.get("type", "image")).strip() or "image"
  1104. if list_type not in ALLOWED_LIST_MODE:
  1105. raise AnnotatorError("invalid_rule", f"list_meta.type 不支持: {list_type}", 400)
  1106. list_roi_back = self._parse_roi_tuple(str(list_meta.get("roiBack", "")))
  1107. if list_type == "image":
  1108. template = self.get_list_item_image_file(task_name, json_relpath, item_name)
  1109. from module.atom.image import RuleImage
  1110. target = RuleImage(
  1111. roi_front=list_roi_back,
  1112. roi_back=list_roi_back,
  1113. method="Template matching",
  1114. threshold=0.8,
  1115. file=str(template),
  1116. )
  1117. detail = detect_image_detail(str(source_path), target)
  1118. return {
  1119. "rule_type": "list",
  1120. "list_type": "image",
  1121. "item_name": item_name,
  1122. "image_name": self._list_item_image_name(item_name),
  1123. "matched": bool(detail.get("matched", False)),
  1124. "similarity": float(detail.get("similarity", 0.0)),
  1125. "roiFront": self._parse_roi(str(detail.get("roiFront", self._parse_roi(str(list_meta.get("roiBack", "0,0,100,100")))))),
  1126. "roiBack": self._parse_roi(str(detail.get("roiBack", self._parse_roi(str(list_meta.get("roiBack", "0,0,100,100")))))),
  1127. "threshold": 0.8,
  1128. "message": str(detail.get("message", "not_match")),
  1129. }
  1130. from module.atom.ocr import RuleOcr
  1131. target = RuleOcr(
  1132. name=item_name,
  1133. mode="Full",
  1134. method="Default",
  1135. roi=list_roi_back,
  1136. area=(0, 0, 10, 10),
  1137. keyword=item_name,
  1138. )
  1139. detail = detect_ocr_detail(str(source_path), target)
  1140. return {
  1141. "rule_type": "list",
  1142. "list_type": "ocr",
  1143. "item_name": item_name,
  1144. "matched": bool(detail.get("matched", False)),
  1145. "similarity": float(detail.get("similarity", 0.0)),
  1146. "text": str(detail.get("text", "")),
  1147. "keyword": item_name,
  1148. "roiFront": self._parse_roi(str(detail.get("roiFront", self._parse_roi(str(list_meta.get("roiBack", "0,0,100,100")))))),
  1149. "roiBack": self._parse_roi(str(detail.get("roiBack", self._parse_roi(str(list_meta.get("roiBack", "0,0,100,100")))))),
  1150. "message": str(detail.get("message", "not_match")),
  1151. }
  1152. raise AnnotatorError("invalid_rule_type", f"不支持测试的 rule_type: {rule_type}", 400)
  1153. def save_rules_and_generate(
  1154. self,
  1155. session_id: str,
  1156. task_name: str,
  1157. json_relpath: str,
  1158. rule_type: str,
  1159. rules: list[dict[str, Any]],
  1160. list_meta: dict[str, Any] | None = None,
  1161. ) -> dict[str, Any]:
  1162. _ = self._get_session(session_id)
  1163. if not isinstance(rules, list):
  1164. raise AnnotatorError("invalid_rule", "rules 必须是数组", 400)
  1165. task_root, target_json = self._resolve_json_path(task_name, json_relpath)
  1166. if rule_type == "image":
  1167. payload = self._normalize_image_rules(rules) if rules else []
  1168. elif rule_type == "ocr":
  1169. payload = self._normalize_ocr_rules(rules) if rules else []
  1170. elif rule_type == "click":
  1171. payload = self._normalize_click_rules(rules) if rules else []
  1172. elif rule_type == "swipe":
  1173. payload = self._normalize_swipe_rules(rules) if rules else []
  1174. elif rule_type == "long_click":
  1175. payload = self._normalize_long_click_rules(rules) if rules else []
  1176. elif rule_type == "list":
  1177. payload = self._normalize_list_rules(rules, list_meta)
  1178. else:
  1179. raise AnnotatorError("invalid_rule_type", f"不支持的 rule_type: {rule_type}", 400)
  1180. target_json.parent.mkdir(parents=True, exist_ok=True)
  1181. lock = FileLock(f"{target_json}.lock")
  1182. with lock:
  1183. with atomic_write(target_json, overwrite=True, encoding="utf-8") as f:
  1184. json.dump(payload, f, ensure_ascii=False, indent=2)
  1185. save_status = "success"
  1186. generate_status = "success"
  1187. generate_error = ""
  1188. assets_file = ""
  1189. extract_root = None
  1190. try:
  1191. extract_root, assets_path = self._resolve_assets_extract_target(task_root, target_json)
  1192. assets_file = str(assets_path.relative_to(PROJECT_ROOT).as_posix())
  1193. AssetsExtractor(str(extract_root)).extract()
  1194. except Exception as e:
  1195. generate_status = "failed"
  1196. generate_error = str(e)
  1197. logger.exception(
  1198. f"[annotator] assets generate failed, session={session_id}, task={task_name}, "
  1199. f"target={target_json}, extract_root={extract_root}"
  1200. )
  1201. logger.info(
  1202. f"[annotator] save rules, session={session_id}, task={task_name}, target={target_json}, "
  1203. f"rule_type={rule_type}, save_status={save_status}, generate_status={generate_status}, "
  1204. f"assets_file={assets_file or 'n/a'}"
  1205. )
  1206. rule_count = len(payload.get("list", [])) if isinstance(payload, dict) and "list" in payload else len(payload)
  1207. return {
  1208. "save_status": save_status,
  1209. "generate_status": generate_status,
  1210. "error": generate_error,
  1211. "target_json": str(target_json.relative_to(PROJECT_ROOT).as_posix()),
  1212. "assets_file": assets_file,
  1213. "rule_count": rule_count,
  1214. }
  1215. def save_cropped_image(
  1216. self,
  1217. session_id: str,
  1218. image_id: str,
  1219. task_name: str,
  1220. json_relpath: str,
  1221. image_name: str,
  1222. roi: str,
  1223. ) -> dict[str, Any]:
  1224. session = self._get_session(session_id)
  1225. image = session.images.get(image_id)
  1226. if image is None:
  1227. raise AnnotatorError("image_not_found", f"图片不存在: {image_id}", 404)
  1228. if not image.path.exists():
  1229. raise AnnotatorError("image_not_found", "源图片文件不存在", 404)
  1230. task_root, target_json = self._resolve_json_path(task_name, json_relpath)
  1231. image_rel = Path(image_name)
  1232. if image_rel.is_absolute():
  1233. raise AnnotatorError("invalid_path", "image_name 必须是相对路径", 400)
  1234. target_image = (target_json.parent / image_rel).resolve()
  1235. self._ensure_within_root(target_image, task_root)
  1236. if target_image.suffix.lower() not in ALLOWED_IMAGE_EXT:
  1237. raise AnnotatorError("invalid_image_ext", "目标图片后缀不受支持", 400)
  1238. raw = cv2.imread(str(image.path), cv2.IMREAD_COLOR)
  1239. if raw is None:
  1240. raise AnnotatorError("invalid_image_data", "源图片读取失败", 400)
  1241. roi_text = self._parse_roi(roi)
  1242. x_f, y_f, w_f, h_f = [float(v) for v in roi_text.split(",")]
  1243. x = int(round(x_f))
  1244. y = int(round(y_f))
  1245. w = int(round(w_f))
  1246. h = int(round(h_f))
  1247. ih, iw = raw.shape[:2]
  1248. x = max(0, min(x, iw - 1))
  1249. y = max(0, min(y, ih - 1))
  1250. w = max(1, min(w, iw - x))
  1251. h = max(1, min(h, ih - y))
  1252. crop = raw[y:y + h, x:x + w]
  1253. target_image.parent.mkdir(parents=True, exist_ok=True)
  1254. ok = cv2.imwrite(str(target_image), crop)
  1255. if not ok:
  1256. raise AnnotatorError("save_image_failed", "保存裁剪图片失败", 500)
  1257. logger.info(
  1258. f"[annotator] save crop image, session={session_id}, image={image_id}, target={target_image}, roi={roi_text}"
  1259. )
  1260. return {
  1261. "save_status": "success",
  1262. "target_image": str(target_image.relative_to(PROJECT_ROOT).as_posix()),
  1263. "roi": roi_text,
  1264. }
  1265. annotator_manager = AnnotatorManager()