File size: 1,667 Bytes
cf27ba5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e425192
cf27ba5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e425192
 
 
 
 
cf27ba5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import os
from tqdm import tqdm

from src.landmark_detector import MediaPipeLandmarkDetector, DlibLandmarkDetector


def get_images_and_landmarks(image_list, is_dlib):
     
    # Get the list of images in the directory
    image_paths = []
    for file in image_list:
        if file.endswith(".jpg") or file.endswith(".png"):
            image_paths.append(file)
        else:
            print(f"Skipping file: {file}. Not a supported image format. (jpg or png)")

    if len(image_paths) < 2:
        raise ValueError("At least two images are required for morphing.")
        # exit()


    landmarks_list = [] # List of landmarks for each image
    images_list = []    # List of images

    # Initialize the landmark detector
    landmark_detector = DlibLandmarkDetector() if is_dlib else MediaPipeLandmarkDetector()

    print("Generating landmarks for the images...")

    # Detect landmarks for each image
    for image_path in tqdm(image_paths):
        try:
            landmarks, image = landmark_detector.get_landmarks(image_path)
            landmarks_list.append(landmarks)
            images_list.append(image)
        except Exception as e:
            print(f"{e} \nSkipping image: {image_path}\n")
            continue

    if len(landmarks_list) < 2:
        raise ValueError("At least two faces are required for morphing.")
        # exit()

    # if images dont have the same dimensions raise an error
    if len(set([image.shape for image in images_list])) > 1:
        raise ValueError("Images must have the same dimensions for morphing.")
        # exit()

    return images_list, landmarks_list