Skip to content
This repository has been archived by the owner on Nov 8, 2021. It is now read-only.

Commit

Permalink
Support more face attributes in SDK & Sample. (#11)
Browse files Browse the repository at this point in the history
* Update docstrings for face.detect by introducing emotion.

* Update sample to support headPose,facialHair,emotion.

* Add missing emotion attribute.

* Update README.md.

* pylint.

* Bump version to 1.2.5.
  • Loading branch information
huxuan authored Mar 18, 2017
1 parent c4965ae commit 2044352
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 15 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ python sample
```

## Contributing
We welcome contributions. Feel free to file issues and pull requests on the repo and we'll address them as we can. Learn more about how you can help on our [Contribution Rules & Guidelines](</CONTRIBUTING.md>).
We welcome contributions. Feel free to file issues and pull requests on the repo and we'll address them as we can. Learn more about how you can help on our [Contribution Rules & Guidelines](</CONTRIBUTING.md>).

You can reach out to us anytime with questions and suggestions using our communities below:
- **Support questions:** [StackOverflow](<https://stackoverflow.com/questions/tagged/microsoft-cognitive>)
Expand All @@ -68,7 +68,7 @@ You can reach out to us anytime with questions and suggestions using our communi
This project has adopted the [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/). For more information see the [Code of Conduct FAQ](https://opensource.microsoft.com/codeofconduct/faq/) or contact [[email protected]](mailto:[email protected]) with any additional questions or comments.

## Updates
* 8/2016: Sample updated for [July 2016 Face API changes](https://www.microsoft.com/cognitive-services/en-us/face-api/documentation/overview#changes)
* [Face API Release Notes](https://www.microsoft.com/cognitive-services/en-us/face-api/documentation/ReleaseNotes)

## License
All Microsoft Cognitive Services SDKs and samples are licensed with the MIT License. For more details, see
Expand Down
19 changes: 10 additions & 9 deletions cognitive_face/face.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,16 @@ def detect(image, face_id=True, landmarks=False, attributes=''):
Args:
image: A URL or a file path or a file-like object represents an image.
face_id: Optional parameter. Return `face_id`s of the detected faces or
not. The default value is `True`.
landmarks: Optional parameter. Return face landmarks of the detected
faces or not. The default value is `False`.
attributes: Optional parameter. Analyze and return the one or more
specified face attributes in the comma-separated string like
`age,gender`. Supported face attributes include age, gender,
headPose, smile, facialHair, and glasses. Note that each face
attribute analysis has additional computational and time cost.
face_id: [Optional] Return faceIds of the detected faces or not. The
default value is true.
landmarks: [Optional] Return face landmarks of the detected faces or
not. The default value is false.
attributes: [Optional] Analyze and return the one or more specified
face attributes in the comma-separated string like
"returnFaceAttributes=age,gender". Supported face attributes
include age, gender, headPose, smile, facialHair, glasses and
emotion. Note that each face attribute analysis has additional
computational and time cost.
Returns:
An array of face entries ranked by face rectangle size in descending
Expand Down
11 changes: 11 additions & 0 deletions sample/model/face.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,19 @@ def __init__(self, res, path, size=util.MAX_THUMBNAIL_SIZE):
attr = res['faceAttributes']
self.age = int(attr['age'])
self.gender = attr['gender']
self.head_pose = "Pitch: {}, Roll:{}, Yaw:{}".format(
attr['headPose']['pitch'],
attr['headPose']['roll'],
attr['headPose']['yaw']
)
self.smile = float(attr['smile']) > 0 and 'Smile' or 'Not Smile'
self.facial_hair = sum(attr['facialHair'].values()) > 0 and 'Yes' \
or 'No'
self.glasses = attr['glasses']
self.emotion = max(
attr['emotion'],
key=lambda key: attr['emotion'][key]
)
self.bmp = util.scale_bitmap(self.bmp, size)

def set_name(self, name):
Expand Down
1 change: 1 addition & 0 deletions sample/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
)
LOG_FACE_LIST_NOT_EXIST = 'Response: Face List {} does not exist before.'
LOG_FACE_LIST_EXIST = 'Response: Face List {} exists.'
LABEL_FACE = '{} years old, {}\n{}\n{}, {}\nFacial Hair: {}\nEmotion: {}\n'


class SubscriptionKey(object):
Expand Down
7 changes: 5 additions & 2 deletions sample/view/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,11 +242,14 @@ def OnDrawItem(self, dc, rect, index):

textx = rect.x + 2 + face.bmp.GetWidth() + 2
label_rect = wx.Rect(textx, rect.y, rect.width - textx, rect.height)
label = '{} years old\n{}\n{}\n{}\n'.format(
label = util.LABEL_FACE.format(
face.age,
face.gender,
face.head_pose,
face.smile,
face.glasses
face.glasses,
face.facial_hair,
face.emotion
)
dc.DrawLabel(label, label_rect, wx.ALIGN_LEFT | wx.ALIGN_TOP)

Expand Down
2 changes: 1 addition & 1 deletion sample/view/panel_detection.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def async_detect(self, path):
self.rsizer.Layout()
self.vhsizer.Layout()

attributes = 'age,gender,smile,glasses'
attributes = 'age,gender,headPose,smile,facialHair,glasses,emotion'
res = util.CF.face.detect(path, False, False, attributes)
faces = [model.Face(face, path) for face in res]
self.face_list.SetItems(faces)
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def readme():

setup(
name='cognitive_face',
version='1.2.1',
version='1.2.5',
packages=find_packages(exclude=['tests']),
install_requires=['requests'],
author='Microsoft',
Expand Down

0 comments on commit 2044352

Please sign in to comment.