Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/350.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将本地图像上载到microsoft认知人脸_Python_Azure_Image Processing_Microsoft Cognitive_Face Recognition - Fatal编程技术网

Python 将本地图像上载到microsoft认知人脸

Python 将本地图像上载到microsoft认知人脸,python,azure,image-processing,microsoft-cognitive,face-recognition,Python,Azure,Image Processing,Microsoft Cognitive,Face Recognition,我希望图像应该转换成字节数组,但我不知道怎么做。本地图像必须上传到认知API。尝试了许多方法,但无法解决错误 imgurl = urllib.request.pathname2url(os.path.join(imageFolder, filename)) 上面这一行是存在错误的地方欢迎来到堆栈溢出,@arun 首先,根据,您正在使用的API已被弃用,您应该改用 其次,在这个新的API中,有一个名为detect_with_stream的方法,它将使用字节流而不是URL向人脸识别端点发出请求。它

我希望图像应该转换成字节数组,但我不知道怎么做。本地图像必须上传到认知API。尝试了许多方法,但无法解决错误

imgurl = urllib.request.pathname2url(os.path.join(imageFolder, filename))

上面这一行是存在错误的地方

欢迎来到堆栈溢出,@arun

首先,根据,您正在使用的API已被弃用,您应该改用

其次,在这个新的API中,有一个名为detect_with_stream的方法,它将使用字节流而不是URL向人脸识别端点发出请求。它将使用与基于URL的方法不同的请求头。此方法接受包含图像的字节流。我使用过另一个执行文本识别的认知服务API,因此我遇到了发送图像URL或图像字节流的问题。您可以从该文件生成ByTestStream,如下所示:

image_data = open(image_path, "rb").read()
变量image_数据可以传递给方法

编辑:关于如何将新API与image ByTestStream一起使用的说明

首先,安装以下pip包:

pip安装azure认知服务愿景面

然后,您可以尝试这种方法

import sys
import os, time
import global_variables as global_var
from azure.cognitiveservices.vision.face import FaceClient
from msrest.authentication import CognitiveServicesCredentials
import urllib
import sqlite3
import requests
from requests.packages.urllib3.exceptions import InsecureRequestWarning

requests.packages.urllib3.disable_warnings(InsecureRequestWarning)


KEY = global_var.key

ENDPOINT = global_var.endpoint

face_client = FaceClient(ENDPOINT,CognitiveServicesCredentials(KEY))

def get_person_id():
    person_id = ''
    extractId = str(sys.argv[1])[-2:]
    connect = sqlite3.connect("Face-DataBase")
    c = connect.cursor()
    cmd = "SELECT * FROM Students WHERE ID = " + extractId
    c.execute(cmd)
    row = c.fetchone()
    person_id = row[3]
    connect.close()
    return person_id

if len(sys.argv) is not 1:
    currentDir = os.path.dirname(os.path.abspath(__file__))
    imageFolder = os.path.join(currentDir, "dataset/" + str(sys.argv[1]))
    person_id = get_person_id()
    for filename in os.listdir(imageFolder):
        if filename.endswith(".jpg"):
            print(filename)
            img_data = open(filename, "rb").read()
            res = face_client.face.detect_with_stream(img_data)
            if not res:
                print('No face detected from image {}'.format(filename))
                continue

            res = face_client.person_group_person.add_face_from_stream(global_var.personGroupId, person_id, img_data)
            print(res)  
            time.sleep(6)
else:
    print("supply attributes please from dataset folder")
编辑2:关于遍历目录中所有文件的注释

好的@arun,您当前的问题源于这样一个事实:您使用的是os.listdir,它只列出文件名,所以您没有它们的路径。最快的解决方案是使用以下方法打开循环中的每个图像:

img_data = open(os.path.join(imageFolder, filename), "rb").read()

感谢您提供的信息,但我想为一个用户发送多个图像。请参阅更新的错误屏幕截图。请帮我修改代码我试过了,但发现FileNotFoundError。我认为必须定义下降路径。以下是完全错误文件add\u person\u faces.py,第39行,在img\u data=openfilename,rb.read FileNotFoundError:[Errno 2]没有这样的文件或目录:“User.39.1.jpg```@arun,你是在Linux还是在Windows?我在使用Windows 10谢谢!一切正常。res=CF.person\u group.get\u statusglobal\u var.personGroupId什么是更新的API命令获取状态也更新了res=CF.face.identiffaceids、global\u var.personGroupId的API该错误消息不需要是图像。
img_data = open(os.path.join(imageFolder, filename), "rb").read()