Spaces:
Runtime error
Runtime error
File size: 1,033 Bytes
8400f04 be58bdb 8400f04 |
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 40 41 42 43 44 45 46 47 |
from PIL import Image
import pytesseract
from fastapi import FastAPI, UploadFile, File
from fastapi.middleware.cors import CORSMiddleware
from io import BytesIO
app = FastAPI()
origins = ["*"]
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
@app.get('/')
def welcome():
return {
'success': True,
'message': 'server of "image text extractor" is up and running successfully.'
}
@app.post('/extract-text-from-image')
async def extract_text_from_img(imageUploadedByUser: UploadFile = File(...)):
img = await imageUploadedByUser.read()
img_bytes_io = Image.open(BytesIO(img))
gray_scale_img = img_bytes_io.convert('L')
text = pytesseract.image_to_string(gray_scale_img)
text_cleaned = ' '.join(text.split())
return {
'success': True,
'message': 'Text has been successfully extracted from the uploaded image',
'extracted_text': text_cleaned
}
|