Skip to content

Instantly share code, notes, and snippets.

@skalpin
Last active July 10, 2017 19:03
Show Gist options
  • Save skalpin/4d8f3542b1d984a1d735233feed859c5 to your computer and use it in GitHub Desktop.
Save skalpin/4d8f3542b1d984a1d735233feed859c5 to your computer and use it in GitHub Desktop.
Show image of faces with red rectangle from cognitive services.
import cognitive_face as CF
import requests
from io import BytesIO
from PIL import Image, ImageDraw
def getRectangle(faceDictionary):
rect = faceDictionary['faceRectangle']
left = rect['left']
top = rect['top']
bottom = top + rect['height']
right = left + rect['width']
return ((left, top), (right, bottom))
KEY = 'your_key' # Replace with a valid subscription key (keeping the quotes in place).
CF.Key.set(KEY) # You can use this example JPG or replace the URL below with your own URL to a JPEG image.
# If you need to you can change your base api url with:
#CF.BaseUrl.set('https://westcentralus.api.cognitive.microsoft.com/face/v1.0/')
img_url = 'https://aka.ms/fourthhorizoncrewphoto'
faces = CF.face.detect(img_url)
print(faces)
response = requests.get(img_url)
img = Image.open(BytesIO(response.content))
draw = ImageDraw.Draw(img)
for face in faces:
draw.rectangle(getRectangle(face), outline='red')
img.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment