KinFaceW-I和KinFaceW-II的亲属关系图片及性别数据处理

import numpy as np
import shutil
import os


def fuc1(src, dir):
    c = 0
    f = open(src)
    lines = f.readlines()
    f.close()
    l = len(lines)
    f = open(dir, 'a')
    for line in lines:
        s = line.split()
        a = s[2] + ' ' + s[3] + ' ' + s[1] + ' \n'
        f.write(a)
        c += 1
    f.close()
    if (c == l):
        print('True')
    else:
        print('False')


# fuc1('KinFaceW-II/fd.txt','KinFaceW-II/temp.txt')
def fuc2(src, dir, label):
    f = open(src)
    lines = f.readlines()
    f.close()
    f = open(dir, 'a')
    for line in lines:
        l = line.split()
        if (int(l[2]) == 1):
            s = l[0] + ' ' + l[1] + ' ' + str(label) + ' \n'
            f.write(s)
    f.close()


# fuc2('KinFaceW-II/temp.txt','KinFaceW-II/label.txt',label=0)
def fuc3(path):
    f = open(path, 'a')
    f.seek(0)
    f.truncate()
    f.close()


# fuc3('KinFaceW-II/temp.txt')
def fuc4(path):
    f = open(path)
    lines = f.readlines()
    f.close()
    f = open(path, 'w')
    for line in lines:
        l = line.split()
        s = 'II-' + l[0] + ' II-' + l[1] + ' ' + l[2] + ' \n'
        f.write(s)
    f.close()


def fuc5(path):
    f = open(path)
    lines = f.readlines()
    f.close()
    np.random.shuffle(lines)
    f = open(path, 'w')
    for line in lines:
        f.write(line)
    f.close()


def fuc6(src, dir, path):
    f = open(path)
    lines = f.readlines()
    f.close()
    if os.path.exists(dir) is False:
        os.mkdir(dir)
    for line in lines:
        s = line.split()
        shutil.copy(src + s[0], dir + s[0])
        shutil.copy(src + s[1], dir + s[1])
        os.rename(dir + s[0], dir + 'I-' + s[0])
        os.rename(dir + s[1], dir + 'I-' + s[1])


def fuc7(path, txt):
    f = open(txt)
    lines = f.readlines()
    f.close()
    for line in lines:
        s = line.split()
        if os.path.exists(path + s[0]) is False:
            print(s[0])
        if os.path.exists(path + s[1]) is False:
            print(s[1])


if __name__ == '__main__':
    fuc1('KinFaceW-I/fd.txt', 'KinFaceW-I/temp.txt')
    fuc2('KinFaceW-I/temp.txt', 'KinFaceW-I/l.txt', label=0)
    fuc3('KinFaceW-I/temp.txt')

    fuc1('KinFaceW-I/fs.txt', 'KinFaceW-I/temp.txt')
    fuc2('KinFaceW-I/temp.txt', 'KinFaceW-I/l.txt', label=1)
    fuc3('KinFaceW-I/temp.txt')

    fuc1('KinFaceW-I/md.txt', 'KinFaceW-I/temp.txt')
    fuc2('KinFaceW-I/temp.txt', 'KinFaceW-I/l.txt', label=0)
    fuc3('KinFaceW-I/temp.txt')

    fuc1('KinFaceW-I/ms.txt', 'KinFaceW-I/temp.txt')
    fuc2('KinFaceW-I/temp.txt', 'KinFaceW-I/l.txt', label=1)
    fuc3('KinFaceW-I/temp.txt')

    fuc4('KinFaceW-I/label.txt')

    fuc5('KinFaceW-I/label.txt')

    fuc6('KinFaceW-I/img/', 'KinFaceW-I/kin/', 'KinFaceW-I/l.txt')

    fuc7('KinFaceW-I/kin/','KinFaceW-I/label.txt')

fuc1~fuc3:提取数据集中有亲属关系的图片并加性别标签;

fuc4:文件重命名

fuc5:打乱文件

fuc6:提取图片到新的文件夹并重命名

fuc7:检测重命名后的图片与标签文件是否一样

猜你喜欢

转载自blog.csdn.net/weixin_38582851/article/details/83585943