python - 有没有一种方法可以检查是否在没有cap = cv2.videocapture的情况下连接了摄像机

标签 python opencv tkinter

我正在制作一个程序,检查相机是否已连接,如果可以,请显示网络摄像机录像,问题是:我构造程序的方式无法在执行命令的时间内拥有cap = cv2.videocapture()。这会破坏showframe功能,并使它仅每隔约1秒钟显示一帧。除了cap = cv2.videocapture()cap.isOpened()之外,还有其他方法来检查相机是否已连接吗?

由于tkinter的root.mainloop命令,我的程序中也没有while循环,但是,如果没有办法检查相机的状态,而不是cap.isOpened(),我可以将root.mainloop命令移到可以有while True循环的地方在我的程序中?

我尝试了多进程和线程都没有成功。

这是一些代码:

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.")
    cap = cv2.VideoCapture(ImageSource)

    if cap.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()
print("[INFO] [DEBUG] Root.Mainoop triggered")

任何和所有帮助将不胜感激!

最佳答案

如果没有网络摄像头/图像源,则cap.read()将为(False, none)。因此,如果执行以下操作,则可以检查是否已连接网络摄像头:

import cv2
cap=cv2.VideoCapture(ImageSource)
while True:
    if cap.read()[0]==False:
        print("Not connected")
        cap=cv2.VideoCapture(imageSource)
    else:
        ret, frame=cap.read()
        cv2.imshow("webcam footage",frame)

    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
cap.release()
cv2.destroyAllWindows()

希望这可以帮助 :)

关于python - 有没有一种方法可以检查是否在没有cap = cv2.videocapture的情况下连接了摄像机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58207291/

相关文章:

asp.net - 以Webform显示从REST Web服务检索到的base64图像

python - 如何在启动 Tkinter 应用程序时隐藏控制台窗口,但在按下 GUI 按钮运行 python 脚本时重新打开它?

python - tkinter 强制缩进

python - AxesSubplot 对象的 xticks 等效函数

python - 导入 cv2 : ImportError: DLL load failed: windows 7 Anaconda 4. 3.0(64 位)Python 3.6.0

python - 是否可以在 Networkx 图中混合不同形状的节点?

python - 如何在 python 中使用 xpath 查询带有命名空间的 xml 数据

python - 如何使用python根据同一数据框中另一列的值对数据框中的列中的值进行排序

c# - EMGU CV使用C#进行实时眼动跟踪

python - 用Python重写Curl请求: Result Invalid Json