from fastapi import FastAPI, File, UploadFile
from fastapi.responses import HTMLResponse
from io import BytesIO
import pytesseract
from PIL import Image
import subprocess
# Initialize FastAPI app
app = FastAPI()
def install_tesseract():
try:
subprocess.run(['apt-get', 'update'], check=True)
subprocess.run(['apt-get', 'install', '-y', 'tesseract-ocr'], check=True)
except subprocess.CalledProcessError as e:
print(f"Error installing Tesseract: {e}")
install_tesseract()
# Home route
@app.get("/", response_class=HTMLResponse)
async def home():
html_content = """
Image to Text Converter
Image to Text Converter
Quickly extract text from your uploaded images!
"""
return HTMLResponse(content=html_content)
# Upload image route (image is processed directly in memory)
@app.post("/upload_image/")
async def upload_image(image: UploadFile = File(...)):
# Read the image file directly into memory
image_bytes = await image.read()
image_stream = BytesIO(image_bytes)
# Open the image with PIL (Pillow)
img = Image.open(image_stream)
# Process the image and extract text
extracted_text = pytesseract.image_to_string(img)
# Convert image to base64 for displaying in HTML
import base64
buffered = BytesIO()
img.save(buffered, format="PNG")
img_str = base64.b64encode(buffered.getvalue()).decode("utf-8")
# HTML response with image and extracted text
html_response = f"""
Extracted Text
Extracted Text
Here is the text extracted from the image you uploaded:
{extracted_text}
"""
return HTMLResponse(content=html_response)