def _check_image_height_and_width(imageData, imageHeight, imageWidth): img_arr = utils.img_b64_to_arr(imageData) if imageHeight is not None and img_arr.shape[0] != imageHeight: imageHeight = img_arr.shape[0] if imageWidth is not None and img_arr.shape[1] != imageWidth: imageWidth = img_arr.shape[1] return imageHeight, imageWidth
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!')
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))
def extract_heatmaps_info(json_data, label_name_to_value=LABEL_NAME_TO_VALUE): """ Given labelme json data, construct a heatmaps info :param json_data: dict, labelme data from a json file :param label_name_to_value: dict :return: dict, heatmaps info """ img = img_b64_to_arr(json_data['imageData']) info = { 'img_height': img.shape[0], 'img_width': img.shape[1], 'cwh_list': [], } for shape in json_data['shapes']: lbl = shape['label'] if lbl in label_name_to_value: points = shape['points'] if len(points) > 3: c_x, c_y, w, h = points_to_cwh(points) info['cwh_list'].append({ 'label': lbl, 'center': (c_x, c_y), 'width': w, 'height': h, }) return info
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)) # 加载json文件 img = utils.img_b64_to_arr(data['imageData']) # 解析原图片数据 dense(img, data['shapes']) #src=cv2.imread('json/bird.jpg') #cv2.imshow('input_image',img) #lbl, lbl_names = utils.labelme_shapes_to_label(img.shape, data['shapes']) # 解析'shapes'中的字段信息,解析出每个对象的mask与对应的label lbl存储 mask,lbl_names 存储对应的label # lal 像素取值 0、1、2 其中0对应背景,1对应第一个对象,2对应第二个对象 # 使用该方法取出每个对象的mask mask=[] mask.append((lbl==1).astype(np.uint8)) # 解析出像素值为1的对象,对应第一个对象 mask 为0、1组成的(0为背景,1为对象) # lbl_names ['background','cat_1','cat_2'] #captions = ['%d: %s' % (l, name) for l, name in enumerate(lbl_names)] #lbl_viz = utils.draw_label(lbl, img, captions) a = 1 cv2.waitKey(0) cv2.destroyAllWindows()
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, }
def main(): parser = argparse.ArgumentParser() parser.add_argument('json_file') parser.add_argument('-o', '--out', default=None) args = parser.parse_args() json_file = args.json_file list = os.listdir(json_file) for i in range(0, len(list)): path = os.path.join(json_file, list[i]) if os.path.isfile(path): data = json.load(open(path)) img = utils.img_b64_to_arr(data['imageData']) lbl, lbl_names = utils.labelme_shapes_to_label( img.shape, data['shapes']) captions = [ '%d: %s' % (l, name) for l, name in enumerate(lbl_names) ] lbl_viz = utils.draw_label(lbl, img, captions) out_dir = osp.basename(list[i]).replace('.', '_') out_dir = osp.join(osp.dirname(list[i]), 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 lbl_names: f.write(lbl_name + '\n') warnings.warn('info.yaml is being replaced by label_names.txt') info = dict(label_names=lbl_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)
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')
def singleFile(filePath): if os.path.isfile(filePath) and filePath.endswith('.json'): data = json.load(open(filePath)) img = lUtils.img_b64_to_arr(data['imageData']) lbl, lbl_names = lUtils.labelme_shapes_to_label( img.shape, data['shapes']) # lbl[lbl>0] = 255 # print(lbl) captions = ['%d: %s' % (l, name) for l, name in enumerate(lbl_names)] lbl_viz = lUtils.draw_label(lbl, img, captions) out_dir = osp.basename(filePath).replace('.', '_') out_dir = osp.join(osp.dirname(filePath), out_dir) if not osp.exists(out_dir): os.mkdir(out_dir) PIL.Image.fromarray(img).save(osp.join(out_dir, 'img.png')) PIL.Image.fromarray(lbl).save(osp.join(out_dir, 'label.png')) print(np.max(lbl)) lbl = np.array(lbl, dtype=np.uint8) lbl[lbl > 0] = 255 PIL.Image.fromarray(lbl).save(osp.join(out_dir, 'label_255.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 lbl_names: f.write(lbl_name + '\n') warnings.warn('info.yaml is being replaced by label_names.txt') info = dict(label_names=lbl_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)
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))
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)) # 加载json文件 img = utils.img_b64_to_arr(data['imageData']) # 解析原图片数据 lbl, lbl_names = utils.labelme_shapes_to_label( img.shape, data['shapes'] ) # 解析'shapes'中的字段信息,解析出每个对象的mask与对应的label lbl存储 mask,lbl_names 存储对应的label # lal 像素取值 0、1、2 其中0对应背景,1对应第一个对象,2对应第二个对象 # 使用该方法取出每个对象的mask mask=[] mask.append((lbl==1).astype(np.uint8)) # 解析出像素值为1的对象,对应第一个对象 mask 为0、1组成的(0为背景,1为对象) # lbl_names ['background','cat_1','cat_2'] captions = ['%d: %s' % (l, name) for l, name in enumerate(lbl_names)] lbl_viz = utils.draw_label(lbl, img, captions) #画左边图 plt.subplot(121) plt.imshow(img) #画右边的对比图 plt.subplot(122) plt.imshow(lbl_viz) plt.show()
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
def main(): ''' batch convert json to signle channel labels src_path: the file path for json file target_path: the file path for labels out format: python batch_json_to_dataset.py [src_path] [target_path] ''' parser = argparse.ArgumentParser() parser.add_argument('src_path') parser.add_argument('target_path') args = parser.parse_args() src_path = args.src_path target_path = args.target_path pattern = '*.json' for jpath in gb.glob(src_path + "/" + pattern): json_name = jpath.split('/')[-1].split('.')[0] data = json.load(open(jpath)) img = utils.img_b64_to_arr(data['imageData']) lbl, lbl_names = our_labelme_shapes_to_label(img.shape, data['shapes']) print(json_name.split('\\')[1]) print(osp.join(target_path, json_name + '.png')) PIL.Image.fromarray(lbl).save( osp.join(target_path, json_name.split('\\')[1] + '.png')) print('done...')
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()
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!')
def load_json(path): '''for labelme ''' def flat(lists): result = [] for listx in lists: result += listx return result with open(path, 'rb') as f: data = json.load(f) img = utils.img_b64_to_arr(data['imageData']) # mask, labels = utils.labelme_shapes_to_label(img.shape, data['shapes']) # print(mask.shape, labels) # masks = [(mask == i).astype(np.uint8) for i in range(len(labels))] # result = utils.draw_label(mask, img, labels, colormap=None) # Image.fromarray(result).show() blob = { 'imagePath': data['imagePath'], 'imageData': Image.fromarray(img), 'labels': [], 'points': [] } for info in data['shapes']: blob['labels'] += [info['label']] blob['points'] += [flat(info['points'])] assert len(blob['labels']) == len(blob['points']), '' return blob
def _image(self, obj, path): image = {} img_x = utils.img_b64_to_arr(obj['imageData']) image["height"] = img_x.shape[0] image["width"] = img_x.shape[1] image['id'] = self.img_id image['file_name'] = os.path.basename(path).replace(".json", ".jpg") return image
def json_generation(self): # image_names = [str(self.id)+'_flip_x', str(self.id)+'_flip_y', str(self.id)+'_flip_x_y'] image_names = [str(self.id) + '_flip_x_y'] if self.gaussianBlur: image_names.append(str(self.id) + '_Gaussian') # image_names.append(str(self.id)+'_flip_x'+'_Gaussian') # image_names.append(str(self.id)+'_flip_y' + '_Gaussian') image_names.append(str(self.id) + '_flip_x_y' + '_Gaussian') if self.changeExposure: image_names.append(str(self.id) + '_ReduceEp') # image_names.append(str(self.id)+'_flip_x'+'_ReduceEp') # image_names.append(str(self.id)+'_flip_y'+'_ReduceEp') image_names.append(str(self.id) + '_flip_x_y' + '_ReduceEp') image_names.append(str(self.id) + '_IncreaseEp') # image_names.append(str(self.id)+'_flip_x'+'_IncreaseEp') # image_names.append(str(self.id)+'_flip_y'+'_IncreaseEp') image_names.append(str(self.id) + '_flip_x_y' + '_IncreaseEp') if self.add_saltNoise: image_names.append(str(self.id) + '_Salt') # image_names.append(str(self.id)+'_flip_x' + '_Salt') # image_names.append(str(self.id)+'_flip_y' + '_Salt') image_names.append(str(self.id) + '_flip_x_y' + '_Salt') for image_name in image_names: with open(image_name + ".jpg", "rb") as b64: base64_data_original = str(base64.b64encode(b64.read())) # In pycharm: # match_pattern=re.compile(r'b\'(.*)\'') # base64_data=match_pattern.match(base64_data_original).group(1) # In terminal: base64_data = base64_data_original with open(str(self.id) + ".json", 'r') as js: json_data = json.load(js) img = utils.img_b64_to_arr(json_data['imageData']) height, width = img.shape[:2] shapes = json_data['shapes'] for shape in shapes: points = shape['points'] for point in points: # match_pattern2 = re.compile(r'(.*)_x(.*)') # match_pattern3 = re.compile(r'(.*)_y(.*)') match_pattern4 = re.compile(r'(.*)_x_y(.*)') if match_pattern4.match(image_name): point[0] = width - point[0] point[1] = height - point[1] # elif match_pattern3.match(image_name): # point[0] = width - point[0] # point[1] = point[1] # elif match_pattern2.match(image_name): # point[0] = point[0] # point[1] = height - point[1] else: point[0] = point[0] point[1] = point[1] json_data['imagePath'] = image_name + ".jpg" json_data['imageData'] = None json.dump(json_data, open("./" + image_name + ".json", 'w'), indent=2)
def getJsonInfo(self,file_path): print(file_path) with open(file_path,'r') as f: data = json.load(f) img = utils.img_b64_to_arr(data['imageData']) for shapes in data['shapes']: shapes['label'] = 'chromosome' with open(file_path,'w') as f: json.dump(data,f)
def _image(self, obj, path): image = {} img_x = utils.img_b64_to_arr(obj['imageData']) h, w = img_x.shape[:-1] image['height'] = h image['width'] = w image['id'] = self.img_id image['file_name'] = os.path.basename(path).replace(".json", ".jpg") return image
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)
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)
def _image(self, obj, path): image = OrderedDict() from labelme import utils img_x = utils.img_b64_to_arr(obj['imageData']) h, w = img_x.shape[:-1] image['file_name'] = os.path.basename(path).replace(".json", ".BMP") image['height'] = h image['width'] = w image['id'] = self.img_id return image
def _image(self, obj, path): image = {} from labelme import utils # utils.img_b64_to_arr读取文件很耗时,若图片大小都一致,可以固定h,w img_x = utils.img_b64_to_arr(obj['imageData']) h, w = img_x.shape[:-1] image['height'] = h image['width'] = w image['id'] = self.img_id image['file_name'] = os.path.basename(path).replace(".json", ".jpg") return image
def main(): json_file = '/home/liuenli/Mask_RCNN-master--TrainOwerDatset-master/assets/total' list = os.listdir(json_file) for i in range(0, len(list)): path = os.path.join(json_file, list[i]) if os.path.isfile(path): data = json.load(open(path)) img = utils.img_b64_to_arr(data['imageData']) lbl, lbl_names = utils.labelme_shapes_to_label(img.shape, data['shapes']) captions = ['%d: %s' % (l, name) for l, name in enumerate(lbl_names)] lbl_viz = utils.draw_label(lbl, img, captions) out_dir = osp.basename(list[i]).replace('.', '_') out_dir = osp.join(osp.dirname(list[i]), out_dir) out_dir=json_file+"/"+out_dir if not osp.exists(out_dir): os.mkdir(out_dir) PIL.Image.fromarray(img).save(osp.join(out_dir, 'img.png')) # PIL.Image.fromarray(lbl).save() labelpath = osp.join(out_dir, 'label.png') # PIL.Image.fromarray(lbl).save(labelpath) # opencvimg16 = cv2.imread(labelpath) # opencvimg.convertTo(opencvimg6,) lbl8u=np.zeros((lbl.shape[0],lbl.shape[1]),dtype=np.uint8) for i in range(lbl.shape[0]): for j in range(lbl.shape[1]): lbl8u[i,j]=lbl[i,j] PIL.Image.fromarray(lbl8u).save(labelpath); # Alllabelpath="%s" 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 lbl_names: f.write(lbl_name + '\n') warnings.warn('info.yaml is being replaced by label_names.txt') info = dict(label_names=lbl_names) with open(osp.join(out_dir, 'info.yaml'), 'w') as f: yaml.dump(info, f, default_flow_style=False) fov=open(osp.join(out_dir,'info.yaml'),'w') for key in info: fov.writelines(key) fov.write(':\n') for k,v in lbl_names.items(): fov.write(' ') fov.write(k) fov.write(':\n') fov.close() print('Saved to: %s' % out_dir)
def save_image_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) PIL.Image.fromarray(img).save(osp.join(out_dir, 'image.png'))
def _image(self, obj, path): image = {} from labelme import utils img_x = utils.img_b64_to_arr(obj['imageData']) h, w = img_x.shape[:-1] image['height'] = h image['width'] = w self.img_id = int(os.path.basename(path).split(".json")[0]) image['id'] = self.img_id # image['id'] = self.img_id image['file_name'] = os.path.basename(path).replace(".json", ".jpg") return image
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 _check_image_height_and_width(imageData, imageHeight, imageWidth): img_arr = utils.img_b64_to_arr(imageData) if imageHeight is not None and img_arr.shape[0] != imageHeight: logger.error( 'imageHeight does not match with imageData or imagePath, ' 'so getting imageHeight from actual image.') imageHeight = img_arr.shape[0] if imageWidth is not None and img_arr.shape[1] != imageWidth: logger.error( 'imageWidth does not match with imageData or imagePath, ' 'so getting imageWidth from actual image.') imageWidth = img_arr.shape[1] return imageHeight, imageWidth
def folder_files_to_list(path): print(path) files = [path + f for f in listdir(path) if isfile(join(path, f))] x = [] y = [] labels_count = [] for file in files: with open(file) as json_file: data = json.load(json_file) x.append(utils.img_b64_to_arr(data['imageData'])) y.append(data['shapes'][0]['label']) labels_count.append(len(data['shapes'])) return x, y, labels_count
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()
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 = utils.draw_label(lbl, img, label_names) plt.subplot(121) plt.imshow(img) plt.subplot(122) plt.imshow(lbl_viz) plt.show()
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)