I am developing an psychology experiment that analyzes facial expressions made by users while completing a behavioral task. The application mostly runs through Tkinter, and i'm using openCV to capture video.
In a minimal case, i need to start and stop recording based on user responses. For example, in the code below, i want the user to specify when to start and stop recording video using the mouse to press a button.
import Tkinter as tk
import cv2
# -------begin capturing and saving video
def startrecording():
cap = cv2.VideoCapture(0)
fourcc = cv2.cv.CV_FOURCC(*'XVID')
out = cv2.VideoWriter('output.avi',fourcc, 20.0, (640,480))
while(cap.isOpened()):
ret, frame = cap.read()
if ret==True:
out.write(frame)
else:
break
# -------end video capture and stop tk
def stoprecording():
cap.release()
out.release()
cv2.destroyAllWindows()
root.quit()
root.destroy()
# -------configure window
root = tk.Tk()
root.geometry("%dx%d+0+0" % (100, 100))
startbutton=tk.Button(root,width=10,height=1,text='START',command = startrecording)
stopbutton=tk.Button(root,width=10,height=1,text='STOP', command = stoprecording)
startbutton.pack()
stopbutton.pack()
# -------begin
root.mainloop()
The problem is that OpenCV uses a loop to record video, during which Tkinter is unable to listen for user responses. The program gets stuck in the OpenCV loop and the user is unable to continue. How can i simultaneously record video and listen for user responses?
I have looked into parallel processing (e.g., Display an OpenCV video in tkinter using multiprocessing), but this sounds like a larger endeavor than seems necessary.
I have also looked into using root.after command (e.g., Show webcam sequence TkInter), but using this it appears that you can only capture a single frame, whereas i want a video.
Is there another way? will i need to use two processing streams?