Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
@@ -1,25 +1,19 @@
|
|
1 |
-
from fastapi import FastAPI, File, UploadFile
|
2 |
from fastapi.responses import HTMLResponse
|
3 |
-
from
|
4 |
-
import cv2
|
5 |
import pytesseract
|
6 |
-
import
|
|
|
|
|
7 |
|
8 |
# Initialize FastAPI app
|
9 |
app = FastAPI()
|
10 |
|
11 |
-
# Define the upload folder path
|
12 |
-
UPLOAD_FOLDER = "static/uploads/"
|
13 |
-
|
14 |
-
# Ensure the 'uploads' folder exists and has correct permissions
|
15 |
-
if not os.path.exists(UPLOAD_FOLDER):
|
16 |
-
os.makedirs(UPLOAD_FOLDER, mode=0o777) # Create folder with read, write, execute permissions for everyone
|
17 |
-
|
18 |
# Inform pytesseract where Tesseract is installed
|
19 |
pytesseract.pytesseract.tesseract_cmd = "/usr/local/bin/tesseract"
|
20 |
|
21 |
# Serve static files (images, etc.)
|
22 |
-
app.mount("/static", StaticFiles(directory="static"), name="static")
|
23 |
|
24 |
# Home route
|
25 |
@app.get("/", response_class=HTMLResponse)
|
@@ -68,24 +62,17 @@ async def home():
|
|
68 |
"""
|
69 |
return HTMLResponse(content=html_content)
|
70 |
|
71 |
-
# Upload image route
|
72 |
@app.post("/upload_image/")
|
73 |
async def upload_image(image: UploadFile = File(...)):
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
return {"image_url": f"/static/uploads/{image.filename}"}
|
79 |
-
|
80 |
-
# Process image and extract text
|
81 |
-
@app.post("/process_image/")
|
82 |
-
async def process_image(image_path: str = Form(...)):
|
83 |
-
img = cv2.imread(image_path)
|
84 |
|
85 |
-
|
86 |
-
|
87 |
|
88 |
-
#
|
89 |
extracted_text = pytesseract.image_to_string(img)
|
90 |
|
91 |
return {"extracted_text": extracted_text}
|
|
|
1 |
+
from fastapi import FastAPI, File, UploadFile
|
2 |
from fastapi.responses import HTMLResponse
|
3 |
+
from io import BytesIO
|
|
|
4 |
import pytesseract
|
5 |
+
from PIL import Image
|
6 |
+
import cv2
|
7 |
+
import numpy as np
|
8 |
|
9 |
# Initialize FastAPI app
|
10 |
app = FastAPI()
|
11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
# Inform pytesseract where Tesseract is installed
|
13 |
pytesseract.pytesseract.tesseract_cmd = "/usr/local/bin/tesseract"
|
14 |
|
15 |
# Serve static files (images, etc.)
|
16 |
+
# app.mount("/static", StaticFiles(directory="static"), name="static") # Not needed if you're not saving the images
|
17 |
|
18 |
# Home route
|
19 |
@app.get("/", response_class=HTMLResponse)
|
|
|
62 |
"""
|
63 |
return HTMLResponse(content=html_content)
|
64 |
|
65 |
+
# Upload image route (image is processed directly in memory)
|
66 |
@app.post("/upload_image/")
|
67 |
async def upload_image(image: UploadFile = File(...)):
|
68 |
+
# Read the image file directly into memory
|
69 |
+
image_bytes = await image.read()
|
70 |
+
image_stream = BytesIO(image_bytes)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
71 |
|
72 |
+
# Open the image with PIL (Pillow)
|
73 |
+
img = Image.open(image_stream)
|
74 |
|
75 |
+
# Process the image and extract text
|
76 |
extracted_text = pytesseract.image_to_string(img)
|
77 |
|
78 |
return {"extracted_text": extracted_text}
|