как назвать Microsoft Cognitive Face и передаваемое изображение как Python байтов с Cognitive_face - PullRequest
0 голосов
/ 07 апреля 2019

Привет, я пытаюсь сделать то же самое в этом вопросе. Как передать изображение захвата непосредственно в виде двоичных данных для обработки в вызовах API (Microsoft Cognitive Services) с использованием Python передача байтового изображения в библиотеку обнаружения лиц но с библиотекой cognitive_face

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

но я получаю ошибку

Traceback (последний вызов был последним): файл ". \ Cam.py", строка 80, в face = CF.face.detect (buf.tobytes (), True, False, attribute = 'age, sex, headPose) , smile>, facialHair, очки, эмоции, волосы, макияж, окклюзия, аксессуары, размытие, экспозиция, n> oise ') Файл "Python37 \ lib \ site-packages \ cognitive_face \ face.py", строка 33, в заголовках обнаружения , data, json = util.parse_image (image) Файл "Python37 \ lib \ site-packages \ cognitive_face \ util.py", строка 133, в parse_image elif os.path.isfile (image): # Когда изображение является путем к файлу , Файл "Python37 \ lib \ genericpath.py", строка 30, в isfile st = os.stat (путь) ValueError: stat: встроенный нулевой символ в пути

1 Ответ

1 голос
/ 09 апреля 2019

Вы используете старый пакет с именем 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))
...