Harry Pham commited on
Commit
13c4496
·
1 Parent(s): 582343c

update OCR

Browse files
Files changed (1) hide show
  1. src/inference.py +25 -10
src/inference.py CHANGED
@@ -185,35 +185,50 @@ def ocr_note(img_path, backend="paddle"):
185
  if img is None:
186
  return ""
187
 
188
- # Tiền xử lý mạnh: tăng độ phân giải, lọc nhiễu, làm sắc nét
189
  h, w = img.shape[:2]
190
  if w < 1000:
191
  scale = 1200 / w
192
  img = cv2.resize(img, (int(w*scale), int(h*scale)), interpolation=cv2.INTER_CUBIC)
193
 
194
  gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
195
- # Sử dụng CLAHE mạnh hơn
196
  clahe = cv2.createCLAHE(clipLimit=3.5, tileGridSize=(4,4))
197
  gray = clahe.apply(gray)
198
- # Khử nhiễu nhưng giữ cạnh
199
  gray = cv2.bilateralFilter(gray, 9, 75, 75)
200
- # Làm sắc nét
201
  kernel_sharpen = np.array([[-1,-1,-1],[-1,9,-1],[-1,-1,-1]])
202
  gray = cv2.filter2D(gray, -1, kernel_sharpen)
203
- # Nhị phân hóa thích ứng
204
  gray = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 11, 2)
205
  img_proc = cv2.cvtColor(gray, cv2.COLOR_GRAY2BGR)
206
 
207
  reader = get_ocr_reader(backend)
208
- if hasattr(reader, 'ocr'):
209
  result = reader.ocr(img_proc, cls=True)
210
  if result and result[0]:
211
  texts = [line[1][0].strip() for line in result[0] if line[1][1] > 0.3]
212
  return "\n".join(texts)
213
- else:
214
- results = reader.readtext(img_proc, detail=1, paragraph=True)
215
- texts = [text for (_, text, conf) in results if conf > 0.3]
216
- return "\n".join(texts)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
217
  return ""
218
 
219
  import cv2
 
185
  if img is None:
186
  return ""
187
 
188
+ # Tiền xử lý mạnh
189
  h, w = img.shape[:2]
190
  if w < 1000:
191
  scale = 1200 / w
192
  img = cv2.resize(img, (int(w*scale), int(h*scale)), interpolation=cv2.INTER_CUBIC)
193
 
194
  gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
 
195
  clahe = cv2.createCLAHE(clipLimit=3.5, tileGridSize=(4,4))
196
  gray = clahe.apply(gray)
 
197
  gray = cv2.bilateralFilter(gray, 9, 75, 75)
 
198
  kernel_sharpen = np.array([[-1,-1,-1],[-1,9,-1],[-1,-1,-1]])
199
  gray = cv2.filter2D(gray, -1, kernel_sharpen)
 
200
  gray = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 11, 2)
201
  img_proc = cv2.cvtColor(gray, cv2.COLOR_GRAY2BGR)
202
 
203
  reader = get_ocr_reader(backend)
204
+ if hasattr(reader, 'ocr'): # PaddleOCR
205
  result = reader.ocr(img_proc, cls=True)
206
  if result and result[0]:
207
  texts = [line[1][0].strip() for line in result[0] if line[1][1] > 0.3]
208
  return "\n".join(texts)
209
+ else: # EasyOCR
210
+ results = reader.readtext(img_proc, detail=1, paragraph=False)
211
+ if not results:
212
+ return ""
213
+ # Sắp xếp theo tọa độ y (dòng) và x (cột)
214
+ results_sorted = sorted(results, key=lambda x: (x[0][0][1], x[0][0][0]))
215
+ lines = []
216
+ current_line = []
217
+ prev_y = None
218
+ for (pts, text, conf) in results_sorted:
219
+ if conf < 0.3:
220
+ continue
221
+ y_center = sum(p[1] for p in pts) / 4
222
+ if prev_y is None or abs(y_center - prev_y) < 15:
223
+ current_line.append(text.strip())
224
+ else:
225
+ if current_line:
226
+ lines.append(" ".join(current_line))
227
+ current_line = [text.strip()]
228
+ prev_y = y_center
229
+ if current_line:
230
+ lines.append(" ".join(current_line))
231
+ return "\n".join(lines)
232
  return ""
233
 
234
  import cv2