0
votes

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())
1
error shows that you get None instead image. First you could use print() to check what you get in variables. And next you could use if/else to skip code when you get None in original or background. BTW: you don't have to check request.method == 'GET': because you have methods=['GET'] . But checking request.method == 'GET': doesn't check if you have ?original=... &background=... in url.furas
it would be better to send JSON with field i.e. "status": "ok" when you send image, or "status": "error"when it couldn't create image.furas
first you could check what you have in request.args . Maybe problem makes native chars é because it shows Téléchargements instead of Téléchargementsfuras
I tested code and it seems problem makes curl which doesn't convert é. If I test your URL using web browser then it gets correctly Téléchargements. Maybe curl uses different encoding to send native chars.furas
running 'é'.encode('latin1').decode() I get é - and it can means curl sends URL as latin1 but flask expects utf-8. Web browser and command wget don't have this problem.furas

1 Answers

2
votes

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)