Update app.py
Browse files
app.py
CHANGED
|
@@ -3,8 +3,14 @@ import cv2
|
|
| 3 |
import numpy as np
|
| 4 |
from paddleocr import PaddleOCR
|
| 5 |
|
| 6 |
-
# Khởi tạo PaddleOCR 1 lần duy nhất, CPU mode, đa ngôn ngữ
|
| 7 |
-
ocr = PaddleOCR(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
def preprocess_image(image, max_width=1024):
|
| 10 |
"""
|
|
@@ -15,20 +21,23 @@ def preprocess_image(image, max_width=1024):
|
|
| 15 |
h, w = image.shape[:2]
|
| 16 |
if w > max_width:
|
| 17 |
ratio = max_width / w
|
| 18 |
-
image = cv2.resize(image, (max_width, int(h*ratio)))
|
| 19 |
return image
|
| 20 |
|
| 21 |
def ocr_image(image):
|
| 22 |
"""
|
| 23 |
-
Nhận diện text từ ảnh với PaddleOCR.
|
| 24 |
"""
|
| 25 |
image = preprocess_image(image)
|
| 26 |
-
|
|
|
|
| 27 |
result = ocr.ocr(image)
|
|
|
|
| 28 |
texts = []
|
| 29 |
for line in result:
|
| 30 |
for res in line:
|
| 31 |
-
texts.append(res[1][0])
|
|
|
|
| 32 |
if texts:
|
| 33 |
return "\n".join(texts)
|
| 34 |
return "Không nhận diện được văn bản"
|
|
@@ -39,7 +48,7 @@ demo = gr.Interface(
|
|
| 39 |
inputs=gr.Image(type="numpy"),
|
| 40 |
outputs="text",
|
| 41 |
title="OCR Đa Ngôn Ngữ với PaddleOCR (CPU)",
|
| 42 |
-
description="Nhận diện chữ trong ảnh - Hỗ trợ
|
| 43 |
)
|
| 44 |
|
| 45 |
if __name__ == "__main__":
|
|
|
|
| 3 |
import numpy as np
|
| 4 |
from paddleocr import PaddleOCR
|
| 5 |
|
| 6 |
+
# Khởi tạo PaddleOCR 1 lần duy nhất, CPU mode, mô hình đa ngôn ngữ
|
| 7 |
+
ocr = PaddleOCR(
|
| 8 |
+
use_angle_cls=True,
|
| 9 |
+
lang='multi_languages', # Hỗ trợ nhiều ngôn ngữ cùng lúc
|
| 10 |
+
use_doc_orientation_classify=False,
|
| 11 |
+
use_doc_unwarping=False,
|
| 12 |
+
use_textline_orientation=False
|
| 13 |
+
)
|
| 14 |
|
| 15 |
def preprocess_image(image, max_width=1024):
|
| 16 |
"""
|
|
|
|
| 21 |
h, w = image.shape[:2]
|
| 22 |
if w > max_width:
|
| 23 |
ratio = max_width / w
|
| 24 |
+
image = cv2.resize(image, (max_width, int(h * ratio)))
|
| 25 |
return image
|
| 26 |
|
| 27 |
def ocr_image(image):
|
| 28 |
"""
|
| 29 |
+
Nhận diện text từ ảnh với PaddleOCR (đa ngôn ngữ).
|
| 30 |
"""
|
| 31 |
image = preprocess_image(image)
|
| 32 |
+
|
| 33 |
+
# Chạy OCR trên CPU
|
| 34 |
result = ocr.ocr(image)
|
| 35 |
+
|
| 36 |
texts = []
|
| 37 |
for line in result:
|
| 38 |
for res in line:
|
| 39 |
+
texts.append(res[1][0]) # res[1][0] là text, res[1][1] là confidence
|
| 40 |
+
|
| 41 |
if texts:
|
| 42 |
return "\n".join(texts)
|
| 43 |
return "Không nhận diện được văn bản"
|
|
|
|
| 48 |
inputs=gr.Image(type="numpy"),
|
| 49 |
outputs="text",
|
| 50 |
title="OCR Đa Ngôn Ngữ với PaddleOCR (CPU)",
|
| 51 |
+
description="Nhận diện chữ trong ảnh - Hỗ trợ nhiều ngôn ngữ cùng lúc (CPU mode)"
|
| 52 |
)
|
| 53 |
|
| 54 |
if __name__ == "__main__":
|