(本系列随笔持续更新)
搭建要求
详细的搭建过程在 参考资料1 中已经有啦。
TensorFlow 1.6.0
OpenCV 2.4.8 仅仅是加载和读取图片的需要
Ubuntu 14.04 64bits
代码:加载图片,找出人脸。运行依赖detect_face.py
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) minsize = 20
threshold = [0.6, 0.7, 0.7]
factor = 0.709
df_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) fig = 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 #ax = fig.subplot(plt_nrow, plt_ncol, i)
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)
#plt.imshow(img_a_face)
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) plt.figure('img')
plt.imshow(img)
plt.show() sess.close()
显示效果:
参考资料:
1 https://blog.csdn.net/mr_evanchen/article/details/77650883
2 https://github.com/ShyBigBoy/face-detection-mtcnn