Spaces:
Sleeping
Sleeping
Update services/ocr_service.py
Browse files- services/ocr_service.py +19 -12
services/ocr_service.py
CHANGED
|
@@ -14,42 +14,47 @@ class OCRService:
|
|
| 14 |
self.ocr = PaddleOCR(lang='vi', use_textline_orientation=False, use_doc_orientation_classify=False,
|
| 15 |
use_doc_unwarping=False)
|
| 16 |
|
| 17 |
-
def extract_text(self, img_path: str) ->
|
| 18 |
"""
|
| 19 |
Trích xuất văn bản từ ảnh và lọc rác.
|
| 20 |
"""
|
|
|
|
|
|
|
|
|
|
| 21 |
if not os.path.exists(img_path):
|
| 22 |
print(f"❌ Không tìm thấy file ảnh: {img_path}")
|
| 23 |
return []
|
| 24 |
|
| 25 |
try:
|
| 26 |
-
|
| 27 |
results = self.ocr.ocr(img_path, cls=True)
|
| 28 |
|
| 29 |
raw_texts = []
|
| 30 |
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
|
|
|
|
|
|
| 37 |
|
| 38 |
-
|
| 39 |
-
raw_texts.extend(res['rec_texts'])
|
| 40 |
-
|
| 41 |
-
|
| 42 |
clean_texts = []
|
| 43 |
for text in raw_texts:
|
| 44 |
text = text.strip()
|
| 45 |
if not text: continue
|
| 46 |
|
|
|
|
| 47 |
if text.lower() in ['k', 'đ', 'd', '$', 'vnd', 'xu']:
|
| 48 |
clean_texts.append(text)
|
| 49 |
continue
|
| 50 |
|
|
|
|
| 51 |
if len(text) < 2 and not re.search(r'\d', text):
|
| 52 |
continue
|
|
|
|
| 53 |
if not re.search(r'[a-zA-Z0-9àáạảãâầấậẩẫăằắặẳẵèéẹẻẽêềếệểễìíịỉĩòóọỏõôồốộổỗơờớợởỡùúụủũưừứựửữỳýỵỷỹđ]', text):
|
| 54 |
continue
|
| 55 |
|
|
@@ -63,6 +68,8 @@ class OCRService:
|
|
| 63 |
traceback.print_exc()
|
| 64 |
return []
|
| 65 |
|
|
|
|
|
|
|
| 66 |
#--- TEST CODE ---
|
| 67 |
|
| 68 |
#if __name__ == "__main__":
|
|
|
|
| 14 |
self.ocr = PaddleOCR(lang='vi', use_textline_orientation=False, use_doc_orientation_classify=False,
|
| 15 |
use_doc_unwarping=False)
|
| 16 |
|
| 17 |
+
def extract_text(self, img_path: str) -> list[str]: # Đã sửa List thành list hoặc giữ nguyên List của bạn
|
| 18 |
"""
|
| 19 |
Trích xuất văn bản từ ảnh và lọc rác.
|
| 20 |
"""
|
| 21 |
+
import os
|
| 22 |
+
import re
|
| 23 |
+
|
| 24 |
if not os.path.exists(img_path):
|
| 25 |
print(f"❌ Không tìm thấy file ảnh: {img_path}")
|
| 26 |
return []
|
| 27 |
|
| 28 |
try:
|
| 29 |
+
# 1. Chạy OCR
|
| 30 |
results = self.ocr.ocr(img_path, cls=True)
|
| 31 |
|
| 32 |
raw_texts = []
|
| 33 |
|
| 34 |
+
# 2. Bóc tách dữ liệu từ mảng lồng nhau của PaddleOCR
|
| 35 |
+
# Kết quả cho 1 ảnh thường nằm ở results[0]
|
| 36 |
+
if results and results[0] is not None:
|
| 37 |
+
for line in results[0]:
|
| 38 |
+
# Cấu trúc mỗi line: [[tọa_độ_4_góc], ('Văn bản', độ_tự_tin)]
|
| 39 |
+
# Phần chữ nằm ở phần tử thứ 2 của tuple -> line[1][0]
|
| 40 |
+
text = line[1][0]
|
| 41 |
+
raw_texts.append(str(text))
|
| 42 |
|
| 43 |
+
# 3. Dọn dẹp văn bản (Phần logic cực kỳ tốt của bạn giữ nguyên)
|
|
|
|
|
|
|
|
|
|
| 44 |
clean_texts = []
|
| 45 |
for text in raw_texts:
|
| 46 |
text = text.strip()
|
| 47 |
if not text: continue
|
| 48 |
|
| 49 |
+
# Giữ lại các ký hiệu tiền tệ
|
| 50 |
if text.lower() in ['k', 'đ', 'd', '$', 'vnd', 'xu']:
|
| 51 |
clean_texts.append(text)
|
| 52 |
continue
|
| 53 |
|
| 54 |
+
# Bỏ qua chuỗi quá ngắn không có số
|
| 55 |
if len(text) < 2 and not re.search(r'\d', text):
|
| 56 |
continue
|
| 57 |
+
# Bỏ qua chuỗi không có ký tự alphabet/số nào (rác hoàn toàn)
|
| 58 |
if not re.search(r'[a-zA-Z0-9àáạảãâầấậẩẫăằắặẳẵèéẹẻẽêềếệểễìíịỉĩòóọỏõôồốộổỗơờớợởỡùúụủũưừứựửữỳýỵỷỹđ]', text):
|
| 59 |
continue
|
| 60 |
|
|
|
|
| 68 |
traceback.print_exc()
|
| 69 |
return []
|
| 70 |
|
| 71 |
+
|
| 72 |
+
|
| 73 |
#--- TEST CODE ---
|
| 74 |
|
| 75 |
#if __name__ == "__main__":
|