Beispiel #1
0
    def save_patches(self, img, objs, patch_name, left, top, right, down):
        # anno_out = self.anno_out_path / f"{patch_name}.xml"
        patch_box = np.array([left, top, right, down])[None, :]
        obj_boxes = np.array([obj["bbox"] for obj in objs])
        half_ious, inter_boxes = np_half_iou(obj_boxes, patch_box)

        half_ious = half_ious.squeeze()
        inter_boxes = inter_boxes.squeeze()

        width = right - left
        height = down - top
        xml_writer = PascalVocWriter("TianzhiDataset", f'{patch_name}.jpg',
                                     (height, width, 3))
        for idx, half_iou in enumerate(half_ious):
            if half_iou >= self.iou_thresh:
                # print(patch_name, inter_boxes[idx])
                new_box = inter_boxes[idx] - np.array([left, top, left, top])
                new_box = new_box.astype("int")
                xml_writer.add_bbox(objs[idx]["name"], False, new_box.tolist())

        xml_writer.save(self.anno_out_path / f'{patch_name}.xml')
        self.save_image_patch(img, patch_name, left, top, width, height)