1 from __future__ import absolute_import
2 __copyright__ = "Copyright (C) 2013 David Braam - Released under terms of the AGPLv3 License"
11 from Cura.util import profile
12 from Cura.util.resources import getPathForImage
15 #Try to find the OpenCV library for video capture.
17 from opencv import highgui
22 #Use the vidcap library directly from the VideoCapture package. (Windows only)
23 # http://videocapture.sourceforge.net/
24 # We're using the binary interface, not the python interface, so we don't depend on PIL
25 import vidcap as win32vidcap
29 def hasWebcamSupport():
30 if cv == None and win32vidcap == None:
32 if not os.path.exists(getFFMPEGpath()):
38 if platform.system() == "Windows":
39 return os.path.normpath(os.path.join(os.path.split(__file__)[0], "../../ffmpeg.exe"))
40 elif os.path.exists('/usr/bin/ffmpeg'):
41 return '/usr/bin/ffmpeg'
42 return os.path.normpath(os.path.join(os.path.split(__file__)[0], "../../ffmpeg"))
48 self._cameraList = None
50 self._overlayImage = wx.Bitmap(getPathForImage('cura-overlay.png'))
51 self._overlayUltimaker = wx.Bitmap(getPathForImage('ultimaker-overlay.png'))
52 self._doTimelapse = False
55 #open the camera and close it to check if we have a camera, then open the camera again when we use it for the
63 if self._cam is not None:
64 cameraList.append(self._cam.getdisplayname())
71 self._cameraList = cameraList
74 return len(self._cameraList) > 0
76 def listCameras(self):
77 return self._cameraList
79 def setActiveCamera(self, cameraIdx):
80 self._camId = cameraIdx
83 if self._cameraList is not None and self._camId >= len(self._cameraList):
85 if self._cam is not None:
86 if self._activeId != self._camId:
92 self._activeId = self._camId
94 self._cam = highgui.cvCreateCameraCapture(self._camId)
95 elif win32vidcap is not None:
97 self._cam = win32vidcap.new_Dev(self._camId, False)
100 return self._cam is not None
102 def propertyPages(self):
104 #TODO Make an OpenCV property page
106 elif win32vidcap is not None:
107 return ['Image properties', 'Format properties']
109 def openPropertyPage(self, pageType=0):
110 if not self._openCam():
114 elif win32vidcap is not None:
116 self._cam.displaycapturefilterproperties()
120 tmp = win32vidcap.new_Dev(0, False)
121 tmp.displaycapturepinproperties()
124 def takeNewImage(self, withOverlay = True):
125 if not self._openCam():
128 frame = cv.QueryFrame(self._cam)
129 cv.CvtColor(frame, frame, cv.CV_BGR2RGB)
130 bitmap = wx.BitmapFromBuffer(frame.width, frame.height, frame.imageData)
131 elif win32vidcap is not None:
132 buffer, width, height = self._cam.getbuffer()
134 wxImage = wx.EmptyImage(width, height)
135 wxImage.SetData(buffer[::-1])
136 wxImage = wxImage.Mirror()
137 if self._bitmap is not None:
139 bitmap = wxImage.ConvertToBitmap()
147 dc.SelectObject(bitmap)
148 dc.DrawBitmap(self._overlayImage, bitmap.GetWidth() - self._overlayImage.GetWidth() - 5, 5, True)
149 if profile.getMachineSetting('machine_type').startswith('ultimaker'):
150 dc.DrawBitmap(self._overlayUltimaker, (bitmap.GetWidth() - self._overlayUltimaker.GetWidth()) / 2,
151 bitmap.GetHeight() - self._overlayUltimaker.GetHeight() - 5, True)
152 dc.SelectObject(wx.NullBitmap)
154 self._bitmap = bitmap
156 if self._doTimelapse:
157 filename = os.path.normpath(os.path.join(os.path.split(__file__)[0], "../__tmp_snap",
158 "__tmp_snap_%04d.jpg" % (self._snapshotCount)))
159 self._snapshotCount += 1
160 bitmap.SaveFile(filename, wx.BITMAP_TYPE_JPEG)
164 def getLastImage(self):
167 def startTimelapse(self, filename):
168 if not self._openCam():
171 self._timelapseFilename = filename
172 self._snapshotCount = 0
173 self._doTimelapse = True
174 print "startTimelapse"
176 def endTimelapse(self):
177 if self._doTimelapse:
178 ffmpeg = getFFMPEGpath()
179 basePath = os.path.normpath(
180 os.path.join(os.path.split(__file__)[0], "../__tmp_snap", "__tmp_snap_%04d.jpg"))
182 [ffmpeg, '-r', '12.5', '-i', basePath, '-vcodec', 'mpeg2video', '-pix_fmt', 'yuv420p', '-r', '25', '-y',
183 '-b:v', '1500k', '-f', 'vob', self._timelapseFilename])
184 self._doTimelapse = False
186 def _cleanTempDir(self):
187 basePath = os.path.normpath(os.path.join(os.path.split(__file__)[0], "../__tmp_snap"))
189 os.makedirs(basePath)
192 for filename in glob.iglob(basePath + "/*.jpg"):