1
+ import base64
1
2
import os
2
3
import json
3
4
@@ -20,7 +21,7 @@ def test_health_check(self):
20
21
assert response .status_code == 200
21
22
assert response .json () == {"status" : "UP" }
22
23
23
- def test_ocr (self ):
24
+ def test_image_file_to_text (self ):
24
25
# load the files
25
26
with (
26
27
open (segmentation_template_path , "rb" ) as segmentation_template_file ,
@@ -35,7 +36,67 @@ def test_ocr(self):
35
36
]
36
37
37
38
# 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
+ )
39
100
40
101
assert response .status_code == 200
41
102
0 commit comments