Advertisement
pbredikhin

emotion_detect.py

Jul 17th, 2018
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.68 KB | None | 0 0
  1. # Import the Face API SDK
  2. import cognitive_face as Face_API
  3. import requests
  4. from io import BytesIO
  5. from PIL import Image, ImageDraw, ImageFont
  6.  
  7. # Set the key which are you generated in the Azure service in the previous steps
  8. Face_API.Key.set("INSERT_YOUR_KEY_HERE")
  9.  
  10. # Set the service url which defined in the Azure service.
  11. # Because we created the service in the "West Central US" region the link
  12. # will be the next "https://westcentralus.api.cognitive.microsoft.com/face/v1.0"
  13. # Your correct url you can find in the section two http://joxi.ru/ZrJNPlkSwngkJr.png
  14. Face_API.BaseUrl.set("https://westcentralus.api.cognitive.microsoft.com/face/v1.0")
  15.  
  16. # Sending the image to the Face API.
  17. # The image can be the URL, file path or a file-like object represents an image.
  18. # The attributes must be the comma-separated string like "age,emotion".
  19. # Supported face attributes include age, gender, headPose, smile, facialHair,
  20. # glasses, emotion, makeup, accessories, occlusion, blur, exposure, noise.
  21. img_url = "http://joxi.ru/BA0MXPGSMpzY1r.png"
  22. response = Face_API.face.detect(image=img_url, attributes="emotion")
  23. # Printing the response.
  24. print(response)
  25.  
  26. # Get the best mutch emotion and convert
  27. # to the string like "emotion_name: score".
  28. def get_best_emotion(resp):
  29.     emotions = resp["faceAttributes"]["emotion"]
  30.     b_key, b_value = "", 0
  31.     for key, value in emotions.items():
  32.         if value > b_value:
  33.             b_key, b_value = key, value
  34.     return b_key + ": " + str(b_value)
  35.  
  36.  
  37. # Get the face area from the response "faceRectangle".
  38. def get_face_box(resp):
  39.     values = resp["faceRectangle"]
  40.     left = values["left"]
  41.     top = values["top"]
  42.     bottom = left + values["height"]
  43.     right = top + values["width"]
  44.     return ((left, top), (bottom, right))
  45.  
  46.  
  47. # Download the image by the url
  48. image_data = requests.get(img_url)
  49. # Opens and identifies the given image file.
  50. image = Image.open(BytesIO(image_data.content))
  51.  
  52. # Creates an object that can be used to draw in the given image.
  53. draw = ImageDraw.Draw(image)
  54. # Load a TrueType or OpenType font from a file or file-like object,
  55. # and create a font object.
  56. fnt = ImageFont.truetype("Ubuntu-B.ttf", size=13)
  57. # Iterate through all responses and draw box and text.
  58. for resp in response:
  59.     # Get the best emotion.
  60.     emotion = get_best_emotion(resp)
  61.     # Get the face box.
  62.     face_box = get_face_box(resp)
  63.     # Draw the text on the image.
  64.     draw.text((face_box[0][0], face_box[0][1] - 12), emotion, fill="purple", font=fnt)
  65.     # Draw box on the image
  66.     draw.rectangle(face_box, outline="purple")
  67.    
  68.  
  69. # Now you can display the final image.
  70. image.show()
  71. # Or save it.
  72. image.save("output.png", "PNG")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement