Skip to content

Commit

Permalink
Introduced code argument
Browse files Browse the repository at this point in the history
  • Loading branch information
Anwar, Malik Aqeel committed Jul 30, 2020
1 parent 62dd26b commit 249dc21
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 12 deletions.
47 changes: 43 additions & 4 deletions mask_the_face.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@
parser.add_argument(
"--path",
type=str,
default="data/images16.jpg",
help="Path to the folder containing images within folders as classes",
default="F:\VGGface2/vggface2_train\original_160_subset",
help="Path to either the folder containing images or the image itself",
)
parser.add_argument(
"--mask_type",
type=str,
default="N95",
choices=['surgical', 'N95', 'KN95', 'cloth', 'gas', 'inpaint', 'random', 'all'],
help="Type of the mask to be applied. Available options: all, surgical_blue, surgical_green, N95, cloth",
)

Expand Down Expand Up @@ -50,6 +51,15 @@
default=0.5,
help="Weight of the color intensity. Must be between 0 and 1",
)

parser.add_argument(
"--code",
type = str,
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",
help="Generate specific formats",
)


parser.add_argument(
"--verbose", dest="verbose", action="store_true", help="Turn verbosity on"
)
Expand All @@ -58,7 +68,31 @@
args = parser.parse_args()
args.write_path = args.path + "_masked"

# Check in path is file or directory or none

# Extract data from code
mask_code = "".join(args.code.split()).split(',')
args.code_count = np.zeros(len(mask_code))
args.mask_dict_of_dict = {}

for i, entry in enumerate(mask_code):
mask_dict = {}
mask_color = ''
mask_texture = ''
mask_type = entry.split('-')[0]
if len(entry.split('-'))==2:
mask_variation = entry.split('-')[1]
if '#' in mask_variation:
mask_color = mask_variation
else:
mask_texture = mask_variation
mask_dict['type'] = mask_type
mask_dict['color'] = mask_color
mask_dict['texture'] = mask_texture

args.mask_dict_of_dict[i] = mask_dict


# Check if path is file or directory or none
is_directory, is_file, is_other = check_path(args.path)
display_MaskTheFace()

Expand Down Expand Up @@ -132,10 +166,15 @@
+ "."
+ split_path[1]
)

w_path_original = write_path + "/" + f
img = masked_image[i]
# Write the masked image
cv2.imwrite(w_path, img)

# Write the original image
cv2.imwrite(w_path_original, original_image)
print(args.code_count)

# Process if the path was a file
elif is_file:
print("Masking image file")
Expand Down
1 change: 0 additions & 1 deletion masks/masks.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,6 @@ mask_f: 636, 404
mask_e: 523, 502
mask_d: -103, 406


[KN95]
template: masks/templates/KN95.png
mask_a: 20, 47
Expand Down
19 changes: 13 additions & 6 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# MaskTheFace - Convert face dataset to masked dataset
![cover_photo](images/MaskTheFace.png)
## What is MaskTheFace?
MaskTheFace is computer vision based scripts to mask faces in images. It provides a number of masks to select from.
It is difficult to collect mask dataset under various conditions. MaskTheFace can be used to convert any existing face dataset to masked-face dataset.
MaskTheFace is computer vision based scripts to mask faces in images.
It provides a number of masks to select from. It is difficult to collect mask dataset under various conditions. MaskTheFace can be used to convert any existing face dataset to masked-face dataset.
MaskTheFace identifies all the faces within an image, and applies the user selected masks to them taking into account various limitations such as face angle, mask fit, lighting conditions etc.
A single image, or entire directory of images can be used as input to code.
![cover_photo](images/example1.png)
Expand Down Expand Up @@ -36,10 +36,16 @@ python mask_the_face.py --path 'data/office.jpg' --mask_type 'N95' --verbose
```
![cover_photo](images/run.png)
### Arguments
* __--path__: is used to provide the path of the image file or the directory containing face images.
* __--mask_type__: is used to select the mask to be applied. Available options are 'N95', 'surgical_blue', 'surgical_green', 'cloth', 'empty' and 'inpaint'. The details of these mask types can be seen in the image above. More masks will be added
* __--verbose__: used to display useful messages during conversion

| Argument | Explanation |
|:--------------:|:-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------:|
| path | Path to the image file or a folder containing images to be masked |
| mask_type | Select the mask to be applied. Available options are 'N95', 'surgical_blue', 'surgical_green', 'cloth', 'empty' and 'inpaint'. The details of these mask types can be seen in the image above. More masks will be added |
| pattern | Selects the pattern to be applied to the mask-type selected above. The textures are available in the masks/textures folder. User can add more textures. |
| pattern_weight | Selects the intensity of the pattern to be applied on the mask. The value should be between 0 (no texture strength) to 1 (maximum texture strength) |
| color | Selects the color to be applied to the mask-type selected above. The colors are provided as hex values. |
| color_weight | Selects the intensity of the color to be applied on the mask. The value should be between 0 (no color strength) to 1 (maximum color strength) |
| code | Can be used to create specific mask formats at random. More can be found in the section below. |
| verbose | If set to True, will be used to display useful messages during masking |

## Supported Masks:
### Mask Types:
Expand All @@ -48,6 +54,7 @@ Currently MaskTheFace supports the following 4 mask types
2. N95
3. KN95
4. Cloth
5. Gas

![mask_types](images/mask_types.png)

Expand Down
11 changes: 10 additions & 1 deletion utils/aux_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,16 @@ def mask_image(image_path, args):
face_locations = face_recognition.face_locations(image)
# draw_landmarks(face_landmarks_list[0], image)

if mask_type == "random":
# Check if specific code was provided for masking, overwrite arguments
if args.code:
ind = random.randint(0, len(args.code_count)-1)
mask_dict = args.mask_dict_of_dict[ind]
mask_type = mask_dict['type']
args.color = mask_dict['color']
args.pattern = mask_dict['texture']
args.code_count[ind]+=1

elif mask_type == "random":
available_mask_types = get_available_mask_types()
mask_type = random.choice(available_mask_types)

Expand Down

0 comments on commit 249dc21

Please sign in to comment.