人脸属性,递归目录自动打标签

功能:

                                                                       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)

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值