from fastapi import FastAPI, File, UploadFile from fastapi.responses import HTMLResponse from io import BytesIO import pytesseract from PIL import Image import cv2 import numpy as np # Initialize FastAPI app app = FastAPI() # 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") # Not needed if you're not saving the images # 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!

Drag & Drop the Images
Or Click to Browse
""" 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) return {"extracted_text": extracted_text}