pmrony commited on
Commit
b676ee9
·
verified ·
1 Parent(s): ed5fece

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +40 -0
  2. requirements.txt +7 -0
app.py ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, File, UploadFile
2
+ from PIL import Image
3
+ import easyocr
4
+ import io
5
+ import logging
6
+
7
+ # Configure logging
8
+ logging.basicConfig(level=logging.INFO)
9
+ logger = logging.getLogger(__name__)
10
+
11
+ app = FastAPI()
12
+
13
+ # Load the EasyOCR model once when the application starts
14
+ # This makes subsequent requests much faster
15
+ logger.info("Loading EasyOCR model...")
16
+ reader = easyocr.Reader(['en'], gpu=False)
17
+ logger.info("EasyOCR model loaded successfully.")
18
+
19
+ @app.get("/")
20
+ def read_root():
21
+ return {"status": "OCR API is running"}
22
+
23
+ @app.post("/ocr-from-image/")
24
+ async def ocr_from_image(file: UploadFile = File(...)):
25
+ try:
26
+ # Read the image file from the request
27
+ image_bytes = await file.read()
28
+
29
+ # Use EasyOCR to read text from the image bytes
30
+ logger.info("Processing image with EasyOCR...")
31
+ result = reader.readtext(image_bytes, detail=0, paragraph=True)
32
+
33
+ # Combine the detected text lines into a single string
34
+ extracted_text = " ".join(result)
35
+ logger.info(f"Extracted Text: {extracted_text}")
36
+
37
+ return {"extracted_text": extracted_text}
38
+ except Exception as e:
39
+ logger.error(f"An error occurred: {e}")
40
+ return {"error": str(e)}
requirements.txt ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ fastapi
2
+ uvicorn
3
+ python-multipart
4
+ Pillow
5
+ easyocr
6
+ torch --extra-index-url https://download.pytorch.org/whl/cpu
7
+ torchvision --extra-index-url https://download.pytorch.org/whl/cpu