def initAcquisition(self): # At the beginning, the thread creates the acquisition device and extracts # specific information. This allow to create the mamba images and # sequences. err, w, h = core.MBRT_GetAcqSize() if err != core.MBRT_NO_ERR: self.error = core.MBRT_StrErr(err) self.mustStop = True return (0, 0) err, freq = core.MBRT_GetAcqFrameRate() if err != core.MBRT_NO_ERR: self.error = core.MBRT_StrErr(err) self.mustStop = True return (0, 0) self.frequency = freq self.period = 1.0 / self.frequency core.MBRT_StartAcq() self.red = mamba.imageMb(w, h) self.green = mamba.imageMb(w, h) self.blue = mamba.imageMb(w, h) self.redSeq = mamba3D.sequenceMb(w, h, self.seqDepth) self.greenSeq = mamba3D.sequenceMb(w, h, self.seqDepth) self.blueSeq = mamba3D.sequenceMb(w, h, self.seqDepth) self.seqIndex = 0 return self.red.getSize()
def displayInfo(self): # Display some information on the screen # Displays an icon signaling an error if self.error and self.curIcon == None: self.curIcon = _errorIcon err = core.MBRT_IconDisplay(64, 64, self.curIcon) if err != core.MBRT_NO_ERR: self.error = core.MBRT_StrErr(err) self.mustStop = True if not self.error and self.curIcon != None: self.curIcon = None err = core.MBRT_IconDisplay(0, 0, []) if err != core.MBRT_NO_ERR: self.error = core.MBRT_StrErr(err) self.mustStop = True
def initDisplay(self, w, h): # Creates the display for width w and height h if not self.mustStop: err = core.MBRT_CreateDisplay(w, h) if err != core.MBRT_NO_ERR: self.error = core.MBRT_StrErr(err) self.mustStop = True return self.displayCreated = True
def recordResult(self): # Records the image if self.colorOn: err = core.MBRT_RecordColorImage(self.red.mbIm, self.green.mbIm, self.blue.mbIm) else: err = core.MBRT_RecordImage(self.red.mbIm) if err != core.MBRT_NO_ERR: self.error = core.MBRT_StrErr(err) # recording is stopped core.MBRT_RecordEnd() self.rec0n = False
def displayResult(self): # Updates the display with the computed image if self.colorOn: err, fps = core.MBRT_UpdateDisplayColor(self.red.mbIm, self.green.mbIm, self.blue.mbIm, self.frequency) else: err, fps = core.MBRT_UpdateDisplay(self.red.mbIm, self.frequency) if err != core.MBRT_NO_ERR: self.error = core.MBRT_StrErr(err) self.mustStop = True
def acquireImageFromDevice(self): # Obtains the images from the acquisition device # the red channel is used when working greyscale only if self.colorOn: err = core.MBRT_GetColorImageFromAcq( self.redSeq[self.seqIndex].mbIm, self.greenSeq[self.seqIndex].mbIm, self.blueSeq[self.seqIndex].mbIm) else: err = core.MBRT_GetImageFromAcq(self.redSeq[self.seqIndex].mbIm) if err != core.MBRT_NO_ERR: self.error = core.MBRT_StrErr(err) self.mustStop = True
def initialize(device, devType, seqlength=10): """ Initializes the realtime module using 'device' for the acquisition. 'devType' indicates the type of the device (either V4L2(linux), DSHOW(windows) or AVC). This function must be called before any other. 'seqlength' controls the length of the image sequence the thread is filling with the acquisition image. This sequence can be used in computation as an input. """ global _display_thread # Verification over the display thread, it must be None # so the init function could create a new one if _display_thread: if _display_thread.isAlive(): raise MambaRealtimeError( "Realtime is already initialized and running") else: # In the case _display_thread is only created but not running # then it is deleted _display_thread = None # The context is destroyed by precaution core.MBRT_DestroyContext() # The context and video acquisition inits are called in the main thread # due to directshow API restrictions (COM) err = core.MBRT_CreateContext() if err != core.MBRT_NO_ERR: raise MambaRealtimeError(core.MBRT_StrErr(err)) err = core.MBRT_CreateVideoAcq(device, devType) if err != core.MBRT_NO_ERR: raise MambaRealtimeError(core.MBRT_StrErr(err)) _display_thread = _MBRT_Thread(_com_queue, seqlength)
def getSize(): """ Returns a tuple containing the size of the images acquired and displayed by the realtime module. If the realtime module is not properly initialized the returned size will be negative. """ global _display_thread if _display_thread and _display_thread.isAlive(): err, w, h = core.MBRT_GetAcqSize() if err != core.MBRT_NO_ERR: raise MambaRealtimeError(core.MBRT_StrErr(err)) else: w = h = -1 return (w, h)
def handleScreenEvents(self): # Handles the events occuring within the display (close, key, mouse ...) err, event_code = core.MBRT_PollDisplay() if err != core.MBRT_NO_ERR: self.error = core.MBRT_StrErr(err) self.mustStop = True elif event_code == core.EVENT_CLOSE: self.mustStop = True elif event_code == core.EVENT_PROCESS: self.procOn = not self.procOn elif event_code == core.EVENT_PAUSE: self.pauseOn = not self.pauseOn elif event_code == core.EVENT_PALETTE: self.setPaletteDisplay() elif event_code == core.EVENT_COLOR: if not self.pauseOn: self.colorOn = not self.colorOn self.redSeq.reset() self.greenSeq.reset() self.blueSeq.reset() self.seqIndex = 0
def setPaletteDisplay(self): # Changes the display color palette try: names = [""] + mambaDisplay.listPalettes() try: i = names.index(self.palname) except: i = 0 i = (i + 1) % len(names) self.palname = names[i] if self.palname: pal = mambaDisplay.getPalette(self.palname) else: pal = () for i in range(256): pal += (i, i, i) err = core.MBRT_PaletteDisplay(list(pal)) if err != core.MBRT_NO_ERR: self.error = core.MBRT_StrErr(err) except ValueError as exc: self.error = str(exc)
def handleQueueCommands(self): # gets the command from the communication queue and changes the # parameters of the realtime. try: item = self.q.get_nowait() if item.type == _PROC_SET: # Sets the process (replace the previous ones) if item.options[0] == INSTANT or item.options[0] == SEQUENTIAL: self.procList = [[ item.value, item.options[1], item.options[2] ]] self.procOn = True self.procType = item.options[0] elif item.type == _PROC_RST: # Resets the process (no process is applied) self.procList = [] self.procOn = False self.procType = None elif item.type == _PROC_ADD: # Adds a process to the process list # Process must be INSTANT if self.procList == []: item.options[0] = INSTANT if item.options[0] == INSTANT: self.procList.append( [item.value, item.options[1], item.options[2]]) self.procOn = True elif item.type == _REC_START: # recording is started if not self.recOn: err = core.MBRT_RecordStart(item.value) if err != core.MBRT_NO_ERR: self.error = core.MBRT_StrErr(err) else: self.recOn = True elif item.type == _REC_STOP: # recording is stopped core.MBRT_RecordEnd() self.recOn = False elif item.type == _FREQ: # Changes the framerate try: self.frequency = float(item.value) except ValueError: self.error = "Frequency must be a float number" pass self.frequency = min(self.frequency, 50.0) self.frequency = max(self.frequency, 1.0) self.period = 1.0 / self.frequency elif item.type == _ORDER: # Other type of command if item.value == "close": self.mustStop = True elif item.value == "pause": self.pauseOn = not self.pauseOn elif item.value == "color": if not self.pauseOn: self.colorOn = not self.colorOn self.redSeq.reset() self.greenSeq.reset() self.blueSeq.reset() self.seqIndex = 0 elif item.type == _PICTURE: # Takes a snapshot try: if not self.colorOn: self.red.save(str(item.value)) else: mamba.mix(self.red, self.green, self.blue).save(str(item.value)) except ValueError: self.error = "The picture path is invalid" pass elif item.type == _ERR_DEL: # Erases the error stored self.error = "" else: # Error self.error = "Unknown command sent to the display thread" self.mustStop = True self.q.task_done() except queue.Empty: pass