saviopalmieri / ctypes-opencv

Automatically exported from code.google.com/p/ctypes-opencv
0 stars 0 forks source link

Control structure - Alternative to cvWaitkey? #50

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
I am showing camera input in a window, and from time to time recording and 
saving it to a file. 

To trigger the saving I use cvWaitKey(). 
OpenCV highgui reference says "This function is the only method in HighGUI that 
can fetch and handle 
events" http://opencv.willowgarage.com/documentation/python/user_interface.html

How is it possible to have a script internal logic to initiate e.g. a recoding 
of 10''? 
When I have cvWriteFrame() in a while loop, no frame will be updated in the 
window for the time of 
recording. 

I've tried to put the recording code into a python thread, no luck. 
Executing the recording code as a subprocess and piping key commands via stdin 
to it is very clumsy and 
doesn't work because the main script waits until the subprocess has terminated. 
Meaning i cannot execute  
any timing logic in the parent script. 

What am I missing?
Best
Raphael

#############################################################
#!/usr/bin/env python

from ctypes_opencv import *

window = cvNamedWindow('Camera', CV_WINDOW_AUTOSIZE)
cvMoveWindow('Camera', 10, 10)
capture = cvCreateCameraCapture(0) 
writer = cvCreateVideoWriter ("captured.mpg", CV_FOURCC('P','I','M','1'), 30, 
(640,480), True)

if __name__ == '__main__':

    while 1:
        frame = cvQueryFrame(capture)
        cvShowImage('Camera', frame)    

        # record
        if cvWaitKey (5) & 255 == 114:  # wait for 'r'
            cvWriteFrame(writer, frame) 

        # stop recording when 's' is received
        if cvWaitKey (5) & 255 == 115:   # s
            break

#############################################################

Original issue reported on code.google.com by raphael....@zhdk.ch on 31 Mar 2010 at 11:56

GoogleCodeExporter commented 8 years ago
I'm not sure I understand your problem, but this is how cvWaitKey(n) works:

If n = 0, it'll wait until the user presses a key, and return the key code of 
that key.
If n > 0, it'll wait for either a key is pressed or approximately n miliseconds 
has
elapsed. In the former case, it returns the key code. In the latter case, it 
returns -1.

Hope that helps.

Minh-Tri

Original comment by pmtri80@gmail.com on 1 Apr 2010 at 10:27

GoogleCodeExporter commented 8 years ago
Perhaps, you might want to write your code like the following?

recording = False
while 1:
    frame = cvQueryFrame(capture)
    cvShowImage('Camera', frame)

    if recording:
        cvWriteFrame(writer, frame)

    c = cvWaitKey(1) & 255
    if c==114:
        recording = !recording
        print "Started recording..." if recording else "Stopped recording..."
    elif c==115:
        break

Cheers,
Minh-Tri

Original comment by pmtri80@gmail.com on 1 Apr 2010 at 10:35

GoogleCodeExporter commented 8 years ago
Thank you Minh-Tri.

The question in other words would be: Is there another way to control the 
recording besides the cvWaitKey? 
Is there no work around? Because, in the end it means, my finger pressing on 
the keyboard is the logic controlling 
the recording procedure. 

Has no one tried to write a code controlled recording?
Meaning, starting and stopping the recording from a command within the code, 
not the keyboard.

The example below woulc be a function, to record for 5 seconds, but it doesn't 
work flawlessly
the recording takes place, but there is no image being displayed while 
recording. 

def recFiveSeconds():

    timeStart = time.clock()
    timeElapsed = 0

    while timeElapsed <= 5:
        frame = cvQueryFrame(capture)
        cvShowImage('Camera', frame)
        cvWriteFrame(writer, frame)

        timeElapsed = time.clock() - timeStart

Original comment by raphael....@zhdk.ch on 2 Apr 2010 at 8:27

GoogleCodeExporter commented 8 years ago
Oh, I see.

Nope, there's no other way. OpenCV uses polling to refresh the windows. Thus, 
you
have to have a cvWaitKey() call in your main loop. I usually just invoke
"cvWaitKey(1)". It's probably a technical issue that you need to check with the
Willow Garage people. 

Cheers,
Minh-Tri

Original comment by pmtri80@gmail.com on 2 Apr 2010 at 8:43