Speed up live plotting of a footage (cv2)

冷暖自知 提交于 2019-12-01 01:21:27

This question's answer shows two ways to obtain a video in matplotlib.

The main point is not to recreate the complete plot on every iteration. If using the second approach from that answer, the use of blit=True may increase speed even more. This is shown in the below code.

import cv2
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation

df = pd.DataFrame({"time": np.linspace(0,20, num=100), 
                   "force" : np.cumsum(np.random.randn(100))})

def grab_frame(cap):
    ret,frame = cap.read()
    return frame # or cv2.cvtColor(frame,cv2.COLOR_BGR2RGB)

#Initiate 
vidcap = cv2.VideoCapture(0)
# vidcap.set(1,590)

fig, (ax,ax2) = plt.subplots(ncols=2,figsize=(20, 10))

x=df["time"][7:100]
y=df["force"][7:100]

#create two image plots
im1 = ax.imshow(grab_frame(vidcap),extent=[0,200,0,100], aspect='auto')
line, = ax2.plot(x[0:1],y[0:1],'or')
ax2.set_xlim(x.min(), x.max())
ax2.set_ylim(y.min(), y.max())

def update(i):
    im1.set_data(grab_frame(vidcap))
    line.set_data(x[0+i:1+i],y[0+i:1+i])
    return im1, line


ani = FuncAnimation(fig, update, frames=len(x), interval=1, blit=True)
plt.show()
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!