Crytrus
Crytrus

Reputation: 821

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()

Upvotes: 0

Views: 2021

Answers (2)

sarvani videla
sarvani videla

Reputation: 41

import numpy as np
import cv2

cap = cv2.VideoCapture(0)

if not(cap.isOpened()):
    cap.open()

while(cap.isOpened()):
    ret, frame = cap.read()
    cv2.imshow('frame',frame)
    k= cv2.waitKey(1)
    if k == 32:#when u press spacebar display that frame in another window
        cv2.imshow('new',frame)
    elif k == ord('q'):#press q to quit
        break

cap.release()
cv2.destroyAllWindows()

Upvotes: 1

unxnut
unxnut

Reputation: 8839

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.

Upvotes: 0

Related Questions