models.py 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. from typing import Dict
  2. from module.base.decorator import cached_property
  3. from module.ocr.ppocr import TextSystem
  4. from module.ocr.rpc import ModelProxy
  5. from module.server.setting import State
  6. class OcrModel:
  7. @cached_property
  8. def ch(self):
  9. return TextSystem()
  10. OCR_MODEL = OcrModel()
  11. _OCR_PROXY_CACHE: Dict[str, ModelProxy] = {}
  12. def get_ocr_model(lang: str = "ch"):
  13. deploy_config = State.deploy_config
  14. if deploy_config.UseOcrServer:
  15. address = deploy_config.OcrClientAddress or "127.0.0.1:22268"
  16. if address not in _OCR_PROXY_CACHE:
  17. _OCR_PROXY_CACHE[address] = ModelProxy(address)
  18. return _OCR_PROXY_CACHE[address]
  19. return getattr(OCR_MODEL, lang)
  20. if __name__ == "__main__":
  21. model = OCR_MODEL.ch
  22. import cv2
  23. import time
  24. from memory_profiler import profile
  25. image = cv2.imread(r"E:\Project\OnmyojiAutoScript-assets\jade.png")
  26. # 引入ocr 会导致非常巨大的内存开销
  27. @profile
  28. def test_memory():
  29. for i in range(2):
  30. start_time = time.time()
  31. result = model.detect_and_ocr(image)
  32. print(result)
  33. end_time = time.time()
  34. print(f'耗时:{end_time-start_time}')
  35. test_memory()