Ejemplo n.º 1
0
    def load(self):
        PATH = '/root/code/model_data/train_bmp'
        for file in os.listdir(PATH):
            if '.json' not in file:
                continue

            json_path = os.path.join(PATH, file)
            img_path = os.path.join(PATH, os.path.splitext(file)[0] + '.bmp')
            img = cv2.imread(img_path)

            with open(json_path, 'rb') as f:
                mask_json = json.load(f)

            shapes = []
            for temp in mask_json['shapes']:
                if temp['label'] == 'whole':
                    shapes.append(temp)
                    break
            lbl, _ = utils.shapes_to_label(img.shape, shapes, label_lunkuo)
            where = np.where(lbl == 1)
            img_mask = np.zeros(img.shape, dtype=np.uint8)
            img_mask[where[0], where[1], :] = img[where[0], where[1], :]

            shapes = []
            for temp in mask_json['shapes']:
                if temp['label'] not in self.use_labels:
                    continue
                shapes.append(temp)
            if not shapes:
                continue
            lbl, _ = utils.shapes_to_label(img.shape, shapes, label_dic)
            self.masks.append(lbl)
            self.imgs.append(img_mask)
Ejemplo n.º 2
0
    def load(self):
        PATH = '/root/code/model_data/train_bmp'
        for file in os.listdir(PATH):
            if '.json' not in file:
                continue

            json_path = os.path.join(PATH, file)
            img_path = os.path.join(PATH, os.path.splitext(file)[0] + '.bmp')
            img = cv2.imread(img_path)

            with open(json_path, 'rb') as f:
                mask_json = json.load(f)
            foot, shapes = [], []
            for temp in mask_json['shapes']:
                if temp['label'] not in use_labels:
                    continue
                if temp['label'] in ['lf', 'rf', 'lb', 'rb']:
                    temp['label'] = 'foot'
                    shapes.append(temp)
                    continue
                shapes.append(temp)
            lbl, _ = utils.shapes_to_label(img.shape, shapes, label_dic_4)
            if not shapes:
                continue
            img_resize = self.resize_transform(image=img)
            img = img_resize['image']
            lbl = lbl.astype(np.uint8)
            lbl_resize = self.resize_transform(image=lbl)
            lbl = lbl_resize['image']
            self.imgs.append(img)
            self.masks.append(lbl)

        PATH = '/root/code/model_data/farm_24'
        for file in os.listdir(PATH):
            if '.json' not in file:
                continue

            json_path = os.path.join(PATH, file)
            img_path = os.path.join(PATH, os.path.splitext(file)[0] + '.bmp')
            img = cv2.imread(img_path)

            with open(json_path, 'rb') as f:
                mask_json = json.load(f)
            foot, shapes = [], []
            for temp in mask_json['shapes']:
                if temp['label'] not in use_labels:
                    continue
                if temp['label'] in ['lf', 'rf', 'lb', 'rb']:
                    temp['label'] = 'foot'
                    shapes.append(temp)
                    continue
                shapes.append(temp)
            lbl, _ = utils.shapes_to_label(img.shape, shapes, label_dic_4)
            if not shapes:
                continue

            self.imgs.append(img)
            self.masks.append(lbl)
Ejemplo n.º 3
0
    def load(self):

        PATH = '/root/code/test_pytorch/takeoff_maskrcnn/train_bmp'
        for file in os.listdir(PATH):
            if '.json' not in file:
                continue

            json_path = os.path.join(PATH, file)
            img_path = os.path.join(PATH, os.path.splitext(file)[0] + '.bmp')
            img = cv2.imread(img_path)
            self.imgs.append(img)

            with open(json_path, 'rb') as f:
                mask_json = json.load(f)
            shapes = []
            foot = []
            for temp in mask_json['shapes']:
                if temp['label'] not in use_labels:
                    continue
                if temp['label'] in ['lf', 'rf', 'lb', 'rb']:
                    temp['label'] = 'foot'
                    shapes.append(temp)
                    continue
                shapes.append(temp)
            lbl, _ = utils.shapes_to_label(img.shape, shapes, label_dic_4)
            # LBL = np.zeros((self.class_num, img.shape[0], img.shape[1]), dtype=np.float32)
            # for i, label in enumerate(self.use_labels):
            #     v = label_dic_3[label]
            #     where_index = np.where(lbl == v)
            #     LBL[i, where_index[0], where_index[1]] = 1
            self.masks.append(lbl)

        return None

        PATH = '/root/code/test_pytorch/takeoff_maskrcnn/train_jpg'
        for file in os.listdir(PATH):
            if '.json' not in file:
                continue

            json_path = os.path.join(PATH, file)
            img_path = os.path.join(PATH, os.path.splitext(file)[0] + '.jpg')
            img = cv2.imread(img_path)
            self.imgs.append(img)

            with open(json_path, 'rb') as f:
                mask_json = json.load(f)
            shapes = []
            for temp in mask_json['shapes']:
                if temp['label'] == 'cow':
                    shapes.append(temp)
                    break
            lbl, _ = utils.shapes_to_label(img.shape, shapes,
                                           label_name_to_value_2)
            # LBL = np.zeros((self.class_num, img.shape[0], img.shape[1]))
            # for i in range(self.class_num):
            #     where_index = np.where(lbl == i)
            #     LBL[i, where_index[0], where_index[1]] = 1
            self.masks.append(lbl.astype(np.long))
Ejemplo n.º 4
0
    def load(self):
        PATH = '/root/code/test_pytorch/takeoff_maskrcnn/train_bmp'
        for file in os.listdir(PATH):
            if '.bmp' not in file:
                continue

            json_path = os.path.splitext(file)[0] + '.json'
            if os.path.exists(json_path):
                continue

            img_path = os.path.join(PATH, file)
            img = cv2.imread(img_path)
            self.imgs.append(img)

            with open(json_path, 'rb') as f:
                mask_json = json.load(f)
            shapes = []
            for temp in mask_json['shapes']:
                if temp['label'] == 'whole':
                    continue
                shapes.append(temp)
            lbl, _ = utils.shapes_to_label(img.shape, shapes, label_dic_1)
            LBL = np.zeros((self.class_num, img.shape[0], img.shape[1]),
                           dtype=np.float32)
            for k, v in label_dic_1.items():
                if v == 0:
                    continue
                where_index = np.where(lbl == v)
                LBL[v - 1, where_index[0], where_index[1]] = 1
            self.masks.append(LBL)

        return None

        PATH = '/root/code/test_pytorch/takeoff_maskrcnn/train_jpg'
        for file in os.listdir(PATH):
            if '.json' not in file:
                continue

            json_path = os.path.join(PATH, file)
            img_path = os.path.join(PATH, os.path.splitext(file)[0] + '.jpg')
            img = cv2.imread(img_path)
            self.imgs.append(img)

            with open(json_path, 'rb') as f:
                mask_json = json.load(f)
            shapes = []
            for temp in mask_json['shapes']:
                if temp['label'] == 'cow':
                    shapes.append(temp)
                    break
            lbl, _ = utils.shapes_to_label(img.shape, shapes,
                                           label_name_to_value_2)
            # LBL = np.zeros((self.class_num, img.shape[0], img.shape[1]))
            # for i in range(self.class_num):
            #     where_index = np.where(lbl == i)
            #     LBL[i, where_index[0], where_index[1]] = 1
            self.masks.append(lbl.astype(np.float32))
