tools.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import base64
  2. import os
  3. import re
  4. from crnn.crnn import crnnOcr
  5. from ocrapi import ocr
  6. def get_image():
  7. image_list = []
  8. for i in os.listdir('./result/image'):
  9. if re.match('(\d+)-(\d+)-(\d+)-(\d+)',i):
  10. with open(os.path.join('./result/image',i), 'rb') as f:
  11. base64_data = base64.b64encode(f.read())
  12. s = base64_data.decode()
  13. y_min, y_max, x_min, x_max = re.match('(\d+)-(\d+)-(\d+)-(\d+)', i).groups()
  14. image_list.append({'left':x_min,'top':y_min,'b64':s})
  15. return image_list
  16. def get_text():
  17. image_list = []
  18. for i in os.listdir('./result/text_img'):
  19. if re.match('(\d+)-(\d+)-(\d+)-(\d+)',i):
  20. with open(os.path.join('./result/text_img',i), 'rb') as f:
  21. s = crnnOcr(os.path.join('./result/text_img',i))
  22. # print(s)
  23. # s = ocr(os.path.join('./result/text_img',i))
  24. y_min, y_max, x_min, x_max = re.match('(\d+)-(\d+)-(\d+)-(\d+)', i).groups()
  25. image_list.append({'left':x_min,'top':y_min,
  26. 'w':int(x_max)-int(x_min),
  27. 'h':int(y_max)-int(y_min),
  28. 'size':int((int(y_max)-int(y_min))*0.6),
  29. 's':s})
  30. # print(i)
  31. # print({'left':x_min,'top':y_min,
  32. # 'w':int(x_max)-int(x_min),
  33. # 'h':int(y_max)-int(y_min),
  34. # 'size':int((int(y_max)-int(y_min))*0.6),
  35. # 's':s})
  36. return image_list
  37. if __name__ == '__main__':
  38. get_text()