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

2024-04-25 08:13:21 发布

您现在位置:Python中文网/ 问答频道 /正文

Error Screenshot

import sys
import os, time
import cognitive_face as CF
import global_variables as global_var
import urllib
import sqlite3
import requests
from requests.packages.urllib3.exceptions import InsecureRequestWarning

requests.packages.urllib3.disable_warnings(InsecureRequestWarning)


Key = global_var.key

CF.Key.set(Key)

BASE_URL = global_var.BASE_URL  # Replace with your regional Base URL
CF.BaseUrl.set(BASE_URL)

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)
            imgurl = urllib.request.pathname2url(os.path.join(imageFolder, filename))
            imgurl = imgurl[3:]
            print("imageurl = {}".format(imgurl))
            res = CF.face.detect(imgurl)
            if len(res) != 1:
                print("No face detected in image")
            else:
                res = CF.person.add_face(imgurl, global_var.personGroupId, person_id)
                print(res)  
            time.sleep(6)
else:
    print("supply attributes please from dataset folder")

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

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

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


Tags: pathimportidurlosvarconnectsys
1条回答
网友
1楼 · 发布于 2024-04-25 08:13:21

欢迎来到堆栈溢出,@arun

首先,根据here,您正在使用的API已被弃用,您应该切换到this one

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

image_data = open(image_path, "rb").read()

变量image_data可以传递给方法

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

首先,安装以下pip包:

pip install azure-cognitiveservices-vision-face

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

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

相关问题 更多 >