Python 将图像从本地计算机上传到 Microsoft Cognitive Vision 的方法
Python method to upload an image to Microsoft Cognitive Vision from local machine
我一直在编写 python 代码来传递包含图像的文件夹,以便使用 Cognitive Vision API 进行分析。当我对在线托管的任何图像使用 URL 时,我的代码工作正常。
但是,我在从本地机器传递图像时遇到了一些困难。我尝试使用以下方法将文件路径转换为 URL:
imageurl = pathlib.Path(path).as_uri()
有什么办法吗?我有大约 2000 张图像要分析。
此 post Uploading an image to Microsoft Cognitive Services? 提供了一些有关如何使用 C# 执行此操作的见解,但我没有找到 python.
的任何类似文档
提前致谢。
计算机视觉 API 文档说明如下:
Request body:
Input passed within the POST body. Supported input methods: raw image binary or image URL.
因此,如果无法在线获取图像(通过 URL),您需要将原始二进制文件作为 POST 请求的主体传递。
这是一个示例,使用 Requests 库。
import requests
img_filename = '<your image path and filename>'
with open(img_filename, 'rb') as f:
img_data = f.read()
# you'll need to define the following variables yourself:
# - params
# - header
r = requests.post(api_url,
params=params,
headers=header,
data=img_data)
我还有一个博客 post 可能会对您有所帮助:Using Microsoft Cognitive Services to perform OCR on images。这包含 Python 中的示例代码,用于上传图像和检索结果。它使用计算机视觉的 OCR 部分 API,但它应该与您尝试做的类似。
此外,之前所说的 'headers' 部分需要从 'json' 更改为 'octet-stream'。按照文档样式,它应该是这样的。
headers = {
'Content-Type': 'application/octet-stream',
'Ocp-Apim-Subscription-Key': 'XXXXXXX',
}
try:
body = open('User/........', "rb").read()
conn = http.client.HTTPSConnection('westus.api.cognitive.microsoft.com')
conn.request("POST", "/emotion/v1.0/recognize?%s" % body, headers)
response = conn.getresponse()
data = response.read()
print(data)
conn.close()
except Exception as e:
print(e.args)
给你:
import urllib, json
import requests
img_filename = 'RH_Louise_Lillian_Gish.jpg'
with open(img_filename, 'rb') as f:
img_data = f.read()
# Replace the subscription_key string value with your valid subscription key.
subscription_key = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
## Request headers.
header = {
'Content-Type': 'application/octet-stream',
'Ocp-Apim-Subscription-Key': subscription_key,
}
# Request parameters.
params = urllib.urlencode({
'returnFaceId': 'true',
'returnFaceLandmarks': 'false',
'returnFaceAttributes': 'age,gender,headPose,smile,facialHair,glasses,emotion,hair,makeup,occlusion,accessories,blur,exposure,noise',
})
api_url = "https://westcentralus.api.cognitive.microsoft.com/face/v1.0/detect?%s"%params
r = requests.post(api_url,
# params=params,
headers=header,
data=img_data)
print (r.json())
我一直在编写 python 代码来传递包含图像的文件夹,以便使用 Cognitive Vision API 进行分析。当我对在线托管的任何图像使用 URL 时,我的代码工作正常。
但是,我在从本地机器传递图像时遇到了一些困难。我尝试使用以下方法将文件路径转换为 URL:
imageurl = pathlib.Path(path).as_uri()
有什么办法吗?我有大约 2000 张图像要分析。
此 post Uploading an image to Microsoft Cognitive Services? 提供了一些有关如何使用 C# 执行此操作的见解,但我没有找到 python.
的任何类似文档提前致谢。
计算机视觉 API 文档说明如下:
Request body:
Input passed within the POST body. Supported input methods: raw image binary or image URL.
因此,如果无法在线获取图像(通过 URL),您需要将原始二进制文件作为 POST 请求的主体传递。
这是一个示例,使用 Requests 库。
import requests
img_filename = '<your image path and filename>'
with open(img_filename, 'rb') as f:
img_data = f.read()
# you'll need to define the following variables yourself:
# - params
# - header
r = requests.post(api_url,
params=params,
headers=header,
data=img_data)
我还有一个博客 post 可能会对您有所帮助:Using Microsoft Cognitive Services to perform OCR on images。这包含 Python 中的示例代码,用于上传图像和检索结果。它使用计算机视觉的 OCR 部分 API,但它应该与您尝试做的类似。
此外,之前所说的 'headers' 部分需要从 'json' 更改为 'octet-stream'。按照文档样式,它应该是这样的。
headers = {
'Content-Type': 'application/octet-stream',
'Ocp-Apim-Subscription-Key': 'XXXXXXX',
}
try:
body = open('User/........', "rb").read()
conn = http.client.HTTPSConnection('westus.api.cognitive.microsoft.com')
conn.request("POST", "/emotion/v1.0/recognize?%s" % body, headers)
response = conn.getresponse()
data = response.read()
print(data)
conn.close()
except Exception as e:
print(e.args)
给你:
import urllib, json
import requests
img_filename = 'RH_Louise_Lillian_Gish.jpg'
with open(img_filename, 'rb') as f:
img_data = f.read()
# Replace the subscription_key string value with your valid subscription key.
subscription_key = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
## Request headers.
header = {
'Content-Type': 'application/octet-stream',
'Ocp-Apim-Subscription-Key': subscription_key,
}
# Request parameters.
params = urllib.urlencode({
'returnFaceId': 'true',
'returnFaceLandmarks': 'false',
'returnFaceAttributes': 'age,gender,headPose,smile,facialHair,glasses,emotion,hair,makeup,occlusion,accessories,blur,exposure,noise',
})
api_url = "https://westcentralus.api.cognitive.microsoft.com/face/v1.0/detect?%s"%params
r = requests.post(api_url,
# params=params,
headers=header,
data=img_data)
print (r.json())