<s> __copyright__ = "<STR_LIT>" <EOL> import os <EOL> import glob <EOL> import subprocess <EOL> import platform <EOL> import wx <EOL> from Cura . util import profile <EOL> from Cura . util . resources import getPathForImage <EOL> try : <EOL> from opencv import cv <EOL> from opencv import highgui <EOL> except : <EOL> cv = None <EOL> try : <EOL> import vidcap as win32vidcap <EOL> except : <EOL> win32vidcap = None <EOL> def hasWebcamSupport ( ) : <EOL> if cv is None and win32vidcap is None : <EOL> return False <EOL> if not os . path . exists ( getFFMPEGpath ( ) ) : <EOL> return False <EOL> return True <EOL> def getFFMPEGpath ( ) : <EOL> if platform . system ( ) == "<STR_LIT>" : <EOL> return os . path . normpath ( os . path . join ( os . path . split ( __file__ ) [ <NUM_LIT:0> ] , "<STR_LIT>" ) ) <EOL> elif os . path . exists ( '<STR_LIT>' ) : <EOL> return '<STR_LIT>' <EOL> return os . path . normpath ( os . path . join ( os . path . split ( __file__ ) [ <NUM_LIT:0> ] , "<STR_LIT>" ) ) <EOL> class webcam ( object ) : <EOL> def __init__ ( self ) : <EOL> self . _cam = None <EOL> self . _cameraList = None <EOL> self . _activeId = - <NUM_LIT:1> <EOL> self . _overlayImage = wx . Bitmap ( getPathForImage ( '<STR_LIT>' ) ) <EOL> self . _overlayUltimaker = wx . Bitmap ( getPathForImage ( '<STR_LIT>' ) ) <EOL> self . _doTimelapse = False <EOL> self . _bitmap = None <EOL> cameraList = [ ] <EOL> tryNext = True <EOL> self . _camId = <NUM_LIT:0> <EOL> while tryNext : <EOL> tryNext = False <EOL> self . _openCam ( ) <EOL> if self . _cam is not None : <EOL> cameraList . append ( self . _cam . getdisplayname ( ) ) <EOL> tryNext = True <EOL> del self . _cam <EOL> self . _cam = None <EOL> self . _camId += <NUM_LIT:1> <EOL> self . _camId = <NUM_LIT:0> <EOL> self . _activeId = - <NUM_LIT:1> <EOL> self . _cameraList = cameraList <EOL> def hasCamera ( self ) : <EOL> return len ( self . _cameraList ) > <NUM_LIT:0> <EOL> def listCameras ( self ) : <EOL> return self . _cameraList <EOL> def setActiveCamera ( self , cameraIdx ) : <EOL> self . _camId = cameraIdx <EOL> def _openCam ( self ) : <EOL> if self . _cameraList is not None and self . _camId >= len ( self . _cameraList ) : <EOL> return False <EOL> if self . _cam is not None : <EOL> if self . _activeId != self . _camId : <EOL> del self . _cam <EOL> self . _cam = None <EOL> else : <EOL> return True <EOL> self . _activeId = self . _camId <EOL> if cv is not None : <EOL> self . _cam = highgui . cvCreateCameraCapture ( self . _camId ) <EOL> elif win32vidcap is not None : <EOL> try : <EOL> self . _cam = win32vidcap . new_Dev ( self . _camId , False ) <EOL> except : <EOL> pass <EOL> return self . _cam is not None <EOL> def propertyPages ( self ) : <EOL> if cv is not None : <EOL> return [ ] <EOL> elif win32vidcap is not None : <EOL> return [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> def openPropertyPage ( self , pageType = <NUM_LIT:0> ) : <EOL> if not self . _openCam ( ) : <EOL> return <EOL> if cv is not None : <EOL> pass <EOL> elif win32vidcap is not None : <EOL> if pageType == <NUM_LIT:0> : <EOL> self . _cam . displaycapturefilterproperties ( ) <EOL> else : <EOL> del self . _cam <EOL> self . _cam = None <EOL> tmp = win32vidcap . new_Dev ( <NUM_LIT:0> , False ) <EOL> tmp . displaycapturepinproperties ( ) <EOL> self . _cam = tmp <EOL> def takeNewImage ( self , withOverlay = True ) : <EOL> if not self . _openCam ( ) : <EOL> return <EOL> if cv is not None : <EOL> frame = cv . QueryFrame ( self . _cam ) <EOL> cv . CvtColor ( frame , frame , cv . CV_BGR2RGB ) <EOL> bitmap = wx . BitmapFromBuffer ( frame . width , frame . height , frame . imageData ) <EOL> elif win32vidcap is not None : <EOL> buffer , width , height = self . _cam . getbuffer ( ) <EOL> try : <EOL> wxImage = wx . EmptyImage ( width , height ) <EOL> wxImage . SetData ( buffer [ : : - <NUM_LIT:1> ] ) <EOL> wxImage = wxImage . Mirror ( ) <EOL> if self . _bitmap is not None : <EOL> del self . _bitmap <EOL> bitmap = wxImage . ConvertToBitmap ( ) <EOL> del wxImage <EOL> del buffer <EOL> except : <EOL> pass <EOL> if withOverlay : <EOL> dc = wx . MemoryDC ( ) <EOL> dc . SelectObject ( bitmap ) <EOL> dc . DrawBitmap ( self . _overlayImage , bitmap . GetWidth ( ) - self . _overlayImage . GetWidth ( ) - <NUM_LIT:5> , <NUM_LIT:5> , True ) <EOL> if profile . getMachineSetting ( '<STR_LIT>' ) . startswith ( '<STR_LIT>' ) : <EOL> dc . DrawBitmap ( self . _overlayUltimaker , ( bitmap . GetWidth ( ) - self . _overlayUltimaker . GetWidth ( ) ) / <NUM_LIT:2> , <EOL> bitmap . GetHeight ( ) - self . _overlayUltimaker . GetHeight ( ) - <NUM_LIT:5> , True ) <EOL> dc . SelectObject ( wx . NullBitmap ) <EOL> self . _bitmap = bitmap <EOL> if self . _doTimelapse : <EOL> filename = os . path . normpath ( os . path . join ( os . path . split ( __file__ ) [ <NUM_LIT:0> ] , "<STR_LIT>" , <EOL> "<STR_LIT>" % ( self . _snapshotCount ) ) ) <EOL> self . _snapshotCount += <NUM_LIT:1> <EOL> bitmap . SaveFile ( filename , wx . BITMAP_TYPE_JPEG ) <EOL> return self . _bitmap <EOL> def getLastImage ( self ) : <EOL> return self . _bitmap <EOL> def startTimelapse ( self , filename ) : <EOL> if not self . _openCam ( ) : <EOL> return <EOL> self . _cleanTempDir ( ) <EOL> self . _timelapseFilename = filename <EOL> self . _snapshotCount = <NUM_LIT:0> <EOL> self . _doTimelapse = True <EOL> print "<STR_LIT>" <EOL> def endTimelapse ( self ) : <EOL> if self . _doTimelapse : <EOL> ffmpeg = getFFMPEGpath ( ) <EOL> basePath = os . path . normpath ( <EOL> os . path . join ( os . path . split ( __file__ ) [ <NUM_LIT:0> ] , "<STR_LIT>" , "<STR_LIT>" ) ) <EOL> subprocess . call ( <EOL> [ ffmpeg , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , basePath , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , self . _timelapseFilename ] ) <EOL> self . _doTimelapse = False <EOL> def _cleanTempDir ( self ) : <EOL> basePath = os . path . normpath ( os . path . join ( os . path . split ( __file__ ) [ <NUM_LIT:0> ] , "<STR_LIT>" ) ) <EOL> try : <EOL> os . makedirs ( basePath ) <EOL> except : <EOL> pass <EOL> for filename in glob . iglob ( basePath + "<STR_LIT>" ) : <EOL> os . remove ( filename ) </s>
