我想我的问题相对简单且天真,但是我是使用REST API的新手,所以我将不胜感激任何帮助或提示。
我正在尝试使用urllib(或其他我不需要安装的Python库)发送请求。 根据他们的guide,格式为:
POST https://vision.googleapis.com/v1/images:annotate?key=YOUR_API_KEY
{
"requests":[
{
"image":{
"content":"/9j/7QBEUGhvdG9...image contents...eYxxxzj/Coa6Bax//Z"
},
"features":[
{
"type":"LABEL_DETECTION",
"maxResults":1
}
]
}
]
}
当我尝试在浏览器的URL行中发送以下文本(仅供测试)时:
https://vision.googleapis.com/v1/images:{
"requests":[
{
"image":{
"content":"/9j/7QBEUGhvdG9eYxxxzj/Coa6Bax//Z"
},
"features":[
{
"type":"LABEL_DETECTION",
"maxResults":1
}
]
}
]
}?key=my_api_key
很不幸,我收到404错误。
我该怎么办?我应该使用任何库来生成请求吗?还是应该将JSON请求放在URL中的其他位置?
答案 0 :(得分:2)
如果您需要向Request Body
发送URL,则可以使用CURL
。要测试REST API,有一个著名的软件叫做POSTMAN。通过使用此功能,您可以发送请求并接收响应。
CURL,
curl -v -H "Content-Type: application/json" -X POST \
-d '{"image":{"content":"/9j/7QBEUGhvdG9...image contents...eYxxxzj/Coa6Bax//Z"}, "features":[{"type":"LABEL_DETECTION","maxResults":1}]}' https://vision.googleapis.com/v1/images:annotate?key=YOUR_API_KEY
使用POSTMAN,您可以为其赋予这些值并获得结果。
提供URL
https://vision.googleapis.com/v1/images:annotate?key=YOUR_API_KEY
选择HTTP方法为
POST
然后在raw
字段下添加“请求实体”,然后选择JSON(application/json)
,
{
"requests":[
{
"image":{
"content":"/9j/7QBEUGhvdG9...image contents...eYxxxzj/Coa6Bax//Z"
},
"features":[
{
"type":"LABEL_DETECTION",
"maxResults":1
}
]
}
]
}
答案 1 :(得分:0)
这对我有用:
import base64
import requests
import json
URL = "https://vision.googleapis.com/v1/images:annotate?key=YOUR_TOKEN"
#image to base64, which is a long long text
def encode_image(image_path):
with open(image_path, "rb") as image_file:
return base64.b64encode(image_file.read())
#make api call
def image_request(image_path):
data = {
"requests":[
{
"image":{
"content":encode_image(image_path)
},
"features":[
{
"type":"LABEL_DETECTION", #other options: LABEL_DETECTION FACE_DETECTION LOGO_DETECTION CROP_HINTS WEB_DETECTION
"maxResults": 10
}
]
}
]
}
r = requests.post(URL, json = data)
return r.text
#arg = path of image
def main(argv):
api_answer = json.loads(image_request(argv[1]))
try:
rows = api_answer['responses'][0]['labelAnnotations']
except:
print(file_to_proccess)
print(api_answer)
data = []
for item in rows:
data.append([item['mid'], item['description'], item['score'], item['topicality']])
# save somewhere the data list...
if __name__ == "__main__":
main(sys.argv)
答案 2 :(得分:0)
这已经过测试并且可以完美地工作
<ul id = "myList">
<li><a href="https://firebasestorage.googleapis.com/v0/b/cse611-3045f.appspot.com/o/test%40gmail.com%2F10-10-2019%2Ftest%40gmail.com-10-10-2019.xlsx?alt=media&token=21f64bfe-c7ce-4155-a24e-6684e784a8fd">10-10-2019</a></li>
<li><a href="https://firebasestorage.googleapis.com/v0/b/cse611-3045f.appspot.com/o/test%40gmail.com%2F11-10-2019%2Ftest%40gmail.com-11-10-2019.xlsx?alt=media&token=36367037-770e-4e99-b405-571c5aa79a64">11-10-2019</a></li>
<li><a href="https://firebasestorage.googleapis.com/v0/b/cse611-3045f.appspot.com/o/test%40gmail.com%2F5-10-2019%2Ftest%40gmail.com-5-10-2019.xlsx?alt=media&token=407ed4f5-b530-4149-99f6-fde685fba34a">5-10-2019</a></li></ul>