Interactive mode
One way of updating a plot in matplotlib is to use interactive mode (plt.ion()
).
You should then not recreate new subplots for each frame you capture, but create your plot with images once and update it afterwards.
import cv2
import matplotlib.pyplot as plt
def grab_frame(cap):
ret,frame = cap.read()
return cv2.cvtColor(frame,cv2.COLOR_BGR2RGB)
#Initiate the two cameras
cap1 = cv2.VideoCapture(0)
cap2 = cv2.VideoCapture(1)
#create two subplots
ax1 = plt.subplot(1,2,1)
ax2 = plt.subplot(1,2,2)
#create two image plots
im1 = ax1.imshow(grab_frame(cap1))
im2 = ax2.imshow(grab_frame(cap2))
plt.ion()
while True:
im1.set_data(grab_frame(cap1))
im2.set_data(grab_frame(cap2))
plt.pause(0.2)
plt.ioff() # due to infinite loop, this gets never called.
plt.show()
FuncAnimation
Another option is of course to use matplotlib's built in FuncAnimation
which is especially designed to animate plots.
import cv2
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
def grab_frame(cap):
ret,frame = cap.read()
return cv2.cvtColor(frame,cv2.COLOR_BGR2RGB)
#Initiate the two cameras
cap1 = cv2.VideoCapture(0)
cap2 = cv2.VideoCapture(1)
#create two subplots
ax1 = plt.subplot(1,2,1)
ax2 = plt.subplot(1,2,2)
#create two image plots
im1 = ax1.imshow(grab_frame(cap1))
im2 = ax2.imshow(grab_frame(cap2))
def update(i):
im1.set_data(grab_frame(cap1))
im2.set_data(grab_frame(cap2))
ani = FuncAnimation(plt.gcf(), update, interval=200)
plt.show()
In order to close the window on a key press event, you may add a callback, like so
#... other code
ani = FuncAnimation(plt.gcf(), update, interval=200)
def close(event):
if event.key == 'q':
plt.close(event.canvas.figure)
cid = plt.gcf().canvas.mpl_connect("key_press_event", close)
plt.show()
# code that should be executed after window is closed.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…