Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 有没有一种不同于cv2.videocapture的检查摄像头状态的方法?_Python_Opencv - Fatal编程技术网

Python 有没有一种不同于cv2.videocapture的检查摄像头状态的方法?

Python 有没有一种不同于cv2.videocapture的检查摄像头状态的方法?,python,opencv,Python,Opencv,我有一个在循环中执行cv2.VideoCapture的代码。这使得代码不断检查摄像头是否打开,这是导致摄像头未按应有方式显示视频片段的原因。我有cv2.VideoCapture在环路内,因此我可以检测相机是否连接。有没有办法解决这个问题,有没有办法检查我的网络摄像头是否已连接 我尝试了多线程等方法,取得了进一步的成功 一些代码: from tkinter import * # Import the tkinter module (For the Graphical User Interface

我有一个在循环中执行cv2.VideoCapture的代码。这使得代码不断检查摄像头是否打开,这是导致摄像头未按应有方式显示视频片段的原因。我有cv2.VideoCapture在环路内,因此我可以检测相机是否连接。有没有办法解决这个问题,有没有办法检查我的网络摄像头是否已连接

我尝试了多线程等方法,取得了进一步的成功

一些代码:

from tkinter import *  # Import the tkinter module (For the Graphical User Interface)
import cv2  # Import the cv2 module for web camera footage
import PIL  # Import the pillow library for image configuration.
from PIL import Image, ImageTk  # Import the specifics for Image configuration.

print("[INFO] Imports done")

width, height = 800, 600  # Define The width and height widget for cap adjustment
RootGeometry = str(width) + "x" + str(height)  # Make a variable to adjust tkinter frame
print("[INFO] Geometries made")

ImageSource = 0
cap = cv2.VideoCapture(ImageSource)  # First VideoCapture
cap.set(cv2.CAP_PROP_FRAME_WIDTH, width)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, height)
print("[INFO] Cap set")

root = Tk()
print("[INFO] Window made")

root.title("Main Window")
root.configure(background="white")
root.geometry(RootGeometry)
root.bind('<Escape>', lambda e: root.quit())
lmain = Label(root)
lmain.pack()
print("[INFO] Configuration of cap done.")


def ShowFrame():
    ok, frame = cap.read()
    if ok:
        print("[INFO] Show frame Initialized.")

        cv2image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGBA)
        img = PIL.Image.fromarray(cv2image)
        imgtk = ImageTk.PhotoImage(image=img)
        lmain.imgtk = imgtk
        lmain.configure(image=imgtk)
        print("[INFO] After 10 initializing")
        lmain.after(10, ShowFrame)
        print("[INFO] Showed image")

    else:
        lmain.after(10, CheckSource)


def CheckSource():
    print("[INFO] CheckSource Triggered.")
    capture = cv2.VideoCapture(ImageSource)
    if capture.isOpened():
        print("[INFO] [DEBUG] if Ok initialized")
        if cv2.waitKey(1) & 0xFF == ord('q'):
            cv2.destroyAllWindows()
            cv2.waitKey(0)
            print("[WARNING] Exiting app after command")

        ShowFrame()

    else:
        print("[WARNING] No source found. Looking for source.")
        lmain.after(10, CheckSource)


CheckSource()
root.mainloop()
从tkinter导入*#导入tkinter模块(用于图形用户界面)
导入cv2#导入网络摄像机镜头的cv2模块
导入PIL#导入枕头库进行图像配置。
从PIL导入图像,ImageTk#导入图像配置的细节。
打印(“[INFO]导入完成”)
宽度,高度=800600#定义用于调整盖的宽度和高度小部件
RootGeometry=str(宽度)+“x”+str(高度)#生成一个变量以调整tkinter帧
打印(“[INFO]制作的几何图形”)
ImageSource=0
cap=cv2.视频捕获(图像源)#第一次视频捕获
封盖套件(cv2.cap\u PROP\u FRAME\u WIDTH,WIDTH)
盖组件(cv2.cap\u PROP\u FRAME\u HEIGHT,HEIGHT)
打印(“[INFO]封顶套件”)
root=Tk()
打印(“[信息]窗口已创建”)
root.title(“主窗口”)
root.configure(background=“white”)
root.geometry(RootGeometry)
root.bind(“”,lambda e:root.quit())
lmain=标签(根)
l主要包装
打印(“[INFO]完成cap配置”)
def ShowFrame():
好的,frame=cap.read()
如果确定:
打印(“[INFO]显示已初始化的帧”)
cv2image=cv2.CVT颜色(帧,cv2.COLOR_BGR2RGBA)
img=PIL.Image.fromarray(cv2image)
imgtk=ImageTk.PhotoImage(image=img)
lmain.imgtk=imgtk
lmain.configure(image=imgtk)
打印(“[INFO]在10次初始化后”)
主要后(10,展示架)
打印(“[INFO]显示的图像”)
其他:
主要后(10,检查来源)
def CheckSource():
打印(“[INFO]检查源已触发。”)
捕获=cv2.视频捕获(图像源)
如果capture.isOpened():
打印(“[INFO][DEBUG]如果确定已初始化”)
如果cv2.waitKey(1)&0xFF==ord('q'):
cv2.destroyAllWindows()
cv2.等待键(0)
打印(“[警告]命令后退出应用程序”)
ShowFrame()
其他:
打印(“[警告]找不到源。正在查找源。”)
主要后(10,检查来源)
CheckSource()
root.mainloop()

我们将非常感谢您的任何帮助

你能用下面的方法检查一下吗

cap.isOpened()
更多信息请访问:,