Reputation: 35
I'm trying to display the frames captured by a CCD camera in real-time using pyplot animation. I wrote a short python script just to test this out, and while it works, it does so erratically. It will quickly update a dozen or so animation frames, then pause for a second, then update again, then pause again, and so on. I'd like to just have it update the plot continuously and smoothly, but I'm not sure where I'm going wrong.
I know it isn't the part where it makes a call to the camera's frame buffer; I tested out just calling that in a loop and it never slowed down, so I think it's somewhere in the actual processing of the animation frames.
My code is below:
import numpy as np
from matplotlib import pyplot as plt
from matplotlib import animation
import Pixis100
import time
# First set up the figure, the axis, and the plot element we want to animate
fig = plt.figure()
ax = fig.add_subplot(111)
# Create ccd object
ccd = Pixis100.device()
ccd.snapshot()
ccd.focusStart()
# focusStart() tells the camera to start putting captured frames into the buffer
line, = ax.plot(ccd.getFrame()[:,2:].sum(axis=0)[::-1],'b-')
# getFrame() makes a call to the CCD frame buffer and retrieves the most recent frame
# animation function
def update(data):
line.set_ydata(data)
return line,
def data_gen():
while True: yield ccd.getFrame()[:,2:].sum(axis=0)[::-1]
# call the animator
anim = animation.FuncAnimation(fig, update, data_gen,interval=10,blit=True)
plt.show()
ccd.focusStop()
# focusStop() just tells the camera to stop capturing frames
For reference, ccd.getFrame()[:,2:].sum(axis=0)[::-1] returns a 1x1338 array of integers. I wouldn't think this would be too much for the animation to handle at one time.
Upvotes: 0
Views: 3060
Reputation: 87366
The problem is not in animation
, the following works just fine:
import numpy as np
from matplotlib import pyplot as plt
from matplotlib import animation
import time
# First set up the figure, the axis, and the plot element we want to animate
fig = plt.figure()
ax = fig.add_subplot(111)
ax.set_xlim([0, 2 *np.pi])
ax.set_ylim([-1, 1])
th = np.linspace(0, 2 * np.pi, 1000)
line, = ax.plot([],[],'b-', animated=True)
line.set_xdata(th)
# getFrame() makes a call to the CCD frame buffer and retrieves the most recent frame
# animation function
def update(data):
line.set_ydata(data)
return line,
def data_gen():
t = 0
while True:
t +=1
yield np.sin(th + t * np.pi/100)
# call the animator
anim = animation.FuncAnimation(fig, update, data_gen, interval=10, blit=True)
plt.show()
The choppyness may come from either your frame grabber, the computation your are doing on it, or issues with the gui getting enough time on the main thread to re-draw. See time.sleep() required to keep QThread responsive?
Upvotes: 1