根据年龄对图像进行排序

时间:2017-08-18 10:41:56

标签: python mat imdb

我使用以下代码加载 IMDB dataset mat 文件,然后根据年龄将其隔离。以下代码加载mat文件,然后通过从出生日期中减去拍摄照片的数据来计算年龄

from scipy.io import loadmat
from datetime import datetime
import os
import  numpy as np
from shutil import copy
db = "imdb"
mat_path = "data/{}_crop/{}.mat".format(db, db)
print (mat_path)
dataset = loadmat(mat_path)
face_score_treshold = 3
path_root = "data/{}_crop/".format(db)
def calc_age(taken, dob):
    birth = datetime.fromordinal(max(int(dob) - 366, 1))

    # assume the photo was taken in the middle of the year
    if birth.month < 7:
        return taken - birth.year
    else:
        return taken - birth.year - 1


image_names_array = dataset['imdb']['full_path'][0, 0][0]
dob = dataset[db][0, 0]["dob"][0]  # Matlab serial date number
photo_taken = dataset[db][0, 0]["photo_taken"][0]  # year
image_names = []
age = [calc_age(photo_taken[i], dob[i]) for i in range(len(dob))]
print(age)

for image_name_arg in range(image_names_array.shape[0]):
    try:
        os.makedirs(str (age[image_name_arg]))
    except OSError:
        pass
    copy(path_root + "/" + str(image_names_array[image_name_arg][0]),str (age[image_name_arg]))
    image_name = image_names_array[image_name_arg][0]
    image_names.append(image_name)
    print( dict(zip(image_names, age)))

计算年龄后,会创建一个文件夹,其中包含年龄作为文件夹名称,并将图像文件复制到文件夹中。

但是在结果文件夹中有很多错误否定。我怎样才能解决这个问题?

0 个答案:

没有答案