[基于tensorflow的人脸检测] 基于神经网络的人脸检测3——WIDER FACE数据集处理

目录:
1.官网的数据解释

2.处理数据

3.处理好的数据分享

正文:

1.官网数据解释
与FDDB数据集一样,WIDER FACE数据集的官网上也向我们提供了人脸检测图片数据集,不同的是,WIDER FACE数据集还将图片分为了训练、验证和测试数据集,不需要自己分了。不过,测试数据集是没有人脸标注的。下面是WIDER FACE数据集的标注格式。

0–Parade/0_Parade_marchingband_1_849.jpg
1
449 330 122 149 0 0 0 0 0 0

与FDDB数据集一样,第一行表示图片的名称。第二行表示这张图片拥有的人脸数。因为这张图片有一张人脸,所以下面的一行是对人脸位置的标注;与FDDB不同的是,第三行的人脸标注分别是人脸左上角的x轴、左上角y轴、人脸的宽、人脸的高、人脸是否模糊的标志、人脸表情标志、光照标志、是否有效标志、遮挡标志、姿态标志。 其实真正使用到的是前面四位,这里也对后面的标志位进行解释。

标志数字表示的程度
人脸是否模糊的标志0表示清晰,1表示轻度模糊,2表示重度模糊
人脸表情标志0表示正常表情,1表示夸张表情
光照标志0表示正常光照,1表示严重光照
是否有效标志0表示有效,1表示无效
遮挡标志0表示没有遮挡,1表示部分遮挡,2表示严重遮挡
姿态标志0表示正常姿态,1表示非正常姿态
2.处理数据
与FDDB数据类似,人脸的获取也用矩形框进行裁剪。以x、y、w、h代表前四位标志,获取方式如下。
x1 = x
y1 = y
x2 = x + w
y2 = y + h
face_image = image[y1:y2,x1:x2] #注意x y的顺序

在FDDB数据集处理时(FDDB数据集处理)已经给出了获取FDDB非人脸的程序框图,它也适用与WIDER FACE数据集。下面给出如何处理标注人脸的txt文件的程序框图,FDDB数据集的处理方法也是一致的。

在这里插入图片描述

3.处理好的数据分享
下面是我处理得到的人脸以及非人脸训练和验证数据。有需要的可以下载。

WIDER FACE 人脸/非人脸数据
密码:b7qcy9
4.代码

import os
import cv2
from datetime import datetime
import numpy as np
print(datetime.now())
def get_image_name(image_path):
    """获取图片名称"""
    strat_index = 0
    end_index = 0
    for i in range(len(image_path)):
        if image_path[i] == '/':
            strat_index = i
        if image_path[i] == '.':
            end_index = i
        image_name = image_path[strat_index+1: end_index]
        
    return image_name

#测试截取所有人脸-------------------------------------------------------------

def get_random(image_w, image_h, bounding_w, bounding_h):
    """产生随机数"""
    #图像的宽 图像的高 框的宽 框的高
    limx = image_w - bounding_w
    limy = image_h - bounding_h
    x1 = np.random.randint(0, limx)
    x2 = x1 + bounding_w
    y1 = np.random.randint(0, limy)
    y2 = y1 + bounding_h
    
    return x1, y1, x2, y2

def compuet_iou(rect1, rect2):
    """计算两个矩形的IOU"""
    #rect是矩形的四个坐标 左上角和右下角坐标 rect = [(x,1,y2),(x2,y2)]
    area1 = (rect1[3] - rect1[1]) * (rect1[2] - rect1[0])
    area2 = (rect2[3] - rect2[1]) * (rect2[2] - rect2[0])
    all_area = area1 + area2
    x1 = max(rect1[0], rect2[0])
    y1 = max(rect1[1], rect2[1])
    x2 = min(rect1[2], rect2[2])
    y2 = min(rect1[3], rect2[3])
    h = max(0, y2 - y1)
    w = max(0, x2 - x1)
    overlap = h * w
    try :
        iou = overlap/(all_area - overlap)
    except:
        iou = 1
    else : 
        iou = overlap/(all_area - overlap)
    return iou

