ocr.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. # -*- coding: utf-8 -*-
  2. # @Author: privacy
  3. # @Date: 2024-08-28 13:17:32
  4. # @Last Modified by: privacy
  5. # @Last Modified time: 2024-09-05 09:25:28
  6. import os
  7. from typing import List, Optional
  8. from requests import post
  9. from celery_tasks.configure import OCRConf
  10. from celery_tasks.singleton import Singleton
  11. class OcrAgent(Singleton):
  12. def __init__(self):
  13. super().__init__()
  14. self.url = OCRConf.url
  15. def is_file_exists(self, file_path: str) -> str:
  16. return os.path.exists(file_path)
  17. def estimate_postfix(self, file_path: str) -> str:
  18. filepath_lower = file_path.lower()
  19. if filepath_lower.endswith('.jpg') or filepath_lower.endswith('.jpeg'):
  20. return "image/jpeg"
  21. elif filepath_lower.endswith('.png'):
  22. return "image/png"
  23. else:
  24. return "UNK"
  25. def get_content(self, image_bytes: Optional[bytes] = None, image_type: Optional[str] = 'image/jpeg', image_path: Optional[str] = None) -> dict:
  26. if (not image_bytes) and (not image_path):
  27. raise ValueError("Image File Not Exists!")
  28. elif image_bytes and image_path:
  29. image_type = self.estimate_postfix(file_path=image_path)
  30. if image_type == 'UNK':
  31. raise ValueError("Image Type Error!")
  32. else:
  33. files = {"file": ("image.jpg", image_bytes, image_type)}
  34. elif image_bytes:
  35. files = {"file": ("image.jpg", image_bytes, image_type)}
  36. elif image_path and self.is_file_exists(image_path):
  37. image_type = self.estimate_postfix(file_path=image_path)
  38. if image_type == 'UNK':
  39. raise ValueError("Image Type Error!")
  40. else:
  41. with open(image_path, 'rb') as image:
  42. files = {"file": ("image.jpg", image.read(), image_type)}
  43. try:
  44. response = post(self.url, files=files)
  45. return response.json()
  46. except Exception:
  47. raise ValueError(f"图片解析失败")
  48. def find_current_row(ocr_result: List[dict], top: int, bottom: int, float_range: int = 5):
  49. results = []
  50. assert float_range >= 0
  51. top += float_range
  52. bottom -= float_range
  53. for ret in ocr_result:
  54. ct = ret['rect']['top']
  55. cb = ret['rect']['top'] - ret['rect']['height']
  56. if top >= ct > cb >= bottom:
  57. results.append(ret)
  58. return results
  59. if __name__ == '__main__':
  60. agent = OcrAgent()
  61. res = agent.get_content(
  62. image_path=os.path.join('D:\\desktop\\三峡水利\\data\\projects\\2022-2025年度三峡电站9台机组检修密封加工制作重新招标\\投标\\东方电气\\extracted_images\\', 'image_page_27_1.jpg')
  63. )
  64. print(res)