my_infer.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import cv2
  2. from PIL import Image
  3. from paddleocr import PaddleOCR
  4. from tools.infer.utility import draw_ocr
  5. import numpy as np
  6. from format_convert.convert import remove_red_seal, remove_underline
  7. # path = "../temp/complex/710.png"
  8. # path = "../test_files/开标记录表3_page_0.png"
  9. # path = "D:\\Project\\format_conversion\\appendix_test\\temp\\00e959a0bc9011ebaf5a00163e0ae709" + \
  10. # "\\00e95f7cbc9011ebaf5a00163e0ae709_pdf_page0.png"
  11. # path = "../去章文字.jpg"
  12. # path = "../1.jpg"
  13. # path = "../real1.png"
  14. path = "../temp/f1fe9c4ac8e511eb81d700163e0857b6/f1fea1e0c8e511eb81d700163e0857b6.png"
  15. path = "../翻转1.jpg"
  16. # 去掉公章
  17. # image_np = cv2.imread(path)
  18. # cv2.imshow("origin image", image_np)
  19. # cv2.waitKey(0)
  20. # image_np = remove_red_seal(image_np)
  21. # cv2.imwrite("../去章文字.jpg", image_np)
  22. # 去掉下划线
  23. # image_np = cv2.imread(path)
  24. # remove_underline(image_np)
  25. with open(path, "rb") as f:
  26. image = f.read()
  27. ocr_model = PaddleOCR(use_angle_cls=True, lang="ch")
  28. image = cv2.imdecode(np.frombuffer(image, np.uint8), cv2.IMREAD_COLOR)
  29. # # 将bgr转为rbg
  30. np_images = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
  31. # np_images = [cv2.imread(img_data)]
  32. results = ocr_model.ocr(np_images, det=True, rec=True, cls=True)
  33. bbox_list = []
  34. text_list = []
  35. score_list = []
  36. for line in results:
  37. text_list.append(line[-1][0])
  38. bbox_list.append(line[0])
  39. score_list.append(line[-1][1])
  40. # print("len(text_list)", len(text_list))
  41. # print("len(bbox_list)", len(bbox_list))
  42. # print("score_list", score_list)
  43. image = Image.fromarray(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
  44. boxes = bbox_list
  45. image = draw_ocr(image, boxes, text_list, score_list, drop_score=0.2)
  46. print(type(image))
  47. image = Image.fromarray(image)
  48. image.show("image")