Python Forum
Loop face landmarking in all folder using dlib - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Loop face landmarking in all folder using dlib (/thread-25948.html)



Loop face landmarking in all folder using dlib - lokoprof - Apr-16-2020

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.



RE: Loop face landmarking in all folder using dlib - deanhystad - Apr-16-2020

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.


RE: Loop face landmarking in all folder using dlib - lokoprof - Apr-17-2020

(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


RE: Loop face landmarking in all folder using dlib - deanhystad - Apr-17-2020

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")



RE: Loop face landmarking in all folder using dlib - lokoprof - Apr-17-2020

(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.


RE: Loop face landmarking in all folder using dlib - deanhystad - Apr-17-2020

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.