File size: 1,110 Bytes
0b756df
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
detect_conditions = [
    "left most",
    "right most",
    "top most",
    "bottom most",
    "most width",
    "most height",
]


def analyse_face(image, model, return_single_face=True, detect_condition="left most"):
    faces = model.get(image)
    if not return_single_face:
        return faces

    total_faces = len(faces)
    if total_faces == 1:
        return faces[0]

    print(f"{total_faces} face detected. Using {detect_condition} face.")
    if detect_condition == "left most":
        return sorted(faces, key=lambda face: face["bbox"][0])[0]
    elif detect_condition == "right most":
        return sorted(faces, key=lambda face: face["bbox"][0])[-1]
    elif detect_condition == "top most":
        return sorted(faces, key=lambda face: face["bbox"][1])[0]
    elif detect_condition == "bottom most":
        return sorted(faces, key=lambda face: face["bbox"][1])[-1]
    elif detect_condition == "most width":
        return sorted(faces, key=lambda face: face["bbox"][2])[-1]
    elif detect_condition == "most height":
        return sorted(faces, key=lambda face: face["bbox"][3])[-1]