认知服务-如何从实时流向Azure FaceList添加多个面孔?(Python) [英] Cognitive Services - How to add multiple Faces from a Live Stream to Azure FaceList ? (Python)
问题描述
问题背景:
我已经创建了一个Azure FaceList,并且正在使用网络摄像头捕获实时供稿,并且:
I have created an Azure FaceList and I am using my webcam to capture live feed and:
- 将流发送到Azure 人脸检测
- 获取人脸检测 返回的人脸矩形
- 使用返回的Face矩形将从实时视频流中检测到的面部添加到我的FaceList.
- sending the stream to Azure Face Detect
- getting Face Rectangle returned by Face Detect
- using the returned Face rectangle to add Face Detected from Live Video Stream to my FaceList.
((我需要创建面孔列表"以解决我在另一个问题中回答的问题,该问题由Nicolas回答了>,这就是我的关注
(I need to create Face List in order to solve the problem I explained in my other question which is answered by Nicolas, which is what I am following)
问题详细信息:
根据位于 https:/的Azure FaceList文档/docs.microsoft.com/zh-cn/rest/api/cognitiveservices/face/facelist/addfacefromstream ,如果图像中有多个面孔,则需要指定目标面孔以添加到Azure FaceList.
According to Azure FaceList documentation at https://docs.microsoft.com/en-us/rest/api/cognitiveservices/face/facelist/addfacefromstream ,if there are multiple faces in the image, we need to specify the target Face to add to Azure FaceList.
问题是,如果我们需要在人脸列表"中添加所有检测到的人脸(多个人脸),该怎么办?假设一个视频帧中有2张或更多张面孔,那么如何将这两张面孔添加到面孔列表"中?
我尝试将从 Azure Face Detect 返回的脸部矩形添加到Python列表中,然后遍历List索引,以便可以将每个脸部矩形一一传递给Azure FaceList.但是没用.
I have tried adding the face rectangles returned from Azure Face Detect into a Python List and then iterating Over List indexes, so that each face Rectangle can be passed to Azure FaceList one-by-one. But no use.
仍然出现错误:
There are more than one faces in the image
我的代码:
face_list_id = "newtest-face-list"
vid = cv2.VideoCapture(0)
count = 0
face_ids_live_Detected = [] #This list will store faceIds from detected faces
list_of_face_rectangles = []
face_rect_counter=0
while True:
ret, frame = vid.read()
check,buffer = cv2.imencode('.jpg', frame)
img = cv2.imencode('.jpg', frame)[1].tobytes()
base64_encoded = base64.b64encode(buffer).decode()
print(type(img))
detected_faces = utils.detect_face_stream(endpoint=ENDPOINT, key=KEY, image=img,face_attributes=attributes,recognition_model='recognition_03')
print('Image num {} face detected {}'.format(count, detected_faces))
count += 1
color = (255, 0, 0)
thickness = 2
for face in detected_faces:
detected_face_id = face['faceId']
face_ids_live_Detected.append(detected_face_id)
detected_face_rectangle = face['faceRectangle']
list_of_face_rectangles.append(detected_face_rectangle)
print("detected rectangle =",detected_face_rectangle)
face_rect_for_facelist = list_of_face_rectangles[face_rect_counter]
face_rect_counter +=1
frame = cv2.rectangle(frame, *utils.get_rectangle(face), color, thickness)
cv2.imshow('frame', frame)
for face_id_live in face_ids_live_Detected:
similar_faces = face_client.face.find_similar(face_id=face_id_live, face_list_id=face_list_id)
if not similar_faces:
print('No similar faces found !')
print('Adding Unknown Face to FaceList...')
facelist_result = utils.facelist_add(endpoint=ENDPOINT, key=KEY, face_list_id=face_list_id,data=img,params=face_rect_for_facelist)
persisted_face_id = facelist_result['persistedFaceId']
else:
print('Similar Face Found!')
for similar_face in similar_faces:
face_id_similar = similar_face.face_id
print("Confidence: "+str(similar_face.confidence))
在我的 utils 文件中,功能facelist_add 的代码如下:
From my utils file, code for function facelist_add is as follows:
def facelist_add(endpoint, key, face_list_id, data=None, json=None, headers=None,params=None, targetFace=None):
# pylint: disable=too-many-arguments
"""Universal interface for request."""
method = 'POST'
url = endpoint + '/face/v1.0/facelists/'+face_list_id+'/persistedfaces'
# Make it possible to call only with short name (without BaseUrl).
if not url.startswith('https://'):
url = BaseUrl.get() + url
params={}
# Setup the headers with default Content-Type and Subscription Key.
headers = headers or {}
if 'Content-Type' not in headers:
headers['Content-Type'] = 'application/octet-stream'
headers['Ocp-Apim-Subscription-Key'] = key
params['detectionModel']='detection_03'
response = requests.request(
method,
url,
params=params,
data=data,
json=json,
headers=headers)
if response.text:
result = response.json()
else:
result = {}
return result
推荐答案
感谢所有提供帮助的人,尤其是 Nicolas R 一个>.我只是发现了一个错误并纠正了它.现在,程序像Charm一样运行.
Thanks to everyone who helped and especially Nicolas R. I just found a mistake and corrected it. Now the program is working like Charm.
实际上,Azure的面部检测"以顶部,左侧,宽度,高度顺序返回面部矩形,我直接将其输入到faceList的targetFace中.
Actually, Azure 'Face Detect' returns face Rectangle in top,left,width,height sequence which I was feeding directly to the faceList's targetFace.
现在我只是交换了Face Rectangle的前两个值,它变成了 left,top,width,height ,这就是文档所说的内容,并且现在可以正常工作了
Now I just swapped the first two values of Face Rectangle and it becomes left,top,width,height which is what the documentation says, and it's working fine now.
解决方案:
我添加了一个新函数,该函数采用faceRectangle字典并交换前两个值.
I have added a new function that takes the faceRectangle dictionary and swap first two values.
list_of_faceRect_valuesonly=[]
def face_rect_values(faceRect_dict):
temp_list=[]
for key,value in faceRect_dict.items():
temp_list.append(value)
temp_list[0], temp_list[1] = temp_list[1], temp_list[0]
list_of_faceRect_valuesonly.append(temp_list)
为了从列表中提取值,我做了以下事情:
In order to extract values from list, I did following:
face_rect_counter=0
face_rect_for_facelist = list_of_faceRect_valuesonly[face_rect_counter]
face_rect_counter +=1
请求加入facelist_add功能
facelist_result = utils.facelist_add(endpoint=ENDPOINT, key=KEY, face_list_id=face_list_id,targetFace=face_rect_for_facelist,data=img)
我还稍微更改了 facelist_add 功能:
def facelist_add(endpoint, key, face_list_id, targetFace=[],data=None ,jsondata=None, headers=None):
# pylint: disable=too-many-arguments
"""Universal interface for request."""
method = 'POST'
url = endpoint + '/face/v1.0/facelists/'+face_list_id+'/persistedfaces'
# Make it possible to call only with short name (without BaseUrl).
if not url.startswith('https://'):
url = BaseUrl.get() + url
params={}
# Setup the headers with default Content-Type and Subscription Key.
headers = headers or {}
if 'Content-Type' not in headers:
headers['Content-Type'] = 'application/octet-stream'
headers['Ocp-Apim-Subscription-Key'] = key
list_of_targetfaces =[]
list_of_targetfaces.append(targetFace)
params={'targetFace':json.dumps(targetFace)}
params = {'targetFace': ','.join(map(str,targetFace))}
print("Printing TargetFaces(facelist_add function) ...",params['targetFace'])
params['detectionModel']='detection_03'
url=url + "?"
response = requests.post(url,params=params,data=data,headers=headers)
print("Request URL: ", response.url)
result = None
# Prevent `response.json()` complains about empty response.
if response.text:
result = response.json()
else:
result = {}
return result
这篇关于认知服务-如何从实时流向Azure FaceList添加多个面孔?(Python)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!