@@ -129,23 +129,55 @@ def classify_image(self,
129129 #else:
130130 input_image = self .read_tensor_from_image_mat (image_file_or_mat )
131131
132- logger .info ("classify.0" )
133132 self ._interpreter .set_tensor (self ._input_details [0 ]['index' ], input_image )
134133 self ._interpreter .invoke ()
135- logger .info ("classify.1" )
136134 scores = self ._interpreter .get_tensor (self ._output_details [0 ]['index' ])[0 ] # Bounding box coordinates of detected objects
137- #logger.info("classify.2")
138- #classes = self._interpreter.get_tensor(self._output_details[1]['index'])[0] # Class index of detected objects
139- #logger.info("classify.3")
140- #scores = self._interpreter.get_tensor(self._output_details[2]['index'])[0] # Confidence of detected objects
141- #logger.info("classify.4")
142135
143- #pairs = [(classes[i], scores[i], boxes[i]) for i in range(0, len(classes))]
144136 pairs = []
145137 for i in range (0 , len (scores )):
146- if scores [i ] > 0.5 :
138+ if scores [i ] > 128 :
147139 object_name = self ._labels [i ]
148- pairs .append ((object_name , scores [i ]))
140+ pairs .append ((object_name , int ( 100 * scores [i ]/ 256 ) ))
149141
142+ pairs = sorted (pairs , key = lambda x : x [1 ], reverse = True )[:top_results ]
143+ logger .info (str (pairs ))
144+ return pairs
145+
146+ def detect_objects (self ,
147+ image_file_or_mat ,
148+ top_results = 3 ):
149+ input_image = None
150+ #if isinstance(image_file_or_mat, str):
151+ # t = self.read_tensor_from_image_file(file_name=image_file_or_mat)
152+ #else:
153+ input_image = self .read_tensor_from_image_mat (image_file_or_mat )
154+
155+ self ._interpreter .set_tensor (self ._input_details [0 ]['index' ], input_image )
156+ self ._interpreter .invoke ()
157+
158+ # Retrieve detection results
159+ boxes = self ._interpreter .get_tensor (self ._output_details [0 ]['index' ])[0 ] # Bounding box coordinates of detected objects
160+ classes = self ._interpreter .get_tensor (self ._output_details [1 ]['index' ])[0 ] # Class index of detected objects
161+ scores = self ._interpreter .get_tensor (self ._output_details [2 ]['index' ])[0 ] # Confidence of detected objects
162+
163+ # Loop over all detections and draw detection box if confidence is above minimum threshold
164+ min_conf_threshold = 0.1
165+ imH = 100
166+ imW = 100
167+ pairs = []
168+ for i in range (len (scores )):
169+ if ((scores [i ] > min_conf_threshold ) and (scores [i ] <= 1.0 )):
170+
171+ # Get bounding box coordinates and draw box
172+ # Interpreter can return coordinates that are outside of image dimensions, need to force them to be within image using max() and min()
173+ ymin = int (max (1 ,(boxes [i ][0 ] * imH )))
174+ xmin = int (max (1 ,(boxes [i ][1 ] * imW )))
175+ ymax = int (min (imH ,(boxes [i ][2 ] * imH )))
176+ xmax = int (min (imW ,(boxes [i ][3 ] * imW )))
177+
178+ object_name = self ._labels [int (classes [i ])]
179+ pairs .append ((object_name , int (100 * scores [i ]), (xmin , ymin , xmax , ymax )))
180+
181+ pairs = sorted (pairs , key = lambda x : x [1 ], reverse = True )[:top_results ]
150182 logger .info (str (pairs ))
151183 return pairs
0 commit comments