Anoop saju
Anoop saju

Reputation: 480

Error while trying to upload file using kairos

Iam using kairos api for face recognition .Iam trying to enroll an image.The documentation here says it also accepts base64 encode photos.So I have encoded the image using base 64 and I get the following error

{"Errors":[{"ErrCode":5000,"Message":"an invalid image was sent must be jpg or p
ng format"}]}

I have used the following python code for sending the requests

import cv2
import requests
import base64
import json

image=cv2.imread('Face-images/Subject 9.jpg')
encoded_string =base64.b64encode(image)
payload2= {"image":encoded_string ,"subject_id":"Abhishek","gallery_name":"MyGallery"}
headers={'Content-Type':'application/json','app_id':'app_id','app_key':'app_key'}
r = requests.post('https://api.kairos.com/enroll',headers=headers,data=json.dumps(payload2),verify=False)
print  r.text   

Any help would be appreciated

Upvotes: 2

Views: 997

Answers (3)

Neel Pandit
Neel Pandit

Reputation: 23

Try this.

import cv2
import requests
import base64
import json

encoded_string = base64.b64encode(open("Face-images/Subject 9.jpg", 
'r').read())
payload_dict = {
    "image":encoded_string,
    "subject_id": "Abhishek",
    "gallery_name": "MyGallery"
}
payload = json.dumps(payload_dict)
headers={
'Content-Type':'application/json',
'app_id':'app_id',
'app_key':'app_key'
}
request = Request('https://api.kairos.com/enroll', data=payload, 
headers=headers)
response_body = urlopen(request).read()
print(response_body)

Upvotes: 1

Thiagarajan
Thiagarajan

Reputation: 183

I found the answer to the problem. You can try reading the image not using cv2, but as simple raw binary. cv2 reads it into a numpy array and you are encoding a numpy array. Reading like a simple file works for me, like below

with open ('messi.jpg','rb') as imgFh:
    img = imgFh.read()

Upvotes: 1

Attila Kis
Attila Kis

Reputation: 533

Don't encode your photos. Probably they accept it, but its harder to pass. Check this solution:

import requests

files = {"image": (filename,open(location+'/'+filename,"rb"))}
payload= {"subject_id":"Abhishek",
          "gallery_name":"MyGallery"}
headers={'Content-Type':'application/json',
         'app_id':'app_id',
         'app_key':'app_key'}
response = requests.post('https://api.kairos.com/enroll',headers=headers,data=payload,files=files,verify=False)
print response.text 

Upvotes: 1

Related Questions