I am trying to detect faces using an online API which requires the image file to be uploaded in the binary format using multipart/form-data. I have found a way to convert the image to binary. Now, for the uploading part, is it necessary for me to write this binary data to a file? Also, I have written the following code and am getting an error-
ValueError: cannot encode objects that are not 2-tuples
The code I wrote is as follows-
import requests
import pprint
from toBinary import conv
img='sample.jpg'
img=conv(img)
params={
'api_key':<api key>,
'api_secret':<api secret>,
}
print("1.-")
r = requests.post(url='https://api-
us.faceplusplus.com/facepp/v3/detect',data=params,files=img)
for face in range(0,len(r.json()['faces'])):
pprint.pprint(r.json()['faces'][face]['face_token'])
img='01.jpg'
img=conv(img)
print("2-")
s = requests.post(url='https://api-
us.faceplusplus.com/facepp/v3/detect',data=params,files=img)
for face in range(0,len(s.json()['faces'])):
pprint.pprint(s.json()['faces'][face]['face_token'])
The toBinary class is as follows-
import binascii
def conv(image_file):
try:
fin = open(image_file, "rb")
data = fin.read()
fin.close()
except IOError:
print("Image file %s not found" % image_file)
raise SystemExit
hex_str = str(binascii.hexlify(data))
hex_list = []
bin_list = []
for ix in range(2, len(hex_str)-1, 2):
hex = hex_str[ix]+hex_str[ix+1]
hex_list.append(hex)
bin_list.append(bin(int(hex, 16))[2:])
bin_str = "".join(bin_list)
return(bin_str)
edit- Added the code for the toBinary class, changed the source for the images.