def test_decode_img_as_tensor(mock_image_stream): img = decode_img_as_tensor(mock_image_stream) assert isinstance(img, tf.Tensor) assert img.dtype == tf.float32 assert img.shape == (900, 1200, 3) img = decode_img_as_tensor(mock_image_stream, dtype=tf.float16) assert img.dtype == tf.float16 img = decode_img_as_tensor(mock_image_stream, dtype=tf.uint8) assert img.dtype == tf.uint8
async def perform_ocr(file: UploadFile = File(...)): """Runs docTR OCR model to analyze the input image""" img = decode_img_as_tensor(file.file.read()) out = predictor([img]) return [ OCROut(box=(*word.geometry[0], *word.geometry[1]), value=word.value) for word in out.pages[0].blocks[0].lines[0].words ]
async def text_recognition(file: UploadFile = File(...)): """Runs docTR text recognition model to analyze the input image""" img = decode_img_as_tensor(file.file.read()) out = reco_predictor([img]) return RecognitionOut(value=out[0][0])
async def text_detection(file: UploadFile = File(...)): """Runs docTR text detection model to analyze the input image""" img = decode_img_as_tensor(file.file.read()) boxes = det_predictor([img])[0] return [DetectionOut(box=box.tolist()) for box in boxes[:, :-1]]