ranbac commited on
Commit
c5240fd
·
verified ·
1 Parent(s): 1bd8107

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -13
app.py CHANGED
@@ -1,4 +1,4 @@
1
- from fastapi import FastAPI, UploadFile, File, HTTPException
2
  from fastapi.responses import HTMLResponse
3
  from fastapi.middleware.cors import CORSMiddleware
4
  import easyocr
@@ -7,7 +7,7 @@ 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,
@@ -17,11 +17,17 @@ app.add_middleware(
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 hình hoàn tất!")
 
 
 
 
 
 
25
 
26
 
27
  # -----------------------------------------------------
@@ -37,10 +43,13 @@ async def serve_frontend():
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
 
@@ -50,11 +59,15 @@ async def predict_image(file: UploadFile = File(...)):
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}
 
1
+ from fastapi import FastAPI, UploadFile, File, Form, HTTPException
2
  from fastapi.responses import HTMLResponse
3
  from fastapi.middleware.cors import CORSMiddleware
4
  import easyocr
 
7
  import io
8
 
9
  # 1. Khởi tạo FastAPI
10
+ app = FastAPI(title="EasyOCR Đa Ngôn Ngữ API")
11
 
12
  app.add_middleware(
13
  CORSMiddleware,
 
17
  allow_headers=["*"],
18
  )
19
 
20
+ # 2. Khởi tạo 2 mô hình EasyOCR chạy song song (Chỉ tải 1 lần)
21
+ print("Đang tải các mô hình EasyOCR vào RAM (Sẽ mất thêm chút thời gian cho lần đầu)...")
22
+
23
+ print("- Đang nạp hình: Tiếng Việt + Tiếng Anh...")
24
+ reader_vi = easyocr.Reader(['vi', 'en'], gpu=False)
25
+
26
+ print("- Đang nạp mô hình: Tiếng Trung (Giản thể) + Tiếng Anh...")
27
+ reader_zh = easyocr.Reader(['ch_sim', 'en'], gpu=False)
28
+ # Lưu ý: Nếu muốn đọc Tiếng Trung Phồn thể (Đài Loan, HK), bạn đổi 'ch_sim' thành 'ch_tra' nhé.
29
+
30
+ print("Tải mô hình hoàn tất, sẵn sàng phục vụ!")
31
 
32
 
33
  # -----------------------------------------------------
 
43
 
44
 
45
  # -----------------------------------------------------
46
+ # ROUTE 2: Xử lý OCR với cờ chọn ngôn ngữ
47
  # -----------------------------------------------------
48
  @app.post("/predict")
49
+ async def predict_image(
50
+ file: UploadFile = File(...),
51
+ lang: str = Form("vi") # Nhận biến ngôn ngữ từ Frontend (Mặc định là 'vi')
52
+ ):
53
  if not file.content_type.startswith('image/'):
54
  raise HTTPException(status_code=400, detail="Vui lòng tải tệp hình ảnh.")
55
 
 
59
  image = Image.open(io.BytesIO(contents)).convert('RGB')
60
  img_array = np.array(image)
61
 
62
+ # ĐIỀU HƯỚNG HÌNH DỰA VÀO LỰA CHỌN CỦA NGƯỜI DÙNG
63
+ if lang == "zh":
64
+ # Chạy cỗ máy Tiếng Trung
65
+ results = reader_zh.readtext(img_array, detail=0)
66
+ else:
67
+ # Chạy cỗ máy Tiếng Việt
68
+ results = reader_vi.readtext(img_array, detail=0)
69
 
70
+ # Ghép các dòng chữ lại
71
  extracted_text = "\n".join(results)
72
 
73
  return {"text": extracted_text}