Updated webcam support a bit, still needs more work to access properties, and enable/disable timelaps movie creation.

master
daid 2012-09-27 17:20:31 +02:00
parent 19dd02fb75
commit a0c1daa36c
2 changed files with 58 additions and 21 deletions

View File

@ -98,10 +98,8 @@ class printWindow(wx.Frame):
self.termHistoryIdx = 0
self.cam = None
try:
if webcam.hasWebcamSupport():
self.cam = webcam.webcam()
except:
pass
#self.SetIcon(icon.getMainIcon())
@ -239,16 +237,19 @@ class printWindow(wx.Frame):
nb.AddPage(self.termPanel, 'Term')
if self.cam != None:
if self.cam != None and self.cam.hasCamera():
self.camPage = wx.Panel(nb)
sizer = wx.GridBagSizer(2, 2)
self.camPage.SetSizer(sizer)
self.camPreview = wx.Panel(self.camPage)
sizer.Add(self.camPreview, pos=(0,0), flag=wx.EXPAND)
nb.AddPage(self.camPage, 'Camera')
self.camPage.timer = wx.Timer(self)
self.Bind(wx.EVT_TIMER, self.OnCameraTimer, self.camPage.timer)
self.camPage.timer.Start(500)
self.camPage.Bind(wx.EVT_ERASE_BACKGROUND, self.OnCameraEraseBackground)
self.camPreview.timer = wx.Timer(self)
self.Bind(wx.EVT_TIMER, self.OnCameraTimer, self.camPreview.timer)
self.camPreview.timer.Start(500)
self.camPreview.Bind(wx.EVT_ERASE_BACKGROUND, self.OnCameraEraseBackground)
self.sizer.AddGrowableRow(3)
self.sizer.AddGrowableCol(3)
@ -281,7 +282,7 @@ class printWindow(wx.Frame):
if self.machineCom != None and not self.machineCom.isPrinting():
return
self.cam.takeNewImage()
self.camPage.Refresh()
self.camPreview.Refresh()
def OnCameraEraseBackground(self, e):
dc = e.GetDC()
@ -289,7 +290,7 @@ class printWindow(wx.Frame):
dc = wx.ClientDC(self)
rect = self.GetUpdateRegion().GetBox()
dc.SetClippingRect(rect)
dc.SetBackground(wx.Brush(self.camPage.GetBackgroundColour(), wx.SOLID))
dc.SetBackground(wx.Brush(self.camPreview.GetBackgroundColour(), wx.SOLID))
dc.Clear()
if self.cam.getLastImage() != None:
dc.DrawBitmap(self.cam.getLastImage(), 0, 0)
@ -487,7 +488,7 @@ class printWindow(wx.Frame):
def mcZChange(self, newZ):
if self.cam != None:
wx.CallAfter(self.cam.takeNewImage)
wx.CallAfter(self.camPage.Refresh)
wx.CallAfter(self.camPreview.Refresh)
class temperatureGraph(wx.Panel):
def __init__(self, parent):

View File

@ -1,4 +1,4 @@
import os, glob, subprocess
import os, glob, subprocess, platform
import wx
try:
@ -16,21 +16,58 @@ try:
except:
win32vidcap = None
def hasWebcamSupport():
if cv == None and win32vidcap == None:
return False
if not os.path.exists(getFFMPEGpath()):
return False
return True
def getFFMPEGpath():
if platform.system() == "Windows":
return os.path.normpath(os.path.join(os.path.split(__file__)[0], "../ffmpeg.exe"))
elif os.path.exists('/usr/bin/ffmpeg'):
return '/usr/bin/ffmpeg'
return os.path.normpath(os.path.join(os.path.split(__file__)[0], "../ffmpeg"))
class webcam(object):
def __init__(self):
self._cam = None
if cv != None:
self._cam = highgui.cvCreateCameraCapture(-1)
elif win32vidcap != None:
self._cam = win32vidcap.new_Dev(0, False)
#self._cam.displaycapturefilterproperties()
#self._cam.displaycapturepinproperties()
else:
raise exception("No camera implementation available")
try:
self._cam = win32vidcap.new_Dev(0, False)
except:
pass
self._doTimelaps = False
self._bitmap = None
def hasCamera(self):
return self._cam != None
def propertyPages():
if self._cam == None:
return []
if win32vidcap != None:
return ['capture properties', 'pin properties']
if cv != None:
#TODO Make an OpenCV property page
return []
def openPropertyPage(pageType = 0):
if self._cam == None:
return
if win32vidcap != None:
if pageType == 0:
self._cam.displaycapturefilterproperties()
else:
self._cam.displaycapturepinproperties()
def takeNewImage(self):
if self._cam == None:
return
if cv != None:
frame = cv.QueryFrame(self._cam)
cv.CvtColor(frame, frame, cv.CV_BGR2RGB)
@ -56,6 +93,8 @@ class webcam(object):
return self._bitmap
def startTimelaps(self, filename):
if self._cam == None:
return
self._cleanTempDir()
self._timelapsFilename = filename
self._snapshotCount = 0
@ -63,10 +102,7 @@ class webcam(object):
def endTimelaps(self):
if self._doTimelaps:
if platform.system() == "Windows":
ffmpeg = os.path.normpath(os.path.join(os.path.split(__file__)[0], "../ffmpeg.exe"))
else:
ffmpeg = os.path.normpath(os.path.join(os.path.split(__file__)[0], "../ffmpeg"))
ffmpeg = getFFMPEGpath()
basePath = os.path.normpath(os.path.join(os.path.split(__file__)[0], "../__tmp_snap", "__tmp_snap_%04d.jpg"))
subprocess.call([ffmpeg, '-r', '12.5', '-i', basePath, '-vcodec', 'mpeg2video', '-pix_fmt', 'yuv420p', '-r', '25', '-y', '-b:v', '1500k', '-f', 'vob', self._timelapsFilename])
self._doTimelaps = False