Skip to content

Commit 78d2109

Browse files
authored
add ocr endpoints
1 parent af02104 commit 78d2109

1 file changed

Lines changed: 83 additions & 3 deletions

File tree

src/main/start.py

Lines changed: 83 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,18 @@
66
from starlette.staticfiles import StaticFiles
77
from starlette.middleware.cors import CORSMiddleware
88
from deep_learning_service import DeepLearningService
9-
from fastapi import FastAPI, Form, File, UploadFile, Header
9+
from fastapi import FastAPI, Form, File, UploadFile, Header, HTTPException
1010
from inference.exceptions import ModelNotFound, InvalidModelConfiguration, ApplicationError, ModelNotLoaded, \
11-
InferenceEngineNotFound, InvalidInputData
12-
11+
InferenceEngineNotFound, InvalidInputData
12+
from ocr import ocr_service, one_shot_ocr_service
13+
from datetime import datetime
14+
import pytz
15+
from PIL import Image
1316

1417
sys.path.append('./inference')
1518

19+
tz = pytz.timezone("Europe/Berlin")
20+
1621
dl_service = DeepLearningService()
1722
error_logging = Error()
1823
app = FastAPI(version='1.0', title='BMW InnovationLab tensorflow cpu inference Automation',
@@ -185,3 +190,78 @@ async def list_model_config(model_name: str):
185190
"""
186191
config = dl_service.get_config(model_name)
187192
return ApiResponse(data=config)
193+
194+
195+
@app.post('/models/{model_name}/one_shot_ocr')
196+
async def one_shot_ocr(
197+
model_name: str,
198+
image: UploadFile = File(
199+
..., description="Image to perform optical character recognition based on layout inference:")
200+
):
201+
"""
202+
Takes an image and returns extracted text details.
203+
204+
In first place a detection model will be used for cropping interesting areas in the uploaded image. These areas will then be passed to the OCR-Service for text extraction.
205+
206+
:param model_name: Model name or model hash for layout detection
207+
208+
:param image: Image file
209+
210+
:return: Text fields with the detected files inside
211+
212+
"""
213+
output = None
214+
# call detection on image with choosen model
215+
try:
216+
output = await run_model(model_name, image)
217+
except:
218+
raise HTTPException(status_code=404, detail='Invalid Model')
219+
220+
# run ocr_service
221+
response = None
222+
try:
223+
image = Image.open(image.file).convert('RGB')
224+
response = one_shot_ocr_service(image, output.data)
225+
except:
226+
raise HTTPException(
227+
status_code=500, detail='Unexpected Error during Inference (Determination of Texts)')
228+
229+
if not response:
230+
raise HTTPException(
231+
status_code=400, detail='Inference (Determination of Texts) is not Possible with the Specified Model')
232+
233+
return response
234+
235+
236+
@app.post('/models/{model_name}/ocr')
237+
async def optical_character_recognition(
238+
model_name: str,
239+
image: UploadFile = File(
240+
..., description="Image to perform optical character recognition based on layout inference:"),
241+
):
242+
"""
243+
Takes an image and returns extracted text informations.
244+
245+
The image is passed to the OCR-Service for text extraction
246+
247+
:param model: Model name or model hash
248+
249+
:param image: Image file
250+
251+
:return: Text fields with the detected files inside
252+
253+
"""
254+
# run ocr_service
255+
response = None
256+
try:
257+
image = Image.open(image.file).convert('RGB')
258+
response = ocr_service(image)
259+
except:
260+
raise HTTPException(
261+
status_code=500, detail='Unexpected Error during Inference (Determination of Texts)')
262+
263+
if not response:
264+
raise HTTPException(
265+
status_code=400, detail='Inference (Determination of Texts) is not Possible with the Specified Model')
266+
267+
return response

0 commit comments

Comments
 (0)