Markuspierre commited on
Commit
858c1df
·
verified ·
1 Parent(s): 27f55b7

Update ocr_core.py

Browse files
Files changed (1) hide show
  1. ocr_core.py +62 -14
ocr_core.py CHANGED
@@ -19,32 +19,80 @@ def load_bgr(source):
19
  img = ImageOps.exif_transpose(Image.open(source)).convert("RGB")
20
  return cv2.cvtColor(np.array(img), cv2.COLOR_RGB2BGR)
21
 
22
-
23
  def preprocess(bgr, size=2200):
24
  """Redresse la photo, neutralise les ombres, efface les traits de la grille."""
25
  h, w = bgr.shape[:2]
 
26
  s = size / max(h, w)
27
- bgr = cv2.resize(bgr, None, fx=s, fy=s, interpolation=cv2.INTER_CUBIC)
 
 
 
 
28
  gray = cv2.cvtColor(bgr, cv2.COLOR_BGR2GRAY)
29
- bw = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV, 41, 15)
 
 
 
 
 
 
30
 
31
  angle = 0.0
32
- segs = cv2.HoughLinesP(bw, 1, np.pi / 720, threshold=200,
33
- minLineLength=bw.shape[1] // 4, maxLineGap=20)
 
 
 
 
 
 
34
  if segs is not None:
35
- angs = [np.degrees(np.arctan2(y2 - y1, x2 - x1)) for x1, y1, x2, y2 in segs[:, 0]]
36
- angs = [a for a in angs if abs(a) < 20]
 
 
 
 
 
 
 
 
 
 
37
  if angs:
38
  angle = float(np.median(angs))
39
- M = cv2.getRotationMatrix2D((bw.shape[1] / 2, bw.shape[0] / 2), angle, 1)
40
- bw = cv2.warpAffine(bw, M, (bw.shape[1], bw.shape[0]), flags=cv2.INTER_LINEAR)
41
 
42
- hk = cv2.getStructuringElement(cv2.MORPH_RECT, (80, 1))
43
- vk = cv2.getStructuringElement(cv2.MORPH_RECT, (1, 80))
44
- grid = cv2.add(cv2.morphologyEx(bw, cv2.MORPH_OPEN, hk), cv2.morphologyEx(bw, cv2.MORPH_OPEN, vk))
45
- bw = cv2.subtract(bw, cv2.dilate(grid, np.ones((3, 3), np.uint8)))
46
- return 255 - bw, angle
 
 
 
 
 
 
 
 
 
47
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
48
 
49
  def read_words(bw, lang="eng", psm=11):
50
  d = pytesseract.image_to_data(bw, lang=lang, config=f"--psm {int(psm)}",
 
19
  img = ImageOps.exif_transpose(Image.open(source)).convert("RGB")
20
  return cv2.cvtColor(np.array(img), cv2.COLOR_RGB2BGR)
21
 
 
22
  def preprocess(bgr, size=2200):
23
  """Redresse la photo, neutralise les ombres, efface les traits de la grille."""
24
  h, w = bgr.shape[:2]
25
+
26
  s = size / max(h, w)
27
+ bgr = cv2.resize(
28
+ bgr, None, fx=s, fy=s,
29
+ interpolation=cv2.INTER_CUBIC
30
+ )
31
+
32
  gray = cv2.cvtColor(bgr, cv2.COLOR_BGR2GRAY)
33
+
34
+ bw = cv2.adaptiveThreshold(
35
+ gray, 255,
36
+ cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
37
+ cv2.THRESH_BINARY_INV,
38
+ 41, 15
39
+ )
40
 
41
  angle = 0.0
42
+
43
+ segs = cv2.HoughLinesP(
44
+ bw, 1, np.pi / 720,
45
+ threshold=200,
46
+ minLineLength=bw.shape[1] // 4,
47
+ maxLineGap=20
48
+ )
49
+
50
  if segs is not None:
51
+ angs = []
52
+
53
+ for segment in segs.reshape(-1, 4):
54
+ x1, y1, x2, y2 = map(int, segment)
55
+
56
+ a = np.degrees(
57
+ np.arctan2(y2 - y1, x2 - x1)
58
+ )
59
+
60
+ if abs(a) < 20:
61
+ angs.append(float(a))
62
+
63
  if angs:
64
  angle = float(np.median(angs))
 
 
65
 
66
+ M = cv2.getRotationMatrix2D(
67
+ (bw.shape[1] / 2, bw.shape[0] / 2),
68
+ angle, 1
69
+ )
70
+
71
+ bw = cv2.warpAffine(
72
+ bw, M,
73
+ (bw.shape[1], bw.shape[0]),
74
+ flags=cv2.INTER_LINEAR
75
+ )
76
+
77
+ hk = cv2.getStructuringElement(
78
+ cv2.MORPH_RECT, (80, 1)
79
+ )
80
 
81
+ vk = cv2.getStructuringElement(
82
+ cv2.MORPH_RECT, (1, 80)
83
+ )
84
+
85
+ grid = cv2.add(
86
+ cv2.morphologyEx(bw, cv2.MORPH_OPEN, hk),
87
+ cv2.morphologyEx(bw, cv2.MORPH_OPEN, vk)
88
+ )
89
+
90
+ bw = cv2.subtract(
91
+ bw,
92
+ cv2.dilate(grid, np.ones((3, 3), np.uint8))
93
+ )
94
+
95
+ return 255 - bw, angle
96
 
97
  def read_words(bw, lang="eng", psm=11):
98
  d = pytesseract.image_to_data(bw, lang=lang, config=f"--psm {int(psm)}",