根据人脸做年龄预测

说明

做一个针对人脸做不同的年龄预测,需要用到MTCNN模型和SSR-Net模型,MTCNN模型用于做人脸检测和人脸对齐,在MTCNN中,使用深度学习方法结合NMS和边界框回归,将人脸区域坐标和关键点坐标进行识别,相比较机器学习方法,MTCNN能更好地识别不同情况下的人脸。

我实在华为云服务器上跑的,结果ok

MTCNN模型的详解可以参考:link

环境配置

python3 TensorFlow 1.13.1 mtcnn==0.0.8

mtcnn库检测人脸

将图片存储在image_path中

import numpy as np
import cv2
import tensorflow as tf
import random
from PIL import Image

image_path = "path"
img = Image.open(image_path)
img = np.array(img)

调用mtcnn库,进行人脸区域检测,并显示检测结果

from mtcnn.mtcnn import MTCNN as mtcnn

detector = mtcnn()
detected = detector.detect_faces(img)

# 打印检测结果
detected

将检测结果绘制在图片上

# 绘图部分
box = detected[0]["box"]
res_img = cv2.rectangle(img, (box[0],box[1]),(box[0]+box[2],box[1]+box[3]), 0, 1)

keypoints = detected[0]["keypoints"]
res_img = cv2.circle(res_img, keypoints['left_eye'], 1, 255, 4)
res_img = cv2.circle(res_img, keypoints['right_eye'], 1, 255, 4)
res_img = cv2.circle(res_img, keypoints['nose'], 1, 255, 4)
res_img = cv2.circle(res_img, keypoints['mouth_left'], 1, 255, 4)
res_img = cv2.circle(res_img, keypoints['mouth_right'], 1, 255, 4)

res_img = Image.fromarray(res_img)
res_img

在这里插入图片描述

MTCNN模型实现

MTCNN网络分为三部分:PNet RNet ONet 对应于
人脸/非人脸分类分类结果,人脸边界框以及人脸关键点位置。

NMS(non maximum suppression)非极大值抑制 当我们进行人脸检测时,可能会对同一张人脸区域有多个边界框检测结果,虽然这些检测结果都有很高的置信度,但是我们只需要置信度最高的检测结果,所以进行局部最大值检测,将不是最大值的预测结果去掉,完成边界框筛选的任务。NMS被应用在很多目标检测模型当中,例如R-CNN,Faster R-CNN,Mask R-CNN等。

from src.align.detect_face import Network
from src.align.detect_face import rerec, pad
from src.align.detect_face import nms
from src.align.detect_face import imresample
from src.align.detect_face import generateBoundingBox

PNet

我们使用全卷积网络:Proposal 网络(PNet),来生成人脸区域备选框,然后备选框通过边界框回归进行校正。校正后,应用NMS来将高度重复的备选框进行筛选。

class PNet(Network):
    def setup(self):
        (self.feed('data') 
             .conv(3, 3, 10, 1, 1, padding='VALID', relu=False, name='conv1')
             .prelu(name='PReLU1')
             .max_pool(2, 2, 2, 2, name='pool1')
             .conv(3, 3, 16, 1, 1, padding='VALID', relu=False, name='conv2')
             .prelu(name='PReLU2')
             .conv(3, 3, 32, 1, 1, padding='VALID', relu=False, name='conv3')
             .prelu(name='PReLU3')
             .conv(1, 1, 2, 1, 1, relu=False, name='conv4-1')
             .softmax(3,name='prob1'))

        (self.feed('PReLU3') 
             .conv(1, 1, 4, 1, 1, relu=False, name='conv4-2'))

RNet

PNet生成的所有人脸备选框都被输入另一个卷积网络,叫做Refine网络(RNet)。RNet将大量错误的人脸信息去掉,同样通过边界框回归进行校正,以及通过NMS进行筛选。

class RNet(Network):
    def setup(self):
        (self.feed('data') #pylint: disable=no-value-for-parameter, no-member
             .conv(3, 3, 28, 1, 1, padding='VALID', relu=False, name='conv1')
             .prelu(name='prelu1')
             .max_pool(3, 3, 2, 2, name='pool1')
             .conv(3, 3, 48, 1, 1, padding='VALID', relu=False, name='conv2')
             .prelu(name='prelu2')
             .max_pool(3, 3, 2, 2, padding='VALID', name='pool2')
             .conv(2, 2, 64, 1, 1, padding='VALID', relu=False, name='conv3')
             .prelu(name='prelu3')
             .fc(128, relu=False, name='conv4')
             .prelu(name='prelu4')
             .fc(2, relu=False, name='conv5-1')
             .softmax(1,name='prob1'))

        (self.feed('prelu4') #pylint: disable=no-value-for-parameter
             .fc(4, relu=False, name='conv5-2'))

