Spaces:
Sleeping
Sleeping
update ocr service
Browse files- 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
|
| 14 |
-
#
|
| 15 |
-
|
| 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 .
|
| 29 |
-
|
| 30 |
-
results = self.ocr.predict(img_path)
|
| 31 |
|
| 32 |
raw_texts = []
|
| 33 |
|
| 34 |
-
# --- 2. XỬ LÝ KẾT QUẢ (Structure
|
| 35 |
-
# Kết quả trả về là
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 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 |
-
|
| 76 |
-
|
| 77 |
-
#
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 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 vì .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 có 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}")
|
|
|