analitics

Pages

Sunday, May 5, 2019

Python 3.7.3 : Using the cognitive face detection from Azure Microsoft .

The Microsoft Azure comes with this free feature named Computer Vision:
This API key is currently active
7 days remaining
Distill actionable information from images
5,000 transactions, 20 per minute.

You can test with the cognitive_face python module from GitHub.
Another good example of extract printed text can be found at docs.microsoft.com.
Let's install it:
C:\Python373\Scripts>pip install cognitive_face
...
Installing collected packages: cognitive-face
Successfully installed cognitive-face-1.4.2
Now a simple intro using the dir:
C:\Python373>python.exe
Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 22:22:05) [MSC v.1916 64 bit (AMD6
4)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import cognitive_face as CF
>>> dir(CF)
['BaseUrl', 'CognitiveFaceException', 'Key', '__builtins__', '__cached__', '__do
c__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__
', 'face', 'face_list', 'large_face_list', 'large_face_list_face', 'large_person
_group', 'large_person_group_person', 'large_person_group_person_face', 'person'
, 'person_group', 'util']
For detections you need to use the face with detect:
import cognitive_face as CF
# Replace with your valid Subscription Key here.
KEY = '111111111111'  
CF.Key.set(KEY)
# Replace with your regional Base URL based by 
# https://azure.microsoft.com/en-us/try/cognitive-services/my-apis/?apiSlug=computer-vision

BASE_URL = 'https://westcentralus.api.cognitive.microsoft.com/vision/v2.0'  
CF.BaseUrl.set(BASE_URL)

img_url = 'https://previews.123rf.com/images/boarding1now/boarding1now150
8/boarding1now150800143/44403889-background-collage-large-group-portrait
-of-multiracial-young-smile-smiling-people-social-media.jpg'
result = CF.face.detect(img_url)
print (result)

img_url = 'https://raw.githubusercontent.com/Microsoft/Cognitive-Face-
Windows/master/Data/detection1.jpg'
result2 = CF.face.detect(img_url)
print (result2)
The result of this running code is:
C:\Python373>python.exe face_cognitive_001.py
{'objects': [{'rectangle': {'x': 145, 'y': 14, 'w': 121, 'h': 160}, 'object': 'p
erson', 'confidence': 0.535}, {'rectangle': {'x': 533, 'y': 25, 'w': 121, 'h': 1
84}, 'object': 'person', 'confidence': 0.535}], 'requestId': '9a3d41e9-b8cc-49ac
-a99b-f3da8d5fac2a', 'metadata': {'width': 1300, 'height': 866, 'format': 'Jpeg'
}}
{'objects': [{'rectangle': {'x': 236, 'y': 0, 'w': 620, 'h': 657}, 'object': 'pe
rson', 'confidence': 0.912}], 'requestId': 'fd415dd3-0f11-4cac-b307-f58888fc875a
', 'metadata': {'width': 1000, 'height': 664, 'format': 'Jpeg'}}
If you take a look at the images and results will see how this works.