繁体   English   中英

使用python使用Dlib的人脸检测器检测多个人脸

[英]Detect multiple faces using Dlib's face detector using python

以下是用于检测图像中单张脸的代码,但问题是,如果图像中有多张脸,则只需要第一个。 请建议

import caffe, dlib, io
from __future__ import print_function
import os
import matplotlib.pyplot as plt

detector = dlib.get_frontal_face_detector()
img21 = 'group.jpg'
im_name = img21
img = io.imread(os.path.join('./',im_name))
faces=[]
faces= detector(img)
total= len(faces)
print('total faces here :',total)
cropped_face = input_image_cropped[faces[0].top():faces[0].bottom(),
faces[0].left():faces[0].right(), :]
input_image_cropped = caffe.io.load_image(os.path.join('./', im_name))  
cropped_face = input_image_cropped[faces[0].top():faces[0].bottom(), 
faces[0].left():faces[0].right(), :]
h = faces[0].bottom() - faces[0].top()
w = faces[0].right() - faces[0].left()
age_prediction_cropped = age_net.predict([cropped_face])
print('\n\t   predicted age (Dlib-cropped image):',
age_prediction_cropped[0].argmax())
plt.show()

仅供参考:我删除了不需要的代码的其他部分。

我通过下面的链接,但无法为所有检测到的脸部运行循环? 请建议我如何为所有检测到的脸部运行循环。 http://dlib.net/face_detector.py.html

通过以下更改,问题得以解决

对于i,d(枚举)(面):
cropped_face = input_image_cropped [d.top():d.bottom(),d.left():d.right(),:] input_image_cropped = caffe.io.load_image(os.path.join('./',im_name ))
cropped_face = input_image_cropped [d.top():d.bottom(),d.left():d.right(),:]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM