Apr-16-2020, 05:02 PM
I have this code that landmarked selected regions (points) in human face using dlib. I wrote the code to only work for one image, but I have many images in my folder and I want the code to do same for all the faces in my folder "images" and print or save all the coordinates. This is my code below. Please help me edit to work for all images in my folder "images".
import cv2
import dlib
import glob
import numpy as np
import pandas as pd
# set up the 68 point facial landmark detector
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")
img = cv2.imread("images/image_2_jpg.jpg", 1)
img_gray=cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
# detect faces in the image
faces_in_image = detector(img_gray, 1)
# loop through each face in image and print rectangle
for face in faces_in_image:
x, y = face.left(), face.top()
x1, y1 = face.right(), face.bottom()
cv2.rectangle(img, (x, y), (x1, y1), (0, 255, 0), 1)
#generating custom landmarks
landmarks = predictor(img_gray, face)
a = [36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 27, 30, 31, 32, 33, 34, 35, 48, 54, 50, 51, 52, 57]
for n in a:
x = landmarks.part(n).x
y = landmarks.part(n).y
cv2.circle(img, (x, y), 2, (255, 0, 0), -1)
# print out the coordinates
print(x, y)
# show the output image with the face detections + facial landmarks
cv2.imshow("img", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
# output
face-1
x y
23 33
33 43
87 56
face-2
x y
23 33
33 43
87 56
etc.
