ocr_api.py 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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: str) -> dict:
  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('D:\\desktop\\三峡水利\\data\\projects\\2022-2025年度三峡电站9台机组检修密封加工制作重新招标\\投标\\东方电气\\extracted_images\\', 'image_page_27_1.jpg'))
  32. print(res)
  33. pass