rpc.py 5.3 KB

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