def mask_face(image, face_location, six_points, angle, args, type="surgical"): debug = False # Find the face angle threshold = 13 if angle < -threshold: type += "_right" elif angle > threshold: type += "_left" face_height = face_location[2] - face_location[0] face_width = face_location[1] - face_location[3] # image = image_raw[ # face_location[0]-int(face_width/2): face_location[2]+int(face_width/2), # face_location[3]-int(face_height/2): face_location[1]+int(face_height/2), # :, # ] # cv2.imshow('win', image) # cv2.waitKey(0) # Read appropriate mask image w = image.shape[0] h = image.shape[1] if not "empty" in type and not "inpaint" in type: cfg = read_cfg(config_filename="masks/masks.cfg", mask_type=type, verbose=False) else: if "left" in type: str = "surgical_blue_left" elif "right" in type: str = "surgical_blue_right" else: str = "surgical_blue" cfg = read_cfg(config_filename="masks/masks.cfg", mask_type=str, verbose=False) img = cv2.imread(cfg.template, cv2.IMREAD_UNCHANGED) # Process the mask if necessary if args.pattern: # Apply pattern to mask img = texture_the_mask(img, args.pattern, args.pattern_weight) if args.color: # Apply color to mask img = color_the_mask(img, args.color, args.color_weight) mask_line = np.float32([ cfg.mask_a, cfg.mask_b, cfg.mask_c, cfg.mask_f, cfg.mask_e, cfg.mask_d ]) # Warp the mask M, mask = cv2.findHomography(mask_line, six_points) dst_mask = cv2.warpPerspective(img, M, (h, w)) dst_mask_points = cv2.perspectiveTransform(mask_line.reshape(-1, 1, 2), M) mask = dst_mask[:, :, 3] face_height = face_location[2] - face_location[0] face_width = face_location[1] - face_location[3] image_face = image[face_location[0] + int(face_height / 2):face_location[2], face_location[3]:face_location[1], :, ] image_face = image # Adjust Brightness mask_brightness = get_avg_brightness(img) img_brightness = get_avg_brightness(image_face) delta_b = 1 + (img_brightness - mask_brightness) / 255 dst_mask = change_brightness(dst_mask, delta_b) # Adjust Saturation mask_saturation = get_avg_saturation(img) img_saturation = get_avg_saturation(image_face) delta_s = 1 - (img_saturation - mask_saturation) / 255 dst_mask = change_saturation(dst_mask, delta_s) # Apply mask mask_inv = cv2.bitwise_not(mask) img_bg = cv2.bitwise_and(image, image, mask=mask_inv) img_fg = cv2.bitwise_and(dst_mask, dst_mask, mask=mask) out_img = cv2.add(img_bg, img_fg[:, :, 0:3]) if "empty" in type or "inpaint" in type: out_img = img_bg # Plot key points if "inpaint" in type: out_img = cv2.inpaint(out_img, mask, 3, cv2.INPAINT_TELEA) # dst_NS = cv2.inpaint(img, mask, 3, cv2.INPAINT_NS) if debug: for i in six_points: cv2.circle(out_img, (i[0], i[1]), radius=4, color=(0, 0, 255), thickness=-1) for i in dst_mask_points: cv2.circle(out_img, (i[0][0], i[0][1]), radius=4, color=(0, 255, 0), thickness=-1) return out_img, mask
def mask_face(image, face_location, six_points, angle, args, type="surgical"): debug = False # Find the face angle threshold = 13 if angle < -threshold: type += "_right" elif angle > threshold: type += "_left" face_height = face_location[2] - face_location[0] face_width = face_location[1] - face_location[3] if face_height > face_width: w = image.shape[0] h = image.shape[1] if not "empty" in type and not "inpaint" in type: cfg = read_cfg(config_filename="masks/masks.cfg", mask_type=type, verbose=False) else: if "left" in type: str = "surgical_blue_left" elif "right" in type: str = "surgical_blue_right" else: str = "surgical_blue" cfg = read_cfg(config_filename="masks/masks.cfg", mask_type=str, verbose=False) img = cv2.imread(cfg.template, cv2.IMREAD_UNCHANGED) # Process the mask if necessary if args.pattern: # Apply pattern to mask img = texture_the_mask(img, args.pattern, random.uniform(0.0, 0.7)) if args.color: img = color_the_mask(img, args.color, random.uniform(0.0, 0.7)) mask_line = np.float32( [cfg.mask_a, cfg.mask_b, cfg.mask_c, cfg.mask_f, cfg.mask_e, cfg.mask_d] ) # Warp the mask M, mask = cv2.findHomography(mask_line, six_points) dst_mask = cv2.warpPerspective(img, M, (h, w)) dst_mask_points = cv2.perspectiveTransform(mask_line.reshape(-1, 1, 2), M) mask = dst_mask[:, :, 3] face_height = face_location[2] - face_location[0] face_width = face_location[1] - face_location[3] image_face = image # Adjust Brightness mask_brightness = get_avg_brightness(img) img_brightness = get_avg_brightness(image_face) delta_b = 1 + (img_brightness - mask_brightness) / 255 # print('delta_b:', delta_b) dst_mask = change_brightness(dst_mask, delta_b) # Adjust Saturation mask_saturation = get_avg_saturation(img) img_saturation = get_avg_saturation(image_face) delta_s = 1 - (img_saturation - mask_saturation) / 255 # print('delta_s:', delta_s) dst_mask = change_saturation(dst_mask, delta_s) # Apply mask mask_inv = cv2.bitwise_not(mask) inv_mask_inv = cv2.bitwise_not(mask_inv) img_bg = image # img_bg_hsv = cv2.cvtColor(img_bg, cv2.COLOR_RGB2HSV) # h_raw, s_raw, v_raw = cv2.split(img_bg_hsv) img_fg = cv2.bitwise_and(dst_mask,dst_mask, mask=mask) # img_fg_blur = cv2.GaussianBlur(img_fg, (3,3), 5) # cv2.namedWindow("img_fg_blur", cv2.IMREAD_UNCHANGED) # cv2.imshow("img_fg_blur", img_fg_blur) img_fg_rgba = cv2.cvtColor(img_fg, cv2.COLOR_RGB2RGBA) img_fg_rgba[:,:,3] = inv_mask_inv # cv2.namedWindow("img_fg_rgba", cv2.IMREAD_UNCHANGED) # cv2.imshow("img_fg_rgba", img_fg_rgba) ret, thresh = cv2.threshold(img_fg_rgba,0,255,cv2.THRESH_BINARY) # print('ret, thresh:', ret, thresh) ave = cv2.mean(thresh)[0]/255 # print("ave:", ave) if ave > 0.1: blur_score = variance_of_laplacian(img_bg) img_fg_rgba_pil = Image.fromarray(img_fg_rgba) img_bg_pil = Image.fromarray(img_bg) img_bg_pil.paste(img_fg_rgba_pil,(0,0),img_fg_rgba_pil) img_bg_np = np.asarray(img_bg_pil) if blur_score <= 100: gaussian_blur = (7,7) weight_blend = 0.45 elif blur_score > 100 and blur_score <= 300: gaussian_blur = (5,5) weight_blend = 0.5 else: gaussian_blur = (3,3) weight_blend = 0.65 blur_output = cv2.GaussianBlur(img_bg_np, gaussian_blur , 3) histogram_blend = hist_norm(blur_output,img_bg_np) blend_img = cv2.addWeighted(img_bg_np, (1 - weight_blend), blur_output, weight_blend, 3) blend_img = np.asarray(blend_img) # print(histogram_blend) # cv2.namedWindow("blend_img", cv2.WINDOW_NORMAL) # cv2.imshow("blend_img", blend_img) # cv2.waitKey(0) return blend_img else: return None
def mask_face(image, face_location, six_points, angle, args, type="surgical"): cvimage = image for i in six_points: cv2.circle(cvimage, (i[0], i[1]), radius=4, color=(0, 0, 255), thickness=-1) cv2.imwrite('E:\\2020FA\\EECS504\project\\report\\scale2.png', cvimage) # cv2.imshow("image", cvimage) # Find the face angle threshold = 13 if angle < -threshold: type += "_right" elif angle > threshold: type += "_left" w = image.shape[0] h = image.shape[1] if not "empty" in type and not "inpaint" in type: cfg = read_cfg(config_filename="masks/masks.cfg", mask_type=type, verbose=False) else: if "left" in type: str = "surgical_blue_left" elif "right" in type: str = "surgical_blue_right" else: str = "surgical_blue" cfg = read_cfg(config_filename="masks/masks.cfg", mask_type=str, verbose=False) img = cv2.imread(cfg.template, cv2.IMREAD_UNCHANGED) mask_line = np.float32( [cfg.mask_a, cfg.mask_b, cfg.mask_c, cfg.mask_f, cfg.mask_e, cfg.mask_d] ) # Warp the mask M, mask = cv2.findHomography(mask_line, six_points) dst_mask = cv2.warpPerspective(img, M, (h, w)) mask = dst_mask[:, :, 3] face_height = face_location[3] - face_location[1] print("face_location[3] - face_location[1]: {} - {} = {}.".format(face_location[3], face_location[1], face_height)) image_face = image # Adjust Brightness mask_brightness = get_avg_brightness(img) img_brightness = get_avg_brightness(image_face) delta_b = 1 + (img_brightness - mask_brightness) / 255 dst_mask = change_brightness(dst_mask, delta_b) # Adjust Saturation mask_saturation = get_avg_saturation(img) img_saturation = get_avg_saturation(image_face) delta_s = 1 - (img_saturation - mask_saturation) / 255 dst_mask = change_saturation(dst_mask, delta_s) # Apply mask mask_inv = cv2.bitwise_not(mask) img_bg = cv2.bitwise_and(image, image, mask=mask_inv) img_fg = cv2.bitwise_and(dst_mask, dst_mask, mask=mask) out_img = cv2.add(img_bg, img_fg[:, :, 0:3]) if "empty" in type or "inpaint" in type: out_img = img_bg # Plot key points if "inpaint" in type: out_img = cv2.inpaint(out_img, mask, 3, cv2.INPAINT_TELEA) return out_img, mask