-1

I am running my Flask app on Ubuntu VPS (linode) as server. I am capturing image from android studio sending it to flask server to get response. Image is being sent as base64 string from android studio. But the response is too slow when using request.form.to_dict(). Both ubuntu vps and android app are running on different IP. I've put timestamps on different processes and found this request.form.to_dict() is the problem taking around 22 to 35 secs. I am also using image compressing techniques on my android studio code but still too much time.

  • Tried on gunicorn with nginx but same.
  • threaded = True
  • Images are of around 400kb to 500kb size.

Kindly help me to figure out this issue.

Below is my flask code:

@app.route('/predict', methods=['POST', 'GET'])
def predict():

    if request.method == 'POST':
        file = request.form.to_dict()['upload']     # 22.2491 sec
        b64_string = file
        image = imread(io.BytesIO(base64.b64decode(b64_string)))   #0.0023 sec
        image = cv2.cvtColor(np.array(image), cv2.COLOR_BGR2RGB)   # 0.000171 sec
        .....
        .....
        .....

1 Answers1

0

sending the images in base64 format is not the recommended method, you should try to send the photo via a multipart request. Look here to see how to send an image from android in multipart form data (if you are using java).

If you send the images via multipart form data you will then be able to access them on flask using the method request.files['my_file_name']:

@app.route('/upload', methods=['POST'])
def upload_file():
  my_file = request.files['my_file_name']:
Matteo Pasini
  • 1,787
  • 3
  • 13
  • 26
  • I'll try this and then let you know. But just curious about how this is gonna help flask take less time in reading response?? does reading string image taking too long?? – Muhammad Daniyal Mar 18 '22 at 14:11
  • converting a file to base64 format will increase the number of characters needed to represent the file, so the amount of data you pass to the server is greater than sending it via multipart form data. https://stackoverflow.com/questions/47095294/what-is-difference-between-base64-and-multipart – Matteo Pasini Mar 18 '22 at 14:21
  • @Maetto can you suggest any other source for multipart form data. I am new to this multipart form data thing and that suggested link lacks some background info like import, methods etc. – Muhammad Daniyal Mar 19 '22 at 08:46
  • I suggest you open another question if you have another type of problem. – Matteo Pasini Mar 21 '22 at 06:33
  • @Maetto after spending two days finally succeeded in sending image file using multipart to flask. But still same result taking too much time even for low resolution image. Not successful. Any suggestions now?? – Muhammad Daniyal Mar 21 '22 at 11:34
  • How are you serving your application? Are you using gunicorn and nginx? Take a look at how a flask application should be published: https://flask.palletsprojects.com/en/2.0.x/deploying/ the flask development server is expected to be slow – Matteo Pasini Mar 21 '22 at 11:53
  • yes using gunicorn with nginx – Muhammad Daniyal Mar 21 '22 at 12:03