VoTTとはMSが作った画像系機械学習に使うラベリングをGUIで行えるツール
これのおかげでくっそダルくて発狂しそうなラベル付作業が少しだけ楽になる。神ツール
が、当然マイクロソフト謹製の機械学習用アプリケーション(CNTK)ように作られているので、ライバルのAWSのSagemakerの仕様なんか知ったこっちゃない。だけどどうしてもsagemakerで使いたかったのでCNTK用のJSONをSageMaker用に変換するスクリプトを作った
環境
- Python 3.x
コード
import json vott_json_path = "images.json" images_path = "" class_list = {'cat': 0, 'dog': 1, 'bird': 2} images = [] import os # 画像ファイル名の配列作成 for item in os.listdir(images_path): base, ext = os.path.splitext(item) if ext.lower() == '.jpg': images.append(item) # 1つのJSONを1つずつに分割していく with open(vott_json_path) as f: obj = json.load(f) for index, v in obj['frames'].items(): item = {} item['file'] = images[int(index)] item['image_size'] = [{ 'width':int(v[0]['width']), 'height':int(v[0]['height']), 'depth':3 }] item['annotations'] = [] for annotation in v: item['annotations'].append( { 'class_id': class_list[annotation['tags'][0]], 'top': int(annotation['y1']), 'left': int(annotation['x1']), 'width': int(annotation['x2'])-int(annotation['x1']), 'height': int(annotation['y2']-int(annotation['y1'])) } ) item['categories'] = [] for name, class_id in class_list.items(): item['categories'].append( { 'class_id':class_id, 'name':name } ) print(item) # 書き込み os.makedirs(os.path.join("jsons"), exist_ok=True) basename = os.path.splitext(images[int(index)])[0] with open("jsons/{}.json".format(basename), mode='w') as f: json.dump(item, f)
必ず変更が必要なのは以下