我正在尝试使用 Microsoft 的人脸 api 发出 http post 请求,以便将其与我的 azure blob 存储帐户中的照片连接起来。当我运行以下代码时,我收到多个错误,例如握手错误或ssl例程类型错误。我很感激任何帮助!问题代码是:
api_response = requests.post(url, headers=headers, data=blob)
显然,这里的上下文是我之前运行的。第一个 block 设置存储帐户:
%matplotlib inline
import matplotlib.pyplot as plt
import io
from io import StringIO
import numpy as np
import cv2
from PIL import Image
from PIL import Image
import os
from array import array
azure_storage_account_name = 'musicsurveyphotostorage'
azure_storage_account_key = None #dont need key... we will access public blob...
if azure_storage_account_name is None:
raise Exception("You must provide a name for an Azure Storage account")
from azure.storage.blob import BlockBlobService
blob_service = BlockBlobService(azure_storage_account_name, azure_storage_account_key)
# select container (folder) name where the files resides
container_name = 'musicsurveyphotostorage'
# list files in the selected folder
generator = blob_service.list_blobs(container_name)
blob_prefix = 'https://{0}.blob.core.windows.net/{1}/{2}'
# load image file to process
blob_name = 'shiba.jpg' #name of image I have stored
blob = blob_service.get_blob_to_bytes(container_name, blob_name)
image_file_in_mem = io.BytesIO(blob.content)
img_bytes = Image.open(image_file_in_mem)
第二个 block 调用 API 和有问题的post 请求:
#CALL OUT THE API
import requests
import urllib
url_face_api = 'https://eastus.api.cognitive.microsoft.com/face/v1.0'
api_key ='____'
#WHICH PARAMETERS ATTRIBUTES DO YOU WANT RETURNED
headers = {'Content-Type': 'application/octet-stream', 'Ocp-Apim-
Subscription-Key':api_key}
params = urllib.parse.urlencode({
'returnFaceId': 'true',
'returnFaceLandmarks': 'true',
'returnFaceAttributes': 'age,gender,smile,facialHair,headPose,glasses',
})
query_string = '?{0}'.format(params)
url = url_face_api + query_string
#THIS IS THE PROBLEM CODE
api_response = requests.post(url, headers=headers, data=blob)
#print out output in json
import json
res_json = json.loads(api_response.content.decode('utf-8'))
print(json.dumps(res_json, indent=2, sort_keys=True))
最佳答案
如果我打开 Fiddler,我也可以重现您提到的问题。如果是这种情况,您可以在发送请求期间暂停使用 fiddler 来捕获请求。
根据我的测试,您的代码中有 2 行代码需要更改。更多信息您可以引用屏幕截图。 我们还可以从 azure offical document 获取一些演示代码。
url_face_api = 'https://westcentralus.api.cognitive.microsoft.com/face/v1.0/detect' # in your case miss detect
api_response = requests.post(url, headers=headers,data=blob.content) # data should be blob.content
关于python - 使用 azure blob 存储向 API 发出 http post 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49743657/