GottaAimHigherPal
GottaAimHigherPal

Reputation: 125

Is there a way to check if camera is connected without cap = cv2.videocapture

I am making a program that checks if the camera is connected, and if so, Show the webcam footage, The problem is: The way i structured my program i cannot have cap = cv2.videocapture() for the time it takes the command to execute. This makes sabotages for the showframe function and makes it only show a frame every ~1 second. Is there a different way to check if the camera is connected rather than cap = cv2.videocapture() and cap.isOpened()?

I also cannot have a while loop in my program because of the root.mainloop command for tkinter, However, if there is no way to check my camera status rather than cap.isOpened(), can i move the root.mainloop command somewhere where i can have a while True loop in my program?

I've tried both Multiprocessesing and Threading with no further success.

Heres some code:

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

Any and all help would be very appreciated!

Upvotes: 1

Views: 3192

Answers (2)

J.D.
J.D.

Reputation: 4561

You should not do a VideoCapture each frame, you only need to check if it exists. isOpened() is the proper function for that. If it does not yet exist, then retry the cam.

I modified your code:

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)

Upvotes: 1

Bob
Bob

Reputation: 236

When there is no webcam/image source, cap.read() will be (False, none). Therefore you can check if a webcam is connected if you do something like:

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

Hope this helps :)

Upvotes: 3

Related Questions