ocr_work_test.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435
  1. import argparse
  2. import os
  3. from paddleocr import PaddleOCR, draw_ocr
  4. def rec_result(img_dir, save_dir):
  5. if not os.path.exists(save_dir):
  6. os.makedirs(save_dir)
  7. # 模型路径下必须含有model和params文件
  8. ocr = PaddleOCR(det_model_dir='model/det/tw_idcard_det_infer_1202', rec_model_dir='model/rec/ch_PP-OCRv3_rec_infer', rec_char_dict_path='ppocr_keys_v1.txt', cls_model_dir='model/cls/ch_ppocr_mobile_v2.0_cls_slim_infer', use_angle_cls=True)
  9. path_list = os.listdir(img_dir)
  10. for img_p in path_list:
  11. img_path = img_dir + '/' + img_p
  12. result = ocr.ocr(img_path, cls=True)
  13. # for line in result:
  14. # print(line)
  15. # 显示结果
  16. from PIL import Image
  17. image = Image.open(img_path).convert('RGB')
  18. boxes = [line[0] for line in result]
  19. txts = [line[1][0] for line in result]
  20. scores = [line[1][1] for line in result]
  21. im_show = draw_ocr(image, boxes, txts, scores, font_path='font/simfang.ttf')
  22. im_show = Image.fromarray(im_show)
  23. im_show.save(save_dir + '/' + img_p)
  24. if __name__ == "__main__":
  25. parser = argparse.ArgumentParser()
  26. parser.add_argument('--img_dir', type=str, default='./imgs')
  27. parser.add_argument('--save_dir', type=str, default='./result')
  28. args = parser.parse_args()
  29. rec_result(args.img_dir, args.save_dir)