ocr_api.py 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. # ocr外部接口
  2. import os
  3. from typing import List
  4. from requests import post
  5. class OcrAgent:
  6. def __init__(self, url):
  7. self.url = url
  8. def get_content(self, image_path):
  9. try:
  10. with open(image_path, 'rb') as image_file:
  11. files = {"file": ("image.jpg", image_file, "image/jpeg")}
  12. # files = {"file": ("image.png", image_file, "image/png")}
  13. response = post(self.url, files=files)
  14. return response.json()
  15. except ValueError:
  16. raise ValueError(f"传入图像{image_path}已损坏")
  17. def find_current_row(ocr_result: List[dict], top: int, bottom: int, float_range: int = 5):
  18. results = []
  19. assert float_range >= 0
  20. top += float_range
  21. bottom -= float_range
  22. for ret in ocr_result:
  23. ct = ret['rect']['top']
  24. cb = ret['rect']['top'] - ret['rect']['height']
  25. if top >= ct > cb >= bottom:
  26. results.append(ret)
  27. return results
  28. if __name__ == '__main__':
  29. agent = OcrAgent("http://120.48.103.13:18000/ctr_ocr")
  30. res = agent.get_content(
  31. os.path.join('/home/zzh/ocr/pdf/南方电网数字电网研究院有限公司/images', 'image_page_1131_0.png'))
  32. pass