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)
:
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. 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.
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
.
Most probably your shp
variable size is (68, 2)
. Where 68
is detected points in the face and the 2
is the (x, y)
coordinate tuples.
Now, to draw the detected face on the image:
First, get the coordinates
x = rect.left()
y = rect.top()
w = rect.right() - x
h = 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
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:
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)
You can also look at the tutorial