Ejemplo n.º 5
0
    def load(self):

        PATH = '/root/code/model_data/train_bmp'
        for file in os.listdir(PATH):
            if '.json' not in file:
                continue

            json_path = os.path.join(PATH, file)
            img_path = os.path.join(PATH, os.path.splitext(file)[0] + '.bmp')
            img = cv2.imread(img_path)
            self.imgs.append(img)

            with open(json_path, 'rb') as f:
                mask_json = json.load(f)
            shapes = []
            for temp in mask_json['shapes']:
                if temp['label'] not in self.use_labels:
                    continue
                shapes.append(temp)
            lbl, _ = utils.shapes_to_label(img.shape,
                                           shapes,
                                           label_dic)
            self.masks.append(lbl)

        return None

        PATH = '/root/code/model_data/train_jpg'
        for file in os.listdir(PATH):
            if '.json' not in file:
                continue

            json_path = os.path.join(PATH, file)
            img_path = os.path.join(PATH, os.path.splitext(file)[0] + '.jpg')
            img = cv2.imread(img_path)
            self.imgs.append(img)

            with open(json_path, 'rb') as f:
                mask_json = json.load(f)
            shapes = []
            for temp in mask_json['shapes']:
                if temp['label'] == 'cow':
                    shapes.append(temp)
                    break
            lbl, _ = utils.shapes_to_label(img.shape,
                                           shapes,
                                           label_name_to_value_2)
            # LBL = np.zeros((self.class_num, img.shape[0], img.shape[1]))
            # for i in range(self.class_num):
            #     where_index = np.where(lbl == i)
            #     LBL[i, where_index[0], where_index[1]] = 1
            self.masks.append(lbl.astype(np.long))
Ejemplo n.º 6
0
    def get_raw_data(self, key, save_processed_image=False):
        """
        Args:
            key (int): key

        Returns:
            ret_dict
        """
        assert isinstance(key, int)
        json_file = self.json_path_list[key]

        data = json.load(open(json_file))
        imageData = data["imageData"]
        raw_img = utils.img_b64_to_arr(imageData)  # img: H x W x C
        seg_mask, _ = utils.shapes_to_label(raw_img.shape, data["shapes"],
                                            self.label_name_to_value)  # HxW
        seg_mask = torch.tensor(seg_mask, dtype=torch.uint8)
        seg_mask = self.label_unifier(seg_mask)
        loss_mask = torch.ones_like(seg_mask)
        raw_img = Image.fromarray(raw_img)
        return {
            'image': raw_img,
            'seg_mask': seg_mask,
            'loss_mask': loss_mask,
            'valid_label_idx': self.valid_label_idx,
        }
Ejemplo n.º 7
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('json_file')
    args = parser.parse_args()

    json_file = args.json_file

    data = json.load(open(json_file))

    img = utils.img_b64_to_arr(data['imageData'])

    label_name_to_value = {'_background_': 0}
    for shape in data['shapes']:
        label_name = shape['label']
        if label_name in label_name_to_value:
            label_value = label_name_to_value[label_name]
        else:
            label_value = len(label_name_to_value)
            label_name_to_value[label_name] = label_value

    lbl = utils.shapes_to_label(img.shape, data['shapes'], label_name_to_value)

    captions = [
        '{}: {}'.format(lv, ln) for ln, lv in label_name_to_value.items()
    ]
    lbl_viz = utils.draw_label(lbl, img, captions)

    plt.subplot(121)
    plt.imshow(img)
    plt.subplot(122)
    plt.imshow(lbl_viz)
    plt.show()
Ejemplo n.º 8
0
    def Json_to_dataset(self):
        count = os.listdir(self.JsonjpgChange)
        for i in range(0, len(count)):
            path = os.path.join(self.Jsonjpg7, count[i])

            if os.path.isfile(path) and path.endswith('json'):
                data = json.load(open(path))

                if data['imageData']:
                    imageData = data['imageData']
                else:
                    imagePath = os.path.join(os.path.dirname(path), data['imagePath'])
                    with open(imagePath, 'rb') as f:
                        imageData = f.read()
                        imageData = base64.b64encode(imageData).decode('utf-8')
                img = utils.img_b64_to_arr(imageData)
                label_name_to_value = {'_background_': 0}
                for shape in data['shapes']:
                    label_name = shape['label']
                    if label_name in label_name_to_value:
                        label_value = label_name_to_value[label_name]
                    else:
                        label_value = len(label_name_to_value)
                        label_name_to_value[label_name] = label_value

                # label_values must be dense
                label_values, label_names = [], []
                for ln, lv in sorted(label_name_to_value.items(), key=lambda x: x[1]):
                    label_values.append(lv)
                    label_names.append(ln)
                assert label_values == list(range(len(label_values)))

                lbl = utils.shapes_to_label(img.shape, data['shapes'], label_name_to_value)

                captions = ['{}: {}'.format(lv, ln)
                            for ln, lv in label_name_to_value.items()]
                lbl_viz = draw.draw_label(lbl, img, captions)
                out_dir = osp.basename(count[i]).replace('.', '_')
                out_dir = osp.join(osp.dirname(count[i]), out_dir)
                out_dir = osp.join(self.outputChange, out_dir)

                if not osp.exists(out_dir):
                    os.mkdir(out_dir)

                PIL.Image.fromarray(img).save(osp.join(out_dir, 'img.png'))

                utils.lblsave(osp.join(out_dir, 'label.png'), lbl)
                PIL.Image.fromarray(lbl_viz).save(osp.join(out_dir, 'label_viz.png'))

                with open(osp.join(out_dir, 'label_names.txt'), 'w') as f:
                    for lbl_name in label_names:
                        f.write(lbl_name + '\n')

                warnings.warn('info.yaml is being replaced by label_names.txt')
                info = dict(label_names=label_names)
                with open(osp.join(out_dir, 'info.yaml'), 'w') as f:
                    yaml.safe_dump(info, f, default_flow_style=False)

                print('Saved to: %s' % out_dir)
        self.lineEdit_do_jsontodataset.setText('Json To Dataset Complete!')
Ejemplo n.º 9
0
def json2dataset(json_dirpath):
    json_file = json_dirpath
    data = json.load(open(json_file))

    if data['imageData']:
        imageData = data['imageData']
    else:
        imagePath = os.path.join(os.path.dirname(json_file), data['imagePath'])
        with open(imagePath, 'rb') as f:
            imageData = f.read()
            imageData = base64.b64encode(imageData).decode('utf-8')
    img = utils.img_b64_to_arr(imageData)

    label_name_to_value = {'_background_': 0}
    for shape in sorted(data['shapes'], key=lambda x: x['label']):
        label_name = shape['label']
        if label_name in label_name_to_value:
            label_value = label_name_to_value[label_name]
        else:
            label_value = len(label_name_to_value)
            label_name_to_value[label_name] = label_value
    lbl = utils.shapes_to_label(img.shape, data['shapes'], label_name_to_value)

    label_names = [None] * (max(label_name_to_value.values()) + 1)
    for name, value in label_name_to_value.items():
        label_names[value] = name
    lbl_viz = utils.draw_label(lbl, img, label_names)
    return lbl
Ejemplo n.º 10
0
def main():
    logger.warning("This script is aimed to demonstrate how to convert the "
                   "JSON file to a single image dataset.")
    logger.warning("It won't handle multiple JSON files to generate a "
                   "real-use dataset.")

    parser = argparse.ArgumentParser()
    parser.add_argument("json_file")
    parser.add_argument("-o", "--out", default=None)
    args = parser.parse_args()

    json_file = args.json_file

    if args.out is None:
        out_dir = osp.basename(json_file).replace(".", "_")
        out_dir = osp.join(osp.dirname(json_file), out_dir)
    else:
        out_dir = args.out
    if not osp.exists(out_dir):
        os.mkdir(out_dir)

    data = json.load(open(json_file))
    imageData = data.get("imageData")

    if not imageData:
        imagePath = os.path.join(os.path.dirname(json_file), data["imagePath"])
        with open(imagePath, "rb") as f:
            imageData = f.read()
            imageData = base64.b64encode(imageData).decode("utf-8")
    img = utils.img_b64_to_arr(imageData)

    label_name_to_value = {"_background_": 0}
    for shape in sorted(data["shapes"], key=lambda x: x["label"]):
        label_name = shape["label"]
        if label_name in label_name_to_value:
            label_value = label_name_to_value[label_name]
        else:
            label_value = len(label_name_to_value)
            label_name_to_value[label_name] = label_value
    lbl, _ = utils.shapes_to_label(img.shape, data["shapes"],
                                   label_name_to_value)

    label_names = [None] * (max(label_name_to_value.values()) + 1)
    for name, value in label_name_to_value.items():
        label_names[value] = name

    lbl_viz = imgviz.label2rgb(label=lbl,
                               img=imgviz.asgray(img),
                               label_names=label_names,
                               loc="rb")

    PIL.Image.fromarray(img).save(osp.join(out_dir, "img.png"))
    utils.lblsave(osp.join(out_dir, "label.png"), lbl)
    PIL.Image.fromarray(lbl_viz).save(osp.join(out_dir, "label_viz.png"))

    with open(osp.join(out_dir, "label_names.txt"), "w") as f:
        for lbl_name in label_names:
            f.write(lbl_name + "\n")

    logger.info("Saved to: {}".format(out_dir))
Ejemplo n.º 11
0
def main():
    logger.warning('This script is aimed to demonstrate how to convert the'
                   'JSON file to a single image dataset, and not to handle'
                   'multiple JSON files to generate a real-use dataset.')

    parser = argparse.ArgumentParser()
    parser.add_argument('json_file')
    parser.add_argument('-o', '--out', default=None)
    args = parser.parse_args()

    json_file = args.json_file

    if args.out is None:
        out_dir = osp.basename(json_file).replace('.', '_')
        out_dir = osp.join(osp.dirname(json_file), out_dir)
    else:
        out_dir = args.out
    if not osp.exists(out_dir):
        os.mkdir(out_dir)

    data = json.load(open(json_file))

    if data['imageData']:
        imageData = data['imageData']
    else:
        imagePath = os.path.join(os.path.dirname(json_file), data['imagePath'])
        with open(imagePath, 'rb') as f:
            imageData = f.read()
            imageData = base64.b64encode(imageData).decode('utf-8')
    img = utils.img_b64_to_arr(imageData)

    label_name_to_value = {'_background_': 0}
    for shape in sorted(data['shapes'], key=lambda x: x['label']):
        label_name = shape['label']
        if label_name in label_name_to_value:
            label_value = label_name_to_value[label_name]
        else:
            label_value = len(label_name_to_value)
            label_name_to_value[label_name] = label_value
    lbl = utils.shapes_to_label(img.shape, data['shapes'], label_name_to_value)

    label_names = [None] * (max(label_name_to_value.values()) + 1)
    for name, value in label_name_to_value.items():
        label_names[value] = name
    lbl_viz = utils.draw_label(lbl, img, label_names)

    PIL.Image.fromarray(img).save(osp.join(out_dir, 'img.png'))
    utils.lblsave(osp.join(out_dir, 'label.png'), lbl)
    PIL.Image.fromarray(lbl_viz).save(osp.join(out_dir, 'label_viz.png'))

    with open(osp.join(out_dir, 'label_names.txt'), 'w') as f:
        for lbl_name in label_names:
            f.write(lbl_name + '\n')

    logger.warning('info.yaml is being replaced by label_names.txt')
    info = dict(label_names=label_names)
    with open(osp.join(out_dir, 'info.yaml'), 'w') as f:
        yaml.safe_dump(info, f, default_flow_style=False)

    logger.info('Saved to: {}'.format(out_dir))
Ejemplo n.º 12
0
def save_label_from_json(json_file, out_dir):
    data = json.load(open(json_file))

    if data['imageData']:
        imageData = data['imageData']
    else:
        imagePath = osp.join(osp.dirname(json_file), data['imagePath'])
        with open(imagePath, 'rb') as f:
            imageData = f.read()
            imageData = base64.b64encode(imageData).decode('utf-8')
    img = utils.img_b64_to_arr(imageData)

    label_name_to_value = {'_background_': 0}
    for shape in sorted(data['shapes'], key=lambda x: x['label']):
        label_name = shape['label']
        if label_name in label_name_to_value:
            label_value = label_name_to_value[label_name]
        else:
            label_value = len(label_name_to_value)
            label_name_to_value[label_name] = label_value
    lbl = utils.shapes_to_label(img.shape, data['shapes'], label_name_to_value)

    label_names = [None] * (max(label_name_to_value.values()) + 1)
    for name, value in label_name_to_value.items():
        label_names[value] = name
    lbl_viz = utils.draw_label(lbl, img, label_names)

    utils.lblsave(osp.join(out_dir, 'label.png'), lbl)
    PIL.Image.fromarray(lbl_viz).save(osp.join(out_dir, 'label_viz.png'))

    with open(osp.join(out_dir, 'label_names.txt'), 'w') as f:
        for lbl_name in label_names:
            f.write(lbl_name + '\n')
Ejemplo n.º 13
0
    def __getitem__(self, idx):
        img_path, mask_path = self.imgs[idx], self.masks[idx]
        img = cv2.imread(img_path)
        with open(mask_path, 'rb') as f:
            mask_json = json.load(f)

        mask_shapes = []
        if '.jpg' in img_path:
            mask_shapes.append([mask_json["shapes"][0]])
            label_d = {"_background_": 0, 'cow': 1}
        elif '.bmp' in img_path:
            for mask in mask_json['shapes']:
                if mask['label'] != 'whole':
                    continue
                mask_shapes.append([mask])
                break
            label_d = {"_background_": 0, 'whole': 1}

        boxes = []
        lbl, _ = labelme_utils.shapes_to_label(img.shape, mask_shapes[0],
                                               label_d)
        nonzero_idx = np.nonzero(lbl)
        xmin = np.min(nonzero_idx[1])
        xmax = np.max(nonzero_idx[1])
        ymin = np.min(nonzero_idx[0])
        ymax = np.max(nonzero_idx[0])
        boxes.append([xmin, ymin, xmax, ymax])
        boxes = np.asarray(boxes)
        labels = np.ones(len(boxes))

        targets = {}
        targets['boxes'] = torch.from_numpy(boxes)
        targets['labels'] = torch.from_numpy(labels).type(torch.int64)
        img = self.transforms(img)
        return img, targets
Ejemplo n.º 14
0
def main():
    print('This script is aimed to demonstrate how to convert the'
          'JSON file to a single image dataset, and not to handle'
          'multiple JSON files to generate a real-use dataset.')

    path_file_name = glob.glob('*.json')
    file_num = len(path_file_name)
    print('INFO:There are ' + str(file_num) + ' json files')
    file_name = [i for i in range(file_num)]
    for i in range(file_num):
        file_name[i] = path_file_name[i].split('\\')[-1]
        print('INFO:' + file_name[i] + ' is dealt')
        data = json.load(open(path_file_name[i]))
        imageData = data.get('imageData')

        out_dir = osp.basename(path_file_name[i]).replace('.', '_')
        out_dir = osp.join(osp.dirname(path_file_name[i]), out_dir)
        if not os.path.exists(out_dir):
            os.mkdir(out_dir)

        if not imageData:
            imagePath = os.path.join(os.path.dirname(json_file),
                                     data['imagePath'])
            with open(imagePath, 'rb') as f:
                imageData = f.read()
                imageData = base64.b64encode(imageData).decode('utf-8')
        img = utils.img_b64_to_arr(imageData)

        label_name_to_value = {'_background_': 0}
        for shape in sorted(data['shapes'], key=lambda x: x['label']):
            label_name = shape['label']
            if label_name in label_name_to_value:
                label_value = label_name_to_value[label_name]
            else:
                label_value = len(label_name_to_value)
                label_name_to_value[label_name] = label_value
        lbl, _ = utils.shapes_to_label(img.shape, data['shapes'],
                                       label_name_to_value)

        label_names = [None] * (max(label_name_to_value.values()) + 1)
        for name, value in label_name_to_value.items():
            label_names[value] = name

        lbl_viz = imgviz.label2rgb(label=lbl,
                                   img=imgviz.asgray(img),
                                   label_names=label_names,
                                   loc='rb')

        PIL.Image.fromarray(img).save(osp.join(out_dir, 'img.png'))
        utils.lblsave(osp.join(out_dir, 'label.png'), lbl)
        PIL.Image.fromarray(lbl_viz).save(osp.join(out_dir, 'label_viz.png'))

        with open(osp.join(out_dir, 'label_names.txt'), 'w') as f:
            for lbl_name in label_names:
                f.write(lbl_name + '\n')

        print('INFO:Saved to: {}'.format('images/segmentation/test/' +
                                         file_name[i]))

    print('INFO:finished!')
Ejemplo n.º 15
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('json_file')
    args = parser.parse_args()

    label_file = LabelFile(args.json_file)
    img = utils.img_data_to_arr(label_file.imageData)

    label_name_to_value = {'_background_': 0}
    for shape in sorted(label_file.shapes, key=lambda x: x['label']):
        label_name = shape['label']
        if label_name in label_name_to_value:
            label_value = label_name_to_value[label_name]
        else:
            label_value = len(label_name_to_value)
            label_name_to_value[label_name] = label_value
    lbl, _ = utils.shapes_to_label(img.shape, label_file.shapes,
                                   label_name_to_value)

    label_names = [None] * (max(label_name_to_value.values()) + 1)
    for name, value in label_name_to_value.items():
        label_names[value] = name
    lbl_viz = imgviz.label2rgb(
        label=lbl,
        img=imgviz.asgray(img),
        label_names=label_names,
        font_size=30,
        loc='rb',
    )

    plt.subplot(121)
    plt.imshow(img)
    plt.subplot(122)
    plt.imshow(lbl_viz)
    plt.show()
Ejemplo n.º 16
0
def main():
    logger.warning('This script is aimed to demonstrate how to convert the'
                   'JSON file to a single image dataset, and not to handle'
                   'multiple JSON files to generate a real-use dataset.')

    parser = argparse.ArgumentParser()
    parser.add_argument('json_file')
    parser.add_argument('-o', '--out', default=None)
    args = parser.parse_args()

    json_file = args.json_file

    if args.out is None:
        out_dir = osp.basename(json_file).replace('.', '_')
        out_dir = osp.join(osp.dirname(json_file), out_dir)
    else:
        out_dir = args.out
    if not osp.exists(out_dir):
        os.mkdir(out_dir)

    data = json.load(open(json_file))

    if data['imageData']:
        imageData = data['imageData']
    else:
        imagePath = os.path.join(os.path.dirname(json_file), data['imagePath'])
        with open(imagePath, 'rb') as f:
            imageData = f.read()
            imageData = base64.b64encode(imageData).decode('utf-8')
    img = utils.img_b64_to_arr(imageData)

    label_name_to_value = {'_background_': 0}
    for shape in sorted(data['shapes'], key=lambda x: x['label']):
        label_name = shape['label']
        if label_name in label_name_to_value:
            label_value = label_name_to_value[label_name]
        else:
            label_value = len(label_name_to_value)
            label_name_to_value[label_name] = label_value
    lbl = utils.shapes_to_label(img.shape, data['shapes'], label_name_to_value)

    label_names = [None] * (max(label_name_to_value.values()) + 1)
    for name, value in label_name_to_value.items():
        label_names[value] = name
    lbl_viz = utils.draw_label(lbl, img, label_names)

    PIL.Image.fromarray(img).save(osp.join(out_dir, 'img.png'))
    utils.lblsave(osp.join(out_dir, 'label.png'), lbl)
    PIL.Image.fromarray(lbl_viz).save(osp.join(out_dir, 'label_viz.png'))

    with open(osp.join(out_dir, 'label_names.txt'), 'w') as f:
        for lbl_name in label_names:
            f.write(lbl_name + '\n')

    logger.warning('info.yaml is being replaced by label_names.txt')
    info = dict(label_names=label_names)
    with open(osp.join(out_dir, 'info.yaml'), 'w') as f:
        yaml.safe_dump(info, f, default_flow_style=False)

    logger.info('Saved to: {}'.format(out_dir))
Ejemplo n.º 17
0
def generate_label_png(json_file_path):
    print('generate_label_png'+json_file_path)
    data = json.load(open(json_file_path))
    #imageData = data['imageData']
    #img = utils.img_b64_to_arr(imageData)
    label_name_to_value = {'_background_': 0}
    for shape in data['shapes']:
        label_name = shape['label']
        if label_name in label_name_to_value:
            label_value = label_name_to_value[label_name]
        else:
            label_value = len(label_name_to_value)
            label_name_to_value[label_name] = label_value
            
    label_values, label_names = [], []
    for ln, lv in sorted(label_name_to_value.items(), key=lambda x: x[1]):
        label_values.append(lv)
        label_names.append(ln)
    print(label_names)
    assert label_values == list(range(len(label_values)))
    label_only = utils.shapes_to_label((512,512), data['shapes'], label_name_to_value).astype('uint8')
    #有多種標籤的話會生成不同顏色
    #就像最大連通區域
    #屬於第一種標籤的部分 塗上就會標為1
    #屬於第二種標籤的部分 塗上就會標為2
    #背景就會標成 0 
    #但是我現在想把它變成同一張 所以大於等於1的部分都會標成1
    label_only[label_only>=1]=1
    filename = data['imagePath']
    return label_only,filename
Ejemplo n.º 18
0
    def __getitem__(self, idx):
        img_path, mask_path = self.imgs[idx], self.masks[idx]
        img = cv2.imread(img_path)
        with open(mask_path, 'rb') as f:
            mask_json = json.load(f)

        mask_shapes = []
        if '.jpg' in img_path:
            mask_shapes.append([mask_json["shapes"][0]])
            label_name_to_value = {"_background_": 0, 'cow': 1}
        elif '.bmp' in img_path:
            for mask in mask_json['shapes']:
                if mask['label'] != 'whole':
                    continue
                mask_shapes.append([mask])
                break
            label_name_to_value = {"_background_": 0, 'whole': 1}

        label_d = label_name_to_value
        num_objs = 1
        boxes, masks = [], []
        lbl, _ = labelme_utils.shapes_to_label(img.shape, mask_shapes[0],
                                               label_d)

        img_resize = self.resize_transform(image=img)
        img = img_resize['image']
        lbl = lbl.astype(np.uint8)
        lbl_resize = self.resize_transform(image=lbl)
        lbl = lbl_resize['image']

        nonzero_idx = np.nonzero(lbl)
        xmin = np.min(nonzero_idx[1])
        xmax = np.max(nonzero_idx[1])
        ymin = np.min(nonzero_idx[0])
        ymax = np.max(nonzero_idx[0])
        boxes.append([xmin, ymin, xmax, ymax])
        masks.append(lbl)

        # convert everything into a torch.Tensor
        boxes = torch.as_tensor(boxes, dtype=torch.float32)
        # there is only one class
        labels = torch.ones((num_objs, ), dtype=torch.int64)
        masks = torch.as_tensor(masks, dtype=torch.uint8)
        image_id = torch.tensor([idx])
        area = (boxes[:, 3] - boxes[:, 1]) * (boxes[:, 2] - boxes[:, 0])
        # suppose all instances are not crowd
        iscrowd = torch.zeros((num_objs, ), dtype=torch.int64)

        target = {}
        target["boxes"] = boxes
        target["labels"] = labels
        target["masks"] = masks
        target["image_id"] = image_id
        target["area"] = area
        target["iscrowd"] = iscrowd

        if self.transforms is not None:
            img, target = self.transforms(img, target)

        return img, target
Ejemplo n.º 19
0
def json2label(json_folder):
    json_list = glob.glob(os.path.join(json_folder, '*.json'))

    annotations_dir = os.path.join(json_folder, "annotations")
    josn2labels_dir = os.path.join(json_folder, "josn2labels")

    if not os.path.exists(annotations_dir):
        os.makedirs(annotations_dir)
    if not os.path.exists(josn2labels_dir):
        os.makedirs(josn2labels_dir)

    for json_file in json_list:
        temp_name = json_file.split(".")[0].split("/")[-1]

        out_dir = os.path.join(josn2labels_dir, temp_name)
        if not osp.exists(out_dir):
            os.mkdir(out_dir)

        data = json.load(open(json_file))

        if data['imageData']:
            imageData = data['imageData']
        else:
            imagePath = os.path.join(os.path.dirname(json_file),
                                     data['imagePath'])
            with open(imagePath, 'rb') as f:
                imageData = f.read()
                imageData = base64.b64encode(imageData).decode('utf-8')
        img = utils.img_b64_to_arr(imageData)

        label_name_to_value = {'_background_': 0, '1': 1, '2': 2, '3': 3}

        lbl = utils.shapes_to_label(img.shape, data['shapes'],
                                    label_name_to_value)

        label_names = [None] * (max(label_name_to_value.values()) + 1)
        for name, value in label_name_to_value.items():
            label_names[value] = name
        lbl_viz = utils.draw_label(lbl, img, label_names)

        PIL.Image.fromarray(img).save(
            osp.join(out_dir, '%s_img.png' % (temp_name)))
        utils.lblsave(osp.join(out_dir, '%s.png' % (temp_name)), lbl)
        utils.lblsave(osp.join(annotations_dir, '%s.png' % (temp_name)), lbl)
        PIL.Image.fromarray(lbl_viz).save(
            osp.join(out_dir, '%s_label_viz.png' % (temp_name)))

        with open(osp.join(out_dir, 'label_names.txt'), 'w') as f:
            for lbl_name in label_names:
                f.write(lbl_name + '\n')

        warnings.warn('info.yaml is being replaced by label_names.txt')
        info = dict(label_names=label_names)
        with open(osp.join(out_dir, 'info.yaml'), 'w') as f:
            yaml.safe_dump(info, f, default_flow_style=False)

        print('Saved to: %s' % out_dir)
Ejemplo n.º 20
0
    def load(self):
        PATH = '/root/code/model_data/train_bmp'
        for file in os.listdir(PATH):
            if '.json' not in file:
                continue

            json_path = os.path.join(PATH, file)
            img_path = os.path.join(PATH, os.path.splitext(file)[0] + '.bmp')
            img = cv2.imread(img_path)
            self.imgs.append(img)

            with open(json_path, 'rb') as f:
                mask_json = json.load(f)
            shapes = []
            for temp in mask_json['shapes']:
                if temp['label'] not in self.use_labels:
                    continue
                shapes.append(temp)
            lbl, _ = utils.shapes_to_label(img.shape,
                                           shapes,
                                           label_dic_1)
            self.masks.append(lbl)

        PATH = '/root/code/model_data/train_jpg'
        for file in os.listdir(PATH):
            if '.json' not in file:
                continue

            json_path = os.path.join(PATH, file)
            img_path = os.path.join(PATH, os.path.splitext(file)[0] + '.jpg')
            img = cv2.imread(img_path)
            self.imgs.append(img)

            with open(json_path, 'rb') as f:
                mask_json = json.load(f)
            shapes = []
            for temp in mask_json['shapes']:
                if temp['label'] == 'cow':
                    shapes.append(temp)
                    break
            lbl, _ = utils.shapes_to_label(img.shape,
                                           shapes,
                                           label_dic_2)
            self.masks.append(lbl)
Ejemplo n.º 21
0
def main():
    logger.warning('This script is aimed to demonstrate how to convert the'
                   'JSON file to a single image dataset, and not to handle'
                   'multiple JSON files to generate a real-use dataset.')

    # parser = argparse.ArgumentParser()
    # parser.add_argument('json_file')
    # parser.add_argument('-o', '--out', default=None)
    # args = parser.parse_args()

    json_files = glob.glob(r'C:\Users\Zeran\Desktop\loudi\*.json')
    for json_file in json_files:

        out_dir = osp.basename(json_file).replace('.', '_')
        out_dir = osp.join(osp.dirname(json_file), out_dir)

        # reload(sys)
        # sys.setdefaultencoding('utf8')
        f = open(json_file, encoding='utf-8')
        text = f.read()
        # text = text.decode("gbk").encode("utf-8")
        data = json.loads(text)

        # data = f.read().decode(encoding='gbk').encode(encoding='utf-8')

        # data = json.load(open(json_file))

        if data['imageData']:
            imageData = data['imageData']
        else:
            imagePath = os.path.join(os.path.dirname(json_file),
                                     data['imagePath'])
            with open(imagePath, 'rb') as f:
                imageData = f.read()
                imageData = base64.b64encode(imageData).decode('utf-8')
        img = utils.img_b64_to_arr(imageData)

        label_name_to_value = {'_background_': 0}
        for shape in sorted(data['shapes'], key=lambda x: x['label']):
            label_name = shape['label']
            # if label_name in label_name_to_value:
            #     label_value = label_name_to_value[label_name]
            # else:
            #     label_value = len(label_name_to_value)
            label_name_to_value[label_name] = 255
        lbl = utils.shapes_to_label(img.shape, data['shapes'],
                                    label_name_to_value)

        label_names = [None] * (max(label_name_to_value.values()) + 1)
        for name, value in label_name_to_value.items():
            label_names[value] = name
        lbl_viz = utils.draw_label(lbl, img, label_names)
        saved_name = os.path.splitext(os.path.basename(json_file))[0] + '.png'
        utils.lblsave(
            osp.join('D:\\coslight\\0304_beforetolabel\\label\\', saved_name),
            lbl)
Ejemplo n.º 22
0
def lbl_arr_load(data_true, img_true):
    label_name_to_value = {"_background_": 0}
    for shape in sorted(data_true["shapes"], key=lambda x: x["label"]):
        label_name = shape["label"]
        if label_name in label_name_to_value:
            label_value = label_name_to_value[label_name]
        else:
            label_value = len(label_name_to_value)
            label_name_to_value[label_name] = label_value
    lbl, _ = utils.shapes_to_label(img_true.shape, data_true["shapes"],
                                   label_name_to_value)
    return lbl
def label_convert(json_file_path, png_label_path, class_names):
    if not os.path.isdir(json_file_path):
        raise ValueError('Input path does not exist!\n')
    os.makedirs(png_label_path, exist_ok=True)

    # all the json annotation file list
    json_files = glob.glob(os.path.join(json_file_path, '*.json'))

    # form a dict of class_name to label value
    label_name_to_value = {}
    for i, class_name in enumerate(class_names):
        label_name_to_value[class_name] = i

    # count class item number
    class_count = OrderedDict([(item, 0) for item in class_names])

    pbar = tqdm(total=len(json_files), desc='Label converting')
    for i, json_file in enumerate(json_files):
        data = json.load(open(json_file))

        # get image info
        imageData = data.get("imageData")
        if not imageData:
            imagePath = os.path.join(os.path.dirname(json_file), data["imagePath"].replace('\\', '/'))
            with open(imagePath, "rb") as f:
                imageData = f.read()
                imageData = base64.b64encode(imageData).decode("utf-8")
        img = utils.img_b64_to_arr(imageData)

        # convert json labels to numpy label array
        # and save to png
        label_array, _ = utils.shapes_to_label(
            img.shape, data["shapes"], label_name_to_value
        )

        # count object class for statistic
        label_list = list(np.unique(label_array))
        for label in label_list:
            class_name = class_names[label]
            class_count[class_name] = class_count[class_name] + 1

        utils.lblsave(os.path.join(png_label_path, os.path.basename(json_file)+".png"), label_array)
        pbar.update(1)

    pbar.close()
    # show item number statistic
    print('Image number for each class:')
    for (class_name, number) in class_count.items():
        if class_name == 'background':
            continue
        print('%s: %d' % (class_name, number))
    print('total number of converted images: ', len(json_files))
def colored_mask(jsondata, side_flipped=False):
    """
    :param jsondata: image label json
    :param side_flipped: flip SIDE mask
    :return: colored mask and origin point
    """
    data = jsondata

    img = utils.img_b64_to_arr(data['imageData'])
    # lbl, lbl_names = utils.labelme_shapes_to_label(img.shape, data['shapes'])
    lbl_names = dict()
    lbl_names['origin'] = 1
    lbl_names['edge'] = 2
    lbl = utils.shapes_to_label(img.shape, data['shapes'], lbl_names)
    data_origin = []
    for item in data['shapes']:
        if item['label'] == 'origin':
            data_origin.append(item)
    # lbl0, lbl_names0 = utils.labelme_shapes_to_label(img.shape, data_origin)
    lbl_names0 = dict()
    lbl_names0['origin'] = 1
    lbl0 = utils.shapes_to_label(img.shape, data_origin, lbl_names0)

    if 'edge' not in lbl_names.keys() or 'origin' not in lbl_names.keys():
        print("not labeled")
        exit()
    for h in range(0, lbl.shape[0]):
        for w in range(0, lbl.shape[1]):
            if lbl[h, w] == lbl_names['origin'] or lbl0[
                    h, w] == lbl_names0['origin']:
                mask = (lbl == lbl_names['edge']).astype(
                    np.uint8) + (lbl == lbl_names['origin']).astype(np.uint8)
                origin_point = (h, w)
                if side_flipped:
                    mask = np.rot90(mask, 2)
                    origin_point = (img.shape[0] - h, img.shape[1] - w)
                return mask, origin_point
    print("not labeled")
    exit()
Ejemplo n.º 25
0
def json2img(json_file, out, index):
    if out is None:
        out_dir = osp.basename(json_file).replace(".", "_")
        out_dir = osp.join(osp.dirname(json_file), out_dir)
    else:
        out_dir = out
    mkdir(out_dir)
    data = json.load(open(json_file))
    imageData = data.get("imageData")

    if not imageData:
        imagePath = os.path.join(os.path.dirname(json_file), data["imagePath"])
        with open(imagePath, "rb") as f:
            imageData = f.read()
            imageData = base64.b64encode(imageData).decode("utf-8")
    img = utils.img_b64_to_arr(imageData)

    label_name_to_value = {"_background_": 0}
    for shape in sorted(data["shapes"], key=lambda x: x["label"]):
        label_name = shape["label"]
        if label_name in label_name_to_value:
            label_value = label_name_to_value[label_name]
        else:
            label_value = len(label_name_to_value)
            label_name_to_value[label_name] = label_value
    lbl, _ = utils.shapes_to_label(img.shape, data["shapes"],
                                   label_name_to_value)

    label_names = [None] * (max(label_name_to_value.values()) + 1)
    for name, value in label_name_to_value.items():
        label_names[value] = name

    lbl_viz = imgviz.label2rgb(label=lbl,
                               img=imgviz.asgray(img),
                               label_names=label_names,
                               loc="rb")
    img_name = str(index) + '.jpg'
    out_dir_raw = out_dir + '/raw'
    out_dir_label = out_dir + '/label'
    mkdir(out_dir_raw)
    mkdir(out_dir_label)
    PIL.Image.fromarray(img).save(osp.join(out_dir_raw, img_name))  #保存图片 1.jpg
    utils.lblsave(osp.join(out_dir_label, img_name.replace('.jpg', '.png')),
                  lbl)  #保存标签 1.png
    #PIL.Image.fromarray(lbl_viz).save(osp.join(out_dir, "label_viz.png"))

    with open(osp.join(out_dir, "label_names.txt"), "w") as f:
        for lbl_name in label_names:
            f.write(lbl_name + "\n")

    logger.info("Saved to: {}".format(out_dir))
