【问题标题】:Uploading local images to microsoft cognitive face将本地图像上传到微软认知面孔
【发布时间】:2020-06-01 01:33:55
【问题描述】:

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

上面是存在错误的地方

【问题讨论】:

  • 那个错误信息不需要是图片。

标签: python azure image-processing microsoft-cognitive face-recognition


【解决方案1】:

欢迎来到 Stack Overflow,@arun。

首先,根据here,您使用的API 已被弃用,您应该改用this one。

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

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

变量image_data可以传递给方法。

编辑:关于如何将新 API 与图像字节流一起使用的说明

首先,安装以下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()

【讨论】:

  • 感谢您提供信息,但我想为单个用户发送多张图片(请参阅更新后的错误屏幕截图)。请帮我修改代码
  • 我试过了,但我得到了 FileNotFoundError。我认为必须定义绝对路径。以下是完全错误。 ``` 文件“add_person_faces.py”,第 39 行,在 img_data = open(filename, "rb").read() FileNotFoundError: [Errno 2] No such file or directory: 'User.39.1.jpg' ```
  • @arun,你是在 Linux 还是 Windows?
  • 我使用的是 Windows 10
  • 谢谢!一切正常。 res = CF.person_group.get_status(global_var.personGroupId) 更新了“获取状态”的 API 命令也更新了 res = CF.face.identify(faceIds, global_var.personGroupId) 的 API
猜你喜欢
  • 2017-12-04
  • 2017-12-19
  • 1970-01-01
  • 2017-12-24
  • 2018-08-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多