Created
October 16, 2018 21:25
-
-
Save bnorberg/0f56c568320cb1506bd3485063e7a690 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 cognitive_face as CF | |
import requests | |
from io import BytesIO | |
from PIL import Image, ImageDraw | |
KEY = 'ADD YOUR FACE API KEY HERE' # Replace with a valid subscription key (keeping the quotes in place). | |
CF.Key.set(KEY) | |
BASE_URL = 'https://westcentralus.api.cognitive.microsoft.com/face/v1.0/' # Replace with your regional Base URL | |
CF.BaseUrl.set(BASE_URL) | |
# You can use this example JPG or replace the URL below with your own URL to a JPEG image. | |
#img_url = 'https://raw.githubusercontent.com/Microsoft/Cognitive-Face-Windows/master/Data/detection1.jpg' #uncomment out to load this picture | |
img_url = 'https://www.maxpixel.net/static/photo/2x/People-The-Faces-Of-A-Person-Girl-The-Person-Face-369342.jpg' #comment out to ignore this picture | |
faces = CF.face.detect(img_url) | |
print(faces) | |
def getRectangle(faceDictionary): | |
rect = faceDictionary['faceRectangle'] | |
left = rect['left'] | |
top = rect['top'] | |
bottom = left + rect['height'] | |
right = top + rect['width'] | |
return ((left, top), (bottom, right)) | |
def croparea(faceDictionary): | |
rect = faceDictionary['faceRectangle'] | |
left = rect['left'] | |
top = rect['top'] | |
bottom = left + rect['height'] | |
right = top + rect['width'] | |
return (left, top, bottom, right) | |
#Download the image from the url | |
response = requests.get(img_url) | |
img = Image.open(BytesIO(response.content)) | |
#For each face returned use the face rectangle and draw a red box. | |
draw = ImageDraw.Draw(img) | |
f = 0 | |
for face in faces: | |
draw.rectangle(getRectangle(face), outline='red') | |
crop_img = img.crop(croparea(face)) | |
f = f+1 | |
crop_img.save('/Users/brn5/face_crop_{}.png'.format(f)) | |
#Display the image in the users default image browser. | |
#img.show() | |
img.save('/Users/brn5/face.png') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment