I am trying to build a program to create a passport photo from an image.
input.png :
I have been able to remove the background and find the chin as passport measurements need to be from chin to top of the hair to create the photo.
The issue I am having is finding the top of the hair. A percentage from the forehead will not work as everyone has different hair styles.
Here is my current code for finding the chin value using dlib:
import cv2import dlib ???def crop_head(image_path, output_path): # Load the image image = cv2.imread(image_path) if image is None: print("Error loading image") return # Initialize the face detector from dlib detector = dlib.get_frontal_face_detector() predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat") # Detect faces in the image faces = detector(image) if len(faces) == 0: print("No faces found in the image") return # Assume there's only one face in the image face = faces[0] # Get the facial landmarks for the face landmarks = predictor(image, face) # Get the chin landmark (point 8) chin_point = landmarks.part(8) # Calculate the cropping area x, y, w, h = face.left(), face.top(), face.width(), face.height() chin_y = chin_point.y chin_padding = 0.2 # 20% padding below the chin y_end = min(y + h + int((chin_y - y - h) * (1 + chin_padding)), image.shape[0]) # Crop the head from the image cropped_head = image[y:y_end, x:x+w] # Save the cropped head to the output path cv2.imwrite(output_path, cropped_head) print("Head cropped and saved successfully")def main(image_path, output_path): crop_head(image_path, output_path)image_path = ????output_path = 'output.png'main(image_path, output_path)This code only crops the image face out on the chin(correctly) however generally crops it on mid forehead and I need to find the top of the hair. I have tried several options on the internet using opencv etc however none find the top of the hair.
Can someone point me in the right direction?Thanks
