基于TensorFlow的MTCNN人脸检测算法(人脸检测数据和人脸特征点检测数据进行合并 代码注解)

代码源自Github:https://github.com/AITTSMD/MTCNN-Tensorflow
该阶段代码取自 Git主 代码目录下 MTCNN-Tensorflow-master / prepare_data / gen_imglist_pnet.py

import numpy as np
import numpy.random as npr
import os

data_dir = '../DATA'
#anno_file = os.path.join(data_dir, "anno.txt")

size = 12

if size == 12:
    net = "PNet"
elif size == 24:
    net = "RNet"
elif size == 48:
    net = "ONet"

#在data_dir/12/pos_12 or neg_12 or part_12 or landmark_12_aug.txt打开这四个文档,就是人脸框数据对应的三个样本文档,并按行读取数据
with open(os.path.join(data_dir, '%s/pos_%s.txt' % (size, size)), 'r') as f:
    pos = f.readlines()

with open(os.path.join(data_dir, '%s/neg_%s.txt' % (size, size)), 'r') as f:
    neg = f.readlines()

with open(os.path.join(data_dir, '%s/part_%s.txt' % (size, size)), 'r') as f:
    part = f.readlines()

with open(os.path.join(data_dir,'%s/landmark_%s_aug.txt' %(size,size)), 'r') as f:
    landmark = f.readlines()
    
#创建../../DATA/imglists和 ../../DATA/12/imglists/PNet这两个路径
dir_path = os.path.join(data_dir, 'imglists')
if not os.path.exists(dir_path):
    os.makedirs(dir_path)
if not os.path.exists(os.path.join(dir_path, "%s" %(net))):
    os.makedirs(os.path.join(dir_path, "%s" %(net)))

#创建../../DATA/12/imglists/PNet路径,并以写入的方式打开文档train_PNet_landmark.txt
with open(os.path.join(dir_path, "%s" %(net),"train_%s_landmark.txt" % (net)), "w") as f:
    nums = [len(neg), len(pos), len(part)]
    ratio = [3, 1, 1]
    #base_num = min(nums)
    base_num = 250000
    #打印各组数据量
    print(len(neg), len(pos), len(part), base_num)

	#shuffle the order of the initial data
    #if negative examples are more than 750k then only choose 750k
    #当负样本的个数大于750000的时候,只取750000个数,反之就随机选取负样本数
    if len(neg) > base_num * 3:
        neg_keep = npr.choice(len(neg), size=base_num * 3, replace=True)
    else:
        neg_keep = npr.choice(len(neg), size=len(neg), replace=True)
    #随机选取正样本base_num数和无关样本base_num数,打印出各组base_num数
    pos_keep = npr.choice(len(pos), size=base_num, replace=True)
    part_keep = npr.choice(len(part), size=base_num, replace=True)
    print(len(neg_keep), len(pos_keep), len(part_keep))
    #注意这里随机选的是数字,而不是数据,接下来会根据数字的顺序为索引来重新排列裁剪三个样本

    #按照已经打乱的数字顺序重新写入数据到(合并数据)文本中
    # write the data according to the shuffled order
    for i in pos_keep:
        f.write(pos[i])
    for i in neg_keep:
        f.write(neg[i])
    for i in part_keep:
        f.write(part[i])
    #landmark数据量和排序不变
    for item in landmark:
        f.write(item)

你可能感兴趣的:(MTCNN)