This repository was archived by the owner on Mar 31, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathclient.py
204 lines (161 loc) · 7.78 KB
/
client.py
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
"""
Client side code to perform a single API call to a tensorflow model up and running.
"""
import argparse
import json
import numpy as np
import requests
from object_detection.utils import visualization_utils as vis_util
from object_detection.utils import plot_util
from object_detection.utils import label_map_util
import object_detection.utils.ops as utils_ops
from PIL import Image
def format_mask(detection_masks, detection_boxes, N, image_size):
"""
Format the m*m detection soft masks as full size binary masks.
Args:
detection_masks (np.array): of size N * m * m
detection_boxes (np.array): of size N * 4 with the normalized bow coordinates.
Coordinates are written as [y_min, x_min, y_max, x_max]
N (int): number of detections in the image
image_size (tuple(int))
Returns:
detection_masks (np.array): of size N * H * W where H and W are the image Height and Width.
"""
(height, width, _) = image_size
output_masks = np.zeros((N, image_size[0], image_size[1]))
# Process the masks related to the N objects detected in the image
for i in range(N):
normalized_mask = detection_masks[i].astype(np.float32)
normalized_mask = Image.fromarray(normalized_mask, 'F')
# Boxes are expressed with 4 scalars - normalized coordinates [y_min, x_min, y_max, x_max]
[y_min, x_min, y_max, x_max] = detection_boxes[i]
# Compute absolute boundary of box
box_size = (int((x_max - x_min) * width), int((y_max - y_min) * height))
# Resize the mask to the box size using LANCZOS appoximation
resized_mask = normalized_mask.resize(box_size, Image.LANCZOS)
# Convert back to array
resized_mask = np.array(resized_mask).astype(np.float32)
# Binarize the image by using a fixed threshold
binary_mask_box = np.zeros(resized_mask.shape)
thresh = 0.5
(h, w) = resized_mask.shape
for k in range(h):
for j in range(w):
if resized_mask[k][j] >= thresh:
binary_mask_box[k][j] = 1
binary_mask_box = binary_mask_box.astype(np.uint8)
# Replace the mask in the context of the original image size
binary_mask = np.zeros((height, width))
x_min_at_scale = int(x_min * width)
y_min_at_scale = int(y_min * height)
d_x = int((x_max - x_min) * width)
d_y = int((y_max - y_min) * height)
for x in range(d_x):
for y in range(d_y):
binary_mask[y_min_at_scale + y][x_min_at_scale + x] = binary_mask_box[y][x]
# Update the masks array
output_masks[i][:][:] = binary_mask
# Cast mask as integer
output_masks = output_masks.astype(np.uint8)
return output_masks
def load_image_into_numpy_array(image):
(im_width, im_height) = image.size
return np.array(image.getdata()).reshape(
(im_height, im_width, 3)).astype(np.uint8)
def pre_process(image_path):
"""
Pre-process the input image to return a json to pass to the tf model
Args:
image_path (str): Path to the jpeg image
Returns:
formatted_json_input (str)
"""
image = Image.open(image_path).convert("RGB")
image_np = plot_util.load_image_into_numpy_array(image)
# Expand dims to create bach of size 1
image_tensor = np.expand_dims(image_np, 0)
formatted_json_input = json.dumps({"signature_name": "serving_default", "instances": image_tensor.tolist()})
return formatted_json_input
def post_process(server_response, image_size):
"""
Post-process the server response
Args:
server_response (requests.Response)
image_size (tuple(int))
Returns:
post_processed_data (dict)
"""
response = json.loads(server_response.text)
output_dict = response['predictions'][0]
# all outputs are float32 numpy arrays, so convert types as appropriate
output_dict['num_detections'] = int(output_dict['num_detections'])
output_dict['detection_classes'] = np.array([int(class_id) for class_id in output_dict['detection_classes']])
output_dict['detection_boxes'] = np.array(output_dict['detection_boxes'])
output_dict['detection_scores'] = np.array(output_dict['detection_scores'])
# Process detection mask
if 'detection_masks' in output_dict:
# Determine a threshold above wihc we consider the pixel shall belong to the mask
# thresh = 0.5
output_dict['detection_masks'] = np.array(output_dict['detection_masks'])
output_dict['detection_masks'] = format_mask(output_dict['detection_masks'], output_dict['detection_boxes'], output_dict['num_detections'], image_size)
return output_dict
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Performs call to the tensorflow-serving REST API.')
parser.add_argument('--server_url', dest='server_url', type=str, required=True,
help='URL of the tensorflow-serving accepting API call. '
'e.g. http://localhost:8501/v1/models/omr_500:predict')
parser.add_argument('--image_path', dest='image_path', type=str,
help='Path to the jpeg image')
parser.add_argument('--output_json', dest='output_json', type=str, default='tf_output.json',
help='Path to the output json file resulting from the API call')
parser.add_argument('--save_output_image', dest='save_output_image', type=bool, default=False,
help='Whether the output_image should be built from the predictions')
parser.add_argument('--label_map', dest='label_map', type=str, default="mapping_all_classes.txt",
help='Path to the label map, which is json-file that maps each category name '
'to a unique number.')
args = parser.parse_args()
# Map args to var
server_url = args.server_url
image_path = args.image_path
output_image = args.output_json
save_output_image = args.save_output_image
path_to_labels = args.label_map
# Build input data
print(f'\n\nPre-processing input file {image_path}...\n')
formatted_json_input = pre_process(image_path)
print('Pre-processing done! \n')
# Call tensorflow server
headers = {"content-type": "application/json"}
print(f'\n\nMaking request to {server_url}...\n')
server_response = requests.post(server_url, data=formatted_json_input, headers=headers)
print(f'Request returned\n')
# Post process output
print(f'\n\nPost-processing server response...\n')
image = Image.open(image_path).convert("RGB")
image_np = load_image_into_numpy_array(image)
output_dict = post_process(server_response, image_np.shape)
print(f'Post-processing done!\n')
# Save output on disk
print(f'\n\nSaving output to {output_image}\n\n')
with open(output_image, 'w+') as outfile:
json.dump(json.loads(server_response.text), outfile)
print(f'Output saved!\n')
if save_output_image:
# Save output on disk
category_index = label_map_util.create_category_index_from_labelmap(path_to_labels, use_display_name=True)
# Visualization of the results of a detection.
vis_util.visualize_boxes_and_labels_on_image_array(
image_np,
output_dict['detection_boxes'],
output_dict['detection_classes'],
output_dict['detection_scores'],
category_index,
instance_masks=output_dict.get('detection_masks'),
use_normalized_coordinates=True,
line_thickness=8,
)
output_with_no_extension = output_image.split('.', 1)[0]
output_image = ''.join([output_with_no_extension, '.jpeg'])
Image.fromarray(image_np).save(output_image)
print('\n\nImage saved\n\n')