1 import os, glob, subprocess
\r
5 #Try to find the OpenCV library for video capture.
\r
6 from opencv import cv
\r
7 from opencv import highgui
\r
12 #Use the vidcap library directly from the VideoCapture package. (Windows only)
\r
13 # http://videocapture.sourceforge.net/
\r
14 # We're using the binary interface, not the python interface, so we don't depend on PIL
\r
15 import vidcap as win32vidcap
\r
19 class webcam(object):
\r
22 self._cam = highgui.cvCreateCameraCapture(-1)
\r
23 elif win32vidcap != None:
\r
24 self._cam = win32vidcap.new_Dev(0, False)
\r
25 #self._cam.displaycapturefilterproperties()
\r
26 #self._cam.displaycapturepinproperties()
\r
28 raise exception("No camera implementation available")
\r
30 self._doTimelaps = False
\r
33 def takeNewImage(self):
\r
35 frame = cv.QueryFrame(self._cam)
\r
36 cv.CvtColor(frame, frame, cv.CV_BGR2RGB)
\r
37 self._bitmap = wx.BitmapFromBuffer(frame.width, frame.height, frame.imageData)
\r
38 elif win32vidcap != None:
\r
39 buffer, width, height = self._cam.getbuffer()
\r
40 wxImage = wx.EmptyImage(width, height)
\r
41 wxImage.SetData(buffer[::-1])
\r
42 if self._bitmap != None:
\r
44 self._bitmap = wxImage.ConvertToBitmap()
\r
48 if self._doTimelaps:
\r
49 filename = os.path.normpath(os.path.join(os.path.split(__file__)[0], "../__tmp_snap", "__tmp_snap_%04d.jpg" % (self._snapshotCount)))
\r
50 self._snapshotCount += 1
\r
51 self._bitmap.SaveFile(filename, wx.BITMAP_TYPE_JPEG)
\r
55 def getLastImage(self):
\r
58 def startTimelaps(self, filename):
\r
59 self._cleanTempDir()
\r
60 self._timelapsFilename = filename
\r
61 self._snapshotCount = 0
\r
62 self._doTimelaps = True
\r
64 def endTimelaps(self):
\r
65 if self._doTimelaps:
\r
66 if platform.system() == "Windows":
\r
67 ffmpeg = os.path.normpath(os.path.join(os.path.split(__file__)[0], "../ffmpeg.exe"))
\r
69 ffmpeg = os.path.normpath(os.path.join(os.path.split(__file__)[0], "../ffmpeg"))
\r
70 basePath = os.path.normpath(os.path.join(os.path.split(__file__)[0], "../__tmp_snap", "__tmp_snap_%04d.jpg"))
\r
71 subprocess.call([ffmpeg, '-r', '12.5', '-i', basePath, '-vcodec', 'mpeg2video', '-pix_fmt', 'yuv420p', '-r', '25', '-y', '-b:v', '1500k', '-f', 'vob', self._timelapsFilename])
\r
72 self._doTimelaps = False
\r
74 def _cleanTempDir(self):
\r
75 basePath = os.path.normpath(os.path.join(os.path.split(__file__)[0], "../__tmp_snap"))
\r
77 os.makedirs(basePath)
\r
80 for filename in glob.iglob(basePath + "/*.jpg"):
\r