0

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.

1 Answers1

0

I haven't checked this solution

But you may try using this https://github.com/FacePlusPlus/facepp-python-sdk

copy this file to your project https://github.com/FacePlusPlus/facepp-python-sdk/blob/master/facepp.py

and then use it like that:

from facepp import API
api = API(key, secret)
api.detection.detect(url=url)
AvielNiego
  • 1,193
  • 2
  • 12
  • 27
  • Where in your code do I refer the image from which I am trying to detect the face? – Soumya Khurana Feb 01 '18 at 06:42
  • `api.detection.detect(url='https://pmcdeadline2.files.wordpress.com/2015/08/kevin-spacey-house-of- cards-s4.jpg?w=446&h=299&crop=1')` – AvielNiego Feb 01 '18 at 06:45
  • I am eventually trying to go for an image that is saved locally. The api reference states that locally saved images need to be converted to binary and then uploaded in the manner that I stated before. I am sorry if I didnt state that before. I was just trying the conversion for a url image before trying to convert a locally saved image. – Soumya Khurana Feb 01 '18 at 06:52
  • You can try this `api.detection.detect(img = File('/tmp/test.jpg'))` – AvielNiego Feb 01 '18 at 06:55
  • The api documentation states that if a locally saved image has to be uploaded, it has to be converted to its binary or base64 encoded binary form. Still, I will try the method you suggested and report back, thanks for the help. The documentation of the api, in case you need it, is [here](https://console.faceplusplus.com/documents/5679127) – Soumya Khurana Feb 01 '18 at 07:09
  • Tried calling the method as you suggested. Didn't work out as expected. Still shows me an error, except that this time it showed me error 400. So, I'm guessing that this method does not work. Let me know if you think otherwise. P.S- if you are going to try it, the sdk requires python 2.7. Learnt that the hard way. – Soumya Khurana Feb 01 '18 at 09:17