对json文件解析以及解析后进行分类代码

#USAGE  python classify.py --json ./234-5.json 
#--catagor_num 3 
#--old_image_path ./fundus/ 
#--new_image_path ./Category/
#detailed category needed modify

import json
import argparse


ap = argparse.ArgumentParser()
ap.add_argument("-t", "--json", required=True,
                help="path to the training images")
ap.add_argument("-e", "--catagor_num", required=True, 
                help="the number of catagory")
ap.add_argument("-o", "--old_image_path", required=True,
                help="path to the old image path")
ap.add_argument("-n", "--new_image_path", required=True, 
                help="path to the new images path")
args = vars(ap.parse_args())

print(args["json"])
#catagor_num = 3
#data = open("./234-5.json")
data = open(args["json"])
catagor_num = int(args["catagor_num"])
                
data2 = json.load(data)
data_an = data2['annotations']

img_ca = []
img_ca.append([])
img_ca.append([])
for i in range(len(data_an)):
    img_ca[0].append(data_an[i]["image_id"]) 
    img_ca[1].append(data_an[i]["category_id"])

data_id_name = data2['categories']

img_id_name = []
img_id_name.append([])
img_id_name.append([])
for i in range(catagor_num):
    img_id_name[0].append(data_id_name[i]["id"]) 
    img_id_name[1].append(data_id_name[i]["name"])

data_im = data2["images"]

img_name = []
img_name.append([])
img_name.append([])

for i in range(len(data_im)):
    img_name[0].append(data_im[i]["id"]) 
    img_name[1].append(data_im[i]["file_name"])

for i in range(len(data_an)):
    for j in range(catagor_num):
        if(img_ca[1][i] == img_id_name[0][j]):
            img_ca[1][i] = img_id_name[1][j]

for i in range(len(data_an)):
    for j in range(len(data_im)):
        if(img_ca[0][i] == img_name[0][j]):
            img_ca[0][i] = img_name[1][j]

print(img_ca)


img_name = img_ca[0]
img_label = img_ca[1]


import sys
import os
import shutil

def mkdir(path):
    folder = os.path.exists(path)
    if not folder:  # 判断是否存在文件夹如果不存在则创建为文件夹
        os.makedirs(path)  # makedirs 创建文件时如果路径不存在会创建这个路径
        print("---  new folder...  ---")
        print("---  OK  ---")
    else:
        print("---  There is this folder!  ---")

print(img_name,img_label)


old_image_path = args["old_image_path"]
new_image_path = args["new_image_path"]

######################################### need modify 
for i in range(catagor_num):
    if i==0:
        mkdir(new_image_path+"normal")
        category_normal = new_image_path+'normal'
    if i==1:
        mkdir(new_image_path+"abnormal")
        category_abnormal = new_image_path+'abnormal'
    if i==2:
        mkdir(new_image_path+"medium")
        category_medium = new_image_path+'medium'

for i in range(len(img_label)):
    if(img_label[i]=="normal"):
        shutil.copy(old_image_path+img_name[i],  category_normal+img_name[i])
    if(img_label[i]=="abnormal"):
        shutil.copy(old_image_path+img_name[i],  category_abnormal+img_name[i])
    if(img_label[i]=="medium"):
        shutil.copy(old_image_path+img_name[i],  category_medium+img_name[i])

你可能感兴趣的:(code,教程)