diff --git a/samples/dnn/edge_detection.cpp b/samples/dnn/edge_detection.cpp index f1324d8c76..a6993c647e 100644 --- a/samples/dnn/edge_detection.cpp +++ b/samples/dnn/edge_detection.cpp @@ -171,6 +171,11 @@ int main(int argc, char** argv) { else cap.open(0); + if (!cap.isOpened()) { + cerr << "Error: Video could not be opened." << endl; + return -1; + } + namedWindow("Input", WINDOW_AUTOSIZE); namedWindow("Output", WINDOW_AUTOSIZE); moveWindow("Output", 200, 0); diff --git a/samples/dnn/segmentation.cpp b/samples/dnn/segmentation.cpp index 1c9e976017..853c69e941 100644 --- a/samples/dnn/segmentation.cpp +++ b/samples/dnn/segmentation.cpp @@ -229,6 +229,12 @@ int main(int argc, char **argv) cap.open(findFile(parser.get("input"))); else cap.open(parser.get("device")); + + if (!cap.isOpened()) { + cerr << "Error: Video could not be opened." << endl; + return -1; + } + //! [Open a video file or an image file or a camera stream] // Process frames. Mat frame, blob; diff --git a/samples/dnn/segmentation.py b/samples/dnn/segmentation.py index 39b5edc8b5..c3934e8191 100644 --- a/samples/dnn/segmentation.py +++ b/samples/dnn/segmentation.py @@ -110,6 +110,10 @@ def main(func_args=None): cv.namedWindow(winName, cv.WINDOW_AUTOSIZE) cap = cv.VideoCapture(cv.samples.findFile(args.input) if args.input else 0) + if not cap.isOpened(): + print("Failed to open the input video") + exit(-1) + legend = None while cv.waitKey(1) < 0: hasFrame, frame = cap.read()