错误屏幕截图
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))
上行是存在错误的地方
欢迎来到 Stack Overflow,@arun。
首先,根据此处,您使用的 API 已被弃用,您应该切换到此 API。
其次,在这个新的 API 中,有一个名为detect_with_stream
的方法(此处为 ref(,它将使用字节流而不是 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()