Home > database >  Error passing image path to Flask application
Error passing image path to Flask application

Time:02-16

I have a Flask application that it supposed to receive two images and output a , but when I run the app I get these errors, the code is working perfectly without Flask

Errors:

[ WARN:[email protected]] global /io/opencv/modules/imgcodecs/src/loadsave.cpp (239) findDecoder imread_('/home/criuser/Téléchargements/20210728_122019.jpg'): can't open/read file: check file path/integrity
[2022-02-15 22:17:15,439] ERROR in app: Exception on / [GET]
Traceback (most recent call last):
  File "/home/criuser/PycharmProjects/FINAL_AI/venv/lib/python3.8/site-packages/flask/app.py", line 2073, in wsgi_app
    response = self.full_dispatch_request()
  File "/home/criuser/PycharmProjects/FINAL_AI/venv/lib/python3.8/site-packages/flask/app.py", line 1518, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/home/criuser/PycharmProjects/FINAL_AI/venv/lib/python3.8/site-packages/flask/app.py", line 1516, in full_dispatch_request
    rv = self.dispatch_request()
  File "/home/criuser/PycharmProjects/FINAL_AI/venv/lib/python3.8/site-packages/flask/app.py", line 1502, in dispatch_request
    return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)
  File "/home/criuser/PycharmProjects/FINAL_AI/venv/final.py", line 19, in API
    result = change_bg.change_bg_img(f_image_path=original,
  File "/home/criuser/PycharmProjects/FINAL_AI/venv/lib/python3.8/site-packages/pixellib/tune_bg/__init__.py", line 236, in change_bg_img
    seg_image = self.segmentAsPascalvoc(f_image_path)
  File "/home/criuser/PycharmProjects/FINAL_AI/venv/lib/python3.8/site-packages/pixellib/tune_bg/__init__.py", line 53, in segmentAsPascalvoc
    h, w, n = image.shape
AttributeError: 'NoneType' object has no attribute 'shape'
127.0.0.1 - - [15/Feb/2022 22:17:15] "GET /?original=/home/criuser/Téléchargements/20210728_122019.jpg&background=/home/criuser/Téléchargements/Tableau_Dashboard.jpg HTTP/1.1" 500 -

Code:

curl "http://127.0.0.1:5000?original=/home/criuser/Téléchargements/20210728_122019.jpg&background=/home/criuser/Téléchargements/Tableau_Dashboard.jpg"

@app.route('/',methods=['GET'])
def API():
    if request.method == 'GET':
        original = request.args.get('original')
        background = request.args.get('background')
        change_bg = alter_bg(model_type="pb")
        change_bg.load_pascalvoc_model("/home/criuser/Téléchargements/xception_pascalvoc.pb")
        result = change_bg.change_bg_img(f_image_path=original,
                                b_image_path=background,
                                output_image_name="/home/criuser/Téléchargements/new.jpg")
        img_base64 = base64.b64encode(result.read())
        return jsonify(img_base64.decode())

CodePudding user response:

Problem is curl, not flask.

It seems curl sends it as latin1 (iso-8859-1) instead of utf-8 so it converts é into é - and later flask has problem to open Téléchargements (instead of Téléchargements)

Code 'é'.encode('latin1').decode() gives é - so it can confirm that it uses latin1.


This works correctly in curl:

curl -GET 'http://127.0.0.1:5000' --data-urlencode 'original=/home/criuser/Téléchargements/20210728_122019.jpg' --data-urlencode 'background=/home/criuser/Téléchargements/Tableau_Dashboard.jpg' 

Original url

http://127.0.0.1:5000?original=/home/criuser/Téléchargements/20210728_122019.jpg&background=/home/criuser/Téléchargements/Tableau_Dashboard.jpg

works correctly (for me) with:

  • web browser (Firefox, Chrome)
  • console command wget
  • console command http (python module httpie)
  • python module requests
  • tools to test web page (API) postman, insomnia

but standard module urllib.request has problem with native chars (it encodes with ascii)

  • Related