Update app.py
Browse files
app.py
CHANGED
|
@@ -1,17 +1,14 @@
|
|
| 1 |
from fastapi import FastAPI, UploadFile, File, HTTPException
|
| 2 |
from fastapi.responses import HTMLResponse
|
| 3 |
from fastapi.middleware.cors import CORSMiddleware
|
| 4 |
-
|
| 5 |
-
import paddleocr # <-- Thêm dòng này để gọi lõi thư viện
|
| 6 |
import numpy as np
|
| 7 |
from PIL import Image
|
| 8 |
import io
|
| 9 |
-
import os # <-- Thêm dòng này để xử lý đường dẫn
|
| 10 |
|
| 11 |
-
# 1. Khởi tạo
|
| 12 |
-
app = FastAPI(title="
|
| 13 |
|
| 14 |
-
# Cấu hình CORS
|
| 15 |
app.add_middleware(
|
| 16 |
CORSMiddleware,
|
| 17 |
allow_origins=["*"],
|
|
@@ -20,72 +17,47 @@ app.add_middleware(
|
|
| 20 |
allow_headers=["*"],
|
| 21 |
)
|
| 22 |
|
| 23 |
-
# 2.
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
ocr = PaddleOCR(
|
| 29 |
-
use_angle_cls=True,
|
| 30 |
-
lang='vi',
|
| 31 |
-
ocr_version='PP-OCRv3', # Dùng v3 thay vì v4
|
| 32 |
-
rec_char_dict_path=vi_dict_path # Trỏ thẳng vào từ điển tiếng Việt
|
| 33 |
-
)
|
| 34 |
-
print("Tải mô hình hoàn tất, sẵn sàng phục vụ!")
|
| 35 |
-
|
| 36 |
-
# -----------------------------------------------------
|
| 37 |
-
# -----------------------------------------------------
|
| 38 |
|
| 39 |
|
| 40 |
# -----------------------------------------------------
|
| 41 |
-
# ROUTE 1:
|
| 42 |
# -----------------------------------------------------
|
| 43 |
@app.get("/", response_class=HTMLResponse)
|
| 44 |
async def serve_frontend():
|
| 45 |
try:
|
| 46 |
-
# Đọc và hiển thị nội dung file index.html
|
| 47 |
with open("index.html", "r", encoding="utf-8") as f:
|
| 48 |
return f.read()
|
| 49 |
except FileNotFoundError:
|
| 50 |
-
return "<h1>Lỗi: Không tìm thấy file index.html.
|
| 51 |
|
| 52 |
|
| 53 |
# -----------------------------------------------------
|
| 54 |
-
# ROUTE 2:
|
| 55 |
# -----------------------------------------------------
|
| 56 |
@app.post("/predict")
|
| 57 |
async def predict_image(file: UploadFile = File(...)):
|
| 58 |
-
# Bước 1: Kiểm tra định dạng file (Chỉ nhận ảnh)
|
| 59 |
if not file.content_type.startswith('image/'):
|
| 60 |
-
raise HTTPException(status_code=400, detail="
|
| 61 |
|
| 62 |
try:
|
| 63 |
-
#
|
| 64 |
contents = await file.read()
|
| 65 |
-
|
| 66 |
-
# Bước 3: Chuyển Byte thành cấu trúc ảnh RGB bằng thư viện Pillow (PIL)
|
| 67 |
image = Image.open(io.BytesIO(contents)).convert('RGB')
|
| 68 |
-
|
| 69 |
-
# Bước 4: Chuyển ảnh thành ma trận số (Numpy Array) để PaddleOCR có thể đọc được
|
| 70 |
img_array = np.array(image)
|
| 71 |
|
| 72 |
-
#
|
| 73 |
-
|
|
|
|
| 74 |
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
# Bước 6: Xử lý kết quả trả về
|
| 78 |
-
# Dữ liệu từ PaddleOCR có cấu trúc lồng nhau, ta cần bóc tách để lấy đoạn text
|
| 79 |
-
if result and result[0]:
|
| 80 |
-
# Bỏ qua tọa độ (bounding box) và độ chính xác (confidence), chỉ lấy text
|
| 81 |
-
texts = [line[1][0] for line in result[0]]
|
| 82 |
-
|
| 83 |
-
# Nối các dòng chữ lại với nhau, cách nhau bằng dấu xuống dòng
|
| 84 |
-
extracted_text = "\n".join(texts)
|
| 85 |
|
| 86 |
-
# Trả kết quả về cho Frontend dưới dạng JSON
|
| 87 |
return {"text": extracted_text}
|
| 88 |
|
| 89 |
except Exception as e:
|
| 90 |
-
|
| 91 |
-
raise HTTPException(status_code=500, detail=f"Lỗi hệ thống trong quá trình OCR: {str(e)}")
|
|
|
|
| 1 |
from fastapi import FastAPI, UploadFile, File, HTTPException
|
| 2 |
from fastapi.responses import HTMLResponse
|
| 3 |
from fastapi.middleware.cors import CORSMiddleware
|
| 4 |
+
import easyocr
|
|
|
|
| 5 |
import numpy as np
|
| 6 |
from PIL import Image
|
| 7 |
import io
|
|
|
|
| 8 |
|
| 9 |
+
# 1. Khởi tạo FastAPI
|
| 10 |
+
app = FastAPI(title="EasyOCR Tiếng Việt API")
|
| 11 |
|
|
|
|
| 12 |
app.add_middleware(
|
| 13 |
CORSMiddleware,
|
| 14 |
allow_origins=["*"],
|
|
|
|
| 17 |
allow_headers=["*"],
|
| 18 |
)
|
| 19 |
|
| 20 |
+
# 2. Khởi tạo mô hình EasyOCR (Chỉ chạy 1 lần)
|
| 21 |
+
print("Đang tải mô hình EasyOCR (Tiếng Việt & Tiếng Anh) vào RAM...")
|
| 22 |
+
# gpu=False: Khẳng định việc chạy trên CPU
|
| 23 |
+
reader = easyocr.Reader(['vi', 'en'], gpu=False)
|
| 24 |
+
print("Tải mô hình hoàn tất!")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
|
| 27 |
# -----------------------------------------------------
|
| 28 |
+
# ROUTE 1: Giao diện Web
|
| 29 |
# -----------------------------------------------------
|
| 30 |
@app.get("/", response_class=HTMLResponse)
|
| 31 |
async def serve_frontend():
|
| 32 |
try:
|
|
|
|
| 33 |
with open("index.html", "r", encoding="utf-8") as f:
|
| 34 |
return f.read()
|
| 35 |
except FileNotFoundError:
|
| 36 |
+
return "<h1>Lỗi: Không tìm thấy file index.html.</h1>"
|
| 37 |
|
| 38 |
|
| 39 |
# -----------------------------------------------------
|
| 40 |
+
# ROUTE 2: Xử lý OCR
|
| 41 |
# -----------------------------------------------------
|
| 42 |
@app.post("/predict")
|
| 43 |
async def predict_image(file: UploadFile = File(...)):
|
|
|
|
| 44 |
if not file.content_type.startswith('image/'):
|
| 45 |
+
raise HTTPException(status_code=400, detail="Vui lòng tải tệp hình ảnh.")
|
| 46 |
|
| 47 |
try:
|
| 48 |
+
# Đọc ảnh vào RAM
|
| 49 |
contents = await file.read()
|
|
|
|
|
|
|
| 50 |
image = Image.open(io.BytesIO(contents)).convert('RGB')
|
|
|
|
|
|
|
| 51 |
img_array = np.array(image)
|
| 52 |
|
| 53 |
+
# Đưa vào EasyOCR đọc chữ
|
| 54 |
+
# detail=0: Bỏ qua tọa độ, chỉ lấy thẳng danh sách các dòng chữ
|
| 55 |
+
results = reader.readtext(img_array, detail=0)
|
| 56 |
|
| 57 |
+
# Ghép các dòng chữ lại với nhau
|
| 58 |
+
extracted_text = "\n".join(results)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
|
|
|
|
| 60 |
return {"text": extracted_text}
|
| 61 |
|
| 62 |
except Exception as e:
|
| 63 |
+
raise HTTPException(status_code=500, detail=f"Lỗi hệ thống: {str(e)}")
|
|
|