3

I just want to capture one frame, save it to test.png and then exit the code. In this program exit() doesn't work, I have to use CTRL+C from the terminal every time.

import cv2

#cv2.namedWindow("window")
cap = cv2.VideoCapture(0)

if cap.isOpened(): # if we are able to capture the first frame.
    val, frame = cap.read()
else:
    val = False
while val:
    #cv2.imshow("window", frame)
    cv2.imwrite('test.png',frame)
    val, frame = cap.read()
    key = cv2.waitKey(20)
    if key == 27: # exit on ESC
        break

cap.release
cv2.destroyAllWindows()
exit()

Thanks in advance.

6
  • are you sure that the break is reached? Commented Jan 26, 2017 at 11:00
  • I don't think so, ESC doesn't work. @Vanojx1 Commented Jan 26, 2017 at 11:07
  • try puttin a print statement on the key variable before your if statement, this way you will find if the break is reached (which is not). You can update you question then. Commented Jan 26, 2017 at 11:10
  • @Bouramas, it keeps on printing the number 255. Commented Jan 26, 2017 at 13:34
  • Thus you see the issue @amolkulkarni . Thats why the break statement is not reached. Commented Jan 26, 2017 at 13:46

3 Answers 3

1

cv::waitKey only works if any openCV window is present and maybe active.

Since you commented #cv2.namedWindow("window") and #cv2.imshow("window", frame) out there is no waitKey time and no chance to fetch the key.

Does it work if you activate namedWindow and imshow?

To be sure additionally try

if key > 0: # exit on ESC
    break

to cancel on ANY keypress (but you still need an active openCV window)

To capture a single frame try this:

import cv2

cap = cv2.VideoCapture(0)

val = False
maxTry = 100 # maximum number of tries to capture a frame from an opened device
cTry = 0

if cap.isOpened(): # if we are able to capture the first        frame.
    while (!val) and (cTry < maxTry)
        val, frame = cap.read()
        cTry = cTry + 1
else:
    val = False
if val:
    cv2.imwrite('test.png',frame)
else:
    print "No image captured"

cap.release
exit()

I am not a python programmer so please forgive me any syntax errors (and give me a hint to correct them)

Sign up to request clarification or add additional context in comments.

2 Comments

That's basically what I suggested and apparently it still doesn't work.
it works when I remove the comment #. But as i have mentioned in the question, i want it to capture one frame and exit.
0

The method waitkey(t) waits t in milliseconds, so your code is waiting 20 ms for a key press for each loop. Considering that your

cv2.imwrite('test.png',frame)

might take some time to write to file, are you perhaps pressing your exiting key at the wrong moment?

Comments

0

Try adding & 0xFF to the wait key:

key = cv2.waitKey(20) & 0xFF

You also need an active window:

Note The function only works if there is at least one HighGUI window created and the window is active. If there are several HighGUI windows, any of them can be active.

5 Comments

Does any other key work? Instead of comparing with 27 try: ord('q')
Do one more test. Show image with cvw.imshow. Maybe the window capture the keystrokes.
Nothing. Still the same
Note The function only works if there is at least one HighGUI window created and the window is active. If there are several HighGUI windows, any of them can be active.
This is a really good reason that opencv should provide a way to make a window active programmatically!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.