Spaces:
Sleeping
Sleeping
from fastapi import FastAPI, File, UploadFile, Form | |
from fastapi.responses import HTMLResponse | |
from fastapi.staticfiles import StaticFiles | |
import cv2 | |
import pytesseract | |
import os | |
# Initialize FastAPI app | |
app = FastAPI() | |
# Define upload folder | |
UPLOAD_FOLDER = "static/uploads/" | |
if not os.path.exists(UPLOAD_FOLDER): | |
os.makedirs(UPLOAD_FOLDER) | |
# Inform pytesseract where Tesseract is installed | |
pytesseract.pytesseract.tesseract_cmd = "/usr/local/bin/tesseract" | |
# Serve static files (images, etc.) | |
app.mount("/static", StaticFiles(directory="static"), name="static") | |
# Home route | |
async def home(): | |
html_content = """ | |
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<title>Image to Text Converter</title> | |
<style> | |
body { font-family: Arial, sans-serif; background-color: #000000; color: #333; margin: 0; padding: 0; display: flex; justify-content: center; align-items: center; height: 100vh; } | |
.container { text-align: center; background: #fff; padding: 30px; border-radius: 10px; box-shadow: 0px 4px 8px rgba(0, 0, 0, 0.1); width: 90%; max-width: 600px; } | |
h1 { font-size: 24px; color: #4CAF50; } | |
p { color: #666; } | |
.upload-box { margin: 20px auto; padding: 30px; border: 2px dashed #ccc; border-radius: 10px; background-color: #f9f9f9; cursor: pointer; position: relative; } | |
.upload-box:hover { background-color: #f4f4f4; } | |
.upload-box span { color: #888; font-size: 14px; display: block; } | |
.upload-box input[type="file"] { position: absolute; width: 100%; height: 100%; top: 0; left: 0; opacity: 0; cursor: pointer; } | |
.process-button { background-color: #4CAF50; color: white; padding: 10px 20px; border: none; border-radius: 5px; cursor: pointer; font-size: 16px; } | |
.process-button:hover { background-color: #45a049; } | |
.result-box { margin-top: 20px; text-align: left; } | |
.result-box img { max-width: 100%; border-radius: 5px; margin-top: 10px; } | |
.actions { margin-top: 20px; } | |
.actions button { margin: 5px; background-color: #4CAF50; color: white; padding: 8px 16px; border: none; border-radius: 5px; cursor: pointer; font-size: 14px; } | |
.actions button:hover { background-color: #45a049; } | |
pre { background-color: #f4f4f4; padding: 10px; border-radius: 5px; white-space: pre-wrap; word-wrap: break-word; } | |
.error { color: red; font-weight: bold; margin-bottom: 20px; } | |
</style> | |
</head> | |
<body> | |
<div class="container"> | |
<h1>Image to Text Converter</h1> | |
<p>Quickly extract text from your uploaded images!</p> | |
<form action="/upload_image/" method="POST" enctype="multipart/form-data"> | |
<div class="upload-box"> | |
<span>Drag & Drop the Images<br>Or Click to Browse</span> | |
<input type="file" name="image" accept="image/*" id="image-file"> | |
</div> | |
<button class="process-button" type="submit">Upload Image</button> | |
</form> | |
</div> | |
</body> | |
</html> | |
""" | |
return HTMLResponse(content=html_content) | |
# Upload image route | |
async def upload_image(image: UploadFile = File(...)): | |
file_location = f"{UPLOAD_FOLDER}/{image.filename}" | |
with open(file_location, "wb") as file: | |
file.write(await image.read()) | |
return {"image_url": f"/static/uploads/{image.filename}"} | |
# Process image and extract text | |
async def process_image(image_path: str = Form(...)): | |
img = cv2.imread(image_path) | |
if img is None: | |
return {"error": "Unable to read the uploaded image. Please upload a valid image."} | |
# Extract text from the image | |
extracted_text = pytesseract.image_to_string(img) | |
return {"extracted_text": extracted_text} | |