def postprocess(self, predictions): postprocessed_result = [] for result, input_info in zip(predictions[0], predictions[1]): objects = [] original_h, original_w = ( input_info.original_height, input_info.original_width, ) output_shapes = [(1, 255, 13, 13), (1, 255, 26, 26)] for output_name, output_shape in zip( result.getAllLayerNames(), output_shapes, ): layer_params = YoloParams( self.yolo_params[output_name], output_shape[2], ) output = np.array(result.getLayerFp16(output_name)).reshape( output_shape, ) objects += self.parse_yolo_region( output, (self.input_height, self.input_width), (original_h, original_w), layer_params, self.threshold, ) objects = self.nms(objects) boxes = [] confidences = [] class_ids = [] image_predictions = [] for i in range(len(objects)): if objects[i].confidence == 0.0: continue for j in range(i + 1, len(objects)): if self.iou(objects[i], objects[j]) >= self.iou_threshold: if objects[i].confidence < objects[j].confidence: objects[i], objects[j] = objects[j], objects[i] objects[j].confidence = 0.0 for obj in objects: boxes.append([obj.xmin, obj.ymin, obj.xmax, obj.ymax]) confidences.append(obj.confidence) class_ids.append(int(obj.class_id) + 1) boxes = np.array(boxes, dtype=np.float) for box, conf, class_id in zip(boxes, confidences, class_ids): if conf > self.threshold: image_predictions.append( BBox( x1=int(np.clip(box[0], 0, original_w)), y1=int(np.clip(box[1], 0, original_h)), x2=int(np.clip(box[2], 0, original_w)), y2=int(np.clip(box[3], 0, original_h)), score=float(conf), class_name=self.class_names[class_id], ), ) postprocessed_result.append(image_predictions) return postprocessed_result
def postprocess(self, predictions): postprocessed_result = [] for result, input_info in zip(predictions[0], predictions[1]): (scale_x, scale_y), pads = input_info.scales, input_info.pads original_w, original_h = ( input_info.original_width, input_info.original_height, ) h, w = self.input_height, self.input_width image_predictions = [] result = [ np.array(result.getLayerFp16(self.output_names[0])), np.array(result.getLayerFp16(self.output_names[1])).reshape( -1, 18), ] if result[0].size == 0: postprocessed_result.append(image_predictions) return postprocessed_result boxes, keypoints = self.postprocessor.decode_predictions( result, self.output_names, ) for box, kps in zip(boxes, keypoints): if box[4] > self.threshold: palm_box = BBox( x1=int( np.clip((box[0] * w - pads[1]) / scale_x, 0, original_w), ), y1=int( np.clip((box[1] * h - pads[0]) / scale_y, 0, original_h), ), x2=int( np.clip((box[2] * w - pads[1]) / scale_x, 0, original_w), ), y2=int( np.clip((box[3] * h - pads[0]) / scale_y, 0, original_h), ), score=float(box[4]), class_name=self.class_names[1], ) image_predictions.append( Landmarks( bbox=palm_box, keypoints=[ Point( x=int( (keypoint[0] * w - pads[1]) / scale_x), y=int( (keypoint[1] * h - pads[0]) / scale_y), ) for keypoint in kps ], ), ) postprocessed_result.append(image_predictions) return postprocessed_result
def postprocess(self, predictions): postprocessed_result = [] for result, input_info in zip(predictions[0], predictions[1]): (scale_x, scale_y), pads = input_info.scales, input_info.pads original_w, original_h = ( input_info.original_width, input_info.original_height, ) h, w = self.input_height, self.input_width boxes = np.array(result.getLayerFp16( result.getAllLayerNames()[0])).reshape( -1, 7, ) image_predictions = [] for box in boxes: if box[2] > self.threshold: image_predictions.append( BBox( x1=int( np.clip( (box[3] * w - pads[1]) / scale_x, 0, original_w, ), ), y1=int( np.clip( (box[4] * h - pads[0]) / scale_y, 0, original_h, ), ), x2=int( np.clip( (box[5] * w - pads[1]) / scale_x, 0, original_w, ), ), y2=int( np.clip( (box[6] * h - pads[0]) / scale_y, 0, original_h, ), ), score=float(box[2]), class_name=self.class_names[int(box[1])], ), ) postprocessed_result.append(image_predictions) return postprocessed_result
def postprocess(self, predictions): postprocessed_result = [] for result, input_info in zip(predictions[0], predictions[1]): scale_y, scale_x = input_info.scales original_h, original_w = ( input_info.original_height, input_info.original_width, ) hm = np.array(result.getLayerFp16( result.getAllLayerNames()[2])).reshape((1, 1, 120, 160), ) box = np.array(result.getLayerFp16( result.getAllLayerNames()[1])).reshape((1, 4, 120, 160), ) landmark = np.array( result.getLayerFp16(result.getAllLayerNames()[0]), ).reshape( (1, 10, 120, 160)) objs = detect( hm=hm, box=box, landmark=landmark, threshold=self.threshold, nms_iou=0.5, ) image_predictions = [] for obj in objs: box, confidence, landmark = obj image_predictions.append( FacialLandmarks( bbox=BBox( x1=int(np.clip(box[0] / scale_x, 0, original_w)), y1=int(np.clip(box[1] / scale_y, 0, original_h)), x2=int(np.clip(box[2] / scale_x, 0, original_w)), y2=int(np.clip(box[3] / scale_y, 0, original_h)), score=float(confidence), class_name="face", ), keypoints=[ Point( x=int(np.clip(x / scale_x, 0, original_w)), y=int(np.clip(y / scale_y, 0, original_h)), ) for x, y in landmark ], ), ) postprocessed_result.append(image_predictions) return postprocessed_result
def postprocess(self, predictions): postprocessed_result = [] for result, input_info in zip(predictions[0], predictions[1]): scale, pads = input_info.scales[0], input_info.pads original_w, original_h = ( input_info.original_width, input_info.original_height, ) h, w = self.input_height, self.input_width image_predictions = [] result = [ np.array(result.getLayerFp16(self.output_names[0])), np.array(result.getLayerFp16(self.output_names[1])).reshape( -1, 18), ] if result[0].size == 0: postprocessed_result.append(image_predictions) return postprocessed_result boxes = self.postprocessor.decode_predictions(result) for box in boxes: if box[4] > self.threshold: image_predictions.append( BBox( x1=int( np.clip((box[0] * w - pads[1]) / scale, 0, original_w), ), y1=int( np.clip((box[1] * h - pads[0]) / scale, 0, original_h), ), x2=int( np.clip((box[2] * w - pads[1]) / scale, 0, original_w), ), y2=int( np.clip((box[3] * h - pads[0]) / scale, 0, original_h), ), score=float(box[4]), class_name="palm", ), ) postprocessed_result.append(image_predictions) return postprocessed_result
def postprocess(self, predictions): postprocessed_result = [] for result, input_info in zip(predictions[0], predictions[1]): objects = [] original_h, original_w = ( input_info.original_height, input_info.original_width, ) h, w = self.input_height, self.input_width output_shapes = [(-1, 255, 26, 26), (-1, 255, 13, 13)] for output_name, output_shape in zip( result.getAllLayerNames(), output_shapes, ): objects.extend( self.parse_yolov3_output( np.array(result.getLayerFp16(output_name)).reshape( output_shape, ), h, w, original_h, original_w, self.iou_threshold, ), ) objects = self.nms(objects) image_predictions = [] for obj in objects: image_predictions.append( BBox( x1=int(np.clip(obj.xmin, 0, original_w)), y1=int(np.clip(obj.ymin, 0, original_h)), x2=int(np.clip(obj.xmax, 0, original_w)), y2=int(np.clip(obj.ymax, 0, original_h)), score=float(obj.confidence), class_name=self.class_names[int(obj.class_id) + 1], ), ) postprocessed_result.append(image_predictions) return postprocessed_result