【问题标题】:Is there a way to check if camera is connected without cap = cv2.videocapture有没有办法检查相机是否在没有 cap = cv2.videocapture 的情况下连接
【发布时间】:2020-02-01 01:46:32
【问题描述】:

我正在制作一个程序来检查摄像头是否已连接,如果是,则显示网络摄像头镜头,问题是:我构建程序的方式在执行命令时无法使用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() 内部ShowFrame() 之前检查cap.read()
  • 你到底是什么意思,我不太明白...@QuangHoang

标签: python opencv tkinter


【解决方案1】:

当没有网络摄像头/图像源时,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()

希望这会有所帮助:)

【讨论】:

    【解决方案2】:

    你不应该每帧都做一个 VideoCapture,你只需要检查它是否存在。 isOpened() 是正确的功能。如果它尚不存在,则重试 cam。

    我修改了你的代码:

    def CheckSource():
    
        print("[INFO] CheckSource Triggered.")
    
        # check if cam is open, if so,  do showFrame   
        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:
            # cam is not open, try VideoCapture
            print("[WARNING] No source found. Looking for source.")
            cap = cv2.VideoCapture(ImageSource)
            lmain.after(10, CheckSource)
    

    【讨论】:

      猜你喜欢
      • 2019-06-17
      • 1970-01-01
      • 2020-10-23
      • 1970-01-01
      • 2021-12-04
      • 1970-01-01
      • 1970-01-01
      • 2011-06-08
      • 2019-11-01
      相关资源
      最近更新 更多