def generate_AUCouple_ROI_mask_image(database_name, img_path): adaptive_AU_database(database_name) global MASK_COLOR mask_color_lst = [] for color in MASK_COLOR: mask_color_lst.append(color_bgr(color)) cropped_face, AU_mask_dict = FaceMaskCropper.get_cropface_and_mask( img_path, channel_first=False) AU_couple_dict = get_zip_ROI_AU() land = FaceLandMark(config.DLIB_LANDMARK_PRETRAIN) landmark, _, _ = land.landmark(image=cropped_face) roi_polygons = land.split_ROI(landmark) for roi_no, polygon_vertex_arr in roi_polygons.items(): polygon_vertex_arr[0, :] = np.round(polygon_vertex_arr[0, :]) polygon_vertex_arr[1, :] = np.round(polygon_vertex_arr[1, :]) polygon_vertex_arr = sort_clockwise(polygon_vertex_arr.tolist()) cv2.polylines(cropped_face, [polygon_vertex_arr], True, color_bgr(RED), thickness=1) font = cv2.FONT_HERSHEY_SIMPLEX cv2.putText(cropped_face, str(roi_no), tuple( np.mean(polygon_vertex_arr, axis=0).astype(np.int32)), font, 0.7, (0, 255, 255), thickness=1) already_fill_AU = set() idx = 0 gen_face_lst = dict() AU_couple_mask = dict() for AU in config.AU_ROI.keys(): AU_couple = AU_couple_dict[AU] if AU_couple in already_fill_AU: continue already_fill_AU.add(AU_couple) mask = AU_mask_dict[AU] AU_couple_mask[AU_couple] = mask color_mask = cv2.cvtColor(mask, cv2.COLOR_GRAY2RGB) color_mask[mask != 0] = random.choice(mask_color_lst) idx += 1 new_face = cv2.addWeighted(cropped_face, 0.75, color_mask, 0.25, 0) gen_face_lst[AU_couple] = new_face return gen_face_lst, AU_couple_mask
def generate_AUCouple_ROI_mask_image(self, database_name, img_path, roi_activate): adaptive_AU_database(database_name) cropped_face, AU_mask_dict = FaceMaskCropper.get_cropface_and_mask( img_path, channel_first=False) AU_couple_dict = get_zip_ROI_AU() land = FaceLandMark(config.DLIB_LANDMARK_PRETRAIN) landmark, _, _ = land.landmark(image=cropped_face) roi_polygons = land.split_ROI(landmark) for roi_no, polygon_vertex_arr in roi_polygons.items(): polygon_vertex_arr[0, :] = np.round(polygon_vertex_arr[0, :]) polygon_vertex_arr[1, :] = np.round(polygon_vertex_arr[1, :]) polygon_vertex_arr = sort_clockwise(polygon_vertex_arr.tolist()) cv2.polylines(cropped_face, [polygon_vertex_arr], True, (0, 0, 255), thickness=1) font = cv2.FONT_HERSHEY_SIMPLEX cv2.putText(cropped_face, str(roi_no), tuple( np.mean(polygon_vertex_arr, axis=0).astype(np.int32)), font, 0.7, (0, 255, 255), thickness=1) already_fill_AU = set() AUCouple_face_dict = dict() for AU in config.AU_ROI.keys(): AU_couple = AU_couple_dict[AU] if AU_couple in already_fill_AU or AU_couple not in roi_activate: continue already_fill_AU.add(AU_couple) mask = AU_mask_dict[AU] color_mask = cv2.cvtColor(mask, cv2.COLOR_GRAY2RGB) color_mask[mask != 0] = (199, 21, 133) new_face = cv2.add(cropped_face, color_mask) AUCouple_face_dict[AU_couple] = new_face return AUCouple_face_dict
def trans_landmark2pointarr(landmark_ls): point_arr = [] for land in landmark_ls: if land.endswith("uu"): land = int(land[:-2]) x, y = landmark[land] y -= 40 point_arr.append((x, y)) elif land.endswith("u"): land = int(land[:-1]) x, y = landmark[land] y -= 20 point_arr.append((x, y)) elif "~" in land: land_a, land_b = land.split("~") land_a = int(land_a) land_b = int(land_b) x = (landmark[land_a][0] + landmark[land_b][0]) / 2 y = (landmark[land_a][1] + landmark[land_b][1]) / 2 point_arr.append((x, y)) else: x, y = landmark[int(land)] point_arr.append((x, y)) return sort_clockwise(point_arr)
def generate_mask_contain_img(database_name, img_path): adaptive_AU_database(database_name) mask_color = {} for parent_color, child_color in MASK_CONTAIN.items(): mask_color[color_bgr(parent_color)] = color_bgr(child_color) cropped_face, AU_mask_dict = FaceMaskCropper.get_cropface_and_mask( img_path, channel_first=False) AU_couple_dict = get_zip_ROI_AU() AU_couple_child = get_AU_couple_child(AU_couple_dict) land = FaceLandMark(config.DLIB_LANDMARK_PRETRAIN) landmark, _, _ = land.landmark(image=cropped_face) roi_polygons = land.split_ROI(landmark) for roi_no, polygon_vertex_arr in roi_polygons.items(): polygon_vertex_arr[0, :] = np.round(polygon_vertex_arr[0, :]) polygon_vertex_arr[1, :] = np.round(polygon_vertex_arr[1, :]) polygon_vertex_arr = sort_clockwise(polygon_vertex_arr.tolist()) cv2.polylines(cropped_face, [polygon_vertex_arr], True, color_bgr(RED), thickness=1) font = cv2.FONT_HERSHEY_SIMPLEX cv2.putText(cropped_face, str(roi_no), tuple( np.mean(polygon_vertex_arr, axis=0).astype(np.int32)), font, 0.7, (0, 255, 255), thickness=1) already_fill_AU = set() gen_face_lst = dict() all_child_set = set() for child_set in AU_couple_child.values(): for child in child_set: all_child_set.add(child) new_face = np.zeros_like(cropped_face) for AU in config.AU_ROI.keys(): AU_couple = AU_couple_dict[AU] if AU_couple in all_child_set: continue if AU_couple in already_fill_AU: continue already_fill_AU.add(AU_couple) mask = AU_mask_dict[AU] child_AU_set = AU_couple_child[AU_couple] color_parent = list(MASK_CONTAIN.keys())[0] color_child = MASK_CONTAIN[color_parent] color_mask = cv2.cvtColor(mask, cv2.COLOR_GRAY2RGB) color_mask[mask != 0] = color_parent # cv2.addWeighted(color_mask,0.5, color_mask,1-0.5,0,color_mask) if np.any(new_face): cropped_face = new_face cv2.addWeighted(cropped_face, 1, color_mask, 0.3, 0, new_face, -1) for child_AU in child_AU_set: if child_AU in already_fill_AU: continue already_fill_AU.add(child_AU) mask = AU_mask_dict[child_AU[0]] color_mask = cv2.cvtColor(mask, cv2.COLOR_GRAY2RGB) color_mask[mask != 0] = random.choice(color_child) cv2.addWeighted(new_face, 1, color_mask, 0.5, 0, new_face, -1) return new_face
#mask = np.zeros(trn_img.shape[:-1], np.uint8) landmark, _, _ = land.landmark(image=trn_img) roi_polygons = land.split_ROI(landmark) in_size = trn_img.shape print(trn_img.shape[0], trn_img.shape[0] * 1 / 4.0) # trn_img = cv2.resize(trn_img, (round(trn_img.shape[1] * 1/4.0), round(trn_img.shape[0] * 1/4.0))) out_size = trn_img.shape print(out_size) y_scale = float(out_size[0]) / in_size[0] x_scale = float(out_size[1]) / in_size[1] print(y_scale, x_scale) for roi_no, polygon_vertex_arr in roi_polygons.items(): # if int(roi_no) == 40 or int(roi_no) == 41: polygon_vertex_arr[0, :] = np.round(x_scale * polygon_vertex_arr[0, :]) polygon_vertex_arr[1, :] = np.round(y_scale * polygon_vertex_arr[1, :]) polygon_vertex_arr = sort_clockwise(polygon_vertex_arr.tolist()) cv2.polylines(trn_img, [polygon_vertex_arr], True, (2, 0, 200), thickness=4) font = cv2.FONT_HERSHEY_SIMPLEX cv2.putText(trn_img, str(roi_no), tuple( np.mean(polygon_vertex_arr, axis=0).astype(np.int32)), font, 1, (0, 255, 255), thickness=2) for AU in config.AU_ROI.keys(): copy_face = trn_img.copy() mask = face_img_mask(AU, face_img_path, land)