从celeba 筛选 Male 属性, 3步完成(window与ubuntu 适用)

从 官网提供 的链接中,获取到list_attr_celeba 文件.

step: 1

新建 main.py, 并输入如下代码 ,(注意main.py与 list_attr_celeba 同级)

f = open("list_attr_celeba.txt")
newTxt = "Male.txt"
newf = open(newTxt, "a+")
newNoTxt = "FeMale.txt"
newNof = open(newNoTxt, "a+")

line = f.readline() 
line = f.readline() 
line = f.readline() 
while line:
    array = line.split()
    if (array[0] == "000011.jpg"): print(array[21])

    if (array[21] == "-1"): 
        new_context = array[0] + '\n'
        newNof.write(new_context)
    else: 
        new_context = array[0] + '\n'
        newf.write(new_context)
    line = f.readline()

lines = len(newf.readlines()) 
print ("There are %d lines in %s" % (lines, newTxt)) 
lines = len(newNof.readlines()) 
print ("There are %d lines in %s" % (lines, newNoTxt))

f.close()
newf.close()
newNof.close()

step 2:

运行 python main.py  得到了  FeMale.txt  以及 Male.txt   两个文件.

 

------------特别说明---------------------------

代码第  12 行:  print(array[21])

对于为什么是打印这个21,详细请看我的另一博客 celebA数据集 40 属性介绍

--------------------------------------------------

 

step  3:

将生成的 两个 文件 (FeMale.txt   以及 Male.txt) 移到    要处理的  20w  的 文件夹,同目录 :

如图:

 

note : 文件夹以及文件说明

FeMale (新建文件夹,用于存放女性)

Male (新建文件夹,用于存放男性)

 

img_celeba  (待处理的图片,官网解压出来的)

move_images.py (用于将 img_celeba 的数据分开,分男女,按照 Male.txt,FeMale.txt (这两个文件,step2 已经完成)   规则 )

 

blazeface.py && face_detection_front.tflite  (用于裁剪人脸,用到tensorflow)

test_src  ( 新建的文件夹,    从 img_celeba 中 拷贝部分 图片至  该文件夹  , 用于测试使用  )

test_crop ( 新建的文件夹, 运行python blazeface.py 保存的结果)

 

执行以下:

3A.  python move_images.py

3B. python  blazeface.py

 

下面是 move_images.py 代码:

import os
import shutil

nof = open("FeMale.txt")
hasf = open("Male.txt")

noLine = nof.readline() 
hasLine = hasf.readline() 

list_images = os.listdir("./img_celeba")
hasGo = True
noGo = True
for i in range(0, len(list_images)):
    imgName = os.path.basename(list_images[i])
    if (os.path.splitext(imgName)[1] != ".jpg"): continue

    noArray = noLine.split()
    if (len(noArray) < 1): noGo = False
    hasArray = hasLine.split()
    if (len(hasArray) < 1): hasGo = False

    if (noGo and (imgName == noArray[0])):
        oldname= "./img_celeba/"+imgName
        newname="./FeMale/"+imgName
        shutil.move(oldname, newname)
        noLine = nof.readline()
    elif (hasGo and (imgName == hasArray[0])):
        oldname= "./img_celeba/"+imgName
        newname="./Male/"+imgName
        shutil.move(oldname, newname)
        hasLine = hasf.readline()

    if (i % 100 == 0): print(imgName)

nof.close()
hasf.close()

筛选 数据python 脚本 参考

简书作者

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Teleger

你的支持是我前进的方向

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值