|
|
from fastapi import FastAPI, UploadFile, File, HTTPException |
|
|
from fastapi.responses import JSONResponse |
|
|
import numpy as np |
|
|
from PIL import Image |
|
|
import io |
|
|
import uvicorn |
|
|
import os |
|
|
|
|
|
|
|
|
os.environ['EASYOCR_MODULE_PATH'] = '/app/model' |
|
|
os.environ['HOME'] = '/app' |
|
|
|
|
|
import easyocr |
|
|
|
|
|
app = FastAPI() |
|
|
|
|
|
|
|
|
reader = easyocr.Reader(['es', 'en']) |
|
|
|
|
|
@app.post("/ocr") |
|
|
async def extract_text(file: UploadFile = File(...)): |
|
|
""" |
|
|
Extrae texto de una imagen usando OCR |
|
|
""" |
|
|
try: |
|
|
|
|
|
if not file.content_type.startswith('image/'): |
|
|
raise HTTPException(status_code=400, detail="El archivo debe ser una imagen") |
|
|
|
|
|
|
|
|
contents = await file.read() |
|
|
|
|
|
|
|
|
image = Image.open(io.BytesIO(contents)) |
|
|
|
|
|
|
|
|
if image.mode != 'RGB': |
|
|
image = image.convert('RGB') |
|
|
|
|
|
|
|
|
image_array = np.array(image) |
|
|
|
|
|
|
|
|
results = reader.readtext(image_array) |
|
|
|
|
|
|
|
|
extracted_text = [] |
|
|
for (bbox, text, confidence) in results: |
|
|
extracted_text.append({ |
|
|
"text": text, |
|
|
"confidence": float(confidence), |
|
|
"bbox": bbox |
|
|
}) |
|
|
|
|
|
|
|
|
full_text = " ".join([item["text"] for item in extracted_text]) |
|
|
|
|
|
return JSONResponse(content={ |
|
|
"success": True, |
|
|
"full_text": full_text, |
|
|
"detailed_results": extracted_text |
|
|
}) |
|
|
|
|
|
except Exception as e: |
|
|
raise HTTPException(status_code=500, detail=f"Error procesando la imagen: {str(e)}") |
|
|
|
|
|
@app.get("/") |
|
|
async def root(): |
|
|
return {"message": "OCR API funcionando", "endpoint": "/ocr"} |
|
|
|
|
|
if __name__ == "__main__": |
|
|
port = int(os.environ.get("PORT", 7860)) |
|
|
uvicorn.run(app, host="0.0.0.0", port=port) |
|
|
|