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
#2
Step 1 is write your program like this:
def find_faces(filename):
    """Function that does everything you are doing right now for 1 file"""

find_faces("image_file_name.jpg")
find_faces("another_image_filename.jpg")
Once you get that working you will replace the two function calls with a loop that iterates over filenames in a folder. There are may ways to iterate though files in a folder and several tutorials on the web.
Reply
#3
(Apr-16-2020, 05:25 PM)deanhystad Wrote: Step 1 is write your program like this:
def find_faces(filename):
    """Function that does everything you are doing right now for 1 file"""

find_faces("image_file_name.jpg")
find_faces("another_image_filename.jpg")
Once you get that working you will replace the two function calls with a loop that iterates over filenames in a folder. There are may ways to iterate though files in a folder and several tutorials on the web.

Thank you for your time but I don't really get what you are explaining here. Can you please show a sample complete code using my coding above. Will be much apreciated. Thanks
Reply
#4
I am guessing that starting with the imread command
img = cv2.imread("images/image_2_jpg.jpg", 1)
...
to the end of the file is the code you want to execute for each image. This would be the body of your function that processes the image in a file.
def process_image(filename):
    """Process image in file filename"""
    print("Processing image file', filename)
    img = cv2.imread(filename, 1)
    …
Below this you would call the function to process an image.
process_image("images/image_2_jpg.jpg")
Reply
#5
(Apr-17-2020, 08:48 AM)deanhystad Wrote: I am guessing that starting with the imread command
img = cv2.imread("images/image_2_jpg.jpg", 1)
...
to the end of the file is the code you want to execute for each image. This would be the body of your function that processes the image in a file.
def process_image(filename):
    """Process image in file filename"""
    print("Processing image file', filename)
    img = cv2.imread(filename, 1)
    …
Below this you would call the function to process an image.
process_image("images/image_2_jpg.jpg")

Thank you so much, this will only process each image one after the other by calling the function repeatedly. But I will like to process all just once.
Reply
#6
I don't understand what you mean by "process all just once"

If you mean call the program once and have it process all files in a directory, there are lots of tutorials on how to "iterate over files in directory" using Python.
Reply


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 530 Jan-05-2024, 05:16 PM
Last Post: Pedroski55
  How to loop through all excel files and sheets in folder jadelola 1 4,459 Dec-01-2022, 06:12 PM
Last Post: deanhystad
  How to zoom on/follow MOUTH in FACE? (OPENCV Face Detection) buzzdarkyear 2 1,794 Jan-12-2022, 12:31 AM
Last Post: buzzdarkyear
  How to crop eyes/nose/mouth in the face only? [OpenCV] buzzdarkyear 0 2,144 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,471 Dec-18-2021, 09:32 PM
Last Post: Larz60+
  pip install dlib packge issue MahendraYadav 1 6,850 Aug-31-2021, 07:06 PM
Last Post: snippsat
  Face detector project? korenron 2 2,104 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,466 Mar-22-2021, 10:59 AM
Last Post: shantanu97
  How to skip a folder directory in a loop mfkzolo 2 12,522 Nov-18-2020, 07:56 AM
Last Post: mfkzolo
  Python Cut/Copy paste file from folder to another folder rdDrp 4 5,038 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