1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

json形式のデータをtxtに直す(yolo使用)

Posted at

yoloを使うためにjson形式のデータをtxt形式のデータに直したい。
#やったこと
jsonをtxtに直すコードはネットに落ちていたが、いまいち使い方がわからなかったので自分で作成することに。
(いろんなコードの一部を切り貼りしてるので見たことあるかも)
#以下コード

import json
import glob

data_path = glob.glob("train_json/*.json")
img_data_path = glob.glob("train_img/*.jpg")
cate_list = ["Car","Pedestrian","Truck","Signal","Signs","Bicycle","Motorbike","Bus","Svehicle","Train"]
box_list  = ["x1","x2","y1","y2"]
input_shape = [640,640]
img_output = "train_img_data"

with open("voc_classes.txt","w") as f:
    f.write('\n'.join(cate_list))

list_file = open("2007_train.txt","w")

def class_encord(class_name):
    cate_id = {"Car":0,"Pedestrian":1,"Truck":2,"Signal":3,"Signs":4,"Bicycle":5,"Motorbike":6,"Bus":7,"SVehicle":8,"Train":9}
    return cate_id[class_name]

def img_box_resize(img_path,input_shape,label_box):
    img = Image.open(img_path)
    iw , ih = img.size
    w, h = input_shape
    scale = min(w/iw,h/ih)
    nw = int(scale*iw)
    nh = int(scale*ih)
    dx = (w-nw)//2
    dy = (h-nh)//2
    img_data = 0 
    image = img.resize((nw,nh),Image.BICUBIC)
    new_image = Image.new("RGB", (w,h), (128,128,128))
    new_image.paste(image, (dx, dy))    
    bax_lx = label_box[0]
    box_ly = label_box[1]
    box_rx = label_box[2]
    box_ry = label_box[3]
    
    new_box_lx = int(box_lx*scale) + int(dx)
    new_box_ly = int(box_ly*scale) + int(dy)
    new_box_rx = int(box_rx*scale) - int(dx)
    new_box_ry = int(box_ry*scale) - int(dy)
    box_list = []
    box_list.append(new_box_lx)
    box_list.append(new_box_ly)
    box_list.append(new_box_rx)
    box_list.append(new_box_ry)
    return new_image , box_list

def convert(data_file,list_file,input_img_path,output_img_path):
    with open(data_file) as j_f:
        load_json = json.load(j_f)
        datas = load_json["labels"]  
        for data in datas:
            box = data["box2d"]
            json_box = []  
            new_img , box_list = img_box_resize(img_path,input_shape,box)
            for i in range(4):
                json_box.append(box_list[i])
            category = data["category"]
            category_num = class_encord(category)
            list_file.write(" "+",".join([str(a) for a in json_box])+","+str(category_num))
            new_img.save(output_img_path+"/"+input_img_path)
            

for data_file in data_path:
    for img_path in img_data_path:
        data = data_file
        list_file.write(output_img_path+"/"+input_img_path)
        convert(data_file,list_file,img_path,img_output)
        list_file.write("\n")

list_file.close()
  • data_path
  • imge_data_path
  • cate_list
  • box_list
  • input_shape
  • img_output
  • openの後のファイルpath
  • cate_id

以上の変数を使う環境に合わせて変えてくださいinput_shapeの大きさに合わせて変換されます。

動くはず:sweat_smile:(僕はこれで動いた)

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?