#!/usr/local/bin/python3 # -*- coding: utf-8 -*- # @Time : 2023/6/11 14:19 # @Author : 志在星空 # @File : jietu12.py # @Software: PyCharm import base64 import urllib import requests import json API_KEY = "jHxdMDf2bhEPxcG6zFoRUejo" SECRET_KEY = "QF5GO9eLQncG2Zr1MKrxLOWvKAkAtVfI" def main(): # 发送 OCR 请求并获取响应 url = "https://aip.baidubce.com/rest/2.0/ocr/v1/general?access_token=" + get_access_token() # payload = 'image=%2F9j%2F4AAQ' headers = { 'Content-Type': 'application/x-www-form-urlencoded', 'Accept': 'application/json' } image_path = "11.jpg" with open(image_path, "rb") as image_file: image_data = image_file.read() response = requests.post(url, headers=headers, data=image_data) # response = requests.request("POST", url, headers=headers, data=payload) # 解析响应并提取 words_result 中的所有 words 字段 result = json.loads(response.text) print(result) # words_list = [item['words'] for item in result['words_result']] # print(words_list) # # # 打印所有提取到的内容 # for words in words_list: # print(words) def get_file_content_as_base64(path, urlencoded=False): """ 获取文件base64编码 :param path: 文件路径 :param urlencoded: 是否对结果进行urlencoded :return: base64编码信息 """ with open(path, "rb") as f: content = base64.b64encode(f.read()).decode("utf8") if urlencoded: content = urllib.parse.quote_plus(content) return content def get_access_token(): """ 使用 AK,SK 生成鉴权签名(Access Token) :return: access_token,或是None(如果错误) """ url = "https://aip.baidubce.com/oauth/2.0/token" params = {"grant_type": "client_credentials", "client_id": API_KEY, "client_secret": SECRET_KEY} return str(requests.post(url, params=params).json().get("access_token")) if __name__ == '__main__': main()运行出现{'log_id': 1667825949995168966, 'error_msg': 'param image not exist', 'error_code': 216101},怎么办
时间: 2024-03-30 15:40:33 浏览: 72
根据API的响应信息,错误信息为"param image not exist",也就是说API无法找到你指定的图片文件。请确保文件路径是正确的,并且文件名和扩展名的大小写与实际文件匹配。如果文件名或路径错误,将导致API无法找到该文件并出现上述错误。如果你确定文件路径和文件名正确无误,那么你可以检查一下API文档以确定API对请求的其他要求,例如请求头、请求参数等是否正确。此外,你还可以尝试使用图片的base64编码代替图片路径传递给API,方法详见上面代码中的`get_file_content_as_base64`函数。
相关问题
#!/usr/local/bin/python3 # -- coding: utf-8 -- # @Time : 2023/6/11 14:19 # @Author : 志在星空 # @File : jietu12.py # @Software: PyCharm import base64 import urllib import requests import json API_KEY = "jHxdMDf2bhEPxcG6zFoRUejo" SECRET_KEY = "QF5GO9eLQncG2Zr1MKrxLOWvKAkAtVfI" def main(): # 发送 OCR 请求并获取响应 url = "https://aip.baidubce.com/rest/2.0/ocr/v1/general?access_token=" + get_access_token() # payload = 'image=%2F9j%2F4AAQ' headers = { 'Content-Type': 'application/x-www-form-urlencoded', 'Accept': 'application/json' } image_path = "11.jpg" with open(image_path, "rb") as image_file: image_data = image_file.read() response = requests.post(url, headers=headers, data=image_data) # response = requests.request("POST", url, headers=headers, data=payload) # 解析响应并提取 words_result 中的所有 words 字段 result = json.loads(response.text) print(result) # words_list = [item['words'] for item in result['words_result']] # print(words_list) # # # 打印所有提取到的内容 # for words in words_list: # print(words) def get_file_content_as_base64(path, urlencoded=False): """ 获取文件base64编码 :param path: 文件路径 :param urlencoded: 是否对结果进行urlencoded :return: base64编码信息 """ with open(path, "rb") as f: content = base64.b64encode(f.read()).decode("utf8") if urlencoded: content = urllib.parse.quote_plus(content) return content def get_access_token(): """ 使用 AK,SK 生成鉴权签名(Access Token) :return: access_token,或是None(如果错误) """ url = "https://aip.baidubce.com/oauth/2.0/token" params = {"grant_type": "client_credentials", "client_id": API_KEY, "client_secret": SECRET_KEY} return str(requests.post(url, params=params).json().get("access_token")) if name == 'main': main()运行出现{'log_id': 1667825949995168966, 'error_msg': 'param image not exist', 'error_code': 216101},请修改一下
根据你提供的代码和运行结果,可能是以下两个问题导致的:
1. 请求头中的 Content-Type 不正确,应该设置为 multipart/form-data,而不是 application/x-www-form-urlencoded。
2. 请求体中的数据格式不正确,应该是 multipart/form-data 格式,而不是 application/x-www-form-urlencoded 格式。
为了解决这个问题,可以尝试以下修改:
1. 修改请求头中的 Content-Type,将其设置为 multipart/form-data:
```
headers = {
'Content-Type': 'multipart/form-data',
'Accept': 'application/json'
}
```
2. 修改请求体中的数据格式,将其设置为 multipart/form-data 格式。可以使用 Python 的 requests 库自动构建这种格式的请求体。修改后的代码如下:
```
import requests
import json
API_KEY = "jHxdMDf2bhEPxcG6zFoRUejo"
SECRET_KEY = "QF5GO9eLQncG2Zr1MKrxLOWvKAkAtVfI"
def main():
# 发送 OCR 请求并获取响应
url = "https://aip.baidubce.com/rest/2.0/ocr/v1/general?access_token=" + get_access_token()
image_path = "11.jpg"
with open(image_path, "rb") as image_file:
image_data = image_file.read()
response = requests.post(url, headers=headers, data={
'image': ('image.jpg', image_data)
})
result = json.loads(response.text)
print(result)
def get_access_token():
url = "https://aip.baidubce.com/oauth/2.0/token"
params = {"grant_type": "client_credentials", "client_id": API_KEY, "client_secret": SECRET_KEY}
return str(requests.post(url, params=params).json().get("access_token"))
if __name__ == '__main__':
headers = {
'Content-Type': 'multipart/form-data',
'Accept': 'application/json'
}
main()
```
这样就可以发送正确格式的 OCR 请求,并获取响应了。
根据下面的Python代码编写对应的订阅这代码#!/usr/bin/env python3 #coding=utf-8 import rospy from quadrotor_msgs.msg import PositionCommand from nav_msgs.msg import Odometry from sensor_msgs.msg import Joy from mavros_msgs.msg import AttitudeTarget from tf.transformations import euler_from_quaternion import math rospy.init_node('traj_pub') local_pos_pub3 = rospy.Publisher('/position_cmd', PositionCommand, queue_size=10) poscom = PositionCommand() rate = rospy.Rate(20.0) i = 0 while not rospy.is_shutdown(): poscom.header.stamp = rospy.Time.now() poscom.header.frame_id = "world" poscom.position.x = 2.5 * math.sin(math.pi * i / 400) poscom.position.y = 5 * math.sin(math.pi * i / 800) poscom.position.z = 2 poscom.velocity.x = 0 poscom.velocity.y = 0 poscom.velocity.z = 0 poscom.acceleration.x = 0 poscom.acceleration.y = 0 poscom.acceleration.z = 0 poscom.yaw = 0 poscom.yaw_dot = 0 poscom.jerk.x = 0 poscom.jerk.y = 0 poscom.jerk.z = 0 local_pos_pub3.publish(poscom) rate.sleep() i += 1
好的,根据您的要求,以下是一段Python代码,用于订阅quadrotor机器人的Odometry话题和Joy话题:
```python
import rospy
from nav_msgs.msg import Odometry
from sensor_msgs.msg import Joy
# 回调函数,接收到Odometry消息时调用
def odom_callback(data):
# 处理Odometry消息
pass
# 回调函数,接收到Joy消息时调用
def joy_callback(data):
# 处理Joy消息
pass
# 初始化ROS节点
rospy.init_node('my_subscriber')
# 创建一个订阅者,订阅Odometry话题
odom_subscriber = rospy.Subscriber('/odom', Odometry, odom_callback)
# 创建一个订阅者,订阅Joy话题
joy_subscriber = rospy.Subscriber('/joy', Joy, joy_callback)
# 持续监听消息
rospy.spin()
```
在上面的代码中,我们使用了ROS中的rospy库来连接到ROS节点,并创建了两个订阅者,分别订阅了"/odom"和"/joy"话题。当接收到来自这两个话题的消息时,我们通过回调函数odom_callback和joy_callback来处理消息。最后,我们使用rospy.spin()来持续监听消息。
阅读全文