2

I have an image and I want to send it to the serve. I'm using requests module to perform simple post request as following(info is a dictionary):

    import requests

    print(type(info["array_image"]))
    print(type(info["visual_features"]))
    response = requests.post("url", data=info)

output :

<class 'numpy.ndarray'>
<class 'torch.Tensor'>

on the server side I'm trying to receive them as arrays at least:

from flask import Flask, request

@app.route('/path', methods=['POST'])
def function_name():
    visual_features = request.form['visual_features']
    array_image = request.form['array_image']
    print(type(array_image))
    print(type(visual_features))

output:

<class 'str'>
<class 'str'>

I want to get a bytes array to build the image, but what I'm getting is a string... If I didn't find a way I'll encode arrays in bas64 and then decode it in the server...

2 Answers 2

1

Thanks to @praba230890 for giving me an easy to follow example.

I would still write the solution here down, since the provided link doesn't fit my case exactly.

import pickle
import io

bytes_image = pickle.dumps(info["array_image"])
stream = io.BytesIO(bytes_image)
files = {"bytes_image": stream}

info["array_image"] = None

response = http.post("url", data=info, files=files)

and in the server side:

from flask import Flask, request

@app.route('/path', methods=['POST'])
def function_name():
    image = request.files.get('bytes_image')
    bytes_image = image.read()

if you want to get the image from a file, then:

requests.post("http://localhost:5000/predict",
                 files={"file": open('<PATH/TO/.jpg/FILE>/cat.jpg','rb')})

The Solution I'm currently using:

remember info["array_image"] is a numpy array, and info is a dictionary

    import io
    info["image_shape_width"] = info["array_image"].shape[0]
    info["image_shape_height"] = info["array_image"].shape[1]

    bytes_image = info["array_image"].tobytes()
    stream = io.BytesIO(bytes_image)
    files = {"bytes_image": stream}

    info["array_image"] = None

    response = http.post(self.ip + "path", data=info, files=files)

then receive it

    from flask import Flask, request
    import numpy as np

    @app.route('/path', methods=['POST'])
    def function_name():    
        bytes_image = request.files.get('bytes_image')
        bytes_image = bytes_image.read()
        array_image = np.frombuffer(bytes_image, dtype=dtype)
        shape = (int(request.form['image_shape_width']), int(request.form['image_shape_height']), 3)         
        array_image = np.reshape(array_image, shape)
                                       
        image = Image.fromarray(array_image)
Sign up to request clarification or add additional context in comments.

Comments

0

You can try to convert your data to proper JSON or use Python pickle module or if you have an image as you've mentioned you can send it as a file (multipart request) to the server as shown in the examples here.

1 Comment

this is the half way...it worked, but I'm saving the image in a file then I pass the file to the post request...this is not good because I have to save every image in the storage, I want to pass a file without storing it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.