Python Forum
Problem with using opencv in python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problem with using opencv in python
#10
Oh dear! What os are you using?

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 key
The 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 ...
Reply


Messages In This Thread
RE: Problem with using opencv in python - by Pedroski55 - Feb-14-2024, 05:40 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Help for the shortest way to install a suitable version of Python, Numpy, and OpenCV. Ezzat 2 2,396 Dec-23-2021, 12:34 PM
Last Post: snippsat
  Python OpenCV window not opening in fullscreen mode Zman350x 0 3,390 Apr-29-2021, 07:54 PM
Last Post: Zman350x
  Python IDE doesn't see opencv-python package on my Jetson Nano sadhaonnisa 1 3,408 Oct-11-2020, 01:04 AM
Last Post: Larz60+
  pip3 install opencv-python fails on 'skbuild' Pedroski55 2 5,919 Sep-15-2020, 11:33 AM
Last Post: snippsat
  python and openCV installation dejhost 16 11,199 Aug-04-2020, 02:23 AM
Last Post: snippsat
  How to detect the text above lines using OpenCV in Python pframe 0 2,557 Apr-14-2020, 09:53 AM
Last Post: pframe
  Developing Python with OpenCV into an Android App AviationFreak 1 6,904 Sep-29-2019, 08:55 AM
Last Post: wavic
  python opencv grayscale conversion error Spandora 1 9,652 May-26-2019, 10:43 AM
Last Post: heiner55
  python variable issues - using spyder and opencv Afrodizzyjack 5 5,926 Jun-19-2018, 09:46 AM
Last Post: gontajones
  Python openCV "img" is not defined error Arontbt 2 8,248 Apr-23-2018, 07:55 PM
Last Post: Arontbt

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020