[英]Try-Catch Exception won't work in OpenCV-python
So i purposely put a file in the cv2.VideoCapture() that doesn't exist in my computer to test the try-catch system i've put into my code but for some reason it still gives off the same error it gives when i didn't put a try-catch into my code.所以我特意在 cv2.VideoCapture() 中放置了一个文件,该文件在我的计算机中不存在以测试我已放入代码中的 try-catch 系统,但由于某种原因,它仍然会发出与我相同的错误没有将 try-catch 放入我的代码中。 how do i fix this??我该如何解决??
code:代码:
import numpy as np
import cv2
try:
cap = cv2.VideoCapture('trial.mkv')
while (cap.isOpened()):
ret, frame = cap.read()
cv2.imshow('frame', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
except:
print("no file found")
error:错误:
[ERROR:0] global C:\Users\appveyor\AppData\Local\Temp\1\pip-req-build-cff9bdsm\opencv\modules\videoio\src\cap.cpp (142) cv::VideoCapture::open VIDEOIO(CV_IMAGES): raised OpenCV exception:
OpenCV(4.4.0) C:\Users\appveyor\AppData\Local\Temp\1\pip-req-build-cff9bdsm\opencv\modules\videoio\src\cap_images.cpp:253: error: (-5:Bad argument) CAP_IMAGES: can't find starting number (in the name of file): trial.mkv in function 'cv::icvExtractPattern'
When an error is caught by the try/except
statement and stderr
msg is still printed it just means that the library is simply sending all the error messages to the stderr
.当try/except
语句捕获stderr
并且仍然打印stderr
msg 时,这仅意味着库只是将所有错误消息发送到stderr
。
OpenCV
library was known to have this problems and there are ways around it.众所周知, OpenCV
库存在这个问题,并且有解决方法。 You should read the detailed answers here - OpenCv error and How to stop OpenCV error message from printing in Python您应该在此处阅读详细答案 - OpenCv 错误和如何在 Python 中停止打印 OpenCV 错误消息
I definitely agree @AzyCrw4282 but I also point-out two issues on your code, therefore I'm writing as an answer.我绝对同意@AzyCrw4282,但我还指出了您代码中的两个问题,因此我正在写作作为答案。
except cv2.error as error
to catch the error:问题 #1:使用except cv2.error as error
来捕获错误: except cv2.error as error: print("[Error]: {}".format(error))
Result:结果:
[Error]: OpenCV(4.0.0) /Users/opencv/modules/highgui/src/window.cpp:350: error: (-215:Assertion failed) size.width>0 && size.height>0 in function 'imshow' Process finished with exit code 0
Even if I give an invalid video file:即使我给出了一个无效的视频文件:
OpenCV: Couldn't read video stream from file "trial.mkv" Process finished with exit code 0
Actually try-catch
block is working if you use except cv2.error as error
.如果您使用except cv2.error as error
则实际上try-catch
块正在工作。
Issue #2: Please always check frame问题 2:请始终检查框架
For instance: If you want to stop the application use:例如:如果您想停止应用程序,请使用:
if frame is None break
or或者
ret, frame = cap.read() if ret: cv2.imshow('frame', frame) if cv2.waitKey(1) & 0xFF == ord('q'): break
if frame returns then display如果帧返回则显示
Full code:完整代码:
import cv2
try:
cap = cv2.VideoCapture('dread.mp4')
while cap.isOpened():
ret, frame = cap.read()
if ret:
cv2.imshow('frame', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
except cv2.error as error:
print("[Error]: {}".format(error))
cap.release()
cv2.destroyAllWindows()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.