Created
November 26, 2022 05:25
-
-
Save dennisseah/7398b28649a62e4d56cb76dfbbc1758f to your computer and use it in GitHub Desktop.
Azure Face Recognition
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
# azure-cognitiveservices-vision-face==0.6.0 | |
# image==1.5.33 | |
import os | |
import json | |
import requests | |
from azure.cognitiveservices.vision.face import FaceClient | |
from azure.cognitiveservices.vision.face.models import DetectedFace | |
from msrest.authentication import CognitiveServicesCredentials | |
from io import BytesIO | |
from PIL import Image, ImageDraw | |
KEY = os.environ["AZURE_FACE_REC_KEY"] | |
ENDPOINT = os.environ["AZURE_FACE_REC_ENDPOINT"] | |
face_client = FaceClient(ENDPOINT, CognitiveServicesCredentials(KEY)) | |
def getRectangle(faceDictionary): | |
rect = faceDictionary.face_rectangle | |
left = rect.left | |
top = rect.top | |
bottom = left + rect.height | |
right = top + rect.width | |
return ((left, top), (bottom, right)) | |
def info(detected_face: DetectedFace): | |
return { | |
"face_id": detected_face.face_id, | |
"face_attributes": { | |
"age": detected_face.face_attributes.age, | |
"gender": detected_face.face_attributes.gender, | |
"smile": detected_face.face_attributes.smile, | |
"facial_hair": { | |
"moustache": detected_face.face_attributes.facial_hair.moustache, | |
"beard": detected_face.face_attributes.facial_hair.beard, | |
"sideburns": detected_face.face_attributes.facial_hair.sideburns | |
}, | |
"glasses": detected_face.face_attributes.glasses, | |
"head_pose": { | |
"roll": detected_face.face_attributes.head_pose.roll, | |
"yaw": detected_face.face_attributes.head_pose.yaw, | |
"pitch": detected_face.face_attributes.head_pose.pitch | |
}, | |
"emotion": { | |
"anger": detected_face.face_attributes.emotion.anger, | |
"contempt": detected_face.face_attributes.emotion.contempt, | |
"disgust": detected_face.face_attributes.emotion.disgust, | |
"fear": detected_face.face_attributes.emotion.fear, | |
"happiness": detected_face.face_attributes.emotion.happiness, | |
"neutral": detected_face.face_attributes.emotion.neutral, | |
"sadness": detected_face.face_attributes.emotion.sadness, | |
"surprise": detected_face.face_attributes.emotion.surprise, | |
}, | |
"hair": { | |
"bald": detected_face.face_attributes.hair.bald, | |
"invisible": detected_face.face_attributes.hair.invisible, | |
"hair_color": [{ | |
"color": c.color, | |
"confidence": c.confidence | |
} for c in detected_face.face_attributes.hair.hair_color] | |
}, | |
"makeup": { | |
"eye_makeup": detected_face.face_attributes.makeup.eye_makeup, | |
"lip_makeup": detected_face.face_attributes.makeup.lip_makeup | |
}, | |
"occlusion": { | |
"forehead_occluded": detected_face.face_attributes.occlusion.forehead_occluded, | |
"eye_occluded": detected_face.face_attributes.occlusion.eye_occluded, | |
"mouth_occluded": detected_face.face_attributes.occlusion.mouth_occluded | |
}, | |
"accessories": [ | |
{"type": a.type, "confidence": a.confidence} for a in detected_face.face_attributes.accessories], | |
"blur": { | |
"blur_level": detected_face.face_attributes.blur.blur_level, | |
"value": detected_face.face_attributes.blur.value | |
}, | |
"exposure": { | |
"exposure_level": detected_face.face_attributes.exposure.exposure_level, | |
"value": detected_face.face_attributes.exposure.value, | |
}, | |
"noise": { | |
"noise_level": detected_face.face_attributes.noise.noise_level, | |
"value": detected_face.face_attributes.noise.value, | |
}, | |
"mask": detected_face.face_attributes.mask, | |
"quality_for_recognition": detected_face.face_attributes.quality_for_recognition | |
} | |
} | |
def detect(image_url: str): | |
detected_faces = face_client.face.detect_with_url( | |
url=image_url, | |
return_face_attributes=[ | |
"age", | |
"gender", | |
"headPose", | |
"smile", | |
"facialHair", | |
"glasses", | |
"emotion", | |
"hair", | |
"makeup", | |
"occlusion", | |
"accessories", | |
"blur", | |
"exposure", | |
"noise" | |
]) | |
if detected_faces: | |
print(json.dumps([info(face) for face in detected_faces], indent=4)) | |
response = requests.get(image_url) | |
img = Image.open(BytesIO(response.content)) | |
draw = ImageDraw.Draw(img) | |
for face in detected_faces: | |
draw.rectangle(getRectangle(face), outline="red", width=3) | |
img.show() | |
if __name__ == "__main__": | |
detect("https://raw.githubusercontent.com/Microsoft/Cognitive-Face-Windows/master/Data/detection1.jpg") | |
# image from https://www.nist.gov/news-events/news/2020/07/nist-launches-studies-masks-effect-face-recognition-software | |
detect("https://www.nist.gov/sites/default/files/styles/960_x_960_limit/public/images/2020/07/27/DoubleRow.png?itok=5CfcABWm") | |
# image from newscientist.com | |
detect("https://images.newscientist.com/wp-content/uploads/2022/02/14174128/PRI_223554170.jpg?width=800") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment