Skip to content

Commit 4911f68

Browse files
authored
Update api to support base64 files (#246)
* support base64 input files for the ocr api * add tests * reformat
1 parent 89ea399 commit 4911f68

File tree

2 files changed

+84
-4
lines changed

2 files changed

+84
-4
lines changed

OCR/ocr/api.py

+21-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import base64
2+
13
import uvicorn
24
import json
35
import cv2 as cv
@@ -33,8 +35,8 @@ async def health_check():
3335
return {"status": "UP"}
3436

3537

36-
@app.post("/image_to_text/")
37-
async def image_to_text(source_image: UploadFile, segmentation_template: UploadFile, labels: str = Form()):
38+
@app.post("/image_file_to_text/")
39+
async def image_file_to_text(source_image: UploadFile, segmentation_template: UploadFile, labels: str = Form()):
3840
source_image_np = np.frombuffer(await source_image.read(), np.uint8)
3941
source_image_img = cv.imdecode(source_image_np, cv.IMREAD_COLOR)
4042

@@ -48,6 +50,23 @@ async def image_to_text(source_image: UploadFile, segmentation_template: UploadF
4850
return results
4951

5052

53+
@app.post("/image_to_text/")
54+
async def image_to_text(source_image: str = Form(), segmentation_template: str = Form(), labels: str = Form()):
55+
source_image_stripped = source_image.replace("data:image/png;base64,", "", 1)
56+
source_image_np = np.frombuffer(base64.b64decode(source_image_stripped), np.uint8)
57+
source_image_img = cv.imdecode(source_image_np, cv.IMREAD_COLOR)
58+
59+
segmentation_template_stripped = segmentation_template.replace("data:image/png;base64,", "", 1)
60+
segmentation_template_np = np.frombuffer(base64.b64decode(segmentation_template_stripped), np.uint8)
61+
segmentation_template_img = cv.imdecode(segmentation_template_np, cv.IMREAD_COLOR)
62+
63+
loaded_json = json.loads(labels)
64+
segments = segmenter.segment(source_image_img, segmentation_template_img, loaded_json)
65+
results = ocr.image_to_text(segments)
66+
67+
return results
68+
69+
5170
def start():
5271
"""Launched with `poetry run start` at root level"""
5372
uvicorn.run(app, host="0.0.0.0", port=8000, reload=False)

OCR/tests/api_test.py

+63-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import base64
12
import os
23
import json
34

@@ -20,7 +21,7 @@ def test_health_check(self):
2021
assert response.status_code == 200
2122
assert response.json() == {"status": "UP"}
2223

23-
def test_ocr(self):
24+
def test_image_file_to_text(self):
2425
# load the files
2526
with (
2627
open(segmentation_template_path, "rb") as segmentation_template_file,
@@ -35,7 +36,67 @@ def test_ocr(self):
3536
]
3637

3738
# call ocr api
38-
response = client.post(url="/image_to_text", files=files_to_send, data={"labels": json.dumps(label_data)})
39+
response = client.post(
40+
url="/image_file_to_text", files=files_to_send, data={"labels": json.dumps(label_data)}
41+
)
42+
43+
assert response.status_code == 200
44+
45+
# assert output
46+
response_json = response.json()
47+
assert response_json["nbs_patient_id"][0] == "SIENNA HAMPTON"
48+
assert response_json["nbs_cas_id"][0] == "123555"
49+
50+
def test_image_to_text(self):
51+
# load the files
52+
with (
53+
open(segmentation_template_path, "rb") as segmentation_template_file,
54+
open(source_image_path, "rb") as source_image_file,
55+
open(labels_path, "rb") as labels,
56+
):
57+
label_data = json.load(labels)
58+
59+
source_image_base64 = base64.b64encode(source_image_file.read()).decode("ascii")
60+
segmentation_template_base64 = base64.b64encode(segmentation_template_file.read()).decode("ascii")
61+
62+
response = client.post(
63+
url="/image_to_text",
64+
data={
65+
"labels": json.dumps(label_data),
66+
"source_image": str(source_image_base64),
67+
"segmentation_template": str(segmentation_template_base64),
68+
},
69+
headers={"Content-Type": "application/x-www-form-urlencoded"},
70+
)
71+
72+
assert response.status_code == 200
73+
74+
# assert output
75+
response_json = response.json()
76+
assert response_json["nbs_patient_id"][0] == "SIENNA HAMPTON"
77+
assert response_json["nbs_cas_id"][0] == "123555"
78+
79+
def test_image_to_text_with_padding(self):
80+
# load the files
81+
with (
82+
open(segmentation_template_path, "rb") as segmentation_template_file,
83+
open(source_image_path, "rb") as source_image_file,
84+
open(labels_path, "rb") as labels,
85+
):
86+
label_data = json.load(labels)
87+
88+
source_image_base64 = base64.b64encode(source_image_file.read()).decode("ascii")
89+
segmentation_template_base64 = base64.b64encode(segmentation_template_file.read()).decode("ascii")
90+
91+
response = client.post(
92+
url="/image_to_text",
93+
data={
94+
"labels": json.dumps(label_data),
95+
"source_image": f"data:image/png;base64,{str(source_image_base64)}",
96+
"segmentation_template": f"data:image/png;base64,{str(segmentation_template_base64)}",
97+
},
98+
headers={"Content-Type": "application/x-www-form-urlencoded"},
99+
)
39100

40101
assert response.status_code == 200
41102

0 commit comments

Comments
 (0)