I'm afraid I have run into something a bit beyond the scope of my novice abilities. A quick summary of the problem: I am attempting to capture a live video stream during an experiment (using OpenSesame) from a webcam using the OpenCV python module. I can get it to work, but my issue is that the code pops open a new window to show the live stream that it is recording. How would I alter this code to NOT show the live window but still be able to press 'q' to shut off the live stream?
import numpy as np
import cv2
subject = str(self.get('subject_nr'))
cap = cv2.VideoCapture(0)
w=int(cap.get(cv2.CAP_PROP_FRAME_WIDTH ))
h=int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT ))
#Define the codec and create VideoWriter object
#fourcc = cv2.VideoWriter_fourcc(*'DIVX')
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('path\to\output'+ subject + '.avi', -1, 20.0, (w,h))
while(cap.isOpened()):
ret, frame = cap.read()
if ret==True:
out.write(frame)
cv2.imshow('frame',frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
else:
break
#Release everything if job is finished
cap.release()
out.release()
cv2.destroyAllWindows()
cv2.waitKey()
captures keystrokes from the highgui window. You cannot use waitKey
to capture keystrokes if you never display the window. You will need something that can capture keystrokes from the terminal.
For linux, you can achieve this using termios
and fcntl
modules. Here is an example from Python's documentation. https://docs.python.org/2/faq/library.html#how-do-i-get-a-single-keypress-at-a-time
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.