【问题标题】: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?在 Python 中使用 Azure Face Api,如果在视频流中检测到同一个人,如何返回单个 faceId 或一组 FaceId?
【发布时间】:2021-03-10 10:42:37
【问题描述】:

我正在使用 Azure Face APi 来检测视频流中的人脸,但对于每个检测到的人脸,Azure 会返回一个唯一的 faceId(这正是文档所说的)。

问题是,假设 Mr.ABC 出现在 20 个视频帧中,生成了 20 个唯一的 faceId。我想要 Azure Face 应该返回一个 faceId 或一组 FaceId 的东西,特别是为 Mr.ABC 生成的,这样我就可以知道它是同一个人在镜头前停留了 x 时间。

我已阅读 Azure Facegrouping 和 Azure FindSimilar 的文档,但不明白如何在实时视频流的情况下使其工作。

我使用 Azure 人脸检测人脸的代码如下:

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()   

【问题讨论】:

    标签: azure microsoft-cognitive azure-cognitive-services face-api facial-identification


    【解决方案1】:

    Face API 没有什么神奇之处:您必须为找到的每个人脸分两步处理它。

    我的建议是使用“查找相似”:

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

    最后,您的面孔列表将包含在视频中找到的所有不同的人。


    对于您的实时用例,不要对 PersonGroup / LargePersonGroup 使用“识别”操作(这两者之间的选择取决于组的大小),因为您将被组培训的需要所困.例如,您将执行以下操作:

    • 第 1 步,第 1 次:为此执行生成 PersonGroup / LargePersonGroup
    • 第 2 步,N 次(针对您要识别人脸的每个图像):
      • 步骤 2a:人脸检测
      • 第 2b 步:根据 PersonGroup / LargePersonGroup 在每个检测到的人脸上“识别”人脸
      • 步骤 2c:对于每个未识别的人脸,将其添加到 PersonGroup / LargePersonGroup。

    这里的问题是,在 2c 之后,您必须再次训练您的团队。就算不是很长,也不能实时使用,太长了。

    【讨论】:

    • 谢谢,尼古拉斯。这让我很开心!
    • 您好 Nicolas,我还有一个与 Azure 人脸列表相关的问题。我能得到你的帮助吗?看看我的以下问题。谢谢stackoverflow.com/q/65412610/7731287
    【解决方案2】:

    根据我的理解,您希望显示一个人的姓名/身份,而不是从人脸 API 检测到的人脸 ID。

    如果是这样,在您通过人脸检测 API 获取人脸 ID 后,您应该使用 Face Identify API 来执行此操作。如果 Azure Face 服务可以识别人脸,则可以获取人员 ID,使用此 ID,您可以使用 PersonGroup Person API 获取此人的信息。

    我还为你写了一个简单的demo,在这个demo中,只有一张图片,我们可以把它成像为视频帧。我用一个超人创建了一个人组,并为他添加了一些面孔。

    这是下面的代码:

    import matplotlib.pyplot as plt
    import matplotlib.patches as patches
    from PIL import Image
    import numpy as np
    import asyncio
    import io
    import glob
    import os
    import sys
    import time
    import uuid
    import requests
    from urllib.parse import urlparse
    from io import BytesIO
    from azure.cognitiveservices.vision.face import FaceClient
    from msrest.authentication import CognitiveServicesCredentials
    
    imPath = "<image path>";
    ENDPOINT = '<endpoint>'
    KEY = '<key>'
    PERSON_GROUP_ID = '<person group name>'
    
    face_client = FaceClient(ENDPOINT, CognitiveServicesCredentials(KEY))
    im = np.array(Image.open(imPath), dtype=np.uint8)
    
    faces = face_client.face.detect_with_stream(open(imPath, 'r+b'),recognition_model='recognition_03');
    
    # Create figure and axes
    fig,ax = plt.subplots()
    
     # Display the image
    ax.imshow(im)
    
    for i in range(len(faces)):
        face = faces[i]
        rect =patches.Rectangle((face.face_rectangle.left,face.face_rectangle.top),face.face_rectangle.height,face.face_rectangle.width,linewidth=1,edgecolor='r',facecolor='none')
        detected_person = face_client.face.identify([face.face_id],PERSON_GROUP_ID)[0]
        if(len(detected_person.candidates) > 0):
            person_id = detected_person.candidates[0].person_id
            person = face_client.person_group_person.get(PERSON_GROUP_ID,person_id)
            plt.text(face.face_rectangle.left,face.face_rectangle.top,person.name,color='r')
        else:
            plt.text(face.face_rectangle.left,face.face_rectangle.top,'unknown',color='r')
    
        
        ax.add_patch(rect)
    
    plt.show()
    

    结果:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-12-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-10
      • 2021-02-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多