我已经有了一个面部地标探测器,并且可以使用opencv和dlib保存图像,代码如下:
# import the necessary packages
from imutils import face_utils
import numpy as np
import argparse
import imutils
import dlib
import cv2
# construct the argument parser and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-p", "--shape-predictor", required=True, help="Path to facial landmark predictor")
ap.add_argument("-i", "--image", required=True, help="Path to input image")
args = vars(ap.parse_args())
# initialize dlib's face detector (HOG-based) and then create the facial landmark predictor
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor(args["shape_predictor"])
# load the input image, resize it, and convert it to grayscale
image = cv2.imread(args["image"])
image = imutils.resize(image, width=500)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# detect faces in the grayscale image
rects = detector(gray, 1)
for (i, rect) in enumerate(rects):
# determine the facial landmarks for the face region, then
# convert the landmark (x, y)-coordinates to a NumPy array
shape = predictor(gray, rect)
shape = face_utils.shape_to_np(shape)
# loop over the face parts individually
print(face_utils.FACIAL_LANDMARKS_IDXS.items())
for (name, (i, j)) in face_utils.FACIAL_LANDMARKS_IDXS.items():
print(" i = ", i, " j = ", j)
# clone the original image so we can draw on it, then
# display the name of the face part of the image
clone = image.copy()
cv2.putText(clone, name, (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2)
# loop over the subset of facial landmarks, drawing the
# specific face part using a red dots
for (x, y) in shape[i:j]:
cv2.circle(clone, (x, y), 1, (0, 0, 255), -1)
# extract the ROI of the face region as a separate image
(x, y, w, h) = cv2.boundingRect(np.array([shape[i:j]]))
roi = image[y:y+h,x:x+w]
roi = imutils.resize(roi, width=250, inter=cv2.INTER_CUBIC)
# show the particular face part
cv2.imshow("ROI", roi)
cv2.imwrite(name + '.jpg', roi)
cv2.imshow("Image", clone)
cv2.waitKey(0)
# visualize all facial landmarks with a transparent overly
output = face_utils.visualize_facial_landmarks(image, shape)
cv2.waitKey(0)
我有阿诺德的脸,我用opencv imwrite
保存了他的一部分脸。在
我要做的是只得到下巴的图像,我不想保存颈部部分。如下图所示:
除了dlib
检测到的下巴,有人知道我该如何移除其他部分吗。在
+=
现在还不太清楚你试图掩盖多少原始图像。假设您使用的是
shape_predictor_68_face_landmarks.dat
,DLib的标志点0到16定义了下颌线,因此您可以制作一个遮罩,将这些标记延伸到框架的下半部分。在请原谅我粗糙的python技巧集,但是代码会遮住下巴线以下的部分,并将图像剪切到感兴趣的区域,以匹配您问题中预期的输出。在
相关问题 更多 >
编程相关推荐