03-人脸识别-基于MTCNN,显示5个人脸特征

import tensorflow as tf
import numpy as np
import cv2
import detect_face
import matplotlib.pyplot as plt
import math
from scipy import misc

img = misc.imread('face_airplane.jpg')

sess = tf.Session()
pnet, rnet, onet = detect_face.create_mtcnn(sess, None)

# pnet, rnet, onet are 3 funtions

minsize = 20
threshold = [0.6, 0.7, 0.7]
factor = 0.709
df_result, df_points_result = detect_face.detect_face(img, minsize, pnet, rnet, onet, threshold, factor)

n_face = df_result.shape[0]
print('detected face number: {}'.format(n_face))
print(df_result)

plt.figure('faces')
i = 0
plt_nrow = n_face / 5
plt_nrow = plt_nrow + int(n_face != plt_nrow * 5)

plt_ncol = 5
crop_face = []
for subfaceRec in df_result:
    i = i + 1
    subfaceRec = subfaceRec.astype(int)
    img_a_face = img[subfaceRec[1]:subfaceRec[3], subfaceRec[0]:subfaceRec[2]]
    crop_face.append(img_a_face)
    img_a_face = cv2.resize(img_a_face, (96, 96), interpolation=cv2.INTER_CUBIC)

    print('plt_nrow:{}, plt_ncol:{}, i:{}'.format(plt_nrow, plt_ncol, i))
    plt.subplot(plt_nrow, plt_ncol, i)
    plt.imshow(img_a_face)

    cv2.rectangle(img, (subfaceRec[0], subfaceRec[1]), (subfaceRec[2], subfaceRec[3]), (0, 255, 0), 2)

# draw points
# df_points_result is faceNumber X 10
# need transpose to 10 X faceNumber
df_points_result = np.transpose(df_points_result)

for subPoints in df_points_result:
    # subPoints: [x1,x2,x3,x4,x5,y1,y2,y3,y4,y5]
    cv2.circle(img, (subPoints[0], subPoints[5]), 2, (255, 0, 0), -1) # Red left eye
    cv2.circle(img, (subPoints[1], subPoints[6]), 2, (0, 0, 255), -1) # Blue right eye
    cv2.circle(img, (subPoints[2], subPoints[7]), 2, (0, 255, 0), -1) # Green nose
    cv2.circle(img, (subPoints[3], subPoints[8]), 2, (255, 255, 0), -1) # yellow left mouse
    cv2.circle(img, (subPoints[4], subPoints[9]), 2, (0, 255, 255), -1) # cyan right mouse

plt.figure('img')
plt.imshow(img)
plt.show()

sess.close()

  

还有待优化。

原文地址:https://www.cnblogs.com/alexYuin/p/8849990.html