get_ocr_result_image.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import os
  2. from paddleocr import PaddleOCR, draw_ocr
  3. import argparse
  4. from PIL import Image
  5. def get_boxes(img_path, language, save_dir):
  6. ocr = PaddleOCR(use_angle_cls=True, rec_model_dir='model/rec/tw_idcard_rec_infer_0103/Student', lang=language) # need to run only once to download and load model into memory
  7. result = ocr.ocr(img_path, cls=True)
  8. for line in result:
  9. # 左上
  10. line[0][0][0] -= 5
  11. line[0][0][1] -= 5
  12. # 右上
  13. line[0][1][0] += 5
  14. line[0][1][1] -= 5
  15. # 右下
  16. line[0][2][0] += 5
  17. line[0][2][1] += 5
  18. # 左下
  19. line[0][3][0] -= 5
  20. line[0][3][1] += 5
  21. # 显示结果
  22. image = Image.open(img_path).convert('RGB')
  23. boxes = [line[0] for line in result]
  24. texts = [line[1][0] for line in result]
  25. scores = [line[1][1] for line in result]
  26. im_show = draw_ocr(image, boxes, texts, scores, font_path='./fonts/STSONG.TTF')
  27. im_show = Image.fromarray(im_show)
  28. im_show.save(save_dir + "/" + language + "_" + str(img_path).split("/")[-1])
  29. if __name__ == '__main__':
  30. parser = argparse.ArgumentParser()
  31. parser.add_argument('--img_dir', type=str, default='imgs')
  32. parser.add_argument('--img_path', type=str, default='')
  33. parser.add_argument('--language', type=str, default='ch')
  34. parser.add_argument('--save_dir', type=str, default='./result')
  35. args = parser.parse_args()
  36. if args.img_path == '':
  37. path_list = os.listdir(args.img_dir)
  38. for path in path_list:
  39. get_boxes(args.img_dir + '/' + path, args.language, args.save_dir)
  40. else:
  41. get_boxes(args.img_path, args.language, args.save_dir)