direction.py 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import dataclasses
  2. import cv2
  3. import numpy as np
  4. from dataclasses import dataclass
  5. from paddleocr import PaddleOCR
  6. @dataclass
  7. class AngleDetector(object):
  8. ocr: PaddleOCR
  9. def detect_angle(self, img, result) -> int:
  10. wc = 0
  11. hc = 0
  12. count_0 = 0
  13. count_180 = 0
  14. angle = 0
  15. print(result)
  16. for res in result:
  17. txt = res[1][0]
  18. if '号' not in txt: continue
  19. a = np.array(res[0])
  20. l, t = np.min(a, axis=0).tolist()
  21. r, b = np.max(a, axis=0).tolist()
  22. l, t, r, b = list(map(int, [l, t, r, b]))
  23. if b - t > r - l:
  24. hc += 1
  25. else:
  26. wc += 1
  27. imgb = img[t:b, l:r, :]
  28. r = self.ocr.ocr(imgb, det=False, rec=False, cls=True)
  29. print(f'ocr angle: {r}')
  30. if int(r[0][0]) == 180:
  31. count_180 += 1
  32. else:
  33. count_0 += 1
  34. if hc >= wc:
  35. if count_0 >= count_180:
  36. angle = 90
  37. else:
  38. angle = 270
  39. else:
  40. if count_0 > count_180:
  41. angle = 0
  42. else:
  43. angle = 180
  44. return angle
  45. def detect_angle(image):
  46. mask = np.zeros(image.shape, dtype=np.uint8)
  47. gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
  48. blur = cv2.GaussianBlur(gray, (3, 3), 0)
  49. adaptive = cv2.adaptiveThreshold(blur, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV, 15, 4)
  50. cnts = cv2.findContours(adaptive, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
  51. cnts = cnts[0] if len(cnts) == 2 else cnts[1]
  52. for c in cnts:
  53. area = cv2.contourArea(c)
  54. if area < 45000 and area > 20:
  55. cv2.drawContours(mask, [c], -1, (255, 255, 255), -1)
  56. mask = cv2.cvtColor(mask, cv2.COLOR_BGR2GRAY)
  57. h, w = mask.shape
  58. # Horizontal
  59. if w > h:
  60. left = mask[0:h, 0:0 + w // 2]
  61. right = mask[0:h, w // 2:]
  62. left_pixels = cv2.countNonZero(left)
  63. right_pixels = cv2.countNonZero(right)
  64. return 0 if left_pixels >= right_pixels else 180
  65. # Vertical
  66. else:
  67. top = mask[0:h // 2, 0:w]
  68. bottom = mask[h // 2:, 0:w]
  69. top_pixels = cv2.countNonZero(top)
  70. bottom_pixels = cv2.countNonZero(bottom)
  71. return 90 if bottom_pixels >= top_pixels else 270
  72. if __name__ == '__main__':
  73. image = cv2.imread('d40.jpg')
  74. angle = detect_angle(image)
  75. print(angle)