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 tKinter窗口不显示opencv片段_Python_Opencv_Video_Tkinter_Usb - Fatal编程技术网

Python tKinter窗口不显示opencv片段

Python tKinter窗口不显示opencv片段,python,opencv,video,tkinter,usb,Python,Opencv,Video,Tkinter,Usb,我遇到了一个问题,如果我从一开始就连接了网络摄像头,我的程序会按预期执行。但是,如果我没有插入网络摄像头,并且在程序启动后将其插入。它制作了一个大小合适的tKinter窗口,但是,它没有像预期的那样显示网络摄像头的镜头 下面是一些代码: from tkinter import * # Import the tkinter module (For the Graphical User Interface) import cv2 # Import the cv2 module for web c

我遇到了一个问题,如果我从一开始就连接了网络摄像头,我的程序会按预期执行。但是,如果我没有插入网络摄像头,并且在程序启动后将其插入。它制作了一个大小合适的tKinter窗口,但是,它没有像预期的那样显示网络摄像头的镜头

下面是一些代码:

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")

cap = cv2.VideoCapture(0)
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(frame):
    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, CheckSource)
    print("[INFO] Showed image")


def CheckSource():
    print("[INFO] CheckSource Triggered.")
    ok, frame = cap.read()
    if ok:
        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(frame)

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


CheckSource()
root.mainloop()

调用
cv2.VideoCapture
时会发生摄像头检测。一开始你只打一次电话。那么你是说我应该调用cv2.VideoCapture(0)而不是ok,frame=cap.read@DanMašek?此外,我还可以使用CheckSource函数中cap的语句要求来定义config cap?您可以使用来检查程序是否知道摄像头。如果为真,请读取帧,如果不是,请尝试使用cv2.VideoCapture检测cam。现在问题是。视频捕获需要花费太多的时间,现在它会出现一种古怪的行为,每2-3秒显示一帧
[INFO] CheckSource Triggered.
[WARNING] No source found. Looking for source.
[INFO] CheckSource Triggered.
[WARNING] No source found. Looking for source.
[INFO] CheckSource Triggered.
[WARNING] No source found. Looking for source.
[INFO] CheckSource Triggered.
[WARNING] No source found. Looking for source.
[INFO] CheckSource Triggered.
[WARNING] No source found. Looking for source.
[INFO] CheckSource Triggered.
[WARNING] No source found. Looking for source.

Somewhere here i connected the camera. 
But the camera is never detected.

[INFO] CheckSource Triggered.
[WARNING] No source found. Looking for source.
[INFO] CheckSource Triggered.
[WARNING] No source found. Looking for source.
[INFO] CheckSource Triggered.
[WARNING] No source found. Looking for source.
[INFO] CheckSource Triggered.
[WARNING] No source found. Looking for source.
[INFO] CheckSource Triggered.
[WARNING] No source found. Looking for source.
[INFO] CheckSource Triggered.
[WARNING] No source found. Looking for source.
[INFO] CheckSource Triggered.
[WARNING] No source found. Looking for source.
[INFO] CheckSource Triggered.
[WARNING] No source found. Looking for source.