Spaces:
Build error
Build error
Upload mask_the_face.py
Browse files- mask_the_face.py +132 -0
mask_the_face.py
ADDED
|
@@ -0,0 +1,132 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Author: aqeelanwar
|
| 2 |
+
# Created: 27 April,2020, 10:22 PM
|
| 3 |
+
# Email: aqeel.anwar@gatech.edu
|
| 4 |
+
|
| 5 |
+
import argparse
|
| 6 |
+
import dlib
|
| 7 |
+
from aux_functions import *
|
| 8 |
+
import numpy as np
|
| 9 |
+
|
| 10 |
+
def maskThisImages(myImg):
|
| 11 |
+
|
| 12 |
+
# Command-line input setup
|
| 13 |
+
parser = argparse.ArgumentParser(
|
| 14 |
+
description="MaskTheFace - Python code to mask faces dataset"
|
| 15 |
+
)
|
| 16 |
+
parser.add_argument(
|
| 17 |
+
"--path",
|
| 18 |
+
type=str,
|
| 19 |
+
default="",
|
| 20 |
+
help="Path to either the folder containing images or the image itself",
|
| 21 |
+
)
|
| 22 |
+
parser.add_argument(
|
| 23 |
+
"--mask_type",
|
| 24 |
+
type=str,
|
| 25 |
+
default="surgical",
|
| 26 |
+
choices=["surgical", "N95", "KN95", "cloth", "gas", "inpaint", "random", "all"],
|
| 27 |
+
help="Type of the mask to be applied. Available options: all, surgical_blue, surgical_green, N95, cloth",
|
| 28 |
+
)
|
| 29 |
+
|
| 30 |
+
parser.add_argument(
|
| 31 |
+
"--pattern",
|
| 32 |
+
type=str,
|
| 33 |
+
default="",
|
| 34 |
+
help="Type of the pattern. Available options in masks/textures",
|
| 35 |
+
)
|
| 36 |
+
|
| 37 |
+
parser.add_argument(
|
| 38 |
+
"--pattern_weight",
|
| 39 |
+
type=float,
|
| 40 |
+
default=0.5,
|
| 41 |
+
help="Weight of the pattern. Must be between 0 and 1",
|
| 42 |
+
)
|
| 43 |
+
|
| 44 |
+
parser.add_argument(
|
| 45 |
+
"--color",
|
| 46 |
+
type=str,
|
| 47 |
+
default="cyan",
|
| 48 |
+
help="Hex color value that need to be overlayed to the mask",
|
| 49 |
+
)
|
| 50 |
+
|
| 51 |
+
parser.add_argument(
|
| 52 |
+
"--color_weight",
|
| 53 |
+
type=float,
|
| 54 |
+
default=0.5,
|
| 55 |
+
help="Weight of the color intensity. Must be between 0 and 1",
|
| 56 |
+
)
|
| 57 |
+
|
| 58 |
+
parser.add_argument(
|
| 59 |
+
"--code",
|
| 60 |
+
type=str,
|
| 61 |
+
# default="cloth-masks/textures/check/check_4.jpg, cloth-#e54294, cloth-#ff0000, cloth, cloth-masks/textures/others/heart_1.png, cloth-masks/textures/fruits/pineapple.png, N95, surgical_blue, surgical_green",
|
| 62 |
+
default="",
|
| 63 |
+
help="Generate specific formats",
|
| 64 |
+
)
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
parser.add_argument(
|
| 68 |
+
"--verbose", dest="verbose", action="store_true", help="Turn verbosity on"
|
| 69 |
+
)
|
| 70 |
+
parser.add_argument(
|
| 71 |
+
"--write_original_image",
|
| 72 |
+
dest="write_original_image",
|
| 73 |
+
action="store_true",
|
| 74 |
+
help="If true, original image is also stored in the masked folder",
|
| 75 |
+
)
|
| 76 |
+
parser.set_defaults(feature=False)
|
| 77 |
+
|
| 78 |
+
args, unknown = parser.parse_known_args()
|
| 79 |
+
args.write_path = args.path + "_masked"
|
| 80 |
+
|
| 81 |
+
# Set up dlib face detector and predictor
|
| 82 |
+
args.detector = dlib.get_frontal_face_detector()
|
| 83 |
+
path_to_dlib_model = "dlib_models/shape_predictor_68_face_landmarks.dat"
|
| 84 |
+
if not os.path.exists(path_to_dlib_model):
|
| 85 |
+
download_dlib_model()
|
| 86 |
+
|
| 87 |
+
args.predictor = dlib.shape_predictor(path_to_dlib_model)
|
| 88 |
+
|
| 89 |
+
# Extract data from code
|
| 90 |
+
mask_code = "".join(args.code.split()).split(",")
|
| 91 |
+
args.code_count = np.zeros(len(mask_code))
|
| 92 |
+
args.mask_dict_of_dict = {}
|
| 93 |
+
|
| 94 |
+
|
| 95 |
+
for i, entry in enumerate(mask_code):
|
| 96 |
+
print
|
| 97 |
+
mask_dict = {}
|
| 98 |
+
mask_color = ""
|
| 99 |
+
mask_texture = ""
|
| 100 |
+
mask_type = entry.split("-")[0]
|
| 101 |
+
if len(entry.split("-")) == 2:
|
| 102 |
+
mask_variation = entry.split("-")[1]
|
| 103 |
+
if "#" in mask_variation:
|
| 104 |
+
mask_color = mask_variation
|
| 105 |
+
else:
|
| 106 |
+
mask_texture = mask_variation
|
| 107 |
+
mask_dict["type"] = mask_type
|
| 108 |
+
mask_dict["color"] = mask_color
|
| 109 |
+
mask_dict["texture"] = mask_texture
|
| 110 |
+
args.mask_dict_of_dict[i] = mask_dict
|
| 111 |
+
|
| 112 |
+
# Check if path is file or directory or none
|
| 113 |
+
is_file=True
|
| 114 |
+
# Process if the path was a file
|
| 115 |
+
if is_file:
|
| 116 |
+
print("Masking image file")
|
| 117 |
+
image_path = args.path
|
| 118 |
+
write_path = args.path.rsplit(".")[0]
|
| 119 |
+
if True:
|
| 120 |
+
# Proceed if file is image
|
| 121 |
+
# masked_images, mask, mask_binary_array, original_image
|
| 122 |
+
masked_image, mask, mask_binary_array, original_image = mask_image(
|
| 123 |
+
myImg, args
|
| 124 |
+
)
|
| 125 |
+
if len(masked_image)==0:
|
| 126 |
+
return masked_image
|
| 127 |
+
else:
|
| 128 |
+
img = masked_image[i]
|
| 129 |
+
return img
|
| 130 |
+
else:
|
| 131 |
+
print("Path is neither a valid file or a valid directory")
|
| 132 |
+
print("Processing Done")
|