rpc.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. # This Python file uses the following encoding: utf-8
  2. # @author runhey
  3. # github https://github.com/runhey
  4. import atexit
  5. import multiprocessing
  6. import pickle
  7. import socket
  8. import time
  9. from typing import Any, Dict, List, Optional
  10. import numpy as np
  11. import zerorpc
  12. from module.exception import ScriptError
  13. from module.logger import logger
  14. from module.ocr.ppocr import TextSystem
  15. _OCR_SERVER_PROCESS: Optional[multiprocessing.Process] = None
  16. def _normalize_address(address: str) -> str:
  17. if address.startswith("tcp://"):
  18. return address
  19. return f"tcp://{address}"
  20. def _split_host_port(address: str) -> tuple[str, int]:
  21. addr = address.replace("tcp://", "")
  22. if ":" not in addr:
  23. return addr, 22268
  24. host, port = addr.rsplit(":", 1)
  25. return host, int(port)
  26. def _is_port_in_use(host: str, port: int) -> bool:
  27. s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  28. try:
  29. s.settimeout(0.5)
  30. s.connect((host, port))
  31. s.shutdown(2)
  32. return True
  33. except Exception:
  34. return False
  35. finally:
  36. s.close()
  37. def ensure_ocr_server_started() -> bool:
  38. from module.server.setting import State
  39. deploy_config = State.deploy_config
  40. if not deploy_config.StartOcrServer:
  41. return False
  42. if deploy_config.OcrServerPort:
  43. port = int(deploy_config.OcrServerPort)
  44. else:
  45. _, port = _split_host_port(str(deploy_config.OcrClientAddress))
  46. host = "0.0.0.0"
  47. if _is_port_in_use("127.0.0.1", port):
  48. logger.info(f"OCR server already running on port {port}")
  49. return True
  50. global _OCR_SERVER_PROCESS
  51. if _OCR_SERVER_PROCESS is not None and _OCR_SERVER_PROCESS.is_alive():
  52. logger.info("OCR server process already started")
  53. return True
  54. _OCR_SERVER_PROCESS = multiprocessing.Process(
  55. target=run_ocr_server,
  56. args=(host, port),
  57. name="ocr_server",
  58. daemon=True,
  59. )
  60. _OCR_SERVER_PROCESS.start()
  61. logger.info(f"Start OCR server on {host}:{port}")
  62. for _ in range(50):
  63. if _is_port_in_use("127.0.0.1", port):
  64. return True
  65. time.sleep(0.1)
  66. logger.error(f"OCR server is not ready on port {port}")
  67. return False
  68. def shutdown_ocr_server(timeout: float = 2.0) -> bool:
  69. global _OCR_SERVER_PROCESS
  70. process = _OCR_SERVER_PROCESS
  71. if process is None:
  72. return False
  73. if not process.is_alive():
  74. _OCR_SERVER_PROCESS = None
  75. return False
  76. logger.info("Stopping OCR server process")
  77. try:
  78. process.terminate()
  79. process.join(timeout=timeout)
  80. if process.is_alive():
  81. logger.warning("OCR server process did not exit in time, force killing")
  82. process.kill()
  83. process.join(timeout=1.0)
  84. logger.info("OCR server process stopped")
  85. return True
  86. except Exception as e:
  87. logger.exception(e)
  88. return False
  89. finally:
  90. _OCR_SERVER_PROCESS = None
  91. def run_ocr_server(host: str, port: int) -> None:
  92. server = zerorpc.Server(OcrServer())
  93. server.bind(f"tcp://{host}:{port}")
  94. server.run()
  95. class OcrServer:
  96. def __init__(self) -> None:
  97. self.model = TextSystem()
  98. def ping(self) -> bool:
  99. return True
  100. @staticmethod
  101. def _rotate_vertical(image: np.ndarray) -> np.ndarray:
  102. height, width = image.shape[0:2]
  103. if height * 1.0 / width >= 1.5:
  104. return np.rot90(image)
  105. return image
  106. def ocr_single_line(self, image_bytes: bytes):
  107. image = pickle.loads(image_bytes)
  108. result, score = self.model.ocr_single_line(image)
  109. return result, float(score)
  110. def detect_and_ocr(
  111. self,
  112. image_bytes: bytes,
  113. drop_score: float = 0.5,
  114. unclip_ratio: Optional[float] = None,
  115. box_thresh: Optional[float] = None,
  116. vertical: bool = False,
  117. ) -> List[Dict[str, Any]]:
  118. image = pickle.loads(image_bytes)
  119. if not vertical:
  120. results = self.model.detect_and_ocr(image, drop_score=drop_score,
  121. unclip_ratio=unclip_ratio,
  122. box_thresh=box_thresh)
  123. return [
  124. {"box": r.box.tolist(), "ocr_text": r.ocr_text, "score": float(r.score)}
  125. for r in results
  126. ]
  127. text_recognizer = self.model.text_recognizer
  128. def vertical_text_recognizer(img_crop_list):
  129. img_crop_list = [self._rotate_vertical(i) for i in img_crop_list]
  130. return text_recognizer(img_crop_list)
  131. self.model.text_recognizer = vertical_text_recognizer
  132. try:
  133. results = self.model.detect_and_ocr(image, drop_score=drop_score,
  134. unclip_ratio=unclip_ratio,
  135. box_thresh=box_thresh)
  136. finally:
  137. self.model.text_recognizer = text_recognizer
  138. return [
  139. {"box": r.box.tolist(), "ocr_text": r.ocr_text, "score": float(r.score)}
  140. for r in results
  141. ]
  142. class ModelProxy:
  143. is_proxy = True
  144. def __init__(self, address: str) -> None:
  145. self.address = _normalize_address(address)
  146. self.client = zerorpc.Client()
  147. try:
  148. self.client.connect(self.address)
  149. self.client.ping()
  150. except Exception as e:
  151. raise ScriptError(f"OCR server connection failed: {self.address}") from e
  152. def ocr_single_line(self, image: np.ndarray):
  153. payload = pickle.dumps(image, protocol=4)
  154. return self.client.ocr_single_line(payload)
  155. def detect_and_ocr(
  156. self,
  157. image: np.ndarray,
  158. drop_score: float = 0.5,
  159. unclip_ratio: Optional[float] = None,
  160. box_thresh: Optional[float] = None,
  161. vertical: bool = False,
  162. ):
  163. payload = pickle.dumps(image, protocol=4)
  164. results = self.client.detect_and_ocr(payload, drop_score, unclip_ratio, box_thresh, vertical)
  165. from ppocronnx.predict_system import BoxedResult
  166. return [
  167. BoxedResult(np.array(item["box"]), None, item["ocr_text"], item["score"])
  168. for item in results
  169. ]
  170. atexit.register(shutdown_ocr_server)