# 获取所有文件名地址、文件名下标、人脸数、人脸数下标
def get_img(label_text):
    image_path = [] #存储地址
    image_path_index = [] #存储txt地址所在行
    face_num = [] #存储人脸数
    row_num =0
    with open(label_text) as obj:
        for l in obj.readlines():
            row_num += 1
            if '.' in l: #图片地址
                image_path.append(l.strip('\n')) #去掉换行符
                image_path_index.append(row_num)#地址所在行数
                flag = True #这个标志位使得只读取图片地址的下一行
            else:
                if flag:
                    face_num.append(int(l))
                    flag =False
                
        
    face_num_index =  [x + 1 for x in image_path_index]  #人脸数所在行数
    
    return image_path, image_path_index,face_num,face_num_index

label_text = 'wider_face_train_bbx_gt.txt'
image_path, image_path_index,face_num,face_num_index = get_img(label_text)
# ten_face = []
for i in range(len(face_num)):
    if face_num[i] == 1:
        print(image_path[i])
        if i ==2:
            break
#         ten_face.append(i)
# print(ten_face)  #[279, 3808, 7512, 9227] 没有人脸
        
#提取框坐标(三维数组)---------------------------------------------------------
def get_bouning(label_text):
    image_path, image_path_index,face_num,face_num_index = get_img(label_text)
    mid_bounding_box = []
    bounding_box = []
    true_bounding = [] #小于10的人脸框
    for i in range(len(face_num_index)):
    
        strat_index = face_num_index[i]
        end_index = face_num_index[i] + face_num[i]
        # print(strat_index,end_index)
        with open(label_text) as obj:
            for l in obj.readlines()[strat_index: end_index]:
                #读取框数据
                begin_bounding_index = 0
                for i in range(len(str(l))) : #遍历某一行
                    if l[i] == ' ' :
                        end_bounding_index = i
                        mid_bounding_box.append(int(
                            str(l)[begin_bounding_index: end_bounding_index]))
                        begin_bounding_index = end_bounding_index
                
                bounding_box.append(mid_bounding_box[:4])
                mid_bounding_box = [] #清空
        true_bounding.append(bounding_box)
        bounding_box = [] #清空
    return true_bounding,image_path, image_path_index,face_num,face_num_index
    

# print(len((true_bounding)[2]))  
# print(image_path)
#从图片中框出人脸(截取)-------------------------------------------------------
def get_face(label_text,save_face_path,root_path):
    true_bounding,image_path, image_path_index,face_num,face_num_index =\
        get_bouning(label_text)
    length = len(image_path)
    for i in range(length):
        if face_num[i] < 5  and face_num[i] > 0: #为了防止脸数太多太小 剔除大于5张脸的
            face_rect = []
            all_path = os.path.join(root_path, image_path[i]) #所有路径
            image_raw_name = get_image_name(image_path[i])
            # save_image_name
            image = cv2.imread(all_path, 1)
            image_h, image_w , _ = image.shape
            for j in range(len(true_bounding[i])) : #多个人脸
                x = int(true_bounding[i][j][0])
                y = int(true_bounding[i][j][1])
                w = int(true_bounding[i][j][2])
                h = int(true_bounding[i][j][3])
                face_flag = 'face_' + str(j) + '.jpg'
                
                image_name = image_raw_name + face_flag #1
                
                save_image_name = os.path.join(save_face_path, image_name)#1
                
                # print(save_image_name)
                #框出
                # image = cv2.rectangle(image, (x, y), (x+w,y+h), (0,255,0), 2)
                image_cut = image[y: y+h, x: x+w]
                rect = (x,y,x+w,y+h)
                face_rect.append(rect)
                if h>100 or w>100:
                    try : 
                        cv2.imwrite(save_image_name, image_cut)
                    except:
                        print(save_image_name)
                    else:
                        cv2.imwrite(save_image_name, image_cut)
 
def get_no_face(label_text,save_no_face_path,root_path):
    true_bounding,image_path, image_path_index,face_num,face_num_index =\
        get_bouning(label_text)
    length = len(image_path)
    for i in range(length):
        if face_num[i] < 5  and face_num[i] > 0: #为了防止脸数太多太小 剔除大于5张脸的
            face_rect = []
            all_path = os.path.join(root_path, image_path[i]) #所有路径
            image_raw_name = get_image_name(image_path[i])
            image = cv2.imread(all_path, 1)
            image_h, image_w , _ = image.shape
            for j in range(len(true_bounding[i])) : #多个人脸
                x = int(true_bounding[i][j][0])
                y = int(true_bounding[i][j][1])
                w = int(true_bounding[i][j][2])
                h = int(true_bounding[i][j][3])

                rect = (x,y,x+w,y+h)
                face_rect.append(rect)

            # 截取非人脸
            flag = True
            no_face_number = 0
            lim_while = 0 #循环10次没有就下次个图片
            while flag:
                lim_while += 1
                if lim_while == 10:
                    break
                #  增加判断 防止随机数产生失败
                ious = []
                try:
                    no_x1, no_y1, no_x2, no_y2 = get_random(image_w, image_h, w, h)
                except:
                    break
                else:
                    no_x1, no_y1, no_x2, no_y2 = get_random(image_w, image_h, w, h)
                    rect2 = (no_x1, no_y1, no_x2, no_y2) #获取随机矩形框
                    for rect in face_rect:
                        iou = compuet_iou(rect, rect2)
                        ious.append(iou)
                    iou = max(ious) #取最大的iou
                    if iou<0.2:
                        no_face_number += 1
                        no_face_image = image[no_y1: no_y2, no_x1: no_x2]
                        no_face_flag = 'no_face_' + str(no_face_number) + '.jpg'
                        image_name2 = image_raw_name + no_face_flag#0
                        save_image_name2 =  os.path.join(save_no_face_path, image_name2)#0
                        cv2.imwrite(save_image_name2, no_face_image)
                        
                    if no_face_number == len(true_bounding[i]) :#1:3 face:no_face 设置产生非人脸个数
                        flag = False

#主程序
#训练数据集
# label_text = 'wider_face_train_bbx_gt.txt'
# root_path = r"C:\Users\user\Desktop\face_data\WIDER\WIDER_train\images" 
# save_path = "pr_face"
# save_path2 = "pr_no_face"                  
         
# start_time = datetime.now()
# get_face(label_text,save_path, root_path) 
# end_time = datetime.now()
# use_time = str(end_time - start_time)
# print('截取并保存人脸图片所用时间时间为:' + use_time)

# start_time = datetime.now()
# get_no_face(label_text,save_path2,root_path)
# end_time = datetime.now()
# use_time = str(end_time - start_time)
# print('截取并保存非人脸图片所用时间时间为:' + use_time)

# 验证数据集
# val_label_text = 'wider_face_val_bbx_gt.txt'
# val_root_path = r"C:\Users\user\Desktop\face_data\WIDER\WIDER_val\images"
# vaL_save_path = "pr_face"
# val_save_path2 = "pr_no_face"                  
         
# start_time = datetime.now()
# get_face(val_label_text,vaL_save_path, val_root_path) 
# end_time = datetime.now()
# use_time = str(end_time - start_time)
# print('截取并保存验证人脸图片所用时间时间为:' + use_time)

# start_time = datetime.now()
# get_no_face(val_label_text,val_save_path2,val_root_path)
# end_time = datetime.now()
# use_time = str(end_time - start_time)
# print('截取并保存验证非人脸图片所用时间时间为:' + use_time)    
                        
# # 测试数据集
# test_label_text = 'wider_face_test_filelist.txt'
# test_root_path = r"C:\Users\user\Desktop\face_data\WIDER\WIDER_test\images" 
# test_save_path = "wider_test_face_data"
# test_save_path2 = "wider_test_no_face_data"                  
         
# start_time = datetime.now()
# get_face(test_label_text,test_save_path, test_save_path2) 
# end_time = datetime.now()
# use_time = str(end_time - start_time)
# print('截取并测试保存人脸图片所用时间时间为:' + use_time)

# start_time = datetime.now()
# get_no_face(test_label_text,test_save_path2,test_save_path2)
# end_time = datetime.now()
# use_time = str(end_time - start_time)
# print('截取并保存测试非人脸图片所用时间时间为:' + use_time)                        
                        
# 查看文件夹人脸数
# count =0
# for root,dirs,files in os.walk('pr_no_face'):   
#     for each in files:
#         count += 1  
        
# print ('得到人脸图片个数为:' + str(count) ) #7784 1892

# count =0
# for root,dirs,files in os.walk('wider_val_no_face_data'):     
#     for each in files:
#         count += 1   
        
# print ('得到非人脸图片个数为:' + str(count) ) # 6562

结语:
如果对你有帮助,就给我一个赞吧,如何有问题,可以在评论区进行讨论。

上一篇:[基于tensorflow的人脸检测] 基于神经网络的人脸检测2——FDDB数据集处理
下一篇:[基于tensorflow的人脸检测] 基于神经网络的人脸检测4——数据集的标签生成

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值