Skip to content

Instantly share code, notes, and snippets.

@ahmadia
Last active December 14, 2015 22:59
Show Gist options
  • Save ahmadia/5162313 to your computer and use it in GitHub Desktop.
Save ahmadia/5162313 to your computer and use it in GitHub Desktop.
capture and playback
import os
import sys
import time
import numpy as np
import matplotlib as mpl
mpl.use('Qt4Agg')
mpl.rcParams['backend.qt4']='PySide'
mpl.rcParams['toolbar'] = 'None'
#from PySide import
from PySide.QtGui import QApplication
from PySide.QtCore import QObject
from skimage.io import ImageCollection
from skimage.io.video import CvVideo
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import cv
class WebcamVideo(CvVideo):
"""
Opencv-based video loader.
Parameters
----------
source : str
Camera number.
size: tuple, optional
Size of returned array.
"""
def __init__(self, source=None, size=None, backend=None):
if source is None:
source = -1
self.source = source
self.capture = cv.CreateCameraCapture(source)
self.width = int(cv.GetCaptureProperty(self.capture,
cv.CV_CAP_PROP_FRAME_WIDTH))
self.height = int(cv.GetCaptureProperty(self.capture,
cv.CV_CAP_PROP_FRAME_HEIGHT))
self.size = size
# Note: color-intensive applications may require a different color allocation
# strategy.
FRAMES = 120
class BlitQT(QApplication):
def __init__(self):
QApplication.__init__(self,sys.argv)
self.v = WebcamVideo()
fourcc = cv.CV_FOURCC('I','4','2','0')
self.writer = cv.CreateVideoWriter('capture.avi', fourcc, 30,
(self.v.width, self.v.height),
is_color=1)
self.fig = plt.figure()
self.ax = self.fig.add_subplot(111)
self.ax.get_xaxis().set_visible(False)
self.ax.get_yaxis().set_visible(False)
self.im = self.ax.imshow(self.v.get())
self.cnt = 0
self.fig.canvas.draw()
def timerEvent(self, evt):
self.im.set_data(self.v.get())
self.fig.canvas.draw()
cv.GrabFrame(self.v.capture)
frame = cv.RetrieveFrame(self.v.capture)
cv.WriteFrame(self.writer, frame)
if self.cnt==FRAMES:
# print the timing info and quit
print 'FPS:' , FRAMES/(time.time()-self.tstart)
self.closeAllWindows()
else:
self.cnt += 1
if __name__ == "__main__":
app = BlitQT()
# for profiling
app.tstart = time.time()
app.startTimer(0)
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment