File size: 9,045 Bytes
aef4429
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import numpy as np
import glob
import cv2
import os
import pickle


def resize_img(img, size=(640, 640), pad_val=0, return_all_infos=False):
    """
    Resize specific image according to original size ratio
    then padding for yielding given size
    
    Input:
        `img` (np.ndarray): original image
        `size` (tuple/list): size of new image (width, height)
        `pad_val` (int): padding value (default: 0)
        `return_all_infos` (bool): Return all informations for creating new sample
    Return:
        `New image` (np.ndarray) with given size
        If `return_all_infos` is True:
            `h_ratio`: h_new/h_old
            `w_ratio`: w_new/w_old
            `pad_top`: number of padding on top of image
            `pad_bot`: number of padding on bot of image
            `pad_left`: number of padding on left of image
            `pad_right`: number of padding on right of image
    """
    # Check pad val
    if isinstance(pad_val, int):
        pad_val = (pad_val, pad_val, pad_val)

    old_size = img.shape[:2]  # h, w

    hw_ratio = old_size[0] / old_size[1]

    if old_size[0] > old_size[1]:
        new_size = (size[0], int(size[1] / hw_ratio))
    else:
        new_size = (int(size[0] * hw_ratio), size[1])

    img = cv2.resize(img, (new_size[1], new_size[0]))  # resize with hw-ratio

    # Get padding corner to yield given new size
    pad_top = (size[0] - new_size[0]) // 2
    pad_bot = size[0] - (new_size[0] + pad_top)
    pad_left = (size[1] - new_size[1]) // 2
    pad_right = size[1] - (new_size[1] + pad_left)

    h_ratio = new_size[0] / size[0]
    w_ratio = new_size[1] / size[1]
    
    # Resize to yield given new size
    img = cv2.copyMakeBorder(img, pad_top, pad_bot, pad_left, pad_right, cv2.BORDER_CONSTANT, value=pad_val)
    
    if return_all_infos:
        return img, h_ratio, w_ratio, pad_top, pad_bot, pad_left, pad_right, new_size
    return img


def get_original_coordinate(detection, img_size, pad_infos):
    """
    Detection result still has padding informations to image
    Need re-calculate to original coordinate info
    """
    pad_top, pad_bot, pad_left, pad_right = pad_infos

    xmin = detection.location_data.relative_bounding_box.xmin
    ymin = detection.location_data.relative_bounding_box.ymin
    width = detection.location_data.relative_bounding_box.width
    height = detection.location_data.relative_bounding_box.height

    xmin_ori = (xmin * img_size[0]) / (img_size[0] - pad_top - pad_bot)
    ymin_ori = (ymin * img_size[1]) / (img_size[1] - pad_left - pad_right)
    width_ori = (width * img_size[0]) / (img_size[0] - pad_top - pad_bot)
    height_ori = (height * img_size[1]) / (img_size[1] - pad_left - pad_right)

    return xmin_ori, ymin_ori, width_ori, height_ori


def get_main_sub_faces(face_detection_infos, min_face_ratio_over_max_face_to_be_main=0.7):
    """
    Determine main or sub attribute for given faces according to
    area ratio according to image and area ratio according to face with max area
    
    Input:
        `face_detection_infos`: face detection info from face detector model
        `min_face_ratio_over_max_face_to_be_main`: area ratio according to max area face to be main
    Return:
        `face_is_main` (list[int]): list of is_main face, if face is main, return 1, otherwise
        `face_ratios` (list[float]): list of area ratios according to image
    """
    face_ratios = [detection.location_data.relative_bounding_box.width*detection.location_data.relative_bounding_box.height for detection in face_detection_infos]
    face_ids = [id for id in range(len(face_detection_infos))]
    face_is_main = [0 for _ in range(len(face_detection_infos))]

    face_ratio_max = np.max(face_ratios)

    for id in face_ids:
        # if ratio according to max face ge given criteria
        if face_ratios[id] / face_ratio_max >= min_face_ratio_over_max_face_to_be_main:
            face_is_main[id] = 1

    return face_is_main, face_ratios


def get_final_result(main_sub_faces, face_ratios, 
                face_detection_infos, face_direction_infos, face_position_infos, face_coordinate_infos,
                face_pose_infos, face_area_infos, chosen_rectangle_pos_list,
                return_all_informations=True):
    """
    Get decision face for yielding final result
    
    Input:
        main_sub_faces (list[int]): list of is_main face, if face is main, return 1, otherwise
        face_ratios (list[float]): list of area ratios according to image
        `face_detection_infos`: face detection info from face detector model
        `face_direction_infos` (list[str]): direction for each face
        `face_position_infos` (list[str]): position for each face
        `face_coordinates_infos`(list(list[float])): coordinates for each face (x1y1x2y2)
        `face_pose_infos` (list(list[float])): pose info for each face (x,y,z)
        `face area_indos` (list[float]): area ratio of face according to image
        `chosen_rectangle_pos_list`(list(list[float])): chosen rectangle for each face
    Return:
        decision infomation of decision face
    """
    face_ids = np.arange(len(main_sub_faces))
    face_ids = [id for _, id in sorted(zip(face_ratios, face_ids), reverse=True)]
    final_id = face_ids[0]  # Return the max ratio face if do not have any `FRONT` face

    for id in face_ids:
        # If face is sub, then the remaining faces also sub
        if main_sub_faces[id] == 0:
            break
        
        # If face is `FRONT`, do not need check the others
        if face_direction_infos[id] == 'FRONT':
            final_id = id
            break
    
    if return_all_informations:
        return face_detection_infos[final_id], face_direction_infos[final_id], face_position_infos[final_id], \
                face_coordinate_infos[final_id], \
                face_pose_infos[final_id], face_area_infos[final_id], chosen_rectangle_pos_list[final_id], \
                final_id
    else:
        return final_id


def get_cache_informations(save_dict, cache_path='cache.pkl'):
    """
    if using cache is enabled, using local cache for storing predicted informations from samples
    avoid time consumption
    
    Input:
        save_dict (dict): current informations
        cache_path (str): path to previous informations
    Return:
        cache_dict (dict): merge informations from preivous cache to current one
    """
    if os.path.exists(cache_path):
        with open(cache_path, 'rb') as f:
            cache_dict = pickle.load(f)

        if cache_dict.get('img_size', None) == save_dict['img_size'] and \
                cache_dict.get('position_horizontal_thresholds', None) == save_dict['position_horizontal_thresholds'] and \
                cache_dict.get('position_vertical_thresholds', None) == save_dict['position_vertical_thresholds']:
            return cache_dict
        return {}
    return {}


def parse_head_pose_informations(head_pose_info, session_state):
    head_pose_info['POSITION']['TOP_LEFT'][:2] = [[0, 0], 
                                            [session_state.position_horizontal_thresholds[0], session_state.position_vertical_thresholds[0]]]
    head_pose_info['POSITION']['TOP'][:2] = [[session_state.position_horizontal_thresholds[0], 0], 
                                            [session_state.position_horizontal_thresholds[1], session_state.position_vertical_thresholds[0]]]
    head_pose_info['POSITION']['TOP_RIGHT'][:2] = [[session_state.position_horizontal_thresholds[1], 0], 
                                            [1, session_state.position_vertical_thresholds[0]]]
    head_pose_info['POSITION']['RIGHT'][:2] = [[session_state.position_horizontal_thresholds[1], session_state.position_vertical_thresholds[0]], 
                                            [1, session_state.position_vertical_thresholds[1]]]
    head_pose_info['POSITION']['BOTTOM_RIGHT'][:2] = [[session_state.position_horizontal_thresholds[1], session_state.position_vertical_thresholds[1]], 
                                            [1, 1]]
    head_pose_info['POSITION']['BOTTOM'][:2] = [[session_state.position_horizontal_thresholds[0], session_state.position_vertical_thresholds[1]], 
                                            [session_state.position_horizontal_thresholds[1], 1]]
    head_pose_info['POSITION']['BOTTOM_LEFT'][:2] = [[0, session_state.position_vertical_thresholds[1]], 
                                            [session_state.position_horizontal_thresholds[0], 1]]
    head_pose_info['POSITION']['LEFT'][:2] = [[0, session_state.position_vertical_thresholds[0]], 
                                            [session_state.position_horizontal_thresholds[0], session_state.position_vertical_thresholds[1]]]
    head_pose_info['POSITION']['MIDDLE'][:2] = [[session_state.position_horizontal_thresholds[0], session_state.position_vertical_thresholds[0]], 
                                            [session_state.position_horizontal_thresholds[1], session_state.position_vertical_thresholds[1]]]