Close

UnicodeDecodeError when I tried to use API

Anything and everything related to using Sighthound Cloud

Moderator: Staff

no avatar
julius_m
 
Posts: 3
Joined: Wed Aug 30, 2017 2:35 pm

UnicodeDecodeError when I tried to use API

by julius_m » Wed Aug 30, 2017 2:45 pm

Hi,
I have tried to use the Detection API but I had an error. I am new in coding
I am using python 3 and I replaced httplib for http.client and I want to test with images from my computer (MAC)

This is my code:

Code: Select all
import base64
import http.client
import json
import os
import ssl

headers = {"Content-type": "application/json",
           "X-Access-Token": "60Lixxxxxxxxxxxxxxxxxxxx"}
conn = http.client.HTTPSConnection("dev.sighthoundapi.com",
       context=ssl.SSLContext(ssl.PROTOCOL_TLSv1))

# To use a hosted image uncomment the following line and update the URL
#image_data = "http://example.com/path/to/hosted/image.jpg"

# To use a local file uncomment the following line and update the path
image_data = base64.b64encode(open("/Users/julius/Desktop/prueba1.jpg").read())
params = json.dumps({"image": image_data})
conn.request("POST", "/v1/detections?type=face,person&faceOption=landmark,gender", params, headers)
response = conn.getresponse()
result = response.read()
print ("Detection Results) = " + str(result))


And the error is this:

Traceback (most recent call last):

File "<ipython-input-15-9ec7afe59fc8>", line 10, in <module>
image_data = base64.b64encode(open("/Users/julius/Desktop/prueba1.jpg").read())

File "/Users/julius/anaconda/lib/python3.6/encodings/ascii.py", line 26, in decode
return codecs.ascii_decode(input, self.errors)[0]

UnicodeDecodeError: 'ascii' codec can't decode byte 0xff in position 0: ordinal not in range(128)

Thank you!

julius

User avatar
ryan
 
Posts: 1015
Joined: Wed Aug 25, 2010 2:52 pm
Location: Palo Alto, California

Re: UnicodeDecodeError when I tried to use API

by ryan » Wed Aug 30, 2017 3:19 pm

For python 3 you'll want to change
Code: Select all
image_data = base64.b64encode(open("/Users/julius/Desktop/prueba1.jpg").read())
params = json.dumps({"image": image_data})

to
Code: Select all
image_data = base64.b64encode(open("/Users/julius/Desktop/prueba1.jpg", 'rb').read())
params = json.dumps({"image": image_data.decode('ascii')})
Learn more about Sighthound Video in our support pages - Reference Guide | All Articles
Are you a developer? Check out our cloud APIs - Demo | Docs

no avatar
julius_m
 
Posts: 3
Joined: Wed Aug 30, 2017 2:35 pm

Re: UnicodeDecodeError when I tried to use API

by julius_m » Wed Aug 30, 2017 3:50 pm

Thank you! Now is working

But I would like to get age and emotion. How I can do it?

Thank so much

no avatar
julius_m
 
Posts: 3
Joined: Wed Aug 30, 2017 2:35 pm

Re: UnicodeDecodeError when I tried to use API

by julius_m » Wed Aug 30, 2017 4:04 pm

I got the age and emotion

thank you!


Return to API Discussion
cron