ppocr.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import ppocronnx.predict_system
  2. class TextSystem(ppocronnx.predict_system.TextSystem):
  3. def __init__(
  4. self,
  5. use_angle_cls=False,
  6. box_thresh=0.6,
  7. unclip_ratio=1.6,
  8. rec_model_path=None,
  9. det_model_path=None,
  10. ort_providers=None
  11. ):
  12. super().__init__(
  13. use_angle_cls=use_angle_cls,
  14. box_thresh=box_thresh,
  15. unclip_ratio=unclip_ratio,
  16. rec_model_path=rec_model_path,
  17. det_model_path=det_model_path,
  18. ort_providers=ort_providers
  19. )
  20. # def ocr_single_line(self, img):
  21. # img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
  22. # return super().ocr_single_line(img)
  23. #
  24. # def detect_and_ocr(self, img: np.ndarray,**kwargs):
  25. # img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
  26. # return super().detect_and_ocr(img, **kwargs)
  27. def sorted_boxes(dt_boxes):
  28. """
  29. Sort text boxes in order from top to bottom, left to right
  30. args:
  31. dt_boxes(array):detected text boxes with shape [4, 2]
  32. return:
  33. sorted boxes(array) with shape [4, 2]
  34. """
  35. num_boxes = dt_boxes.shape[0]
  36. sorted_boxes = sorted(dt_boxes, key=lambda x: (x[0][1], x[0][0]))
  37. _boxes = list(sorted_boxes)
  38. for i in range(num_boxes - 1):
  39. for j in range(i, -1, -1):
  40. if abs(_boxes[j + 1][0][1] - _boxes[j][0][1]) < 10 and \
  41. (_boxes[j + 1][0][0] < _boxes[j][0][0]):
  42. tmp = _boxes[j]
  43. _boxes[j] = _boxes[j + 1]
  44. _boxes[j + 1] = tmp
  45. else:
  46. break
  47. return _boxes
  48. # sorted_boxes() from PaddleOCR 2.6, newer and better than the one in ppocr-onnx
  49. ppocronnx.predict_system.sorted_boxes = sorted_boxes