代码之家  ›  专栏  ›  技术社区  ›  Mohammed Baashar

python在cv2失败后返回。视频捕获(0

  •  0
  • Mohammed Baashar  · 技术社区  · 6 年前

    我在python3中有以下代码:

    import cv2
    import sys
    import os
    import requests
    
    while True:
        try:
            stream =  cv2.VideoCapture(0)
        except Exception as e:
            print(e)
            return
    

    但是cv2。VideoCapture(0)在命令行中返回一个错误(红色),并且从不遵循except语句。

    一旦cam出现故障,我想从程序中返回;我该怎么做?

    0 回复  |  直到 6 年前
        1
  •  1
  •   yushulx    6 年前

    一旦摄像头出现故障,其状态将不会打开:

    while True:
        try:
            stream =  cv2.VideoCapture(0)
            if not stream.isOpened():
                return
        except Exception as e:
            print(e)
            return
    

    OpenCV Python是在CPyton中实现的。这个 try...catch... 如果本机代码不支持它,它将不起作用。