Spaces:
Sleeping
Sleeping
| from fastapi import FastAPI, File, UploadFile | |
| from PIL import Image | |
| import pytesseract | |
| import io | |
| import fitz | |
| app = FastAPI() | |
| async def test_api(): | |
| return {"message": "API is working ..."} | |
| 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} | |
| 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} |