cv2 and Flask, window does not open after destroywindows function





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















I have a problem, i create a script for saving a photo using cv2 when a get request exists on Flask api (get), the image is send a ftp server and send the name the file in json.



The problem is that the first request works fine but when send second request the app it stays loading all the time, not send error and the frame does open, i think what cv2 saving other instance or the process destroywindow is not finished.enter image description here



This my code and the process is commented.



from flask import Flask, jsonify
import numpy as np
import ftplib as ftp
from cv2 import cv2
import datetime
import os



app = Flask(__name__)

@app.route('/image')
def get_images():

user = "user"
password = "pass"
server = ftp.FTP()

server.connect('1.1.1.1', 21)
try:
print("Logging in...")
server.login(user, password)
except:
return jsonify({ "error": "error: not acces ftp " }), 500

try:
cam = cv2.VideoCapture(0)
if not cam.isOpened():
raise NameError('Just a Dummy Exception, write your own')
except cv2.error as e:
print("cv2.error:", e)
except Exception as e:
print("Exception:", e)
else:
print(cam.isOpened())

try:
cv2.namedWindow("test")
except cv2.error as e:
print("cv2.error:", e)
except Exception as e:
print("Exception:", e)
else:
print("no problem reported")


while(cam.isOpened()):
retval, frame = cam.read()

if retval != True:
raise ValueError("Can't read frame")
break

print("cam read")
cv2.imshow("test", frame)
print("show image")
now=datetime.datetime.now().strftime("%Y%m%d%H%M%S")
img_name = "opencv_frame_{}.png".format(now)
cv2.imwrite(img_name, frame)
print("{} written!".format(img_name))
with open(img_name, 'rb') as f:
try:
server.storbinary('STOR '+img_name, f)
print("save image on FTP Server")
print("Delete Local image")
os.remove(img_name)
break
except ftp.all_errors as e:
return json.dumps({ "error": "Error writing ftp server:"+e}), 500
break
print("show image")
cam.release()
cv2.destroyWindow("test")
server.quit()
server.close()
data=[{'image':img_name}]
return jsonify(data)

app.run(debug=True,host='0.0.0.0', port=5000)









share|improve this question























  • You shouldn't open GUI windows in web applications. Also you should not open the video device inside the view. You will get troubles when there are multiple requests at the same time.

    – Klaus D.
    Jan 3 at 7:11











  • What do you recommend me?

    – Omar Duarte
    Jan 3 at 7:36


















0















I have a problem, i create a script for saving a photo using cv2 when a get request exists on Flask api (get), the image is send a ftp server and send the name the file in json.



The problem is that the first request works fine but when send second request the app it stays loading all the time, not send error and the frame does open, i think what cv2 saving other instance or the process destroywindow is not finished.enter image description here



This my code and the process is commented.



from flask import Flask, jsonify
import numpy as np
import ftplib as ftp
from cv2 import cv2
import datetime
import os



app = Flask(__name__)

@app.route('/image')
def get_images():

user = "user"
password = "pass"
server = ftp.FTP()

server.connect('1.1.1.1', 21)
try:
print("Logging in...")
server.login(user, password)
except:
return jsonify({ "error": "error: not acces ftp " }), 500

try:
cam = cv2.VideoCapture(0)
if not cam.isOpened():
raise NameError('Just a Dummy Exception, write your own')
except cv2.error as e:
print("cv2.error:", e)
except Exception as e:
print("Exception:", e)
else:
print(cam.isOpened())

try:
cv2.namedWindow("test")
except cv2.error as e:
print("cv2.error:", e)
except Exception as e:
print("Exception:", e)
else:
print("no problem reported")


while(cam.isOpened()):
retval, frame = cam.read()

if retval != True:
raise ValueError("Can't read frame")
break

print("cam read")
cv2.imshow("test", frame)
print("show image")
now=datetime.datetime.now().strftime("%Y%m%d%H%M%S")
img_name = "opencv_frame_{}.png".format(now)
cv2.imwrite(img_name, frame)
print("{} written!".format(img_name))
with open(img_name, 'rb') as f:
try:
server.storbinary('STOR '+img_name, f)
print("save image on FTP Server")
print("Delete Local image")
os.remove(img_name)
break
except ftp.all_errors as e:
return json.dumps({ "error": "Error writing ftp server:"+e}), 500
break
print("show image")
cam.release()
cv2.destroyWindow("test")
server.quit()
server.close()
data=[{'image':img_name}]
return jsonify(data)

app.run(debug=True,host='0.0.0.0', port=5000)









share|improve this question























  • You shouldn't open GUI windows in web applications. Also you should not open the video device inside the view. You will get troubles when there are multiple requests at the same time.

    – Klaus D.
    Jan 3 at 7:11











  • What do you recommend me?

    – Omar Duarte
    Jan 3 at 7:36














0












0








0








I have a problem, i create a script for saving a photo using cv2 when a get request exists on Flask api (get), the image is send a ftp server and send the name the file in json.



The problem is that the first request works fine but when send second request the app it stays loading all the time, not send error and the frame does open, i think what cv2 saving other instance or the process destroywindow is not finished.enter image description here



This my code and the process is commented.



from flask import Flask, jsonify
import numpy as np
import ftplib as ftp
from cv2 import cv2
import datetime
import os



