以下是用于检测图像中单个面部的代码,但问题是如果图像中有多个面部,则只需要第一个面部。请建议
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
答案 0 :(得分:0)
通过以下更改,问题得以解决
表示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(),:]