python-3.x - 使用 IBM Cloud Function 的 REST API 时如何修复“错误”:“'parameter_name'”?
问题描述
我在 IBM Cloud Functions 中有一个动作,它只接收一个参数:“frame”。我正在使用 Postman 测试随操作提供的 REST API 端点。但是,当我提供“框架”参数时,它返回以下内容:
"response": {
"result": {
"error": "'frame'"
},
"status": "application error",
"success": false
}
我在 IBM Cloud Functions 控制台中调用此操作时遇到了此问题。我通过删除输入模式中的空格并再次添加它来解决它,然后它就像控制台中的魅力一样工作。但是,我不能对 HTTP 请求做同样的事情。
我目前执行 HTTP 请求的方式是这样的:
POST https://us-south.functions.cloud.ibm.com/api/v1/namespaces/{namespace}/actions/{action_name}?blocking=true&frame={value}
该操作应该返回我期望的结果,但它现在没有这样做。请帮助我,任何答案都会很棒!
编辑:
这是动作的代码:
import requests, base64, json, cv2
from PIL import Image
from six import BytesIO
def json_to_dict(json_str):
return json.loads(json.dumps(json_str))
def frame_to_bytes(frame):
frame_im = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
pil_im = Image.fromarray(frame_im)
stream = BytesIO()
pil_im.save(stream, format="JPEG")
stream.seek(0)
img_for_post = stream.read()
img_base64 = base64.b64encode(img_for_post)
return img_base64
def main(dict):
cap = cv2.VideoCapture(dict['frame'])
if not cap.isOpened():
return { "error": "Unable to open video source" }
ret, frame = cap.read()
if ret is False:
return { "error": "Unable to read video source" }
# openALPR API part
OPENALPR_SECRET_KEY = {my_secret_key}
url = "https://api.openalpr.com/v2/recognize_bytes?recognize_vehicle=1&country=us&secret_key=%s" % (
OPENALPR_SECRET_KEY)
r = requests.post(url, data=frame_to_bytes(frame))
resp = json_to_dict(r.json())
print(resp)
if not resp['results']:
return { "error": "Plate number not recognized" }
plates = []
for plate in resp['results']:
if plate['confidence'] < 75:
pass
else:
print(plate['plate'])
plates.append(plate['plate'])
return { "plates": plates }
这是激活响应(根据 Postman,返回的状态是 502 Bad Gateway):
{
"activationId": "5a83396b9f53447483396b9f53e47452",
"annotations": [
{
"key": "path",
"value": "{namespace}/{name}"
},
{
"key": "waitTime",
"value": 5531
},
{
"key": "kind",
"value": "python:3.7"
},
{
"key": "timeout",
"value": false
},
{
"key": "limits",
"value": {
"concurrency": 1,
"logs": 10,
"memory": 1024,
"timeout": 60000
}
},
{
"key": "initTime",
"value": 3226
}
],
"duration": 3596,
"end": 1560669652454,
"logs": [],
"name": "{name}",
"namespace": "{namesapce}",
"publish": false,
"response": {
"result": {
"error": "'frame'"
},
"status": "application error",
"success": false
},
"start": 1560669648858,
"subject": "{my_email}",
"version": "0.0.7"
}
编辑 2:我还尝试将其作为网络操作启用,以查看它是否会改变任何内容。然而,这没有用。当我使用这个 HTTP 请求时:
https://us-south.functions.cloud.ibm.com/api/v1/web/{namespace}/default/{action_name}?frame={value}
我得到:
{
"code": "e1c36666f4db1884c48f028ef58243fc",
"error": "Response is not valid 'message/http'."
}
这是可以理解的,因为我的函数返回的是 json。但是,当我使用这个HTTP 请求时:
https://us-south.functions.cloud.ibm.com/api/v1/web/{namespace}/default/{action_name}.json?frame={value}
我得到:
{
"code": "010fc0efaa29f96b47f92735ff763f50",
"error": "Response is not valid 'application/json'."
}
我真的不知道在这里做什么
解决方案
经过一番谷歌搜索后,我发现了一些对我现在有用的东西,尽管它可能并不适用于所有人。Apache 有一个python“客户端”示例,用于使用使用该requests
库的操作的 REST API。
问题是,为了使用它,您需要提供您的 API KEY,除了直接从 IBM Cloud CLI 获取之外,我不知道如何通过任何其他方式获取它。由于我正在尝试从 Web 服务器访问该功能,因此我需要将密钥保存在环境变量中或将其保存在文本文件中并从那里访问它或在服务器上安装 CLI,使用我的凭据登录并打电话ibmcloud wsk property get --auth
。
此外,当我尝试此方法时,它不适用于 Web 操作端点。
推荐阅读
- react-native - 循环音频的 SetTimeout - 世博会
- html - 来自计算机的图像未显示在 Github io/pages 上
- json - 无法从 JSON 文件中读取数据
- webstorm - yarnpkg add --exact react react-dom react-scripts --cwd /home/dennis/WebstormProjects/studentsdiscussionforum 失败
- javascript - 我无法从 SugarJS 格式化日期
- html - 消除渲染阻塞资源(灯塔)
- python - 如何使用openCV查找帧时间
- lua - 'Text' 的错误参数 #3(预期字符串,得到 nil)
- sql-server - 从 SQL Server 中提取 Oracle 数据的问题
- autodesk-forge - 如何在聚合模型 Autodesk Forge 上选择多个元素