app = Flask(__name__)

@app.route('/image')
def get_images():

user = "user"
password = "pass"
server = ftp.FTP()

server.connect('1.1.1.1', 21)
try:
print("Logging in...")
server.login(user, password)
except:
return jsonify({ "error": "error: not acces ftp " }), 500

try:
cam = cv2.VideoCapture(0)
if not cam.isOpened():
raise NameError('Just a Dummy Exception, write your own')
except cv2.error as e:
print("cv2.error:", e)
except Exception as e:
print("Exception:", e)
else:
print(cam.isOpened())

try:
cv2.namedWindow("test")
except cv2.error as e:
print("cv2.error:", e)
except Exception as e:
print("Exception:", e)
else:
print("no problem reported")


while(cam.isOpened()):
retval, frame = cam.read()

if retval != True:
raise ValueError("Can't read frame")
break

print("cam read")
cv2.imshow("test", frame)
print("show image")
now=datetime.datetime.now().strftime("%Y%m%d%H%M%S")
img_name = "opencv_frame_{}.png".format(now)
cv2.imwrite(img_name, frame)
print("{} written!".format(img_name))
with open(img_name, 'rb') as f:
try:
server.storbinary('STOR '+img_name, f)
print("save image on FTP Server")
print("Delete Local image")
os.remove(img_name)
break
except ftp.all_errors as e:
return json.dumps({ "error": "Error writing ftp server:"+e}), 500
break
print("show image")
cam.release()
cv2.destroyWindow("test")
server.quit()
server.close()
data=[{'image':img_name}]
return jsonify(data)

app.run(debug=True,host='0.0.0.0', port=5000)









share|improve this question














I have a problem, i create a script for saving a photo using cv2 when a get request exists on Flask api (get), the image is send a ftp server and send the name the file in json.



The problem is that the first request works fine but when send second request the app it stays loading all the time, not send error and the frame does open, i think what cv2 saving other instance or the process destroywindow is not finished.enter image description here



This my code and the process is commented.



from flask import Flask, jsonify
import numpy as np
import ftplib as ftp
from cv2 import cv2
import datetime
import os



app = Flask(__name__)

@app.route('/image')
def get_images():

user = "user"
password = "pass"
server = ftp.FTP()

server.connect('1.1.1.1', 21)
try:
print("Logging in...")
server.login(user, password)
except:
return jsonify({ "error": "error: not acces ftp " }), 500

try:
cam = cv2.VideoCapture(0)
if not cam.isOpened():
raise NameError('Just a Dummy Exception, write your own')
except cv2.error as e:
print("cv2.error:", e)
except Exception as e:
print("Exception:", e)
else:
print(cam.isOpened())

try:
cv2.namedWindow("test")
except cv2.error as e:
print("cv2.error:", e)
except Exception as e:
print("Exception:", e)
else:
print("no problem reported")


while(cam.isOpened()):
retval, frame = cam.read()

if retval != True:
raise ValueError("Can't read frame")
break

print("cam read")
cv2.imshow("test", frame)
print("show image")
now=datetime.datetime.now().strftime("%Y%m%d%H%M%S")
img_name = "opencv_frame_{}.png".format(now)
cv2.imwrite(img_name, frame)
print("{} written!".format(img_name))
with open(img_name, 'rb') as f:
try:
server.storbinary('STOR '+img_name, f)
print("save image on FTP Server")
print("Delete Local image")
os.remove(img_name)
break
except ftp.all_errors as e:
return json.dumps({ "error": "Error writing ftp server:"+e}), 500
break
print("show image")
cam.release()
cv2.destroyWindow("test")
server.quit()
server.close()
data=[{'image':img_name}]
return jsonify(data)

app.run(debug=True,host='0.0.0.0', port=5000)






python flask cv2 ftplib






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 3 at 7:04









Omar DuarteOmar Duarte

10613




10613













  • You shouldn't open GUI windows in web applications. Also you should not open the video device inside the view. You will get troubles when there are multiple requests at the same time.

    – Klaus D.
    Jan 3 at 7:11











  • What do you recommend me?

    – Omar Duarte
    Jan 3 at 7:36



















  • You shouldn't open GUI windows in web applications. Also you should not open the video device inside the view. You will get troubles when there are multiple requests at the same time.

    – Klaus D.
    Jan 3 at 7:11











  • What do you recommend me?

    – Omar Duarte
    Jan 3 at 7:36

















You shouldn't open GUI windows in web applications. Also you should not open the video device inside the view. You will get troubles when there are multiple requests at the same time.

– Klaus D.
Jan 3 at 7:11





You shouldn't open GUI windows in web applications. Also you should not open the video device inside the view. You will get troubles when there are multiple requests at the same time.

– Klaus D.
Jan 3 at 7:11













What do you recommend me?

– Omar Duarte
Jan 3 at 7:36





What do you recommend me?

– Omar Duarte
Jan 3 at 7:36












0






active

oldest

votes












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54017748%2fcv2-and-flask-window-does-not-open-after-destroywindows-function%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54017748%2fcv2-and-flask-window-does-not-open-after-destroywindows-function%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

MongoDB - Not Authorized To Execute Command

Npm cannot find a required file even through it is in the searched directory

in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith