在此处输入图像描述
嗨,我正试图将base64图像传递到postapi请求,但出现错误,请帮助我编程
import requests
import json, base64
final_list = []
def vehicle_damage(img):
url = "https://vehicle-damage-assessment.p.rapidapi.com/run"
payload = {
"draw_result": True,
"image": img
}
headers = {
"content-type": "application/json",
"X-RapidAPI-Host": "vehicle-damage-assessment.p.rapidapi.com",
"X-RapidAPI-Key": "bd31338435msha00c21a34e0a18dp10bb9cjsn067429eb078f"
}
response = requests.request("POST", url, json=payload, headers=headers)
res = response
json_data = json.loads(response.text)
anoted_url = (json_data['output_url'])
for item in (json_data['output']['elements']):
damage_cat = (item['damage_category'])
damage_loc = (item['damage_location'])
damage_per = (item['score'])
damage_per = damage_per*100
final_list.append(damage_cat)
final_list.append(damage_loc)
final_list.append(damage_per)
image = open("C:\Users\286303\Pictures\Car Damaged\Damaged\052a5e6f08d199f88379964c4e4d6196--car-body-repairs-audi-a.jpg", 'rb')
image_read = image.read()
image_64_encode = base64.b64decode(image_read)
print(image_64_encode)
vehicle_damage(image_64_encode)
您需要这样更改:
def vehicle_damage(img: bytes):
...
payload = {"draw_result": True, "image": str(img)}
...
将"img"设为函数参数中的字节,并在将其放入有效负载dict时将其设为字符串。