1
+ import requests
2
+ import os
3
+ from PIL import Image
4
+ import numpy as np
5
+ import time
6
+ import tensorflow as tf
7
+ from object_detection .utils import label_map_util
8
+ from object_detection .utils import ops as utils_ops
9
+ from object_detection .utils import visualization_utils as viz_utils
10
+
11
+ from file import make_save_dir
12
+
13
+ SERVER_URL = 'http://localhost:8501/v1/models/{{.Name}}:predict'
14
+
15
+ # Labels
16
+ BASEPATH = os .path .dirname (os .path .realpath (__file__ ))
17
+ PATH_TO_LABELS = os .path .join (BASEPATH , 'label_map.pbtxt' )
18
+ category_index = label_map_util .create_category_index_from_labelmap (
19
+ PATH_TO_LABELS , use_display_name = True )
20
+
21
+
22
+ def predict_image (server_url , file_path , file_name ):
23
+ image = Image .open (file_path )
24
+ image_np = np .array (image )
25
+
26
+ detections = request (image_np , server_url )
27
+
28
+ image_np_with_detections = visualize (image_np , detections )
29
+
30
+ save_path = os .path .join (make_save_dir (), file_name )
31
+ im = Image .fromarray (image_np_with_detections )
32
+ im .save (save_path )
33
+
34
+ return save_path
35
+
36
+
37
+ def request (image_np , server_url = SERVER_URL ):
38
+ payload = {"inputs" : [image_np .tolist ()]}
39
+ headers = {"content-type" : "application/json" }
40
+ start = time .time ()
41
+ res = requests .post (server_url , json = payload , headers = headers )
42
+ end = time .time ()
43
+ print ('duration: %.2fs' % (end - start ))
44
+ json = res .json ()
45
+ detections = json ['outputs' ]
46
+ return detections
47
+
48
+
49
+ def visualize (image_np , detections ):
50
+ image_np_with_detections = image_np .copy ()
51
+
52
+ # https://github.com/vijaydwivedi75/Custom-Mask-RCNN_TF/blob/master/mask_rcnn_eval.ipynb
53
+ # The following processing is only for single image
54
+ detection_boxes = tf .squeeze (detections ['detection_boxes' ], [0 ])
55
+ detection_masks = tf .squeeze (detections ['detection_masks' ], [0 ])
56
+ # Reframe is required to translate mask from box coordinates to image coordinates and fit the image size.
57
+ real_num_detection = tf .cast (detections ['num_detections' ][0 ], tf .int32 )
58
+ detection_boxes = tf .slice (detection_boxes , [0 , 0 ],
59
+ [real_num_detection , - 1 ])
60
+ detection_masks = tf .slice (detection_masks , [0 , 0 , 0 ],
61
+ [real_num_detection , - 1 , - 1 ])
62
+ detection_masks_reframed = utils_ops .reframe_box_masks_to_image_masks (
63
+ detection_masks , detection_boxes , image_np .shape [0 ], image_np .shape [1 ])
64
+ detection_masks_reframed = tf .cast (
65
+ tf .greater (detection_masks_reframed , 0.5 ), tf .uint8 )
66
+ # Follow the convention by adding back the batch dimension
67
+ detections ['detection_masks' ] = tf .expand_dims (detection_masks_reframed , 0 )
68
+
69
+ detections ['num_detections' ] = int (detections ['num_detections' ][0 ])
70
+ detections ['detection_classes' ] = np .array (
71
+ detections ['detection_classes' ][0 ], dtype = np .uint8 )
72
+ detections ['detection_boxes' ] = np .array (detections ['detection_boxes' ][0 ])
73
+ detections ['detection_scores' ] = np .array (
74
+ detections ['detection_scores' ][0 ])
75
+ detections ['detection_masks' ] = detections ['detection_masks' ][0 ].numpy ()
76
+
77
+ viz_utils .visualize_boxes_and_labels_on_image_array (
78
+ image_np_with_detections ,
79
+ detections ['detection_boxes' ],
80
+ detections ['detection_classes' ],
81
+ detections ['detection_scores' ],
82
+ category_index ,
83
+ instance_masks = detections .get ('detection_masks' ),
84
+ use_normalized_coordinates = True ,
85
+ max_boxes_to_draw = 200 ,
86
+ min_score_thresh = .30 )
87
+
88
+ return image_np_with_detections
0 commit comments