Add some OpenCV support to webcam class (untested).

master
daid 2012-09-26 13:19:12 +02:00
parent edba844794
commit 58693d422a
2 changed files with 38 additions and 18 deletions

View File

@ -1,6 +1,13 @@
import os, glob, subprocess
import wx
try:
#Try to find the OpenCV library for video capture.
from opencv import cv
from opencv import highgui
except:
cv = None
try:
#Use the vidcap library directly from the VideoCapture package. (Windows only)
# http://videocapture.sourceforge.net/
@ -9,11 +16,11 @@ try:
except:
win32vidcap = None
#TODO: We can also use OpenCV for camera capture. This should be cross platform compatible.
class webcam(object):
def __init__(self):
if win32vidcap != 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()
@ -24,6 +31,11 @@ class webcam(object):
self._bitmap = None
def takeNewImage(self):
if cv != None:
frame = cv.QueryFrame(self._cam)
cv.CvtColor(frame, frame, cv.CV_BGR2RGB)
self._bitmap = wx.BitmapFromBuffer(frame.width, frame.height, frame.imageData)
elif win32vidcap != None:
buffer, width, height = self._cam.getbuffer()
wxImage = wx.EmptyImage(width, height)
wxImage.SetData(buffer[::-1])
@ -51,7 +63,10 @@ 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"))
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

View File

@ -154,18 +154,18 @@ class MachineCom(object):
if port == 'AUTO':
programmer = stk500v2.Stk500v2()
self._log("Serial port list: %s" % (str(serialList())))
for port in serialList():
for p in serialList():
try:
self._log("Connecting to: %s" % (port))
programmer.connect(port)
self._log("Connecting to: %s" % (p))
programmer.connect(p)
self._serial = programmer.leaveISP()
profile.putPreference('serial_port_auto', port)
profile.putPreference('serial_port_auto', p)
break
except ispBase.IspError as (e):
self._log("Error while connecting to %s: %s" % (port, str(e)))
self._log("Error while connecting to %s: %s" % (p, str(e)))
pass
except:
self._log("Unexpected error while connecting to serial port: %s %s" % (port, getExceptionString()))
self._log("Unexpected error while connecting to serial port: %s %s" % (p, getExceptionString()))
programmer.close()
elif port == 'VIRTUAL':
self._serial = VirtualPrinter()
@ -178,6 +178,11 @@ class MachineCom(object):
self._serial = Serial(port, baudrate, timeout=2)
except:
self._log("Unexpected error while connecting to serial port: %s %s" % (port, getExceptionString()))
if self._serial == None:
self._log("Failed to open serial port (%s)" % (port))
self._errorValue = 'Failed to autodetect serial port.'
self._changeState(self.STATE_ERROR)
return
self._log("Connected to: %s, starting monitor" % (self._serial))
if baudrate == 0:
self._changeState(self.STATE_DETECT_BAUDRATE)