我目前正在尝试从图像中裁剪人脸。
我希望代码能够处理无论图像中有多少张人脸。
以下是输入图像的示例: 我想要裁剪人脸,以便在上面运行面部关键点检测算法(之前已经制作过)。
最终结果将类似于这样: 我的代码目前为:
输出结果如下: 目前,它只输出图像中检测到的最后一个面孔。我相信我错过了一些简单的东西,比如一个for循环。
我想能够在所有灰色裁剪的面部图像上运行我的面部关键点检测算法。
谢谢您的帮助!
我希望代码能够处理无论图像中有多少张人脸。
以下是输入图像的示例: 我想要裁剪人脸,以便在上面运行面部关键点检测算法(之前已经制作过)。
最终结果将类似于这样: 我的代码目前为:
# Load in color image for face detection
image = cv2.imread('images/obamas4.jpg')
# Convert the image to RGB colorspace
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
# Make a copy of the original image to draw face detections on
image_copy = np.copy(image)
# Convert the image to gray
gray_image = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
# Detect faces in the image using pre-trained face dectector
faces = face_cascade.detectMultiScale(gray_image, 1.25, 6)
# Print number of faces found
print('Number of faces detected:', len(faces))
# Get the bounding box for each detected face
for f in faces:
x, y, w, h = [ v for v in f ]
cv2.rectangle(image_copy, (x,y), (x+w, y+h), (255,0,0), 3)
# Define the region of interest in the image
face_crop = gray_image[y:y+h, x:x+w]
# Display the image with the bounding boxes
fig = plt.figure(figsize = (9,9))
axl = fig.add_subplot(111)
axl.set_xticks([])
axl.set_yticks([])
ax1.set_title("Obamas with Face Detection")
axl.imshow(image_copy)
# Display the face crops
fig = plt.figure(figsize = (9,9))
axl = fig.add_subplot(111)
axl.set_xticks([])
axl.set_yticks([])
axl.set_title("Obamas Face Crops")
axl.imshow(face_crop)
输出结果如下: 目前,它只输出图像中检测到的最后一个面孔。我相信我错过了一些简单的东西,比如一个for循环。
我想能够在所有灰色裁剪的面部图像上运行我的面部关键点检测算法。
谢谢您的帮助!