Skip to content

Commit 81b6423

Browse files
authored
Face samples (#18)
1 parent 5ebce75 commit 81b6423

File tree

6 files changed

+86
-1
lines changed

6 files changed

+86
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ This project framework provides examples for the following services:
3131

3232
### Vision
3333

34+
* Using the **Face SDK** [azure-cognitiveservices-vision-face](http://pypi.python.org/pypi/azure-cognitiveservices-vision-face) for the [Face API](https://azure.microsoft.com/services/cognitive-services/face/)
3435
* Using the **Computer Vision SDK** [azure-cognitiveservices-vision-computervision](http://pypi.python.org/pypi/azure-cognitiveservices-vision-computervision) for the [Computer Vision API](https://azure.microsoft.com/services/cognitive-services/computer-vision/)
3536
* Using the **Content Moderator SDK** [azure-cognitiveservices-vision-contentmoderator](http://pypi.python.org/pypi/azure-cognitiveservices-vision-contentmoderator) for the [Content Moderator API](https://azure.microsoft.com/services/cognitive-services/content-moderator/)
3637
* Using the **Custom Vision SDK** [azure-cognitiveservices-vision-customvision](http://pypi.python.org/pypi/azure-cognitiveservices-vision-customvision) for the [Custom Vision API](https://azure.microsoft.com/services/cognitive-services/custom-vision-service/)

requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@ azure-cognitiveservices-search-visualsearch
1111
azure-cognitiveservices-search-websearch
1212
azure-cognitiveservices-vision-computervision>=0.2.0 # sample won't work with previous versions
1313
azure-cognitiveservices-vision-contentmoderator
14-
azure-cognitiveservices-vision-customvision
14+
azure-cognitiveservices-vision-customvision
15+
azure-cognitiveservices-vision-face

samples/vision/face_samples.py

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import os.path
2+
3+
from azure.cognitiveservices.vision.face import FaceClient
4+
from azure.cognitiveservices.vision.face.models import FaceAttributeType, Gender
5+
from msrest.authentication import CognitiveServicesCredentials
6+
7+
SUBSCRIPTION_KEY_ENV_NAME = "FACE_SUBSCRIPTION_KEY"
8+
FACE_LOCATION = os.environ.get("FACE_LOCATION", "westcentralus")
9+
10+
IMAGES_FOLDER = os.path.join(os.path.dirname(os.path.realpath(__file__)), "images", "Face")
11+
12+
def face_detect(subscription_key):
13+
"""ImageAnalysisInStream.
14+
15+
This will analysis an image from a stream and return all available features.
16+
"""
17+
face_base_url = "https://{}.api.cognitive.microsoft.com".format(FACE_LOCATION)
18+
face_client = FaceClient(face_base_url, CognitiveServicesCredentials(subscription_key))
19+
20+
faces = [jpgfile for jpgfile in os.listdir(IMAGES_FOLDER) if jpgfile.startswith("Family1")]
21+
faces_ids = []
22+
23+
for face in faces:
24+
with open(os.path.join(IMAGES_FOLDER, face), "rb") as face_fd:
25+
# result type: azure.cognitiveservices.vision.face.models.DetectedFace
26+
result = face_client.face.detect_with_stream(
27+
face_fd,
28+
# You can use enum from FaceAttributeType, or direct string
29+
return_face_attributes=[
30+
FaceAttributeType.age, # Could have been the string 'age'
31+
'gender',
32+
'headPose',
33+
'smile',
34+
'facialHair',
35+
'glasses',
36+
'emotion',
37+
'hair',
38+
'makeup',
39+
'occlusion',
40+
'accessories',
41+
'blur',
42+
'exposure',
43+
'noise'
44+
]
45+
)
46+
47+
if not result:
48+
print("Unable to detect any face in {}".format(face))
49+
50+
detected_face = result[0]
51+
faces_ids.append(detected_face.face_id)
52+
53+
print("\nImage {}".format(face))
54+
print("Detected age: {}".format(detected_face.face_attributes.age))
55+
print("Detected gender: {}".format(detected_face.face_attributes.gender))
56+
print("Detected emotion: {}".format(detected_face.face_attributes.emotion.happiness))
57+
print("\n")
58+
59+
# Verification example for faces of the same person.
60+
verify_result = face_client.face.verify_face_to_face(
61+
faces_ids[0],
62+
faces_ids[1],
63+
)
64+
if verify_result.is_identical:
65+
print("Faces from {} & {} are of the same (Positive) person, similarity confidence: {}.".format(faces[0], faces[1], verify_result.confidence))
66+
else:
67+
print("Faces from {} & {} are of different (Negative) persons, similarity confidence: {}.".format(faces[0], faces[1], verify_result.confidence))
68+
69+
# Verification example for faces of different persons.
70+
verify_result = face_client.face.verify_face_to_face(
71+
faces_ids[1],
72+
faces_ids[2],
73+
)
74+
if verify_result.is_identical:
75+
print("Faces from {} & {} are of the same (Positive) person, similarity confidence: {}.".format(faces[1], faces[2], verify_result.confidence))
76+
else:
77+
print("Faces from {} & {} are of different (Negative) persons, similarity confidence: {}.".format(faces[1], faces[2], verify_result.confidence))
78+
79+
if __name__ == "__main__":
80+
import sys, os.path
81+
sys.path.append(os.path.abspath(os.path.join(__file__, "..", "..")))
82+
from tools import execute_samples
83+
execute_samples(globals(), SUBSCRIPTION_KEY_ENV_NAME)
43.7 KB
Loading
45.5 KB
Loading
37.1 KB
Loading

0 commit comments

Comments
 (0)