2
0
mirror of https://github.com/Shawn-Shan/fawkes.git synced 2024-12-22 07:09:33 +05:30

fix minor issue in cropping faces

This commit is contained in:
Shawn-Shan 2021-04-30 10:09:22 -05:00
parent 0b663ac422
commit b9d3ca46da
4 changed files with 64 additions and 57 deletions

View File

@ -4,7 +4,7 @@
# @Link : https://www.shawnshan.com/ # @Link : https://www.shawnshan.com/
__version__ = '1.0.1' __version__ = '1.0.2'
from .differentiator import FawkesMaskGeneration from .differentiator import FawkesMaskGeneration
from .protection import main, Fawkes from .protection import main, Fawkes

View File

@ -8,14 +8,13 @@ def to_rgb(img):
ret[:, :, 0] = ret[:, :, 1] = ret[:, :, 2] = img ret[:, :, 0] = ret[:, :, 1] = ret[:, :, 2] = img
return ret return ret
def aligner():
return MTCNN()
def align(orig_img, aligner, margin=0.8, detect_multiple_faces=True): def aligner():
return MTCNN(min_face_size=30)
def align(orig_img, aligner):
""" run MTCNN face detector """ """ run MTCNN face detector """
minsize = 20 # minimum size of face
threshold = [0.6, 0.7, 0.7] # three steps's threshold
factor = 0.709 # scale factor
if orig_img.ndim < 2: if orig_img.ndim < 2:
return None return None
@ -23,45 +22,57 @@ def align(orig_img, aligner, margin=0.8, detect_multiple_faces=True):
orig_img = to_rgb(orig_img) orig_img = to_rgb(orig_img)
orig_img = orig_img[:, :, 0:3] orig_img = orig_img[:, :, 0:3]
bounding_boxes = aligner.detect_faces(orig_img) detect_results = aligner.detect_faces(orig_img)
nrof_faces= len(bounding_boxes)
if nrof_faces > 0:
det = bounding_boxes[0]['box']
det_arr = []
img_size = np.asarray(orig_img.shape)[0:2]
if nrof_faces > 1:
margin = margin / 1.5
if detect_multiple_faces:
for i in range(nrof_faces):
det_arr.append(np.squeeze(bounding_boxes[i]['box']))
else:
bounding_box_size = (det[1] + det[3])
img_center = img_size / 2
offsets = np.vstack([(det[0] + det[2]) / 2 - img_center[1],
(det[1] + det[3]) / 2 - img_center[0]])
offset_dist_squared = np.sum(np.power(offsets, 2.0), 0)
index = np.argmax(bounding_box_size - offset_dist_squared * 2.0) # some extra weight on the centering
det_arr.append(det[index, :])
else:
det_arr.append(np.squeeze(det))
cropped_arr = [] cropped_arr = []
bounding_boxes_arr = [] bounding_boxes_arr = []
for i, det in enumerate(det_arr): for dic in detect_results:
det = np.squeeze(det) if dic['confidence'] < 0.9:
bb = np.zeros(4, dtype=np.int32) continue
# add in margin x, y, width, height = dic['box']
marg1 = int((det[2] - det[0]) * margin) if width < 30 or height < 30:
marg2 = int((det[3] - det[1]) * margin) continue
bb = [y, x, y + height, x + width]
bb[0] = max(det[0] - marg1/2, 0) cropped = orig_img[bb[0]:bb[2], bb[1]:bb[3], :]
bb[1] = max(det[1] - marg2/2, 0) cropped_arr.append(np.copy(cropped))
bb[2] = min(det[0] + det[2] + marg1/2, img_size[0]) bounding_boxes_arr.append(bb)
bb[3] = min(det[1] + det[3] + marg2/2, img_size[1])
cropped = orig_img[bb[0]:bb[2], bb[1]: bb[3],:]
cropped_arr.append(cropped)
bounding_boxes_arr.append([bb[0], bb[1], bb[2], bb[3]])
return cropped_arr, bounding_boxes_arr return cropped_arr, bounding_boxes_arr
else:
return None # if nrof_faces > 0:
# det = bounding_boxes[0]['box']
# det_arr = []
# img_size = np.asarray(orig_img.shape)[0:2]
# if nrof_faces > 1:
# margin = margin / 1.5
# if detect_multiple_faces:
# for i in range(nrof_faces):
# det_arr.append(np.squeeze(bounding_boxes[i]['box']))
# else:
# bounding_box_size = (det[1] + det[3])
# img_center = img_size / 2
# offsets = np.vstack([(det[0] + det[2]) / 2 - img_center[1],
# (det[1] + det[3]) / 2 - img_center[0]])
# offset_dist_squared = np.sum(np.power(offsets, 2.0), 0)
# index = np.argmax(bounding_box_size - offset_dist_squared * 2.0) # some extra weight on the centering
# det_arr.append(det[index, :])
# else:
# det_arr.append(np.squeeze(det))
#
# cropped_arr = []
# bounding_boxes_arr = []
# for i, det in enumerate(det_arr):
# det = np.squeeze(det)
# bb = np.zeros(4, dtype=np.int32)
# # add in margin
# marg1 = int((det[2] - det[0]) * margin)
# marg2 = int((det[3] - det[1]) * margin)
#
# bb[0] = max(det[0] - marg1 / 2, 0)
# bb[1] = max(det[1] - marg2 / 2, 0)
# bb[2] = min(det[0] + det[2] + marg1 / 2, img_size[0])
# bb[3] = min(det[1] + det[3] + marg2 / 2, img_size[1])
# cropped = orig_img[bb[0]:bb[2], bb[1]: bb[3], :]
# cropped_arr.append(cropped)
# bounding_boxes_arr.append([bb[0], bb[1], bb[2], bb[3]])
# return cropped_arr, bounding_boxes_arr
# else:
# return None

View File

@ -57,14 +57,14 @@ class Fawkes(object):
def mode2param(self, mode): def mode2param(self, mode):
if mode == 'low': if mode == 'low':
th = 0.0045 th = 0.005
max_step = 25 max_step = 30
lr = 25 lr = 25
extractors = ["extractor_2"] extractors = ["extractor_2"]
elif mode == 'mid': elif mode == 'mid':
th = 0.012 th = 0.012
max_step = 50 max_step = 75
lr = 20 lr = 20
extractors = ["extractor_0", "extractor_2"] extractors = ["extractor_0", "extractor_2"]

View File

@ -145,13 +145,8 @@ class Faces(object):
p = image_paths[i] p = image_paths[i]
self.org_faces.append(cur_img) self.org_faces.append(cur_img)
if eval_local:
margin = 0
else:
margin = 0.7
if not no_align: if not no_align:
align_img = align(cur_img, self.aligner, margin=margin) align_img = align(cur_img, self.aligner)
if align_img is None: if align_img is None:
print("Find 0 face(s) in {}".format(p.split("/")[-1])) print("Find 0 face(s) in {}".format(p.split("/")[-1]))
self.images_without_face.append(i) self.images_without_face.append(i)
@ -221,6 +216,7 @@ class Faces(object):
org_shape = self.cropped_faces_shape[i] org_shape = self.cropped_faces_shape[i]
old_square_shape = max([org_shape[0], org_shape[1]]) old_square_shape = max([org_shape[0], org_shape[1]])
cur_protected = resize(cur_protected, (old_square_shape, old_square_shape)) cur_protected = resize(cur_protected, (old_square_shape, old_square_shape))
cur_original = resize(cur_original, (old_square_shape, old_square_shape)) cur_original = resize(cur_original, (old_square_shape, old_square_shape))