tool.py 61 KB

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