Ejemplo n.º 26
0
def collate_vocdata(json_dir, out_img, out_xml, out_npy):
    names_list = os.listdir(json_dir)
    label_name_to_value = {
        '_background_': 0,
        "slider": 1,
        "fatigue": 2,
        "laminar": 3
    }
    i = 0
    for name in names_list:
        if name.split(".")[-1] == "json":
            i += 1
            print("第%d个json文件,名字是:" % i, name)
            #生成掩码文件
            per_json = os.path.join(json_dir, name)
            data = json.load(open(per_json))
            img = utils.img_b64_to_arr(data["imageData"])
            lbl = utils.shapes_to_label(img.shape, data['shapes'],
                                        label_name_to_value)
            base = "wear_" + name.split(".")[0] + ".npy"
            save_npy = os.path.join(out_npy, base)
            np.save(save_npy, lbl)
            #生成xml文件
            width, height = img.shape[0], img.shape[1]
            filename = "wear_" + name.split(".")[0] + ".xml"
            filename_noext = "wear_" + name.split(".")[0]
            anno = GEN_Annotations(filename_noext, width, height)
            save_xml = os.path.join(out_xml, filename)
            make_xml(data, anno, save_xml)
        elif name.split(".")[-1] == "jpg":
            #将原图copy一份放在指定目录下
            save_img_path = os.path.join(out_img, "wear_" + name)
            per_img = os.path.join(json_dir,
                                   name)  #json_dir is same with img_dir
            os.system("cp " + per_img + " " + save_img_path)
    #将数据分为训练集和测试集
    f_train = open("train.txt", "w")  #将这两个txt最后移动到ImageSet/Main底下
    f_test = open("test.txt", "w")
    names = [
        name.split(".")[0] for name in names_list
        if name.split(".")[-1] == "json"
    ]
    data_length = len(names)
    print("数据的总长度是:%d" % data_length)
    split_point = int(data_length * 0.7)
    train_names = names[:split_point]
    test_names = names[split_point:]
    for trainn in train_names:
        f_train.write("wear_" + trainn + "\n")
    for testn in test_names:
        f_test.write("wear_" + testn + "\n")
Ejemplo n.º 27
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('json_file')
    args = parser.parse_args()

    json_file = args.json_file

    data = json.load(open(json_file))

    if data['imageData']:
        imageData = data['imageData']
    else:
        imagePath = os.path.join(os.path.dirname(json_file), data['imagePath'])
        with open(imagePath, 'rb') as f:
            imageData = f.read()
            imageData = base64.b64encode(imageData).decode('utf-8')

    img = utils.img_b64_to_arr(imageData)

    lbl, label_name_to_value = utils.shapes_to_label(img.shape, data['shapes'])

    lbl = utils.shapes_to_label(
        img.shape,
        shapes=data['shapes'],
        label_name_to_value=label_name_to_value,
    )

    label_names = [None] * (max(label_name_to_value.values()) + 1)
    for name, value in label_name_to_value.items():
        label_names[value] = name
    lbl_viz = utils.draw_label(lbl, img, label_names)

    plt.subplot(121)
    plt.imshow(img)
    plt.subplot(122)
    plt.imshow(lbl_viz)
    plt.show()
Ejemplo n.º 28
0
    def make_label_visualize_image(self, json_file):
        label_file = LabelFile(json_file)
        img = utils.img_data_to_arr(label_file.imageData)

        label_name_to_value = {"_background_": 0}
        for shape in sorted(label_file.shapes, key=lambda x: x["label"]):
            label_name = shape["label"]
            if label_name in label_name_to_value:
                label_value = label_name_to_value[label_name]
            else:
                label_value = len(label_name_to_value)
                label_name_to_value[label_name] = label_value
        lbl, _ = utils.shapes_to_label(
            img.shape, label_file.shapes, label_name_to_value
        )

        label_names = [None] * (max(label_name_to_value.values()) + 1)
        for name, value in label_name_to_value.items():
            label_names[value] = name
        lbl_viz = imgviz.label2rgb(
            label=lbl,
            img=imgviz.asgray(img),
            label_names=label_names,
            font_size=30,
            loc="rb",
        )

        return lbl_viz

        fig = plt.figure()
        ax1 = fig.add_subplot(111)
        ax1.imshow(img)
        ax2 = fig.add_subplot(111)
        ax2.imshow(lbl_viz)

        fig.canvas.draw()

        data = np.fromstring(fig.canvas.tostring_rgb(), dtype=np.uint8, sep='')
        data = data.reshape(fig.canvas.get_width_height()[::-1] + (3,))

        # plt.subplot(121)
        # plt.imshow(img)
        # plt.subplot(122)
        # plt.imshow(lbl_viz)
        # plt.show()
        #
        # plt.draw()

        return data
Ejemplo n.º 29
0
def main():
    warnings.warn("This script is aimed to demonstrate how to convert the\n"
                  "JSON file to a single image dataset, and not to handle\n"
                  "multiple JSON files to generate a real-use dataset.")
    parser = argparse.ArgumentParser()
    parser.add_argument('json_file')
    parser.add_argument('-o', '--out', default=None)
    args = parser.parse_args()
    json_file = args.json_file
    print("json_flie= ",json_file)
    if args.out is None:
        out_dir = osp.basename(json_file).replace('.', '_')
        out_dir = osp.join(osp.dirname(json_file), out_dir)
    else:
        out_dir = args.out
    if not osp.exists(out_dir):
        os.mkdir(out_dir)
    data = json.load(open(json_file))
    print("json---load")
    img = utils.img_b64_to_arr(data['imageData'])
    label_name_to_value = {'_background_': 0}
    for shape in data['shapes']:
        label_name = shape['label']
        if label_name in label_name_to_value:
            label_value = label_name_to_value[label_name]
        else:
            label_value = len(label_name_to_value)
            label_name_to_value[label_name] = label_value
    # label_values must be dense
    label_values, label_names = [], []
    for ln, lv in sorted(label_name_to_value.items(), key=lambda x: x[1]):
        label_values.append(lv)
        label_names.append(ln)
    assert label_values == list(range(len(label_values)))
    lbl = utils.shapes_to_label(img.shape, data['shapes'], label_name_to_value)
    captions = ['{}: {}'.format(lv, ln)
                for ln, lv in label_name_to_value.items()]
    lbl_viz = utils.draw_label(lbl, img, captions)
    PIL.Image.fromarray(img).save(osp.join(out_dir, 'img.png'))
    PIL.Image.fromarray(lbl).save(osp.join(out_dir, 'label.png'))
    PIL.Image.fromarray(lbl_viz).save(osp.join(out_dir, 'label_viz.png'))
    with open(osp.join(out_dir, 'label_names.txt'), 'w') as f:
        for lbl_name in label_names:
            f.write(lbl_name + '\n')
    warnings.warn('info.yaml is being replaced by label_names.txt')
    info = dict(label_names=label_names)
    with open(osp.join(out_dir, 'info.yaml'), 'w') as f:
        yaml.safe_dump(info, f, default_flow_style=False)
    print('Saved to: %s' % out_dir)
