Last active
November 3, 2018 10:30
-
-
Save ranman/4777d8adafbd16d4bb79b41823a94fd4 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
import os | |
from PIL import Image, ImageFilter | |
import boto3 | |
rek = boto3.client("rekognition") | |
def get_face_boxes(faces, source_size): | |
return [ | |
( | |
int(f['BoundingBox']['Left'] * source_size[0]), | |
int(f['BoundingBox']['Top'] * source_size[1]), | |
int((f['BoundingBox']['Left'] + f['BoundingBox']['Width']) * source_size[0]), | |
int((f['BoundingBox']['Top'] + f['BoundingBox']['Height']) * source_size[1]), | |
f['Pose']['Roll'] | |
) | |
for f in faces | |
] | |
def blur_faces(image): | |
im = Image.open(image) | |
source_size = im.size[0], im.size[1] | |
image.seek(0) | |
resp = rek.detect_faces(Image={"Bytes": image.read()}) | |
faces = [face for face in resp['FaceDetails'] if face['Confidence'] > 90] | |
for face_box in get_face_boxes(faces, source_size): | |
print("Blurring Face") | |
face_img = im.copy() | |
blurred_face = face_img.crop(face_box[:4]) | |
blurred_face = blurred_face.filter(ImageFilter.GaussianBlur(20)) | |
im.paste(blurred_face, (face_box[0], face_box[1])) | |
return im |
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
pth = os.path.expanduser("~/Downloads/example.jpeg") | |
with open(pth, 'rb') as f: | |
img = blur_faces(f) | |
img.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment