Genzo1010 commited on
Commit
2caaf23
·
verified ·
1 Parent(s): 405d877

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -0
app.py ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from fastapi import FastAPI, File, UploadFile
3
+ from paddleocr import PaddleOCR
4
+ from PIL import Image
5
+ import numpy as np
6
+ import io
7
+ import uvicorn
8
+
9
+ app = FastAPI()
10
+
11
+ # Initialize PaddleOCR
12
+ ocr = PaddleOCR(lang='en', use_angle_cls=True)
13
+
14
+ @app.post("/extract-text/")
15
+ async def extract_text(file: UploadFile = File(...)):
16
+ image = Image.open(io.BytesIO(await file.read()))
17
+
18
+ # Convert the image to a NumPy array
19
+ img_array = np.array(image)
20
+
21
+ # Handle different image channels
22
+ if img_array.ndim == 2: # Grayscale image
23
+ img_array = np.stack((img_array,)*3, axis=-1)
24
+ elif img_array.shape[-1] == 4: # RGBA image
25
+ img_array = img_array[..., :3]
26
+
27
+ # Perform OCR
28
+ result = ocr.ocr(img_array)
29
+ extracted_text = ' '.join([line[1][0] for line in result[0]])
30
+ return {"text": extracted_text}
31
+
32
+ if __name__ == "__main__":
33
+ uvicorn.run(app, host="0.0.0.0", port=int(os.environ.get("PORT", 8000)))