0

问题背景:

我创建了一个 Azure FaceList,并且正在使用我的网络摄像头来捕获实时源,并且:

  1. 将流发送到 Azure 人脸检测
  2. 获取人脸检测返回的人脸矩形
  3. 使用返回的人脸矩形将从实时视频流中检测到的人脸添加到我的 FaceList。

(我需要创建人脸列表以解决我在Nicolas 回答的另一个问题中解释的问题,这就是我所关注的)

问题详情:

根据https://docs.microsoft.com/en-us/rest/api/cognitiveservices/face/facelist/addfacefromstream上的 Azure FaceList 文档,如果图像中有多个人脸,我们需要指定要添加的目标人脸到 Azure FaceList。

问题是,如果我们需要在人脸列表中添加所有检测到的人脸(多张人脸)怎么办?假设单帧视频中有 2 个或更多人脸,那么如何将这两个人脸添加到人脸列表?

我尝试将Azure 人脸检测返回的人脸矩形添加到 Python 列表中,然后迭代列表索引,以便每个人脸矩形可以一个接一个地传递给 Azure FaceList。但是没有用。

仍然收到错误:

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的代码如下:

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
4

2 回答 2

0

感谢所有帮助过的人,尤其是Nicolas R。我刚刚发现了一个错误并纠正了它。现在该程序就像 Charm 一样工作。

实际上,Azure 'Face Detect' 在顶部、左侧、宽度、高度序列中返回面部矩形,我直接将其提供给 faceList 的 targetFace。

现在我刚刚交换了 Face Rectangle 的前两个值,它变成了 left,top,width,height这就是文档所说的,现在它工作正常。

解决方案:

我添加了一个新函数,它接受 faceRectangle 字典并交换前两个值。

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)

为了从列表中提取值,我执行了以下操作:

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
于 2020-12-30T10:22:01.677 回答
0

当您在一张图片中有多个面孔时,您必须在调用中提供一个“targetFace” AddFace

人脸矩形,用于指定要添加到人脸列表中的目标人脸,格式为“targetFace=left,top,width,height”。例如“targetFace=10,10,100,100”。如果图像中有多个人脸,则需要 targetFace 来指定要添加的人脸。没有 targetFace 意味着在整个图像中只检测到一个人脸。

请参阅此方法的 API 文档:https ://westeurope.dev.cognitive.microsoft.com/docs/services/563879b61984550e40cbbe8d/operations/563879b61984550f30395250

于 2020-12-22T18:08:57.290 回答