utils.py 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import base64
  2. from dataclasses import dataclass
  3. from typing import Optional
  4. import cv2
  5. import numpy as np
  6. import requests
  7. url = 'http://192.168.199.249:18080'
  8. def send_request(image_path, image_type):
  9. with open(image_path, 'rb') as f:
  10. img_str: str = base64.encodebytes(f.read()).decode('utf-8')
  11. r = requests.post(f'{url}/ocr_system/idcard', json={'image': img_str, 'image_type': image_type})
  12. print(r.json())
  13. return r.json()
  14. def send_request_cv2(image_path, image_type, rotate=None):
  15. img = cv2.imread(image_path)
  16. img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
  17. if rotate is not None:
  18. img = cv2.rotate(img, rotate)
  19. _, img = cv2.imencode('.jpg', img)
  20. img_str = base64.b64encode(img).decode('utf-8')
  21. r = requests.post(f'{url}/ocr_system/idcard', json={'image': img_str, 'image_type': image_type})
  22. print(r.json())
  23. return r.json()
  24. @dataclass
  25. class ResultItem:
  26. # status: str
  27. orientation: int
  28. name: str
  29. id: str
  30. ethnicity: str
  31. gender: str
  32. birthday: str
  33. address: str
  34. card_type: Optional[str]
  35. address_province: Optional[str]
  36. address_city: Optional[str]
  37. address_region: Optional[str]
  38. address_detail: Optional[str]
  39. expire_date: Optional[str]