Update app.py
Browse files
app.py
CHANGED
|
@@ -1,69 +1,77 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import
|
|
|
|
| 3 |
import numpy as np
|
| 4 |
-
import easyocr
|
| 5 |
-
from paddleocr import PaddleOCR
|
| 6 |
|
| 7 |
-
# Khởi tạo
|
| 8 |
-
|
| 9 |
-
"
|
| 10 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
}
|
| 12 |
|
| 13 |
-
#
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
)
|
| 19 |
|
| 20 |
-
def
|
| 21 |
"""
|
| 22 |
-
|
| 23 |
"""
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
if w > max_width:
|
| 28 |
-
ratio = max_width / w
|
| 29 |
-
image = cv2.resize(image, (max_width, int(h * ratio)))
|
| 30 |
-
return image
|
| 31 |
|
| 32 |
-
|
| 33 |
-
""
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
|
|
|
|
|
|
| 37 |
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
-
# Giao diện Gradio
|
| 54 |
demo = gr.Interface(
|
| 55 |
-
fn=
|
| 56 |
inputs=[
|
| 57 |
-
gr.Image(type="
|
| 58 |
-
gr.Dropdown(
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
)
|
| 63 |
],
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
|
|
|
| 67 |
)
|
| 68 |
|
| 69 |
if __name__ == "__main__":
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from paddleocr import PaddleOCR, draw_ocr
|
| 3 |
+
from PIL import Image
|
| 4 |
import numpy as np
|
|
|
|
|
|
|
| 5 |
|
| 6 |
+
# ===== Khởi tạo model OCR cho từng ngôn ngữ =====
|
| 7 |
+
LANG_CONFIG = {
|
| 8 |
+
"ch": "Chinese (Simplified)",
|
| 9 |
+
"en": "English",
|
| 10 |
+
"japan": "Japanese",
|
| 11 |
+
"korean": "Korean",
|
| 12 |
+
"fr": "French",
|
| 13 |
+
"german": "German",
|
| 14 |
}
|
| 15 |
|
| 16 |
+
# Load toàn bộ model sẵn (startup lâu nhưng inference nhanh)
|
| 17 |
+
ocr_models = {
|
| 18 |
+
lang: PaddleOCR(lang=lang, use_angle_cls=True, use_gpu=False)
|
| 19 |
+
for lang in LANG_CONFIG.keys()
|
| 20 |
+
}
|
|
|
|
| 21 |
|
| 22 |
+
def inference(img, lang):
|
| 23 |
"""
|
| 24 |
+
Nhận diện text bằng PaddleOCR và vẽ kết quả ra ảnh.
|
| 25 |
"""
|
| 26 |
+
# OCR
|
| 27 |
+
ocr = ocr_models[lang]
|
| 28 |
+
result = ocr.ocr(img, cls=True)[0]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
+
# Load ảnh gốc bằng PIL
|
| 31 |
+
image = Image.open(img).convert("RGB")
|
| 32 |
+
|
| 33 |
+
# Lấy bbox, text, confidence
|
| 34 |
+
boxes = [line[0] for line in result]
|
| 35 |
+
txts = [line[1][0] for line in result]
|
| 36 |
+
scores = [line[1][1] for line in result]
|
| 37 |
|
| 38 |
+
# Vẽ kết quả OCR
|
| 39 |
+
im_show = draw_ocr(image, boxes, txts, scores, font_path="./simfang.ttf")
|
| 40 |
+
im_show = Image.fromarray(im_show.astype(np.uint8)) # từ numpy -> PIL
|
| 41 |
+
|
| 42 |
+
# Trả về text + ảnh
|
| 43 |
+
text_out = "\n".join([f"{t} (conf={s:.2f})" for t, s in zip(txts, scores)])
|
| 44 |
+
return im_show, text_out
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
# ===== Giao diện Gradio =====
|
| 48 |
+
title = "PaddleOCR Multi-language"
|
| 49 |
+
description = """
|
| 50 |
+
PaddleOCR hỗ trợ Chinese, English, Japanese, Korean, French, German.
|
| 51 |
+
- Upload ảnh + chọn ngôn ngữ để nhận diện.
|
| 52 |
+
- Model load sẵn toàn bộ, inference nhanh.
|
| 53 |
+
"""
|
| 54 |
+
|
| 55 |
+
examples = [
|
| 56 |
+
["en_example.jpg", "en"],
|
| 57 |
+
["cn_example.jpg", "ch"],
|
| 58 |
+
["jp_example.jpg", "japan"],
|
| 59 |
+
]
|
| 60 |
|
|
|
|
| 61 |
demo = gr.Interface(
|
| 62 |
+
fn=inference,
|
| 63 |
inputs=[
|
| 64 |
+
gr.Image(type="filepath", label="Upload ảnh"),
|
| 65 |
+
gr.Dropdown(choices=list(LANG_CONFIG.keys()), value="ch", label="Ngôn ngữ OCR")
|
| 66 |
+
],
|
| 67 |
+
outputs=[
|
| 68 |
+
gr.Image(type="pil", label="Ảnh kết quả"),
|
| 69 |
+
gr.Textbox(label="Kết quả nhận diện")
|
| 70 |
],
|
| 71 |
+
title=title,
|
| 72 |
+
description=description,
|
| 73 |
+
examples=examples,
|
| 74 |
+
cache_examples=False,
|
| 75 |
)
|
| 76 |
|
| 77 |
if __name__ == "__main__":
|