[英]Face landmarks detection with dlib
I have the following code:我有以下代码:
image_1 = cv2.imread('headshot13-14-2.jpg')
image_1_rgb = cv2.cvtColor(image_1, cv2.COLOR_BGR2RGB)
image_1_gray = cv2.cvtColor(image_1_rgb, cv2.COLOR_BGR2GRAY)
p = "shape_predictor_68_face_landmarks.dat"
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor(p)
face = detector(image_1_gray)
face_landmarks = predictor(image_1_gray, face)
And I get the following error for the line face = predictor(image_1_gray, face)
:我得到以下线
face = predictor(image_1_gray, face)
的错误:
TypeError: __call__(): incompatible function arguments. The following argument types are supported:
1. (self: dlib.shape_predictor, image: array, box: dlib.rectangle) -> dlib.full_object_detection
However, I checked the type of face (it's dlib.rectangles) and the image_1_gray is a numpy ndarray.但是,我检查了人脸的类型(它是 dlib.rectangles)并且 image_1_gray 是 numpy ndarray。 Does anyone have any idea why this error still show up?
有谁知道为什么这个错误仍然出现?
face
variable may contain multiple values, therefore you need to use predictor
for each value. face
变量可能包含多个值,因此您需要对每个值使用predictor
。
For instance:例如:
for (i, rect) in enumerate(face):
face_landmarks = predictor(image_1_gray, rect)
To display the detected values on the face:要在面部显示检测到的值:
shp = face_utils.shape_to_np(face_landmarks)
To use face_utils
, you need to install imutils
.要使用
face_utils
,您需要安装imutils
。
Most probably your shp
variable size is (68, 2)
.您的
shp
变量大小很可能是(68, 2)
。 Where 68
is detected points in the face and the 2
is the (x, y)
coordinate tuples.其中
68
个是人脸检测点, 2
是(x, y)
坐标元组。
Now, to draw the detected face on the image:现在,在图像上绘制检测到的人脸:
First, get the coordinates一、获取坐标
x = rect.left() y = rect.top() w = rect.right() - xh = rect.bottom() - y
Draw the coordinates:绘制坐标:
cv2.rectangle(image_1, (x, y), (x + w, y + h), (0, 255, 0), 2)
There may be multiple face in the image, so you may want to label them图像中可能有多个人脸,因此您可能想要 label 他们
cv2.putText(image_1, "Face #{}".format(i + 1), (x - 10, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
Now draw the 68 points on the face:现在在脸上画出 68 个点:
for (x, y) in shp: cv2.circle(image_1, (x, y), 1, (0, 0, 255), -1)
Result:结果:
Code:代码:
for (i, rect) in enumerate(face):
face_landmarks = predictor(image_1_gray, rect)
shp = face_utils.shape_to_np(face_landmarks)
x = rect.left()
y = rect.top()
w = rect.right() - x
h = rect.bottom() - y
cv2.rectangle(image_1, (x, y), (x + w, y + h), (0, 255, 0), 2)
cv2.putText(image_1, "Face #{}".format(i + 1), (x - 10, y - 10),
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
for (x, y) in shp:
cv2.circle(image_1, (x, y), 1, (0, 0, 255), -1)
cv2.imshow("face", image_1)
cv2.waitKey(0)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.