HoagMin commited on
Commit
751fef8
·
1 Parent(s): b29d4b3

update ocr service

Browse files
Files changed (1) hide show
  1. services/ocr_service.py +23 -34
services/ocr_service.py CHANGED
@@ -10,11 +10,9 @@ logging.getLogger("paddle").setLevel(logging.ERROR)
10
 
11
  class OCRService:
12
  def __init__(self):
13
- """Khởi tạo PaddleOCR cho phiên bản Pipeline mới."""
14
- # Với bản mới, các tham số cấu hình thường được nhận tự động hoặc qua config file.
15
- # Ta khởi tạo đơn giản nhất để tránh lỗi tham số lạ.
16
- self.ocr = PaddleOCR(lang='vi', use_textline_orientation=False, use_doc_orientation_classify=False,
17
- use_doc_unwarping=False)
18
 
19
  def extract_text(self, img_path: str) -> List[str]:
20
  """
@@ -25,27 +23,19 @@ class OCRService:
25
  return []
26
 
27
  try:
28
- # --- 1. CHẠY OCR (Dùng .predict theo yêu cầu của thư viện) ---
29
- # KHÔNG truyền cls=True vào đây nữa
30
- results = self.ocr.predict(img_path)
31
 
32
  raw_texts = []
33
 
34
- # --- 2. XỬ LÝ KẾT QUẢ (Structure mới) ---
35
- # Kết quả trả về là một list các object, mỗi object chứa thông tin nhận diện
36
- for res in results:
37
- # Kiểm tra xem object có thuộc tính 'rec_texts' không (đây là đặc trưng bản mới)
38
- if hasattr(res, 'rec_texts'):
39
- if res.rec_texts:
40
- for text in res.rec_texts:
41
- if text:
42
- raw_texts.append(str(text))
43
-
44
- # Dự phòng: Nếu nó trả về Dictionary (một số version khác)
45
- elif isinstance(res, dict) and 'rec_texts' in res:
46
- raw_texts.extend(res['rec_texts'])
47
 
48
- # --- 3. BỘ LỌC RÁC (Giữ nguyên logic ) ---
49
  clean_texts = []
50
  for text in raw_texts:
51
  text = text.strip()
@@ -72,15 +62,14 @@ class OCRService:
72
  traceback.print_exc()
73
  return []
74
 
75
- #--- TEST CODE ---
76
-
77
- #if __name__ == "__main__":
78
- # service = OCRService()
79
- # test_path = r"static/debug/bunbohue.png"
80
- # print(f"🔍 Đang đọc: {test_path}")
81
- # texts = service.extract_text(test_path)
82
- #
83
- # print("-" * 30)
84
- # print(f"✅ KẾT QUẢ ({len(texts)} dòng):")
85
- # for t in texts:
86
- # print(f" - {t}") ##
 
10
 
11
  class OCRService:
12
  def __init__(self):
13
+ """Khởi tạo PaddleOCR cho phiên bản 2.8.1 ổn định."""
14
+ # Tham số use_angle_cls thay thế cho các cấu hình doc orientation bản mới
15
+ self.ocr = PaddleOCR(lang='vi', use_angle_cls=False)
 
 
16
 
17
  def extract_text(self, img_path: str) -> List[str]:
18
  """
 
23
  return []
24
 
25
  try:
26
+ # --- 1. CHẠY OCR (Dùng .ocr thay .predict) ---
27
+ results = self.ocr.ocr(img_path, cls=False)
 
28
 
29
  raw_texts = []
30
 
31
+ # --- 2. XỬ LÝ KẾT QUẢ (Structure của bản 2.x) ---
32
+ # Kết quả trả về là list các dòng, mỗi dòng dạng: [tọa_độ_box, (text, độ_tự_tin)]
33
+ if results and len(results) > 0 and results[0] is not None:
34
+ for line in results[0]:
35
+ text = line[1][0] # line[1] là tuple (text, score), lấy phần tử đầu tiên
36
+ raw_texts.append(text)
 
 
 
 
 
 
 
37
 
38
+ # --- 3. BỘ LỌC RÁC (Giữ nguyên logic cực kỳ xịn của bạn) ---
39
  clean_texts = []
40
  for text in raw_texts:
41
  text = text.strip()
 
62
  traceback.print_exc()
63
  return []
64
 
65
+ # --- TEST CODE ---
66
+ # if __name__ == "__main__":
67
+ # service = OCRService()
68
+ # test_path = r"static/debug/bunbohue.png"
69
+ # print(f"🔍 Đang đọc: {test_path}")
70
+ # texts = service.extract_text(test_path)
71
+ #
72
+ # print("-" * 30)
73
+ # print(f" KẾT QUẢ ({len(texts)} dòng):")
74
+ # for t in texts:
75
+ # print(f" - {t}")