如何调用Microsoft认知面并将图像作为带有cognitive_face的字节python传递

问题描述 投票:0回答:1

嗨我在这个问题尝试同样的事情How can i pass capture image directly as a binary data for processing in API calling (Microsoft Cognitive Services) using Python传递字节图像面对检测库,但与cognitive_face库

faces =CF.face.detect(buf.tobytes(),True,False,attributes='age,gender,emotion')

但我得到一个错误

回溯(最近一次调用最后一次):文件“。\ cam.py”,第80行,在faces = CF.face.detect(buf.tobytes(),True,False,attributes ='age,gender,headPose,smile> ,facialHair,眼镜,情感,头发,化妆,遮挡,配件,模糊,曝光,n> oise')文件“Python37 \ lib \ site-packages \ cognitive_face \ face.py”,第33行,检测标题,数据, json = util.parse_image(image)文件“Python37 \ lib \ site-packages \ cognitive_face \ util.py”,第133行,在parse_image中,elif os.path.isfile(image):#当image是文件路径时。文件“Python37 \ lib \ genericpath.py”,第30行,在isfile中st = os.stat(path)ValueError:stat:在路径中嵌入空字符

python-3.x opencv microsoft-cognitive face-api
1个回答
1
投票

您正在使用名为cognitive_face的旧包,遗憾的是它希望输入参数是文件名或URL。

幸运的是,新的软件包名称azure-cognitiveservices-vision-face支持流,因此如果您切换,您可以执行以下操作:

from azure.cognitiveservices.vision.face import FaceClient
from msrest.authentication import CognitiveServicesCredentials
import cv2
import os

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

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

# img is your unencoded (raw) image, from the camera
img = ...

# buf will be the encoded image
ret,buf = cv2.imencode('.jpg', img)

# stream-ify the buffer
stream = io.BytesIO(buf)

# call the Face API
detected_faces = client.face.detect_with_stream(
    stream,
    return_face_id=True,
    return_face_attributes=['age','gender','emotion'])

# access the response, example:
for detected_face in detected_faces:
    print('{} happiness probability={}'.format(
        detected_face.face_id,
        detected_face.face_attributes.emotion.happiness))
© www.soinside.com 2019 - 2024. All rights reserved.