Search code examples
pythonpython-2.7opencvwebcam

OpenCV display single image webcam


I have a stream from my webcam. But I want to take a single frame when I press a key (spacebar) and display this single frame in another window. And each time I press the button the window gets updated with the new pic. Is this possible?

This is my code so far:

import cv2
import sys

video_capture = cv2.VideoCapture(0)

while True:
    # Capture frame-by-frame
    ret, frame = video_capture.read()

    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    # Display the resulting frame
    cv2.imshow('Video', frame)

    if cv2.waitKey(1) & 0xFF == ord('q'):
        break   
# When everything is done, release the capture
video_capture.release()
cv2.destroyAllWindows()

Solution

  • All you have to do is capture the output of waitKey in a variable. You are already checking it against q to break from the loop. In case it is space, you can display it in a different window using imshow.