[英]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.