Пример #1
0
def show_result_rbox(img,
                     detections,
                     class_names,
                     scale=1.0,
                     threshold=0.2,
                     colormap=None,
                     show_label=False):
    assert isinstance(class_names, (tuple, list))
    if colormap:
        assert len(class_names) == len(colormap)
    img = mmcv.imread(img)
    color_white = (255, 255, 255)

    for j, name in enumerate(class_names):
        if colormap:
            color = colormap[j]
        else:
            color = (random.randint(0, 256), random.randint(0, 256),
                     random.randint(0, 256))
        try:
            dets = detections[j]
        except:
            #             pdb.set_trace()
            print('no result')
            return img  # TODO don't debug


#         import ipdb;ipdb.set_trace()
        for det in dets:
            score = det[-1]
            det = rotated_box_to_poly_single(det[:-1])
            bbox = det[:8] * scale
            if score < threshold:
                continue
            bbox = list(map(int, bbox))

            for i in range(3):
                cv2.line(img, (bbox[i * 2], bbox[i * 2 + 1]),
                         (bbox[(i + 1) * 2], bbox[(i + 1) * 2 + 1]),
                         color=color,
                         thickness=2,
                         lineType=cv2.LINE_AA)
            cv2.line(img, (bbox[6], bbox[7]), (bbox[0], bbox[1]),
                     color=color,
                     thickness=2,
                     lineType=cv2.LINE_AA)
            if show_label:
                cv2.putText(img,
                            '%s %.3f' % (class_names[j], score),
                            (bbox[0], bbox[1] + 10),
                            color=color_white,
                            fontFace=cv2.FONT_HERSHEY_COMPLEX,
                            fontScale=0.5)
    return img
Пример #2
0
def parse_gt(filename):
    objects = []
    tree = ET.parse(filename)
    root = tree.getroot()
    for obj in root.findall('HRSC_Objects')[0].findall('HRSC_Object'):
        object_struct = {}
        object_struct['name'] = 'ship'
        object_struct['difficult'] = int(obj.find('difficult').text)
        bbox = []
        for key in ['mbox_cx', 'mbox_cy', 'mbox_w', 'mbox_h', 'mbox_ang']:
            bbox.append(obj.find(key).text)
        # Coordinates may be float type
        cx, cy, w, h, a = list(map(float, bbox))
        bbox = [cx, cy, w, h, a]
        poly = rotated_box_to_poly_single(bbox)
        object_struct['bbox'] = poly.tolist()
        objects.append(object_struct)
    return objects
Пример #3
0
def show_result_rbox(img,
                     detections,
                     class_names,
                     scale=1.0,
                     threshold=0.2,
                     colormap=None,
                     show_label=False):
    assert isinstance(class_names, (tuple, list))
    if colormap:
        assert len(class_names) == len(colormap)
    img = mmcv.imread(img)
    color_white = (255, 255, 255)

    for dets, name in zip(detections['bboxes'], detections['labels']):
        if colormap:
            color = colormap[name-1]
        else:
            color = (random.randint(0, 256), random.randint(0, 256), random.randint(0, 256))

        for det in dets.reshape(1, -1):
            cv2.circle(img, (det[0], det[1]), radius=2, color=color, thickness=2)
            cv2.putText(img, '%.3f' % (det[4]), (det[0], det[1]),
                            color=color_white, fontFace=cv2.FONT_HERSHEY_COMPLEX, fontScale=0.5)
            
            score = 1.0
            det = rotated_box_to_poly_single(det)
            
            bbox = det[:8] * scale
            if score < threshold:
                continue
            bbox = list(map(int, bbox))

            for i in range(3):
                cv2.line(img, (bbox[i * 2], bbox[i * 2 + 1]), (bbox[(i + 1) * 2], bbox[(i + 1) * 2 + 1]), color=color,
                         thickness=2, lineType=cv2.LINE_AA)
            cv2.line(img, (bbox[6], bbox[7]), (bbox[0], bbox[1]), color=color, thickness=2, lineType=cv2.LINE_AA)
            if show_label:
                cv2.putText(img, '%s %.3f' % (class_names[j], score), (bbox[0], bbox[1] + 10),
                            color=color_white, fontFace=cv2.FONT_HERSHEY_COMPLEX, fontScale=0.5)
    return img
Пример #4
0
    def result_to_txt(self, results, results_path):
        img_names = [img_info['id'] for img_info in self.img_infos]

        assert len(results) == len(img_names), 'len(results) != len(img_names)'

        for classname in self.CLASSES:
            f_out = open(osp.join(results_path, classname + '.txt'), 'w')
            print(classname + '.txt')
            # per result represent one image
            for img_id, result in enumerate(results):
                for class_id, bboxes in enumerate(result):
                    if self.CLASSES[class_id] != classname:
                        continue
                    if bboxes.size != 0:
                        for bbox in bboxes:
                            score = bbox[5]
                            bbox = rotated_box_to_poly_single(bbox[:5])
                            temp_txt = '{} {:.4f} {:.4f} {:.4f} {:.4f} {:.4f} {:.4f} {:.4f} {:.4f} {:.4f}\n'.format(
                                osp.splitext(img_names[img_id])[0], score, bbox[0], bbox[1], bbox[2], bbox[3], bbox[4],
                                bbox[5], bbox[6], bbox[7])
                            f_out.write(temp_txt)
            f_out.close()