Mask R-CNN資料集資訊提取
阿新 • • 發佈:2019-01-31
開始之前,向何凱明大帝致敬
我等後輩虛心向大神學習
復現參考:
剛開始復現Mask R-CNN,這裡只分享資料集從被labelme標記後的json檔案批量化資訊提取
- test_images:是訓練集圖片,通過labelme對資料進行做標籤得到的.json都放於json資料夾
- json : json 資料夾
- json_out: 用labelme中的檔案(json_to_dataset.py)處理json資料夾中檔案所得. 所在位置:anaconda3/lib/python3.6/site-packages/labelme/cli,自帶的檔案真能一次處理一個.json檔案,面對大量的資料是無力的,這裡做了一些修改可以批量處理資料夾裡的.json資料,並輸出到json_out 資料夾。
- CV_mask:因為opencv預設讀取的是8點陣圖片,而上面所需要的label.png是16位的 ,需要進行轉換,並存放於CV_mask資料夾,這裡寫了一個小程式實現批量處理。
程式碼如下:
json_to_dataset.py , 執行方式:進入anaconda3/lib/python3.6/site-packages/labelme/cli,執行
兩個位置引數 1.json檔案所在資料夾 2.輸出資料夾
python json_to_dataset.py /home/cv/圖片/json /home/cv/圖片/json_out
import argparse import json import os import os.path as osp import warnings import numpy as np import PIL.Image import yaml from labelme import utils def main(): parser = argparse.ArgumentParser() parser.add_argument('json_dir') parser.add_argument('json_out') args = parser.parse_args() json_dir = args.json_dir # json檔案所在資料夾 out_path = args.json_out # 返回資料夾 list = os.listdir(json_dir) for i in range(0, len(list)): json_file = os.path.join(json_dir, list[i]) if os.path.isfile(json_file): # ================================================= out_dir = osp.basename(json_file).replace('.', '_') out_dir = osp.join(out_path, out_dir) if not osp.exists(out_dir): os.makedirs(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') 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_path) if __name__ == '__main__': main()
toeight.py
from PIL import Image import numpy as np import math import os path = '/home/cv/圖片/json_out/' out_dir = '/home/cv/圖片/CV_mask/' if not os.path.exists(out_dir): os.makedirs(out_dir) def main(): paths = [] # 所有label.png路徑 for root, dirs, files in os.walk(path, topdown=True): if dirs: dirl = dirs # 資料夾名稱,用來命名新8位照片的名字用 if files: for i in files: if i == "label.png": path_singe = os.path.join(root,i) paths.append(path_singe) i = 0 for file in paths: img = Image.open(file) # 開啟圖片img = Image.open(dir)#開啟圖片 img = np.array(img) # img = Image.fromarray(np.uint8(img / float(math.pow(2, 16) - 1) * 255)) img = Image.fromarray(np.uint8(img)) imgname = dirl[i].replace("_json",".png") img.save(out_dir + imgname) i +=1 main()
- 需要自行修改合適的路徑
再一次致敬何凱明,致敬各位大佬,程式碼寫的不夠好,小白會繼續努力,虛心向各位大佬學習