tool_router.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. # This Python file uses the following encoding: utf-8
  2. # @author runhey
  3. # github https://github.com/runhey
  4. import asyncio
  5. from typing import Any
  6. from fastapi import APIRouter, HTTPException, WebSocket, WebSocketDisconnect
  7. from fastapi.responses import FileResponse
  8. from pydantic import BaseModel
  9. from module.logger import logger
  10. from module.server.tool import AnnotatorError, annotator_manager
  11. tool_app = APIRouter(
  12. prefix="/tool",
  13. tags=["tool"],
  14. )
  15. class EmulatorStartBody(BaseModel):
  16. session_id: str
  17. config_name: str
  18. frame_rate: int = 2
  19. class SessionBody(BaseModel):
  20. session_id: str
  21. class RuleSaveBody(BaseModel):
  22. session_id: str
  23. task_name: str
  24. json_relpath: str
  25. rule_type: str
  26. rules: list[dict[str, Any]]
  27. list_meta: dict[str, Any] | None = None
  28. class UploadImageItem(BaseModel):
  29. name: str
  30. content_base64: str
  31. class UploadImagesBody(BaseModel):
  32. session_id: str
  33. images: list[UploadImageItem]
  34. class BatchDeleteImagesBody(BaseModel):
  35. session_id: str
  36. image_ids: list[str]
  37. class CropSaveBody(BaseModel):
  38. session_id: str
  39. image_id: str
  40. task_name: str
  41. json_relpath: str
  42. image_name: str
  43. roi: str
  44. class RuleImageDeleteBody(BaseModel):
  45. task_name: str
  46. json_relpath: str
  47. image_name: str
  48. class RuleFileCreateBody(BaseModel):
  49. dir_path: str
  50. file_name: str
  51. class RuleFileDeleteBody(BaseModel):
  52. dir_path: str
  53. file_name: str
  54. class RuleTestBody(BaseModel):
  55. session_id: str
  56. image_id: str
  57. task_name: str
  58. json_relpath: str
  59. rule_type: str
  60. rule: dict[str, Any]
  61. list_meta: dict[str, Any] | None = None
  62. def _raise_annotator_error(e: AnnotatorError) -> None:
  63. raise HTTPException(
  64. status_code=e.status_code,
  65. detail={"code": e.code, "message": e.message},
  66. )
  67. def _close_session_safely(session_id: str, reason: str) -> dict[str, Any]:
  68. return annotator_manager.close_session(session_id, reason=reason, raise_if_missing=False)
  69. @tool_app.get('/annotator')
  70. async def tool_annotator_page():
  71. page = annotator_manager.index_file()
  72. if not page.exists():
  73. raise HTTPException(status_code=404, detail={"code": "page_not_found", "message": "标注页面不存在"})
  74. return FileResponse(page)
  75. @tool_app.post('/annotator/api/session')
  76. async def annotator_create_session():
  77. session = annotator_manager.create_session()
  78. return {"code": "ok", "session": session}
  79. @tool_app.get('/annotator/api/session/{session_id}')
  80. async def annotator_get_session(session_id: str):
  81. try:
  82. session = annotator_manager.get_session_snapshot(session_id)
  83. return {"code": "ok", "session": session}
  84. except AnnotatorError as e:
  85. _raise_annotator_error(e)
  86. @tool_app.delete('/annotator/api/session/{session_id}')
  87. async def annotator_close_session(session_id: str, reason: str = "client_close"):
  88. try:
  89. result = _close_session_safely(session_id, f"api:{reason}")
  90. return {"code": "ok", **result}
  91. except AnnotatorError as e:
  92. _raise_annotator_error(e)
  93. @tool_app.post('/annotator/api/session/{session_id}/close')
  94. async def annotator_close_session_beacon(session_id: str, reason: str = "pagehide"):
  95. try:
  96. result = _close_session_safely(session_id, f"beacon:{reason}")
  97. return {"code": "ok", **result}
  98. except AnnotatorError as e:
  99. _raise_annotator_error(e)
  100. @tool_app.post('/annotator/api/images/upload')
  101. async def annotator_upload_images(data: UploadImagesBody):
  102. try:
  103. images = annotator_manager.save_uploaded_images_base64(
  104. data.session_id,
  105. [item.dict() for item in data.images],
  106. )
  107. return {"code": "ok", "images": images}
  108. except AnnotatorError as e:
  109. _raise_annotator_error(e)
  110. @tool_app.get('/annotator/api/images')
  111. async def annotator_list_images(session_id: str):
  112. try:
  113. images = annotator_manager.list_images(session_id)
  114. return {"code": "ok", "images": images}
  115. except AnnotatorError as e:
  116. _raise_annotator_error(e)
  117. @tool_app.get('/annotator/api/images/{session_id}/{image_id}')
  118. async def annotator_image_file(session_id: str, image_id: str):
  119. try:
  120. image = annotator_manager.get_image_file(session_id, image_id)
  121. return FileResponse(image)
  122. except AnnotatorError as e:
  123. _raise_annotator_error(e)
  124. @tool_app.delete('/annotator/api/images/{session_id}/{image_id}')
  125. async def annotator_delete_image(session_id: str, image_id: str):
  126. try:
  127. session = annotator_manager.delete_image(session_id, image_id)
  128. return {"code": "ok", "session": session}
  129. except AnnotatorError as e:
  130. _raise_annotator_error(e)
  131. @tool_app.post('/annotator/api/images/delete-batch')
  132. async def annotator_delete_batch_images(data: BatchDeleteImagesBody):
  133. try:
  134. result = annotator_manager.delete_images(data.session_id, data.image_ids)
  135. return {"code": "ok", **result}
  136. except AnnotatorError as e:
  137. _raise_annotator_error(e)
  138. @tool_app.post('/annotator/api/images/clear')
  139. async def annotator_clear_images(data: SessionBody):
  140. try:
  141. result = annotator_manager.clear_images(data.session_id)
  142. return {"code": "ok", **result}
  143. except AnnotatorError as e:
  144. _raise_annotator_error(e)
  145. @tool_app.get('/annotator/api/configs')
  146. async def annotator_configs():
  147. configs = annotator_manager.list_configs()
  148. return {"code": "ok", "configs": configs}
  149. @tool_app.get('/annotator/api/tasks')
  150. async def annotator_tasks():
  151. tasks = annotator_manager.list_task_names()
  152. return {"code": "ok", "tasks": tasks}
  153. @tool_app.get('/annotator/api/tasks/{task_name}/json')
  154. async def annotator_task_json_files(task_name: str):
  155. try:
  156. files = annotator_manager.list_task_json_files(task_name)
  157. return {"code": "ok", "json_files": files}
  158. except AnnotatorError as e:
  159. _raise_annotator_error(e)
  160. @tool_app.get('/annotator/api/rules/load')
  161. async def annotator_load_rules(task_name: str, json_relpath: str):
  162. try:
  163. data = annotator_manager.load_rule_file(task_name, json_relpath)
  164. return {"code": "ok", **data}
  165. except AnnotatorError as e:
  166. _raise_annotator_error(e)
  167. @tool_app.get('/annotator/api/rules/source')
  168. async def annotator_rule_source(dir_path: str = ""):
  169. try:
  170. data = annotator_manager.list_rule_source(dir_path)
  171. return {"code": "ok", **data}
  172. except AnnotatorError as e:
  173. _raise_annotator_error(e)
  174. @tool_app.post('/annotator/api/rules/source/create')
  175. async def annotator_rule_source_create(data: RuleFileCreateBody):
  176. try:
  177. result = annotator_manager.create_rule_json(data.dir_path, data.file_name)
  178. return {"code": "ok", **result}
  179. except AnnotatorError as e:
  180. _raise_annotator_error(e)
  181. @tool_app.post('/annotator/api/rules/source/delete')
  182. async def annotator_rule_source_delete(data: RuleFileDeleteBody):
  183. try:
  184. result = annotator_manager.delete_rule_json(data.dir_path, data.file_name)
  185. return {"code": "ok", **result}
  186. except AnnotatorError as e:
  187. _raise_annotator_error(e)
  188. @tool_app.get('/annotator/api/rules/image-preview')
  189. async def annotator_rule_image_preview(task_name: str, json_relpath: str, image_name: str):
  190. try:
  191. image = annotator_manager.get_rule_image_file(task_name, json_relpath, image_name)
  192. return FileResponse(image)
  193. except AnnotatorError as e:
  194. _raise_annotator_error(e)
  195. @tool_app.post('/annotator/api/rules/image/delete')
  196. async def annotator_rule_image_delete(data: RuleImageDeleteBody):
  197. try:
  198. result = annotator_manager.delete_rule_image(data.task_name, data.json_relpath, data.image_name)
  199. return {"code": "ok", **result}
  200. except AnnotatorError as e:
  201. _raise_annotator_error(e)
  202. @tool_app.post('/annotator/api/emulator/start')
  203. async def annotator_start_emulator(data: EmulatorStartBody):
  204. try:
  205. status = annotator_manager.start_emulator(data.session_id, data.config_name, data.frame_rate)
  206. return {"code": "ok", "emulator": status}
  207. except AnnotatorError as e:
  208. _raise_annotator_error(e)
  209. @tool_app.post('/annotator/api/emulator/stop')
  210. async def annotator_stop_emulator(data: SessionBody):
  211. try:
  212. status = annotator_manager.stop_emulator(data.session_id)
  213. return {"code": "ok", "emulator": status}
  214. except AnnotatorError as e:
  215. _raise_annotator_error(e)
  216. @tool_app.get('/annotator/api/emulator/status')
  217. async def annotator_emulator_status(session_id: str):
  218. try:
  219. status = annotator_manager.emulator_status(session_id)
  220. return {"code": "ok", "emulator": status}
  221. except AnnotatorError as e:
  222. _raise_annotator_error(e)
  223. @tool_app.post('/annotator/api/emulator/capture')
  224. async def annotator_capture_frame(data: SessionBody):
  225. try:
  226. image = annotator_manager.capture_from_emulator(data.session_id)
  227. return {"code": "ok", "image": image}
  228. except AnnotatorError as e:
  229. _raise_annotator_error(e)
  230. @tool_app.post('/annotator/api/rules/test')
  231. async def annotator_test_rule(data: RuleTestBody):
  232. try:
  233. result = annotator_manager.test_rule(
  234. session_id=data.session_id,
  235. image_id=data.image_id,
  236. task_name=data.task_name,
  237. json_relpath=data.json_relpath,
  238. rule_type=data.rule_type,
  239. rule=data.rule,
  240. list_meta=data.list_meta,
  241. )
  242. return {"code": "ok", "result": result}
  243. except AnnotatorError as e:
  244. _raise_annotator_error(e)
  245. @tool_app.post('/annotator/api/rules/save')
  246. async def annotator_save_rules(data: RuleSaveBody):
  247. try:
  248. result = annotator_manager.save_rules_and_generate(
  249. session_id=data.session_id,
  250. task_name=data.task_name,
  251. json_relpath=data.json_relpath,
  252. rule_type=data.rule_type,
  253. rules=data.rules,
  254. list_meta=data.list_meta,
  255. )
  256. code = "ok" if result.get("generate_status") == "success" else "partial_success"
  257. return {"code": code, **result}
  258. except AnnotatorError as e:
  259. _raise_annotator_error(e)
  260. @tool_app.post('/annotator/api/images/crop-save')
  261. async def annotator_crop_save(data: CropSaveBody):
  262. try:
  263. result = annotator_manager.save_cropped_image(
  264. session_id=data.session_id,
  265. image_id=data.image_id,
  266. task_name=data.task_name,
  267. json_relpath=data.json_relpath,
  268. image_name=data.image_name,
  269. roi=data.roi,
  270. )
  271. return {"code": "ok", **result}
  272. except AnnotatorError as e:
  273. _raise_annotator_error(e)
  274. @tool_app.websocket('/annotator/ws/{session_id}')
  275. async def annotator_frame_ws(websocket: WebSocket, session_id: str):
  276. await websocket.accept()
  277. try:
  278. annotator_manager.get_session_snapshot(session_id)
  279. while True:
  280. frame = annotator_manager.latest_emulator_frame(session_id)
  281. if frame:
  282. await websocket.send_bytes(frame)
  283. continue
  284. status = annotator_manager.emulator_status(session_id)
  285. if status.get("state") == "error":
  286. await websocket.send_json(
  287. {
  288. "event": "error",
  289. "code": "emulator_error",
  290. "message": status.get("error", "unknown"),
  291. }
  292. )
  293. await websocket.close(code=1011)
  294. break
  295. await asyncio.sleep(0.1)
  296. except WebSocketDisconnect:
  297. logger.info(f"[annotator] ws disconnect, session={session_id}")
  298. except AnnotatorError as e:
  299. if e.code != "invalid_session":
  300. logger.warning(f"[annotator] ws annotator error, session={session_id}, code={e.code}")
  301. try:
  302. await websocket.send_json({"event": "error", "code": e.code, "message": e.message})
  303. except Exception:
  304. pass
  305. try:
  306. await websocket.close(code=1008)
  307. except Exception:
  308. pass
  309. except Exception as e:
  310. message = str(e).strip().lower()
  311. if e.__class__.__name__ == "ClientDisconnected" or "disconnected" in message:
  312. logger.info(f"[annotator] ws client disconnected during send, session={session_id}")
  313. else:
  314. logger.exception(f"[annotator] ws failed, session={session_id}")
  315. try:
  316. await websocket.close(code=1011)
  317. except Exception:
  318. pass