tool.py 61 KB

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