功能:
root
Male Female
glass noglass glass noglass
young old young old young old young old
......... ....... ........ ....... ...... ...... .......... ......... ........
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2018/11/27 9:04
# @Author :
# @Site :
# @File : gen_img_label_file.py
# @Software: PyCharm
from collections import Counter
import os
import re
src_folder = './root'
def getCntOfSplits(path):
c = Counter(path)
cnt_split_path = c['/']+c['\\']
# if path.startswith(('/','\\')):
# cnt_split_path -= 1
if src_folder.endswith(('/','\\')):
cnt_split_path -= 1
return cnt_split_path
'''
根据目录结构返回每层目录集合
如: A
B C
D E F H I
J K L M N
返回[[A],[B,C],[D,E,F,H,I],[J,K,L,M,N]]
'''
def getLabels(path):
labels = []
cnt_split_src_folder = getCntOfSplits(path)
for root, dirs, files in os.walk(path):
cur_idx = 0
if len(dirs):
cur_idx = getCntOfSplits(root) - cnt_split_src_folder + 1
if cur_idx > len(labels):
labels.append([])
for dir in dirs:
labels[cur_idx - 1].append(dir)
labels = list(map(set, labels))
labels = list(map(sorted, labels))
return labels
'''
根据类别名称生成对应字典,如male为0,female为1
输入为getLabels函数的返回值
'''
def genLabelID(labels):
labels_list = []
for label in labels:
id = 0
d ={}
for l in label:
d[l]=id
id +=1
labels_list.append(d)
return labels_list
'''
生成txt文件
第一行为对应类别(id为0),如male为0,female为1,glass为0,no glass为1
则第一行为 male glass .......
第二行开始格式: filename 0 1 .......
'''
def genLabelFile(path,labels_dict):
with open("imglist.txt","w") as f:
for d in labels_dict:
for k in d:
f.write(' ' + k)
break
f.write("\n")
n_labels = len(labels_dict)
cnt_split_src_folder = getCntOfSplits(path)+1
for root, dirs, files in os.walk(path):
for file in files:
filepath = os.path.join(root,file)
labels = [x for x in re.split(r'[/\\]',filepath) if x][cnt_split_src_folder:]
res = [filepath]
for i in range(len(labels)-1):
if labels[i] in labels_dict[i]:
res.append(labels_dict[i][labels[i]])
for i in range(len(res),n_labels+1):
res.append("null")
res = " ".join(map(str,res))
f.write(res+"\n")
d = genLabelID(getLabels(src_folder))
genLabelFile(src_folder,d)