NickAsk
Posts: 61
Joined: Mon Mar 23, 2020 7:10 pm

python3 camera on/off problem

Wed Jun 24, 2020 7:42 am

Hello. I'm trying to figure out and solve a problem in my code without result.
I have a file in which I have a function to open camera.

Code: Select all

import cv2

cap = cv2.VideoCapture(0)         
cap.set(3, 320)
cap.set(4 , 240)


def open_camera(capture):
    
    if(capture):
        ret , frame = cap.read()             
        cv2.imshow("I see you !!!" , frame)   

    if(cv2.waitKey(1) == ord('c')):  
        cap.release()      
        cv2.destroyAllWindows()
        capture = False
        return capture

In another file I have a while True inside a function (call_getch() ) that makes another work .
In this while True I use an input and when I press "w" ,then I do something ,when I press "s" , I do something else(not important)
When I press "c" ,then I call open_camera(True) to open camera.Done.It's ok.
When I click above the window of video and then press "c" ,the camera and the window close.This is what I want.OK
But ,when I press again "c" button from keyboard ,I get an error and program terminates:

Code: Select all

cv2.error: OpenCV(4.2.0) /io/opencv/modules/highgui/src/window.cpp:376: error: (-215:Assertion failed) size.width>0 && size.height>0 in function 'imshow'
Code from while True (call_getch() ):

Code: Select all

def call_getch():
    enable_camera = False
    give_move = curses.initscr()
    curses.noecho()
    give_move.nodelay(1) # set getch() non-blocking
    try:
        while True:
            move_bar_with_w_s = give_move.getch()
            if(move_bar_with_w_s == ord("w")):
                do something...
                
            if(move_bar_with_w_s == ord("s")):
                do something else...
            
            if(move_bar_with_w_s == ord("c")) :
                enable_camera = True

            if(enable_camera):
                ac = open_camera(True) #return value from open_camera() method.
                if ac == False:
                    enable_camera = False
            
    finally:
        curses.endwin()
        system("clear")
        
call_getch()
Do you know where the error is and how can I fix it?
Thanks guys...

Heater
Posts: 15949
Joined: Tue Jul 17, 2012 3:02 pm

Re: python3 camera on/off problem

Wed Jun 24, 2020 7:52 am

I think it would be appreciated if you continued the discussion of your problem in one of the other two threads you started about it. Rather than cluttering up the forum with the the same thing over and over.

viewtopic.php?f=63&t=278010&p=1684070#p1684070
Memory in C++ is a leaky abstraction .

NickAsk
Posts: 61
Joined: Mon Mar 23, 2020 7:10 pm

Re: python3 camera on/off problem

Wed Jun 24, 2020 9:06 am

yes,my fault. You're right

NickAsk
Posts: 61
Joined: Mon Mar 23, 2020 7:10 pm

Re: python3 camera on/off problem

Wed Jun 24, 2020 5:22 pm

Any help here?

Heater
Posts: 15949
Joined: Tue Jul 17, 2012 3:02 pm

Re: python3 camera on/off problem

Wed Jun 24, 2020 5:51 pm

The line:

Code: Select all

ret , frame = cap.read()  
Returns two things, ret and frame.

One should always check the return values for erros, in this case what is "ret"?
Memory in C++ is a leaky abstraction .

NickAsk
Posts: 61
Joined: Mon Mar 23, 2020 7:10 pm

Re: python3 camera on/off problem

Wed Jun 24, 2020 6:06 pm

I have read that "ret" obtains return value from getting the camera frame, either true of false
if I omit it ,I get an error ->

Code: Select all

TypeError: Expected Ptr<cv::UMat> for argument 'mat'
So ,what can I do to solve this problem?

Thanks!!

Heater
Posts: 15949
Joined: Tue Jul 17, 2012 3:02 pm

Re: python3 camera on/off problem

Wed Jun 24, 2020 6:24 pm

That is not what I said.

I said check the value of "ret". See my example codes on one of your other threads about this.

Code: Select all

    # Capture an image
    ret, frame = capture.read()
    if ret :
        # Do stuff with frame
    else :
        print ('Capture.read() failed: ')
        # Do something else as .read failed
Memory in C++ is a leaky abstraction .

NickAsk
Posts: 61
Joined: Mon Mar 23, 2020 7:10 pm

Re: python3 camera on/off problem

Wed Jun 24, 2020 6:42 pm

Code: Select all

import cv2

cap = cv2.VideoCapture(0)         
cap.set(3 , 320)
cap.set(4 , 240)
    
def open_camera(capture):
    global cap
    if(capture):
        ret , frame = cap.read() 
        if(ret):            
            cv2.imshow("I see you !!!" , frame)   
        else:
            print("ret is false")
            
    if(cv2.waitKey(1) == ord('c')):  
        cap.release()      
        cv2.destroyAllWindows()
        capture = False
        return capture
When I call for first time open_camera(True) works fine ,then I close it (OK) ,but then when I want to reopen camera by calling open_camera(True)
I get in else condition,because ret = false.
How can I fix it?

NickAsk
Posts: 61
Joined: Mon Mar 23, 2020 7:10 pm

Re: python3 camera on/off problem

Wed Jun 24, 2020 6:53 pm

Ok,I think I did it.
Code:

Code: Select all

import cv2

cap = cv2.VideoCapture(0)         
cap.set(3 , 320)
cap.set(4 , 240)
    
def open_camera(capture):
    global cap
    if(capture):
        ret , frame = cap.read() 
        if(ret):            
            cv2.imshow("I see you !!!" , frame)   
        else:
            cap = cv2.VideoCapture(0)         
            cap.set(3 , 320)
            cap.set(4 , 240)
            
    if(cv2.waitKey(1) == ord('c')):  
        cap.release()      
        cv2.destroyAllWindows()
        capture = False
        return capture
I had to rewrite
cap = cv2.VideoCapture(0)
cap.set(3 , 320)
cap.set(4 , 240) if ret was false.
Anyway,thank you...

Heater
Posts: 15949
Joined: Tue Jul 17, 2012 3:02 pm

Re: python3 camera on/off problem

Wed Jun 24, 2020 7:02 pm

No idea.

But you need to be sure that for every time you call "cv2.VideoCapture" you should also call ".release()" on the captured object.

Given that you have mixed those pairs of call between different functions and with various "if" conditions around them I betting you do not have pairs of ".VideoCapture" and ".release" matched up.
Memory in C++ is a leaky abstraction .

Return to “General discussion”