Skip to content

Instantly share code, notes, and snippets.

@CT83
Created July 21, 2018 18:23
Show Gist options
  • Save CT83/98f257e00651efcdea65f31ea9c9a743 to your computer and use it in GitHub Desktop.
Save CT83/98f257e00651efcdea65f31ea9c9a743 to your computer and use it in GitHub Desktop.
Minimum Working Example Face API Python.
import os
import cognitive_face as CF
from cognitive_face import CognitiveFaceException
KEY = ''
CF.Key.set(KEY)
BASE_URL = 'https://centralindia.api.cognitive.microsoft.com/face/v1.0' # Replace with your regional Base URL
CF.BaseUrl.set(BASE_URL)
personGroup = 'bassist'
personName = 'Peter Hook'
try:
CF.large_person_group.delete(personGroup)
except CognitiveFaceException:
pass
try:
CF.large_person_group.create(personGroup)
except CognitiveFaceException:
pass
res = CF.large_person_group_person.create(personGroup, personName)
person_id = res['personId']
print("Person ID:", person_id)
person_id_name = {person_id: 'Peter Hook'}
image_urls = ['Peter_Hook/train/1.jpg',
'Peter_Hook/train/2.jpg',
'Peter_Hook/train/3.jpg',
'Peter_Hook/train/4.jpg',
'Peter_Hook/train/5.jpg',
'Peter_Hook/train/6.jpg',
'Peter_Hook/train/7.jpg',
'Peter_Hook/train/8.jpg',
'Peter_Hook/train/9.jpg',
'Peter_Hook/train/10.jpg',
]
test_image = 'Peter_hook/test/11.jpg'
# Add all faces
for image_url in image_urls:
try:
if os.path.isfile(image_url):
print("Adding Image", image_url)
persisted_face = CF.large_person_group_person_face.add(image_url,
personGroup, person_id)
except CognitiveFaceException:
pass
res = CF.large_person_group.train(personGroup)
detectedResults = CF.face.detect(test_image)
faceIds = []
for result in detectedResults:
faceIds.append(result['faceId'])
print("Face Ids:", faceIds)
identifyResults = CF.face.identify(faceIds,
large_person_group_id=personGroup)
print("identified Results:", identifyResults)
person_results = identifyResults[0]['candidates'][0]['personId']
print('Results for first Person', person_results)
print("Person in Image:", person_id_name[person_results])
try:
CF.large_person_group.delete(personGroup)
except CognitiveFaceException:
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment