update frame in matplotlib with live camera preview

后端 未结 1 1901
无人及你
无人及你 2020-12-03 06:18

I am new to both Python and Matplotlib. My computer is connected to two usb cameras, and I intend to use the subplot(1,2,1) and subplot(1,2,2) in matplotlib to plot the fram

相关标签:
1条回答
  • 2020-12-03 06:44

    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.
    
    0 讨论(0)
提交回复
热议问题