Skip to content

Commit bacd7d3

Browse files
genquan9telpirion
authored andcommitted
bug: missing preprocessing examples for automl edge models (#131)
* Add preprocessing examples for automl edge models * Add preprocessing examples for automl edge models * Update the comments to be easier understanding * fix typos * fix typos Co-authored-by: Eric Schmidt <[email protected]>
1 parent ec71b23 commit bacd7d3

File tree

1 file changed

+37
-4
lines changed

1 file changed

+37
-4
lines changed

automl/vision_edge/edge_container_predict/automl_vision_edge_container_predict.py

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,48 @@
1818
1919
Examples:
2020
python automl_vision_edge_container_predict.py \
21-
--image_file_path=./test.jpg --image_key=1 --port_number=8051
21+
--image_file_path=./test.jpg --image_key=1 --port_number=8501
2222
2323
"""
2424

2525
import argparse
2626
# [START automl_vision_edge_container_predict]
2727
import base64
28+
import cv2
2829
import io
2930
import json
3031

3132
import requests
3233

34+
def preprocess_image(image_file_path, max_width, max_height):
35+
"""Preprocesses input images for AutoML Vision Edge models.
36+
37+
Args:
38+
image_file_path: Path to a local image for the prediction request.
39+
max_width: The max width for preprocessed images. The max width is 640
40+
(1024) for AutoML Vision Image Classfication (Object Detection)
41+
models.
42+
max_height: The max width for preprocessed images. The max height is
43+
480 (1024) for AutoML Vision Image Classfication (Object
44+
Detetion) models.
45+
Returns:
46+
The preprocessed encoded image bytes.
47+
"""
48+
# cv2 is used to read, resize and encode images.
49+
encode_param = [int(cv2.IMWRITE_JPEG_QUALITY), 85]
50+
im = cv2.imread(image_file_path)
51+
[height, width, _] = im.shape
52+
if height > max_height or width > max_width:
53+
ratio = max(height / float(max_width), width / float(max_height))
54+
new_height = int(height / ratio + 0.5)
55+
new_width = int(width / ratio + 0.5)
56+
resized_im = cv2.resize(
57+
im, (new_width, new_height), interpolation=cv2.INTER_AREA)
58+
_, processed_image = cv2.imencode('.jpg', resized_im, encode_param)
59+
else:
60+
_, processed_image = cv2.imencode('.jpg', im, encode_param)
61+
return base64.b64encode(processed_image).decode('utf-8')
62+
3363

3464
def container_predict(image_file_path, image_key, port_number=8501):
3565
"""Sends a prediction request to TFServing docker container REST API.
@@ -41,9 +71,12 @@ def container_predict(image_file_path, image_key, port_number=8501):
4171
Returns:
4272
The response of the prediction request.
4373
"""
44-
45-
with io.open(image_file_path, 'rb') as image_file:
46-
encoded_image = base64.b64encode(image_file.read()).decode('utf-8')
74+
# AutoML Vision Edge models will preprocess the input images.
75+
# The max width and height for AutoML Vision Image Classification and
76+
# Object Detection models are 640*480 and 1024*1024 separately. The
77+
# example here is for Image Classification models.
78+
encoded_image = preprocess_image(
79+
image_file_path=image_file_path, max_width=640, max_height=480)
4780

4881
# The example here only shows prediction with one image. You can extend it
4982
# to predict with a batch of images indicated by different keys, which can

0 commit comments

Comments
 (0)