Created
March 1, 2021 18:12
-
-
Save ThibautLucas/33cf9f1b06735b36d32ed3f35b43abdf to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def preprocess(path): | |
img = cv2.imread(path) | |
input_tensor = tf.convert_to_tensor(image_np) | |
return input_tensor | |
def blur(img, bbox): | |
imagette = img[bbox[0]:bbox[2], bbox[1]:bbox[3]] | |
w, h = imagette.shape[:2] | |
to_blur = imagette[0:int(0.7*w), :, :] | |
blur = cv2.medianBlur(to_blur,55) | |
imagette[0:int(0.7*w), :, :] = blur | |
img[bbox[0]:bbox[2], bbox[1]:bbox[3]] = imagette | |
return img | |
def anonymize(face_detector, imgpath, output_dir, threshold=0.5): | |
im = cv2.imread(imgpath) | |
input_image = preprocess(imgpath) | |
input_tensor = tf.convert_to_tensor(im) | |
input_tensor = input_tensor[tf.newaxis, ...] | |
detections = face_detector(input_tensor) | |
num_detections = int(detections.pop('num_detections')) | |
detections = {key: value[0, :num_detections].numpy() | |
for key, value in detections.items()} | |
detections['num_detections'] = num_detections | |
detections['detection_classes'] = detections['detection_classes'].astype(np.int64) | |
(w, h) = im.shape[:2] | |
im1 = im.copy() | |
for bbox, conf in list(zip(detections["detection_boxes"], detections["detection_scores"])): | |
if conf > threshold: | |
bbox = bbox * np.array([w, h, w, h]) | |
bbox = bbox.astype("int") | |
im = blur(im, bbox) | |
output_path = os.path.join(output_dir,imgpath.split('/')[-1]) | |
cv2.imwrite(os.path.join(output_dir, imgpath.split('/')[-1]), im) | |
return |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment