[英]Face landmarks detection with dlib
我有以下代码:
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)
我得到以下线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
但是,我检查了人脸的类型(它是 dlib.rectangles)并且 image_1_gray 是 numpy ndarray。 有谁知道为什么这个错误仍然出现?
face
变量可能包含多个值,因此您需要对每个值使用predictor
。
例如:
for (i, rect) in enumerate(face):
face_landmarks = predictor(image_1_gray, rect)
要在面部显示检测到的值:
shp = face_utils.shape_to_np(face_landmarks)
要使用face_utils
,您需要安装imutils
。
您的shp
变量大小很可能是(68, 2)
。 其中68
个是人脸检测点, 2
是(x, y)
坐标元组。
现在,在图像上绘制检测到的人脸:
一、获取坐标
x = rect.left() y = rect.top() w = rect.right() - xh = rect.bottom() - y
绘制坐标:
cv2.rectangle(image_1, (x, y), (x + w, y + h), (0, 255, 0), 2)
图像中可能有多个人脸,因此您可能想要 label 他们
cv2.putText(image_1, "Face #{}".format(i + 1), (x - 10, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
现在在脸上画出 68 个点:
for (x, y) in shp: cv2.circle(image_1, (x, y), 1, (0, 0, 255), -1)
结果:
代码:
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.