1. 程式人生 > >KinFaceW-I和KinFaceW-II的親屬關係圖片及性別資料處理

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:檢測重新命名後的圖片與標籤檔案是否一樣