def _init(self, cam, size=(640, 480), fps=None, timebase=None): # setting up device self.device = cam.device self.deviceSettings = vi.DeviceSettings() self.deviceSettings.symbolicLink = self.device.symbolicName self.deviceSettings.indexStream = 0 self.deviceSettings.indexMediaType = 0 self.captureSettings = vi.CaptureSettings() self.captureSettings.readMode = vi.ReadMode.SYNC self.captureSettings.videoFormat = vi.CaptureVideoFormat.RGB32 self.stream = self.device.listStream[self.deviceSettings.indexStream] # collecting additional information if timebase == None: logger.debug("Capture will run with default system timebase") self.timebase = 0 else: logger.debug("Capture will run with app wide adjustable timebase") self.timebase = timebase self.width = size[0] self.height = size[1] self.preferred_fps = fps self._initFrameRates() self._initMediaTypeId() self.context = _getVideoInputInstance() res = self.context.setupDevice(self.deviceSettings, self.captureSettings) if res != vi.ResultCode.OK: raise CameraCaptureError("Could not setup device. Error code: %d" % (res)) # creating frame buffer and initializing capture settings frame = np.empty((self.actual_height * self.actual_width * 4), dtype=np.uint8) self.readSetting = vi.ReadSetting() self.readSetting.symbolicLink = self.deviceSettings.symbolicLink self.readSetting.setNumpyArray(frame) frame.shape = (self.actual_height, self.actual_width, -1) self._frame = frame logger.debug( "Successfully set up device: %s @ %dx%dpx %dfps (mediatype %d)" % (self.name, self.actual_height, self.actual_width, self.frame_rate, self.deviceSettings.indexMediaType))
def init_capture(self, uid, size=(640, 480), fps=None, timebase=None): self.uid = uid if uid is not None: # validate parameter UID devices = device_list( ) # TODO: read list only once (initially) to save runtime for device in devices: print if device['uid'] == uid: break if device['uid'] != uid: msg = ERR_INIT_FAIL + "UID of camera was not found." logger.error(msg) self.init_capture(None, size, fps, timebase) return # validate parameter SIZE if not len(size) == 2: msg = ERR_INIT_FAIL + "Parameter 'size' must have length 2." logger.error(msg) self.init_capture(None, size, fps, timebase) return # setting up device self.device = device self.deviceSettings = vi.DeviceSettings() self.deviceSettings.symbolicLink = self.device['uid'] self.deviceSettings.indexStream = 0 self.deviceSettings.indexMediaType = 0 self.captureSettings = vi.CaptureSettings() self.captureSettings.readMode = vi.ReadMode.SYNC self.captureSettings.videoFormat = vi.CaptureVideoFormat.RGB32 self.stream = self.device['handle'].listStream[ self.deviceSettings.indexStream] # set timebase if timebase == None: logger.debug("Capture will run with default system timebase") self.timebase = 0 else: logger.debug( "Capture will run with app wide adjustable timebase") self.timebase = timebase # set properties self.width = size[0] self.height = size[1] self.preferred_fps = fps self._initFrameRates() self._initMediaTypeId() # robust camera initialization self.context = _getVideoInputInstance() while True: res = self.context.setupDevice(self.deviceSettings, self.captureSettings) if res != vi.ResultCode.OK: self._failed_inits += 1 msg = ERR_INIT_FAIL + "Fall back to Fake Capture. Error code: %d" % ( res) if self._failed_inits < MAX_RETRY_INIT_CAMERA: logger.warning( "Retry initializing camera: {0}/{1}: ".format( self._failed_inits, MAX_RETRY_INIT_CAMERA) + msg) else: logger.error(msg) self._failed_inits = 0 self.init_capture(None, size, fps, timebase) return sleep(0.25) else: break # creating frame buffer and initializing capture settings frame = np.empty((self.actual_height * self.actual_width * 4), dtype=np.uint8) self.readSetting = vi.ReadSetting() self.readSetting.symbolicLink = self.deviceSettings.symbolicLink self.readSetting.setNumpyArray(frame) frame.shape = (self.actual_height, self.actual_width, -1) self._frame = frame logger.debug( "Successfully set up device: %s @ %dx%dpx %dfps (mediatype %d)" % (self.name, self.actual_height, self.actual_width, self.frame_rate, self.deviceSettings.indexMediaType)) else: self.device = Fake_Capture() self.width = size[0] self.height = size[1] self.preferred_fps = fps self._is_initialized = True self._failed_inits = 0
def _init(self, uid, size=(640,480), fps=None, timebase=None): devices = vi.DeviceList() for cam in _getVideoInputInstance().getListOfDevices(devices): if cam.symbolicName == uid: break if cam.symbolicName != uid: raise CameraCaptureError("uid for camera not found.") if not len(size) == 2: msg = ERR_INIT_FAIL + "Parameter 'size' must have length 2." logger.error(msg) raise CameraCaptureError(msg) # setting up device self.device = cam.device self.deviceSettings = vi.DeviceSettings() self.deviceSettings.symbolicLink = self.device.symbolicName self.deviceSettings.indexStream = 0 self.deviceSettings.indexMediaType = 0 self.captureSettings = vi.CaptureSettings() self.captureSettings.readMode = vi.ReadMode.SYNC self.captureSettings.videoFormat = vi.CaptureVideoFormat.RGB32 self.stream = self.device.listStream[self.deviceSettings.indexStream] # collecting additional information if timebase == None: logger.debug("Capture will run with default system timebase") self.timebase = 0 else: logger.debug("Capture will run with app wide adjustable timebase") self.timebase = timebase self.width = size[0] self.height = size[1] self.preferred_fps = fps self._initFrameRates() self._initMediaTypeId() self.context = _getVideoInputInstance() while True: res = self.context.setupDevice(self.deviceSettings, self.captureSettings) if res != vi.ResultCode.OK: self._failed_inits += 1 msg = ERR_INIT_FAIL + "Error code: %d" %(res) if self._failed_inits < MAX_RETRY_INIT_CAMERA: logger.error("Retry initializing camera: {0}/{1}: ".format(self._failed_inits, MAX_RETRY_INIT_CAMERA) + msg) else: logger.error(msg) raise CameraCaptureError(msg) sleep(1) else: break # creating frame buffer and initializing capture settings frame = np.empty((self.actual_height * self.actual_width * 4), dtype=np.uint8) self.readSetting = vi.ReadSetting() self.readSetting.symbolicLink = self.deviceSettings.symbolicLink self.readSetting.setNumpyArray(frame) frame.shape = (self.actual_height, self.actual_width, -1) self._frame = frame logger.debug("Successfully set up device: %s @ %dx%dpx %dfps (mediatype %d)" %(self.name, self.actual_height, self.actual_width, self.frame_rate, self.deviceSettings.indexMediaType)) self._is_initialized = True self._failed_inits = 0
print "LOADED VIDEOINPUT LIB" vin = vi.videoInput_getInstance() assert isinstance(vin, vi.videoInput) devices = vi.DeviceList() vin.getListOfDevices(devices) for d in devices: assert isinstance(d, vi.Device) print d.friendlyName, d.symbolicName dev = d dev = devices[1] assert isinstance(dev, vi.Device) print "LOADED CAPTURE DEVICES" deviceSettings = vi.DeviceSettings() deviceSettings.symbolicLink = dev.symbolicName deviceSettings.indexStream = 0 deviceSettings.indexMediaType = 195 captureSettings = vi.CaptureSettings() captureSettings.readMode = vi.ReadMode.SYNC captureSettings.videoFormat = vi.CaptureVideoFormat.RGB24 stream = dev.listStream[0] assert isinstance(stream, vi.Stream) mediaType = stream.listMediaType[195] for mt in stream.listMediaType: print mt.width, mt.height, mt.MF_MT_FRAME_RATE assert isinstance(mediaType, vi.MediaType) frame = np.zeros((mediaType.height, mediaType.width, 3), dtype=np.uint8)