整个识别流程为流程:创建人员组->创建人员->添加人脸到人员->训练->识别
subscription_key='7XXXXXXXXXXXXXXXXXXXX71'
uri_base = 'https://api.cognitive.azure.cn'
创建人员组
#创建人员组
#personGroupId为人员组ID,name为人员组名称,userData为用户提供的附加到人员组的数据
def createGroup(personGroupId,name,userData):
try:
headers = {
'Ocp-Apim-Subscription-Key': subscription_key,
}
conn = http.client.HTTPSConnection(uri_base2)
Request='PUT'
#personGroupId=input("输入创建人员组ID(英文):")
#name=input("人员组名称:")
#userData=input("附加到人员组用户提供的数据(可选):")
body=("{"+"'name':'"+name+"',"+"'userData':'"+userData+"'}")
conn.request(Request,"/face/v1.0/persongroups/%s" % personGroupId,body,headers)
response=conn.getresponse()
print(response.reason)
if response.reason=="OK":
print("人员组创建成功")
else:
print("人员组创建失败")
conn.close()
except Exception as e:
print("Error")
print(e)
创建人员需要用到人员组ID
#创建一个人(名字可以相同,但是返回的ID是唯一需要记住)
#personGroupId已创建好的人员组ID,personName创建的人员名称
#返回的personId唯一,需要记住
def addPersonNa(personGroupId,personName):
try:
headers = {
'Content-Type': 'application/json',
'Ocp-Apim-Subscription-Key': subscription_key,
}
name="personone"
userData=""
Request="POST"
body=("{"+"'name':'"+personName+"',"+"'userData':'"+userData+"'}")
conn = http.client.HTTPSConnection('api.cognitive.azure.cn')
conn.request(Request,"/face/v1.0/persongroups/%s/persons?" % personGroupId,body,headers)
response = conn.getresponse()
data = response.read()
print(data)
s1=str(data)
s2=s1[2:-1]
s3=eval(s2)
s4=s3["personId"]
return s4
conn.close()
except Exception as e:
print("Error")
print(e)
向人员中添加人脸(本地上传)
最好在图片中只有一张人脸,如果多人人脸比较麻烦,如果是在多个人脸中选择可以先用人脸检测然后切图为单张人脸再进行人员的人脸添加较为快捷
#向人员中添加人脸(返回的人脸ID是持久的:用作于人脸-识别和人员-删除人脸)
#personGroupId已创建好的人员组ID,personsId已创建好的人员ID,imagesUrl本地图片路径
def addPersonFace(personGroupId,personsId,imagesUrl):
try:
Request='POST'
#Content_Type1传递类型
Content_Type1='application/octet-stream'
headers = {
'Content-Type': Content_Type1,
'Ocp-Apim-Subscription-Key': subscription_key,
}
params = urllib.parse.urlencode({
# Request parameters
'userData': '',
'targetFace': '',
})
#上传本地图片
f=open(imagesUrl,"rb")
body = {'url': imagesUrl}
response = requests.request(Request, uri_base + '/face/v1.0/persongroups/%s/persons/%s/persistedFaces?%s' % (personGroupId,personsId,params), json=body, data=f, headers=headers, params=params)
print ('Response:')
parsed = json.loads(response.text)
print(parsed)
facenums=0;
time.sleep(0.2)
except Exception as e:
print("Error")
print(e)
进行人员训练
#训练图片上传
def gogogoImage(personId):
personGroupId='usergroupwlw'
#imagesUrl="E:\Photo\Data\PersonGroup\Family1-Mom\Family1-Mom1.jpg"
stra="E:/Photo/1/"
strb=".png"
for i in range(200):
imagesUrl='%s%s%s' %(stra,i,strb)
print(imagesUrl)
addPersonFace(personGroupId,personId,imagesUrl)
训练的状态可以用如下查询
#查询训练人员组状态
def seltrainGroup(personGroupId):
try:
headers = {
# Request headers
'Ocp-Apim-Subscription-Key': subscription_key,
}
params = urllib.parse.urlencode({
})
Request="GET"
conn = http.client.HTTPSConnection('api.cognitive.azure.cn')
conn.request(Request, "/face/v1.0/persongroups/%s/training?%s" % (personGroupId,params), "{body}", headers)
response = conn.getresponse()
data = response.read()
print(data)
conn.close()
except Exception as e:
print("Error")
print(e)
训练速度还是很快的,训练完毕后就可以使用识别了