Tensorflow调用facenet的mtcnn实现对图片的人脸检测功能

本文介绍了一种基于深度学习的人脸检测与识别方法,使用了MTCNN模型进行人脸定位,并通过OpenCV和Matplotlib展示检测结果。文章详细展示了如何在Python环境中利用Scipy、TensorFlow等库实现人脸识别流程,包括设置参数、加载模型、读取图片、检测人脸位置及绘制边界框。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

from scipy import misc
import tensorflow as tf
import align.detect_face
import cv2
import matplotlib.pyplot as plt


minsize = 20  # minimum size of face
threshold = [0.6, 0.7, 0.7]  # three steps's threshold
factor = 0.709  # scale factor
gpu_memory_fraction = 1.0

print('Creating networks and loading parameters')

with tf.Graph().as_default():
    gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=gpu_memory_fraction)
    sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options, log_device_placement=False))
    with sess.as_default():
        pnet, rnet, onet = align.detect_face.create_mtcnn(sess, None)

image_path = 'C:\\Users\\rjx\\PycharmProjects\\untitled1\\facenet-master\\data\\test\\test2.jpg'

img = misc.imread(image_path)
bounding_boxes, _ = align.detect_face.detect_face(img, minsize, pnet, rnet, onet, threshold, factor)
nrof_faces = bounding_boxes.shape[0]  # 人脸数目
print('找到人脸数目为:{}'.format(nrof_faces))

print(bounding_boxes)

crop_faces = []
for face_position in bounding_boxes:
    face_position = face_position.astype(int)
    print(face_position[0:4])
    cv2.rectangle(img, (face_position[0], face_position[1]), (face_position[2], face_position[3]), (0, 255, 0), 2)
    crop = img[face_position[1]:face_position[3],
           face_position[0]:face_position[2], ]

    crop = cv2.resize(crop, (96, 96), interpolation=cv2.INTER_CUBIC)
    print(crop.shape)
    crop_faces.append(crop)
    plt.imshow(crop)
    plt.show()

plt.imshow(img)
plt.show()

效果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值