Exemplo n.º 1
0
    def draw_bboxes(self, image_canvas, info_tree, conf_threshold=None):
        for object in info_tree['objects']:
            class_name = self.configer.get('details', 'name_seq')[object['label']]
            if 'score' in object:
                if conf_threshold is not None and float(object['score']) < conf_threshold:
                    continue

                class_name = '{}_{}'.format(class_name, object['score'])

            color_num = len(self.configer.get('details', 'color_list'))
            cv2.rectangle(image_canvas,
                          (int(object['bbox'][0]), int(object['bbox'][1])),
                          (int(object['bbox'][2]), int(object['bbox'][3])),
                          color=self.configer.get('details', 'color_list')[object['label'] % color_num], thickness=3)

            cv2.putText(image_canvas, class_name,
                        (int(object['bbox'][0]) + 5, int(object['bbox'][3]) - 5),
                        cv2.FONT_HERSHEY_SIMPLEX, fontScale=0.5,
                        color=self.configer.get('details', 'color_list')[object['label'] % color_num], thickness=2)

            height, width, _ = image_canvas.shape
            if isinstance(object['segm'], list):
                maskmap = MaskHelper.polys2mask(object['segm'], width, height)
            else:
                maskmap = MaskHelper.rle2mask(object['segm'], width, height)

            ori_image_canvas = image_canvas.copy()
            mask_color = self.configer.get('details', 'color_list')[object['label'] % color_num]
            mask_color = np.array(mask_color, dtype=np.uint8)
            mask_canvas = np.repeat(maskmap[:, :, np.newaxis], 3, 2).astype(np.uint8) * mask_color
            image_canvas = cv2.addWeighted(image_canvas, 0.6, mask_canvas, 0.5, 0)
            image_canvas[mask_canvas == 0] = ori_image_canvas[mask_canvas == 0]

        return image_canvas
Exemplo n.º 2
0
    def relabel(self, json_dir, method='mask_rcnn'):
        submission_file = os.path.join(
            json_dir,
            'person_instances_val2017_{}_results.json'.format(method))
        img_id_list = list()
        object_list = list()

        for json_file in os.listdir(json_dir):
            json_path = os.path.join(json_dir, json_file)
            shotname, extensions = os.path.splitext(json_file)
            try:
                img_id = int(shotname)
            except ValueError:
                Log.info('Invalid Json file: {}'.format(json_file))
                continue

            img_id_list.append(img_id)
            with open(json_path, 'r') as json_stream:
                info_tree = json.load(json_stream)
                for object in info_tree['objects']:
                    object_dict = dict()
                    object_dict['image_id'] = img_id
                    object_dict['category_id'] = int(
                        self.configer.get('data',
                                          'coco_cat_seq')[object['label']])
                    object_dict['score'] = object['score']
                    object_dict['bbox'] = [
                        object['bbox'][0], object['bbox'][1],
                        object['bbox'][2] - object['bbox'][0],
                        object['bbox'][3] - object['bbox'][1]
                    ]

                    if isinstance(object['segm'], dict):
                        object_dict['segmentation'] = object['segm']
                    else:
                        object_dict['segmentation'] = maskUtils.encode(
                            np.asfortranarray(
                                MaskHelper.polys2mask(object['segm'],
                                                      info_tree['height'],
                                                      info_tree['width'])))

                    object_list.append(object_dict)

        with open(submission_file, 'w') as write_stream:
            write_stream.write(json.dumps(object_list))

        Log.info('Evaluate {} images...'.format(len(img_id_list)))
        return submission_file, img_id_list