ONet

ONet与RNet相似,但是在ONet将输出5个人脸关键点位置,全称为Output Network,作为最后一层网络,将输出人脸区域坐标以及人脸关键点坐标。

class ONet(Network):
    def setup(self):
        (self.feed('data') #pylint: disable=no-value-for-parameter, no-member
             .conv(3, 3, 32, 1, 1, padding='VALID', relu=False, name='conv1')
             .prelu(name='prelu1')
             .max_pool(3, 3, 2, 2, name='pool1')
             .conv(3, 3, 64, 1, 1, padding='VALID', relu=False, name='conv2')
             .prelu(name='prelu2')
             .max_pool(3, 3, 2, 2, padding='VALID', name='pool2')
             .conv(3, 3, 64, 1, 1, padding='VALID', relu=False, name='conv3')
             .prelu(name='prelu3')
             .max_pool(2, 2, 2, 2, name='pool3')
             .conv(2, 2, 128, 1, 1, padding='VALID', relu=False, name='conv4')
             .prelu(name='prelu4')
             .fc(256, relu=False, name='conv5')
             .prelu(name='prelu5')
             .fc(2, relu=False, name='conv6-1')
             .softmax(1, name='prob1'))

        (self.feed('prelu5') #pylint: disable=no-value-for-parameter
             .fc(4, relu=False, name='conv6-2'))

        (self.feed('prelu5') #pylint: disable=no-value-for-parameter
             .fc(10, relu=False, name='conv6-3'))

数据准备

# 打开原图
test_img = Image.open(image_path)
test_img

# 进行图片预处理
test_img = np.array(test_img)
img_size = np.asarray(test_img.shape)[0:2]
factor_count=0
minsize = 20
total_boxes=np.empty((0,9))
points=np.empty(0)
h=test_img.shape[0] # h=410
w=test_img.shape[1] # w=599

minl=np.amin([h, w]) # minl = [410,599] 中最小值 410
m=12.0/minsize # m=12/20
minl=minl*m # minl = 410*12/20 = 410* 0.6 
factor = 0.709 
scales=[]

while minl>=12:
    scales += [m*np.power(factor, factor_count)]
    minl = minl*factor 
    factor_count += 1

# first stage
for scale in scales:
    hs=int(np.ceil(h*scale)) #大于等于该值的最小整数
    ws=int(np.ceil(w*scale))
    im_data = cv2.resize(test_img, (ws, hs), interpolation=cv2.INTER_AREA)
    im_data = (im_data-127.5)*0.0078125
    img_x = np.expand_dims(im_data, 0)
    img_y = np.transpose(img_x, (0,2,1,3))

运行PNet

运行PNet,并加载预训练权重

with tf.Graph().as_default():
    with tf.Session() as sess:
        with tf.variable_scope('pnet'):
            data = tf.placeholder(tf.float32, shape=(None, None, None, 3), name="input")
            pnet = PNet({'data':data})
            pnet.load("./src/align/PNet.npy", sess)
            out = sess.run(('pnet/conv4-2/BiasAdd:0', 'pnet/prob1:0'), feed_dict={'pnet/input:0':img_y})
            
#  boundingbox regression 结果 
out0 = np.transpose(out[0], (0,2,1,3))
#  face classification 结果
out1 = np.transpose(out[1], (0,2,1,3))

threshold = 0.5
boxes, reg = generateBoundingBox(out1[0,:,:,1].copy(), out0[0,:,:,:].copy(), scale, threshold)
print("PNet产生结果为:"+str(boxes.shape))
            
total_boxes = boxes.copy()


# 边界框绘制函数
def draw_bboxes(img, total_boxes):
   
    for i in range(total_boxes.shape[0]):
        r = random.randint(0, 255)
        g = random.randint(0, 255)
        b = random.randint(0, 255)

        x1 = int(total_boxes[:,0][i])
        y1 =  int(total_boxes[:,1][i])
        x2=  int(total_boxes[:,2][i])
        y2 =  int(total_boxes[:,3][i])

        img = cv2.rectangle(img,(x1,y1),(x2,y2), (r,g,b), 2)
    
    return img

将PNet预测结果进行筛选和回归,结果绘制在图片上

img = Image.open(image_path)
img = np.array(img)
Image.fromarray(draw_bboxes(img,total_boxes))
total_boxes=np.empty((0,9))
pick = nms(boxes.copy(), 0.7, 'Union')

if boxes.size>0 and pick.size>0:
    boxes = boxes[pick,:]
    total_boxes = np.append(total_boxes, boxes, axis=0)
print("筛选之后结果为:"+str(total_boxes.shape))
# 绘制筛选后的边界框
img = Image.open(image_path)
img = np.array(img)

# 进行nms计算 参数为0.7
pick = nms(total_boxes.copy(), 0.6, 'Union')
total_boxes = total_boxes[pick,:]
print(total_boxes.shape)

# 边界框回归
regw = total_boxes[:,2]-total_boxes[:,0]
regh = total_boxes[:,3]-total_boxes[:,1]
qq1 = total_boxes[:,0]+total_boxes[:,5]*regw
qq2 = total_boxes[:,1]+total_boxes[:,6]*regh
qq3 = total_boxes[:,2]+total_boxes[:,7]*regw
qq4 = total_boxes[:,3]+total_boxes[:,8]*regh
total_boxes = np.transpose(np.vstack([qq1, qq2, qq3, qq4, total_boxes[:,4]]))
print(total_boxes.shape)
img = Image.open(image_path)
img = np.array(img)

# 将边界框形状转为正方形
total_boxes = rerec(total_boxes.copy())
print(total_boxes)

# 将边界框坐标整理成整数
total_boxes[:,0:4] = np.fix(total_boxes[:,0:4]).astype(np.int32)
print(total_boxes)
dy, edy, dx, edx, y, ey, x, ex, tmpw, tmph = pad(total_boxes.copy(), w, h)

img = Image.open(image_path)
img = np.array(img)
Image.fromarray(draw_bboxes(img,total_boxes))

在这里插入图片描述

运行RNet

MTCNN的PNet计算结束后,可以看到已经有若干个边界框已经被预测出来。接下来我们将进行RNet预测,通过RNet预测之后,边界框将更加准确。

numbox = total_boxes.shape[0] 
tempimg = np.zeros((24,24,3,numbox))
for k in range(0,numbox):
    tmp = np.zeros((int(tmph[k]),int(tmpw[k]),3))
    tmp[dy[k]-1:edy[k],dx[k]-1:edx[k],:] = img[y[k]-1:ey[k],x[k]-1:ex[k],:]
    if tmp.shape[0]>0 and tmp.shape[1]>0 or tmp.shape[0]==0 and tmp.shape[1]==0:
        tempimg[:,:,:,k] = imresample(tmp, (24, 24))
    else:
        print(0)
tempimg = (tempimg-127.5)*0.0078125
tempimg1 = np.transpose(tempimg, (3,1,0,2))
with tf.Graph().as_default():
    with tf.Session() as sess:
        with tf.variable_scope('rnet'):
            data = tf.placeholder(tf.float32, shape=(None, 24, 24, 3), name="input")
            rnet = RNet({'data':data})
            rnet.load("./src/align/RNet.npy", sess)
            out = sess.run(('rnet/conv5-2/conv5-2:0', 'rnet/prob1:0'), feed_dict={'rnet/input:0':tempimg1})
            
# 检测到的人脸坐标
out0 = np.transpose(out[0])
out1 = np.transpose(out[1])

score = out1[1,:]
threshold = 0.7
ipass = np.where(score>0.2)
total_boxes = np.hstack([total_boxes[ipass[0],0:4].copy(), np.expand_dims(score[ipass].copy(),1)])
mv = out0[:,ipass[0]]
if total_boxes.shape[0]>0:
    pick = nms(total_boxes, threshold, 'Union')
    total_boxes = total_boxes[pick,:]
    print(total_boxes)

img = Image.open(image_path)
img = np.array(img)
    
from src.align.detect_face import bbreg

# 边界框回归
total_boxes = bbreg(total_boxes.copy(), np.transpose(mv[:,pick]))
print(total_boxes)
# 边界框整理成正方形
total_boxes = rerec(total_boxes.copy())
print(total_boxes)

img = Image.open(image_path)
img = np.array(img)
Image.fromarray(draw_bboxes(img,total_boxes))

运行ONet

最后,我们进行ONet预测,不仅使人脸的边界框检测更加准确,这一步还将关键点检测出来。


numbox = total_boxes.shape[0]
total_boxes = np.fix(total_boxes).astype(np.int32)
dy, edy, dx, edx, y, ey, x, ex, tmpw, tmph = pad(total_boxes.copy(), w, h)

tempimg = np.zeros((48,48,3,numbox))
for k in range(0,numbox):
    tmp = np.zeros((int(tmph[k]),int(tmpw[k]),3))
    tmp[dy[k]-1:edy[k],dx[k]-1:edx[k],:] = img[y[k]-1:ey[k],x[k]-1:ex[k],:]
    if tmp.shape[0]>0 and tmp.shape[1]>0 or tmp.shape[0]==0 and tmp.shape[1]==0:
        tempimg[:,:,:,k] = imresample(tmp, (48, 48))
    else:
        print(0)
tempimg = (tempimg-127.5)*0.0078125
tempimg1 = np.transpose(tempimg, (3,1,0,2))
with tf.Graph().as_default():
    with tf.Session() as sess:
        with tf.variable_scope('onet'):
            data = tf.placeholder(tf.float32, shape=(None, 48, 48, 3), name="input")
            onet = ONet({'data':data})
            rnet.load("./src/align/ONet.npy", sess)
            out = sess.run(('onet/conv6-2/conv6-2:0', 'onet/conv6-3/conv6-3:0', 'onet/prob1:0'), feed_dict={'onet/input:0':tempimg1})
            
# 人脸区域边界框预测结果
out0 = np.transpose(out[0])
# 人脸关键点预测结果
out1 = np.transpose(out[1])
# 人脸区域置信度
out2 = np.transpose(out[2])

score = out2[1,:]
points = out1
# threshold = 0.7
ipass = np.where(score>0.7)
points = points[:,ipass[0]]
total_boxes = np.hstack([total_boxes[ipass[0],0:4].copy(), np.expand_dims(score[ipass].copy(),1)])
mv = out0[:,ipass[0]]

w = total_boxes[:,2]-total_boxes[:,0]+1
h = total_boxes[:,3]-total_boxes[:,1]+1
points[0:5,:] = np.tile(w,(5, 1))*points[0:5,:] + np.tile(total_boxes[:,0],(5, 1))-1
points[5:10,:] = np.tile(h,(5, 1))*points[5:10,:] + np.tile(total_boxes[:,1],(5, 1))-1
if total_boxes.shape[0]>0:
    total_boxes = bbreg(total_boxes.copy(), np.transpose(mv))
    pick = nms(total_boxes.copy(), 0.7, 'Min')
    total_boxes = total_boxes[pick,:]
    points = points[:,pick]
img = Image.open(image_path)
img = np.array(img)

r = random.randint(0, 255)
g = random.randint(0, 255)
b = random.randint(0, 255)

point_color = (r, g, b) 

for i in range(5):
    cv2.circle(img,(int(points[i]),int(points[i+5])),1, point_color, 4)
    
Image.fromarray(draw_bboxes(img,total_boxes))

在这里插入图片描述

年龄预测

我们使用SSR-Net模型预测年龄

加载模型

from SSRNET_model import SSR_net

weight_file = "./ssrnet_3_3_3_64_1.0_1.0.h5"

img_size = 64
stage_num = [3,3,3]
lambda_local = 1
lambda_d = 1
model = SSR_net(img_size,stage_num, lambda_local, lambda_d)()
model.load_weights(weight_file)

模型层级结构

model.summary()

准备输入数据

faces = np.empty((len(detected), img_size, img_size, 3))
faces.shape

获取人脸区域图片,并缩放
将人脸检测结果进行裁剪和缩放

ad = 0.4

img_h, img_w, _ = np.shape(img)

for i,d in enumerate(detected):
    if d['confidence'] >=0.95 :
        x1,y1,w,h = d['box']
        x2 = x1 + w
        y2 = y1 + h
        xw1 = max(int(x1 - ad * w), 0)
        yw1 = max(int(y1 - ad * h), 0)
        xw2 = min(int(x2 + ad * w), img_w - 1)
        yw2 = min(int(y2 + ad * h), img_h - 1)
        img = cv2.resize(img[yw1:yw2+1, xw1:xw2+1, :], (img_size, img_size))
        faces[i,:,:,:] = img
        
res_img = Image.fromarray(img)
res_img

预测年龄
将人脸区域图片输入模型,获得预测结果

res = model.predict(faces)

print("预测年龄为:"+str(int(res[0])))
  • 1
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值