Spaces:
Sleeping
Sleeping
File size: 1,161 Bytes
9c5e6eb |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
from fastapi import FastAPI, File, UploadFile
from PIL import Image
import pytesseract
import io
import fitz
app = FastAPI()
@app.get("/test")
async def test_api():
return {"message": "API is working ..."}
@app.post("/ocr_image")
async def perform_ocr_image(image: UploadFile = File(...)):
image_data = await image.read()
image = Image.open(io.BytesIO(image_data))
text = pytesseract.image_to_string(image, lang='tha+eng')
return {"text": text}
@app.post("/ocr_pdf")
async def perform_ocr_pdf(image: UploadFile = File(...)):
image_data = await image.read()
doc = fitz.open(stream=image_data, filetype="pdf")
page = doc[0] # Access the desired page (zero-indexed)
# Generate a high-quality image of the page
zoom = 2 # Adjust for better OCR resolution if needed
mat = fitz.Matrix(zoom, zoom) # Zoom matrix
pix = page.get_pixmap(matrix=mat)
img = Image.frombytes("RGB", [pix.width, pix.height], pix.samples)
# Analyze text orientation with Pytesseract
#text_orientation = pytesseract.image_to_osd(img)
text = pytesseract.image_to_string(img, lang='tha+eng')
return {"text": text} |