在Python中使用Azure Face Api,如果在Video Stream中检测到同一个人,如何返回单个faceId或一组FaceId? [英] Using Azure Face Api in Python, How to Return a single faceId or a group of FaceIds if the same person is detected in Video Stream?

查看:69
本文介绍了在Python中使用Azure Face Api,如果在Video Stream中检测到同一个人,如何返回单个faceId或一组FaceId?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Azure Face APi检测视频流中的面部,但是Azure对于每个检测到的面部都会返回一个唯一的faceId(这正是文档所说的内容).

I am using Azure Face APi to detect faces in video stream, but for each detected face Azure returns a unique faceId( which is exactly what the documentation says).

问题是,假设ABC先生出现在20个视频帧中,则生成了20个唯一的faceId.我希望Azure Face应该为我返回一个特别为ABC先生生成的一个faceId或一组FaceId,以便我可以知道它是在镜头前停留x倍的同一个人.

The problem is, Let's say Mr.ABC appears in 20 video frames, 20 unique faceIds gets generated. I want something that Azure Face should return me a single faceId or a group of FaceIds generated particularly for Mr.ABC so that I can know that its the same person that stays in front of camera for x amount of time.

我已经阅读了Azure Facegrouping和Azure FindSimilar的文档,但不了解在直播视频流的情况下如何使它工作.

I have read the documentation of Azure Facegrouping and Azure FindSimilar, but didn't understand how can I make it work in case of live video stream.

下面给出了我用于使用Azure人脸检测人脸的代码:

The code I am using for detecting faces using Azure face is given below:

from azure.cognitiveservices.vision.face import FaceClient
from msrest.authentication import CognitiveServicesCredentials
from azure.cognitiveservices.vision.face.models import TrainingStatusType, Person, SnapshotObjectType, OperationStatusType
import cv2
import os
import requests
import sys,glob, uuid,re
from PIL import Image, ImageDraw
from urllib.parse import urlparse
from io import BytesIO
from azure.storage.blob import BlobServiceClient, BlobClient, ContainerClient,__version__

face_key = 'XABC' #API key
face_endpoint = 'https://XENDPOINT.cognitiveservices.azure.com' #endpoint, e.g. 'https://westus.api.cognitive.microsoft.com'

credentials = CognitiveServicesCredentials(face_key)
face_client = FaceClient(face_endpoint, credentials)

camera = cv2.VideoCapture(0)
samplenum =1
im = ""
work_dir = os.getcwd()

person_group_id = 'test02-group'
target_person_group_id = str(uuid.uuid4())
face_ids = []

#cv2 font
font = cv2.FONT_HERSHEY_SIMPLEX
#empty tuple
width = ()
height = ()
left=0
bottom=0
def getRectangle(faceDictionary):
    rect = faceDictionary.face_rectangle
    left = rect.left
    top = rect.top
    right = left + rect.width
    bottom = top + rect.height
    
    return ((left, top), (right, bottom))

while True:
    check,campic = camera.read()
    samplenum=samplenum+1
    cv2.imwrite("live_pics/"+str(samplenum)+".jpg",campic)
    path = work_dir+"/live_pics/"+str(samplenum)+".jpg"
    #im = cv2.imread("pics/"+str(samplenum)+".jpg")
    stream = open(path, "r+b")
    detected_faces = face_client.face.detect_with_stream(
        stream,
        return_face_id=True,
        return_face_attributes=['age','gender','emotion'],recognitionModel="recognition_03")
    for face in detected_faces:
        width,height = getRectangle(face)
        cv2.rectangle(campic,width,height,(0,0,170),2)
        face_ids.append(face.face_id)
    #cv2.waitKey(100);
    if(samplenum>10):
        break
    cv2.imshow("campic", campic)
    if cv2.waitKey(1) == ord("q"):
        break

camera.release()
cv2.destroyAllWindows()   

推荐答案

Face API没有什么神奇之处:您必须为找到的每个面孔分2个步骤来处理它.

There is no magic on Face API: you have to process it with 2 steps for each face found.

我建议使用查找相似":

What I suggest is to use "Find similar":

  • 在开始时,创建一个"FaceList"
  • 然后处理您的视频:
    • 每帧人脸检测
    • 对于找到的每个面孔,在创建的面孔列表上使用查找"类似操作.如果没有匹配项(有足够的信心),则将面孔添加到面孔列表中.

    最后,您的面孔列表将包含视频中找到的所有其他人.

    At the end, your facelist will contain all the different people found on the video.

    对于您的实时用例,请不要使用标识"使用PersonGroup/LargePersonGroup进行操作(这两个选项之间的选择取决于组的大小),因为您会因需要对组进行培训而陷入困境.例如,您将执行以下操作:

    For your realtime use-case, don't use "Identify" operation with PersonGroup / LargePersonGroup (the choice between those 2 depends on the size of the group), because you will be stuck by the need of training on the group. Example, you would be doing the following:

    • 第1步,第1次:为此执行生成PersonGroup/LargePersonGroup
    • 步骤2,N次(对于要识别脸部的每个图像):
      • 第2a步:面部检测
      • 步骤2b:在步骤2b中,面对识别".基于PersonGroup/LargePersonGroup的每张检测到的脸部
      • 步骤2c:对于每个未识别的面孔,将其添加到PersonGroup/LargePersonGroup.

      这里的问题是2c之后,您必须再次训练您的小组.即使时间不长,也会因为太长而无法实时使用.

      Here the issue is the fact that after 2c, you have to train your group again. Even if it is not so long, it cannot be used in real time as it will be too long.

      这篇关于在Python中使用Azure Face Api,如果在Video Stream中检测到同一个人,如何返回单个faceId或一组FaceId?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