Feb-14-2024, 05:40 PM
(This post was last modified: Feb-14-2024, 05:40 PM by Pedroski55.)
Oh dear! What os are you using?
I use Ubuntu 22.04, I am doing this in Idle.
cap = cv2.VideoCapture(0)
to
cap = cv2.VideoCapture(1)
I get this in Idle:
If you run the above function camON() in Idle, do you at least see see:
I use Ubuntu 22.04, I am doing this in Idle.
def camON(): print('This is function camON() starting ... ') cap = cv2.VideoCapture(0) if not cap.isOpened(): print("Error: Could not open camera.") return else: while True: ret, frame = cap.read() if not ret: print("Error: Could not read frame.") break cv2.imshow('Camera Feed', frame) # setting waitKey(0) freezes the picture while you wait # cv2.waitKey(1) waits 1 millisecond so the picture stays alive key = cv2.waitKey(1) if key == 27 or key == 113: # esc or q to quit break cap.release() cv2.destroyAllWindows() print(f'Escape key was {chr(key)}.') return keyThe ouput, apart from the window with my ugly face, in Idle is:
Output:camON()
This is function camON() starting ...
Escape key was q.
113
When I changed cap = cv2.VideoCapture(0)
to
cap = cv2.VideoCapture(1)
I get this in Idle:
Output:camON()
This is function camON() starting ...
Error: Could not open camera.
Traceback (most recent call last):
File "/usr/lib/python3.10/idlelib/run.py", line 578, in runcode
exec(code, self.locals)
File "<pyshell#19>", line 1, in <module>
File "<pyshell#18>", line 20, in camON
UnboundLocalError: local variable 'key' referenced before assignment
Could it be that you have more than 1 cam? Try a different number? Strange that you don't get any message at all.If you run the above function camON() in Idle, do you at least see see:
Quote:This is function camON() starting ...