Python Forum
Loop face landmarking in all folder using dlib
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Loop face landmarking in all folder using dlib
#1
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.
Reply


Messages In This Thread
Loop face landmarking in all folder using dlib - by lokoprof - Apr-16-2020, 05:02 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Compare folder A and subfolder B and display files that are in folder A but not in su Melcu54 3 636 Jan-05-2024, 05:16 PM
Last Post: Pedroski55
  How to loop through all excel files and sheets in folder jadelola 1 4,612 Dec-01-2022, 06:12 PM
Last Post: deanhystad
  How to zoom on/follow MOUTH in FACE? (OPENCV Face Detection) buzzdarkyear 2 1,839 Jan-12-2022, 12:31 AM
Last Post: buzzdarkyear
  How to crop eyes/nose/mouth in the face only? [OpenCV] buzzdarkyear 0 2,202 Jan-11-2022, 01:41 PM
Last Post: buzzdarkyear
  Compare filename with folder name and copy matching files into a particular folder shantanu97 2 4,590 Dec-18-2021, 09:32 PM
Last Post: Larz60+
  pip install dlib packge issue MahendraYadav 1 7,020 Aug-31-2021, 07:06 PM
Last Post: snippsat
  Face detector project? korenron 2 2,161 Mar-24-2021, 03:43 PM
Last Post: korenron
  Move file from one folder to another folder with timestamp added end of file shantanu97 0 2,522 Mar-22-2021, 10:59 AM
Last Post: shantanu97
  How to skip a folder directory in a loop mfkzolo 2 12,723 Nov-18-2020, 07:56 AM
Last Post: mfkzolo
  Python Cut/Copy paste file from folder to another folder rdDrp 4 5,170 Aug-19-2020, 12:40 PM
Last Post: rdDrp

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020