Ejemplo n.º 30
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('json_file')
    args = parser.parse_args()

    json_file = args.json_file

    data = json.load(open(json_file))

    if data['imageData']:
        imageData = data['imageData']
    else:
        imagePath = os.path.join(os.path.dirname(json_file), data['imagePath'])
        with open(imagePath, 'rb') as f:
            imageData = f.read()
            imageData = base64.b64encode(imageData).decode('utf-8')

    img = utils.img_b64_to_arr(imageData)

    lbl, label_name_to_value = utils.shapes_to_label(img.shape, data['shapes'])

    lbl = utils.shapes_to_label(
        img.shape, shapes=data['shapes'],
        label_name_to_value=label_name_to_value,
    )

    label_names = [None] * (max(label_name_to_value.values()) + 1)
    for name, value in label_name_to_value.items():
        label_names[value] = name
    lbl_viz = utils.draw_label(lbl, img, label_names)

    plt.subplot(121)
    plt.imshow(img)
    plt.subplot(122)
    plt.imshow(lbl_viz)
    plt.show()
Ejemplo n.º 31
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('json_file')
    args = parser.parse_args()

    json_file = args.json_file

    data = json.load(open(json_file))

    if data['imageData']:
        imageData = data['imageData']
    else:
        imagePath = os.path.join(os.path.dirname(json_file), data['imagePath'])
        with open(imagePath, 'rb') as f:
            imageData = f.read()
            imageData = base64.b64encode(imageData).decode('utf-8')
    img = utils.img_b64_to_arr(imageData)

    label_name_to_value = {'_background_': 0}
    for shape in sorted(data['shapes'], key=lambda x: x['label']):
        label_name = shape['label']
        if label_name in label_name_to_value:
            label_value = label_name_to_value[label_name]
        else:
            label_value = len(label_name_to_value)
            label_name_to_value[label_name] = label_value
    lbl, _ = utils.shapes_to_label(
        img.shape, data['shapes'], label_name_to_value
    )

    label_names = [None] * (max(label_name_to_value.values()) + 1)
    for name, value in label_name_to_value.items():
        label_names[value] = name
    lbl_viz = imgviz.label2rgb(
        label=lbl,
        img=imgviz.rgb2gray(img),
        label_names=label_names,
        font_size=30,
        loc='rb',
    )

    plt.subplot(121)
    plt.imshow(img)
    plt.subplot(122)
    plt.imshow(lbl_viz)
    plt.show()
Ejemplo n.º 32
0
def load_label_names(data, img):
    label_name_to_value = {"_background_": 0}
    for shape in sorted(data["shapes"], key=lambda x: x["label"]):
        label_name = shape["label"]
        if label_name in label_name_to_value:
            label_value = label_name_to_value[label_name]
        else:
            label_value = len(label_name_to_value)
            label_name_to_value[label_name] = label_value
    lbl, _ = utils.shapes_to_label(img.shape, data["shapes"],
                                   label_name_to_value)

    label_names = [None] * (max(label_name_to_value.values()) + 1)
    for name, value in label_name_to_value.items():
        label_names[value] = name

    return label_names
Ejemplo n.º 33
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('json_file')
    args = parser.parse_args()

    json_file = args.json_file

    data = json.load(open(json_file))

    if data['imageData']:
        imageData = data['imageData']
    else:
        imagePath = os.path.join(os.path.dirname(json_file), data['imagePath'])
        with open(imagePath, 'rb') as f:
            imageData = f.read()
            imageData = base64.b64encode(imageData).decode('utf-8')

    img = utils.img_b64_to_arr(imageData)

    label_name_to_value = {'_background_': 0}
    for shape in data['shapes']:
        label_name = shape['label']
        if label_name in label_name_to_value:
            label_value = label_name_to_value[label_name]
        else:
            label_value = len(label_name_to_value)
            label_name_to_value[label_name] = label_value

    lbl = utils.shapes_to_label(
        img.shape, data['shapes'], label_name_to_value)

    captions = ['{}: {}'.format(lv, ln)
                for ln, lv in label_name_to_value.items()]
    lbl_viz = utils.draw_label(lbl, img, captions)

    plt.subplot(121)
    plt.imshow(img)
    plt.subplot(122)
    plt.imshow(lbl_viz)
    plt.show()
Ejemplo n.º 34
0
def main():
    warnings.warn("This script is aimed to demonstrate how to convert the\n"
                  "JSON file to a single image dataset, and not to handle\n"
                  "multiple JSON files to generate a real-use dataset.")

    parser = argparse.ArgumentParser()
    parser.add_argument('json_file')
    parser.add_argument('-o', '--out', default=None)
    args = parser.parse_args()

    json_file = args.json_file

    if args.out is None:
        out_dir = osp.basename(json_file).replace('.', '_')
        out_dir = osp.join(osp.dirname(json_file), out_dir)
    else:
        out_dir = args.out
    if not osp.exists(out_dir):
        os.mkdir(out_dir)

    data = json.load(open(json_file))

    if data['imageData']:
        imageData = data['imageData']
    else:
        imagePath = os.path.join(os.path.dirname(json_file), data['imagePath'])
        with open(imagePath, 'rb') as f:
            imageData = f.read()
            imageData = base64.b64encode(imageData).decode('utf-8')

    img = utils.img_b64_to_arr(imageData)

    label_name_to_value = {'_background_': 0}
    for shape in data['shapes']:
        label_name = shape['label']
        if label_name in label_name_to_value:
            label_value = label_name_to_value[label_name]
        else:
            label_value = len(label_name_to_value)
            label_name_to_value[label_name] = label_value

    # label_values must be dense
    label_values, label_names = [], []
    for ln, lv in sorted(label_name_to_value.items(), key=lambda x: x[1]):
        label_values.append(lv)
        label_names.append(ln)
    assert label_values == list(range(len(label_values)))

    lbl = utils.shapes_to_label(img.shape, data['shapes'], label_name_to_value)

    captions = ['{}: {}'.format(lv, ln)
                for ln, lv in label_name_to_value.items()]
    lbl_viz = utils.draw_label(lbl, img, captions)

    PIL.Image.fromarray(img).save(osp.join(out_dir, 'img.png'))
    PIL.Image.fromarray(lbl).save(osp.join(out_dir, 'label.png'))
    PIL.Image.fromarray(lbl_viz).save(osp.join(out_dir, 'label_viz.png'))

    with open(osp.join(out_dir, 'label_names.txt'), 'w') as f:
        for lbl_name in label_names:
            f.write(lbl_name + '\n')

    warnings.warn('info.yaml is being replaced by label_names.txt')
    info = dict(label_names=label_names)
    with open(osp.join(out_dir, 'info.yaml'), 'w') as f:
        yaml.safe_dump(info, f, default_flow_style=False)

    print('Saved to: %s' % out_dir)