exadel-inc / CompreFace

Leading free and open-source face recognition system
https://exadel.com/accelerator-showcase/compreface/
Apache License 2.0
5.71k stars 775 forks source link

POST request with numpy arrays instead of local files #791

Open radisc opened 2 years ago

radisc commented 2 years ago

Hello,

I would like to use the face recognition service with numpy arrays instead of local files

Describe the solution you'd like I would like to make a POST request to the face recognition service with a numpy array insted of athe local address of the file

Describe alternatives you've considered Is there a way to convert them in such a way that the REST API wiil accept them?

Thanks

pospielov commented 2 years ago

Hello, do you mean Python SDK functionality? If yes - there are three possible ways to send the image: using a local path, using bytes, and using a URL. in your case, the best way is to use bytes. For this, first, you need to convert your NumPy array to an image: https://stackoverflow.com/a/2659378 Then, you can convert image into bytes: https://stackoverflow.com/a/33117447

Not sure if there is a way to convert NumPy array to bytes directly.

radisc commented 2 years ago

Thanks for the quick response, i didn't notice there was a python sdk, i will try it immediately

radisc commented 2 years ago

Sadly i don't think i can use the python sdk because it requires a newer version of python (>3.7) that i can't install because i'm on a jetson TX2.

By the way, when using bytes, how should i structure the rest of the query? For example, what content type i should use? "multipart/form-data" or "application/json"?

This is what i've tried after your suggestion

` cv_img = cv2.imread("/home/next/Pictures/photo5872717936299456967.jpg" )

img = Image.fromarray(cv_img, 'RGB') img_byte_arr = io.BytesIO() img.save(img_byte_arr, format='PNG')

img_byte_arr = img_byte_arr.getvalue()

headers = {'x-api-key': '701a07af-6ca8-4950-943f-4305adff200c', 'Content-Type': 'application/json'}

data = {'file' : img_byte_arr}

response = requests.post("http://172.16.92.198:8000/api/v1/recognition/recognize?face_plugins=landmarks, gender, age", headers = headers, data = data) ` And i keep getting a 400 Error

Also i found this, but i keep getting a 414 or similar

pospielov commented 2 years ago

It should be multipart/formdata instead of JSON Please look at the implementation of Pythos SDK: https://github.com/exadel-inc/compreface-python-sdk/blob/9f6e2c6fdd7b477697c83ed2210aab898d8b0ecf/compreface/client/detect_face_from_image.py#L47 and how to make a multipart/formdata: https://github.com/exadel-inc/compreface-python-sdk/blob/9f6e2c6fdd7b477697c83ed2210aab898d8b0ecf/compreface/common/multipart_constructor.py#L21