Question

I'm experimenting a bit with an API which can detect faces from an image. I'm using Python and want to be able to upload an image that specifies an argument (in the console). For example:

python detect.py jack.jpg

This is meant to send file jack.jpg up to the API. And afterwards print JSON response. Here is the documentation of the API to identify the face.

http://rekognition.com/developer/docs#facerekognize

Below is my code, I'm using Python 2.7.4

#!/usr/bin/python

# Imports
import sys
import requests
import json
# Facedetection.py sends us an argument with a filename
filename = (sys.argv[1])
# API-Keys
rekognition_key = ""
rekognition_secret = ""
array = {'api_key': rekognition_key,
                'api_secret': rekognition_secret,
                'jobs': 'face_search',
                'name_space': 'multify',
                'user_id': 'demo',
                'uploaded_file': open(filename)
                }  

endpoint = 'http://rekognition.com/func/api/'
response = requests.post(endpoint, params= array)

data = json.loads(response.content)
print data

I can see that everything looks fine, but my console gets this output:

Traceback (most recent call last):
  File "upload.py", line 23, in <module>
    data = json.loads(response.content)
  File "/usr/lib/python2.7/json/__init__.py", line 338, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python2.7/json/decoder.py", line 365, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/lib/python2.7/json/decoder.py", line 383, in raw_decode
    raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded

What is wrong?

Was it helpful?

Solution

Success!

The question didn't contain good code. I used code from here: uploading a file to imgur via python

#!/usr/bin/python

# Imports
import base64
import sys
import requests
import json
from base64 import b64encode
# Facedetection.py sends us an argument with a filename
filename = (sys.argv[1])
# API-Keys
rekognition_key = ""
rekognition_secret = ""
url = "http://rekognition.com/func/api/"

j1 = requests.post(
    url, 
    data = {
        'api_key': rekognition_key,
        'api_secret': rekognition_secret,
        'jobs': 'face_recognize',
        'name_space': 'multify',
        'user_id': 'demo',
        'base64': b64encode(open(filename, 'rb').read()),
    }
)

data = json.loads(j1.text)
print data

Now this: python detect.py jack.jpg returns the wanted JSON. Fully working.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top