2

I guess my question is relatively simple and naive, but I'm new to use REST APIs so I would be grateful for any help or hint.

I'm trying to send a request with urllib (or another Python's library that I no need to install). Based on their guide, The format is:

POST https://vision.googleapis.com/v1/images:annotate?key=YOUR_API_KEY

and the JSON request format is:

{
  "requests":[
    {
      "image":{
        "content":"/9j/7QBEUGhvdG9...image contents...eYxxxzj/Coa6Bax//Z"
      },
      "features":[
        {
          "type":"LABEL_DETECTION",
          "maxResults":1
        }
      ]
    }
  ]
}

When I try to send the following text (just for test) in the URL line in my browser:

https://vision.googleapis.com/v1/images:{
  "requests":[
    {
      "image":{
        "content":"/9j/7QBEUGhvdG9eYxxxzj/Coa6Bax//Z"
      },
      "features":[
        {
          "type":"LABEL_DETECTION",
          "maxResults":1
        }
      ]
    }
  ]
}?key=my_api_key

I get unfortunately a 404 error.

What should I do? Should I use any library in order to generate the request? Or should I to place the JSON request in another place in the URL?

Super Mario
  • 923
  • 10
  • 16

3 Answers3

2

If you need to send Request Body with the URL you can use CURL. To test REST API's there is a famous software called POSTMAN. By using this you can send requests and receive the response.

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

Using POSTMAN you can give these values to it and get results.

Give URL,

https://vision.googleapis.com/v1/images:annotate?key=YOUR_API_KEY

Choose HTTP METHOD as,

POST

And add the REQUEST BODY under the raw field and choose JSON(application/json),

{
  "requests":[
    {
      "image":{
        "content":"/9j/7QBEUGhvdG9...image contents...eYxxxzj/Coa6Bax//Z"
      },
      "features":[
        {
          "type":"LABEL_DETECTION",
          "maxResults":1
        }
      ]
    }
  ]
}
Hasitha Jayawardana
  • 2,326
  • 4
  • 18
  • 36
  • Can I make this request with a simple URL request in browser or I must use POSTMAN? – Super Mario Mar 26 '19 at 10:55
  • 1
    Use postman. It's a lightweight software. Else you can use `Firefox`. Read 2nd answer. https://stackoverflow.com/questions/4797534/how-do-i-manually-fire-http-post-requests-with-firefox-or-chrome – Hasitha Jayawardana Mar 26 '19 at 11:06
1

This works for me:

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)
Super Mario
  • 923
  • 10
  • 16
1

this was tested and work perfect

            import base64
            import requests
            import json

            url = "https://vision.googleapis.com/v1/images:annotate"

            querystring = {"key":".........."}
            headers = {
                    'Content-Type': "application/json",
                    }
            def encode_image(image_path):
                with open(image_path, "rb") as image_file:
                    return base64.b64encode(image_file.read())

            def image_request(image_path):

                payload = '{  \"requests\":[    {      \"image\":{        \"content\":\"'+encode_image(image_path).decode('utf-8')+'"      },      \"features\":[        {          \"type\":\"TEXT_DETECTION\" }      ]    }  ]}'
                response = requests.request("POST", url, data=payload, headers=headers, params=querystring)

                return response.text
Ameur Bennaoui
  • 309
  • 3
  • 5