示例#1
0
class CalibrationDataCapturePage(CalibrationPage):
    """This code is used for live capture
    
     This code  captures data and calibrates, saving files into the profile path. Profile path = /path/to/.Voxel/profiles. """
    STATE_START = 0
    STATE_NEAR_CAPTURED = 1
    STATE_FAR_CAPTURED = 2
    STATE_COMMON_PHASE_CAPTURED = 3
    STATE_PER_PIXEL_CAPTURED = 4
    STRATEGY = 'FlatWall'
    BUTTON_TEXTS = {
        'lens': [
            "Capture Far View", "Capture Near View",
            "Capture Common Phase OFfset Data", "Capture Per Pixel Data",
            "Calibrate"
        ],
        'commonPhase': [
            "Capture Phase Data",
            "Capture Phase Data for Second Modulation Frequency",
            "Capture Per Pixel Offset Data", "Calibrate"
        ],
        'perPixel': ["Capture Pixel Data", "Calibrate"]
    }

    MESSAGE_TEXTS = {
        'lens': [
            "Point the depth camera flat to a wall. Make sure you only see the flat wall in the camera view below and nothing else. This will be for far view capture. Later you'll need to capture near view. Now, for far view click on the capture button.",
            "Maintain the depth camera orientation to the flat wall but move it closer. Make sure you only see the flat wall in the camera view below and nothing else. Now, click on the capture button to the right.",
            "Capture Data for Common Phase and Per Pixel Offset Calculation. ",
            "Capture Data for Per Pixel Offset Calculation",
            "Great! You've got all views captured. Now, click on the calibrate button to compute the calibration coefficents."
        ],
        'commonPhase': [
            "Point the depth camera flat to a wall. Make sure you only see the flat wall in the camera view below and nothing else.",
            "dealias_en is turned on. Please click on capture to record data with the second modulation frequency.",
            "Capture Data for Per Pixel Offset Calculation",
            "All views captured. Click on calibrate to calibrate"
        ],
        'perPixel': [
            'Point the camera to a flat wall and capture data for calculating per pixel offsets.',
            "Great! you have all the data captured. CLick on calibrate. "
        ]
    }

    def __init__(self, calibrationWizard):
        super(CalibrationDataCapturePage, self).__init__()
        self.calibrationWizard = calibrationWizard
        self.setTitle("Capture Data and Calibrate Using Depth Camera")
        self.setSubTitle(
            "Please Follow the instructions to calibrate the camera")
        self.layout = QtGui.QVBoxLayout(self)
        self.captureGroupBox = QtGui.QGroupBox("Data Capture")
        self.layout.addWidget(self.captureGroupBox)
        self.dataView = None
        self.progressBar = None
        self.progressText = None
        self.hlayout = None
        self.dataEngine = None
        self.depthCameraController = None
        self.lensCaptured = False
        self.farCaptured = False
        self.nearCaptured = False
        self.commonPhaseCaptured = False
        self.perPixelCaptured = False
        self.nearFileName = None
        self.farFileName = None
        self.commonPhaseFileName = None
        self.perPixelFileName = None
        self.complete = False
        self.state = CalibrationDataCapturePage.STATE_START
        #layouts for groupboxes
        captureLayout = QtGui.QVBoxLayout()
        self.captureGroupBox.setLayout(captureLayout)
        hlayout = QtGui.QHBoxLayout()
        hlayout.setAlignment(QtCore.Qt.AlignTop)
        self.message = QtGui.QLabel()
        self.message.setSizePolicy(QtGui.QSizePolicy.Expanding,
                                   QtGui.QSizePolicy.Preferred)
        self.message.setWordWrap(True)
        self.message.setAlignment(QtCore.Qt.AlignTop)
        hlayout.addWidget(self.message)
        captureLayout.addLayout(hlayout)
        self.captureButton = QtGui.QPushButton()
        captureLayout.addWidget(self.captureButton)
        self.captureButton.clicked.connect(self.captureDataAndCalibrate)
        self.paramsGroupBox = None
        self.paramsText = ''

    def initializePage(self, *args, **kwargs):
        self.depthCamera = self.calibrationWizard.depthCamera
        self.calibrations = []
        for calibration in self.calibrationWizard.calibs:
            if self.calibrationWizard.calibs[
                    calibration] == CalibrationDataCapturePage.STRATEGY:
                self.calibrations.append(calibration)
        if 'lens' in self.calibrations:
            self.calibrationToDo = 'lens'
            self.buttonTexts = CalibrationDataCapturePage.BUTTON_TEXTS['lens']
            self.messageTexts = CalibrationDataCapturePage.MESSAGE_TEXTS[
                'lens']
        elif 'commonPhase' in self.calibrations:
            self.calibrationToDo = 'commonPhase'
            self.buttonTexts = CalibrationDataCapturePage.BUTTON_TEXTS[
                'commonPhase']
            self.messageTexts = CalibrationDataCapturePage.MESSAGE_TEXTS[
                'commonPhase']
        elif 'perPixel' in self.calibrations:
            self.calibrationToDo = 'perPixel'
            self.buttonTexts = CalibrationDataCapturePage.BUTTON_TEXTS[
                'perPixel']
            self.messageTexts = CalibrationDataCapturePage.MESSAGE_TEXTS[
                'perPixel']
        self.captureButton.setText(self.buttonTexts[0])
        self.message.setText(self.messageTexts[0])

        if not self.hlayout:
            self.hlayout = QtGui.QHBoxLayout()
            self.numberOfFrames = 100
            self.hlayout.addWidget(
                QtGui.QLabel('Number of frames to capture: '))
            self.frameCount = QtGui.QSpinBox()
            self.frameCount.setRange(50, 1000)
            self.frameCount.setValue(self.numberOfFrames)
            self.frameCount.valueChanged.connect(self.setNumberOfFrames)
            self.hlayout.addWidget(self.frameCount)
            self.hlayout.addStretch()

            self.hlayout.addWidget(QtGui.QLabel(' Distance from flat wall: '))

            self.distance = QtGui.QDoubleSpinBox()
            self.distance.setRange(0.1, 5000)
            self.distance.setSingleStep(0.1)
            self.distance.setValue(25.0)
            self.distanceValue = self.distance.value()
            self.distance.setDecimals(1)
            self.distance.valueChanged.connect(self.setDistance)
            self.hlayout.addWidget(self.distance)
            self.hlayout.addWidget(QtGui.QLabel('cm'))
            self.layout.addLayout(self.hlayout)
        if not self.dataEngine:
            self.depthCameraController = DepthCameraStreamController(
                self.calibrationWizard.cameraSystem,
                self.calibrationWizard.depthCamera)
            self.dataEngine = DataEngine(self.depthCameraController)
            self.dataEngine.disableStatistics()

        self.captureRunning = False
        self.dataEngine.connectData("phase", self.captureData,
                                    QtCore.Qt.QueuedConnection)
        if not self.dataView:
            self.dataView = DataViewContainer(self.dataEngine,
                                              'phase',
                                              shouldLinkViewBox=False,
                                              showFormatMenu=True)
            self.layout.addWidget(self.dataView)

        if not self.progressBar:
            self.progressBar = QtGui.QProgressBar()
            self.currentProgressValue = 0
            self.progressBar.setValue(0)

            self.layout.addWidget(self.progressBar)
        if not self.progressText:
            self.progressText = QtGui.QLabel()
            self.layout.addWidget(self.progressText)

        if not self.paramsGroupBox:
            self.paramsGroupBox = QtGui.QGroupBox("Calibration Parameters")
        vlayout = QtGui.QVBoxLayout()
        self.paramsGroupBox.setLayout(vlayout)
        self.paramsLabel = QtGui.QLabel()
        vlayout.addWidget(self.paramsLabel)
        self.paramsGroupBox.hide()
        self.layout.addWidget(self.paramsGroupBox)

        self.depthCameraController.start()

    def captureData(self, value):
        if not self.captureRunning:
            return

        self.profilePath = self.calibrationWizard.profilePath\
             + os.sep + self.calibrationWizard.depthCamera.name() + os.sep + self.calibrationWizard.depthCamera.id().split(":")[-1].split(')')[0]
        if not os.path.exists(self.profilePath):
            os.makedirs(self.profilePath)

        fileNames = {
            'lens': ["far.vxl", "near.vxl", "commonPhase.vxl", "perPixel.vxl"],
            'commonPhase': ["commonPhase.vxl", "commonPhase2.vxl"],
            'perPixel': ["perPixel.vxl"]
        }
        files = fileNames[self.calibrationToDo]
        self.files = [self.profilePath + os.sep + f for f in files]
        if self.currentFrameCount > self.numberOfFrames:
            self.depthCamera.closeFrameStream()
            self.stopCapture()
        else:
            if self.depthCamera.isSavingFrameStream():
                self.currentFrameCount += 1
                self.progressBar.setValue(self.currentFrameCount * 100 /
                                          self.numberOfFrames)
            else:
                ret, self.modFreq1 = self.depthCamera.getf("mod_freq1")
                ret, self.modFreq2 = self.depthCamera.getf("mod_freq2")
                self.fileName = self.files[value]
                if not self.depthCamera.saveFrameStream(self.files[value]):
                    QtGui.QMessageBox.critical(
                        self, "Can't save stream",
                        "Cannot save the current stream")

    def startCapture(self):
        self.captureButton.setDisabled(True)
        self.frameCount.setDisabled(True)
        self.average = None
        self.captureRunning = True
        self.currentFrameCount = 0

    def captureDataAndCalibrate(self):
        r, dealiasEnabled = self.depthCamera.getb("dealias_en")
        if not r:
            QtGui.QMessageBox.critical(self, "Can't get parameter",
                                       "Cannot get the dealias_en parameter")
        if dealiasEnabled:
            r = self.depthCamera.setb('ind_freq_data_en', True)
            r1 = self.depthCamera.setb('ind_freq_data_sel', False)
        if self.calibrationToDo == 'lens':
            if self.farCaptured == False:
                self.startCapture()
                self.captureData(0)
                self.farFileName = self.fileName
                self.farDistance = self.distanceValue
                self.farCaptured = True
                self.message.setText(self.messageTexts[1])
                self.captureButton.setText(self.buttonTexts[1])
            elif self.nearCaptured == False:
                self.startCapture()
                self.captureData(1)
                self.nearFileName = self.fileName
                self.nearDistance = self.distanceValue
                if 'commonPhase' in self.calibrations:
                    self.commonPhaseDistance = self.distanceValue
                    self.commonPhaseFileName = self.fileName
                    self.commonPhaseFileName1 = None
                    if not dealiasEnabled:
                        self.commonPhaseCaptured = True
                        self.nearCaptured = True
                if 'perPixel' in self.calibrations:
                    self.perPixelFileName = self.nearFileName
                    self.perPixelCaptured = True
                    self.message.setText(self.messageTexts[4])
                    self.captureButton.setText(self.buttonTexts[4])
                if 'commonPhase' in self.calibrations and dealiasEnabled is True and self.commonPhaseCaptured == False:
                    self.message.setText(self.messageTexts[2])
                    self.captureButton.setText(self.buttonTexts[2])
                    self.nearCaptured = True
                else:
                    self.message.setText(self.messageTexts[4])
                    self.captureButton.setText(self.buttonTexts[4])

            elif self.commonPhaseCaptured is False and 'commonPhase' in self.calibrations:
                r1 = self.depthCamera.setb('ind_freq_data_sel', True)
                self.startCapture()
                self.captureData(2)
                self.commonPhaseFileName1 = self.fileName
                self.commonPhaseCaptured = True
                self.message.setText(self.messageTexts[4])
                self.captureButton.setText(self.buttonTexts[4])
            elif self.perPixelCaptured is False and 'perPixel' in self.calibrations:
                self.startCapture()
                self.captureData(3)
                self.perPixelFileName = self.fileName
                self.message.setText(self.messageTexts[4])
                self.captureButton.setText(self.buttonTexts[4])
            else:
                self.calibrate()

        elif self.calibrationToDo == "commonPhase":
            if self.commonPhaseCaptured == False:
                self.startCapture()
                if not self.commonPhaseFileName:
                    self.captureData(0)
                    self.commonPhaseFileName = self.fileName
                    self.commonPhaseFileName1 = None
                    if not dealiasEnabled:
                        self.commonPhaseCaptured = True
                        self.message.setText(self.messageTexts[3])
                        self.captureButton.setText(self.buttonTexts[3])
                    else:
                        self.message.setText(self.messageTexts[1])
                        self.captureButton.setText(self.buttonTexts[1])
                        r1 = self.depthCamera.setb('ind_freq_data_sel', True)
                elif dealiasEnabled:
                    self.captureData(1)
                    self.commonPhaseFileName1 = self.fileName
                    self.message.setText(self.messageTexts[3])
                    self.captureButton.setText(self.buttonTexts[3])
                    self.commonPhaseCaptured = True
                self.commonPhaseDistance = self.distanceValue

                if 'perPixel' in self.calibrations:
                    self.perPixelFileName = self.fileName
                    self.perPixelCaptured = True

            elif self.perPixelCaptured is False and 'perPixel' in self.calibrations:
                self.startCapture()
                self.captureData(1)
                self.perPixelFileName = self.fileName
                self.perPixelCaptured = True
                self.message.setText(self.messageTexts[2])
                self.captureButton.setText(self.buttonTexts[2])
            else:
                self.message.setText(self.messageTexts[3])
                self.captureButton.setText(self.buttonTexts[3])
                self.calibrate()

        elif self.calibrationToDo == 'perPixel':

            if self.perPixelCaptured == False:
                self.startCapture()
                self.captureData(0)
                self.perPixelCaptured = True
                self.perPixelFileName = self.fileName
                self.message.setText(self.messageTexts[1])
                self.captureButton.setText(self.buttonTexts[1])
            else:
                self.calibrate()

    def calibrate(self):
        if self.nearCaptured and self.farCaptured:
            try:
                ret, mtx, dist = flatWallLensCalibration(
                    self.nearFileName, self.nearDistance / 100,
                    self.farFileName, self.farDistance / 100)
            except Exception, e:
                ret = False
            if ret:
                self.paramsText += "cx = %f\n cy = %f/n fx = fy = %f/n k1 = %f\n k2 = %f/n k3 = %f\n"\
                                    %(mtx[0,2], mtx[1,2], mtx[0,0], dist[0], dist[1], dist[2])
                self.message.setText(
                    "Calibration Done. Click on next to proceed")
                self.calibrationWizard.calibParams['cx'] = mtx[0, 2]
                self.calibrationWizard.calibParams['cy'] = mtx[1, 2]
                self.calibrationWizard.calibParams['fx'] = mtx[0, 0]
                self.calibrationWizard.calibParams['fy'] = mtx[0, 0]
                self.calibrationWizard.calibParams['k1'] = dist[0]
                self.calibrationWizard.calibParams['k2'] = dist[1]
                self.calibrationWizard.calibParams['k3'] = dist[2]
                self.calibrationWizard.calibParams['p1'] = 0
                self.calibrationWizard.calibParams['p1'] = 0
                self.completed = True
                self.completeChanged.emit()
            else:
                QtGui.QMessageBox.critical(self, "Check Data",
                                           "Can't get the coefficients")

        if self.commonPhaseCaptured:
            try:
                if 'cx' in self.calibrationWizard.calibParams:                    \
                                        cx = self.calibrationWizard.calibParams['cx']
                else:
                    cx = 0
                if 'cy' in self.calibrationWizard.calibParams:
                    cy = self.calibrationWizard.calibParams['cy']
                else:
                    cy = 0
                ret, phaseCorr1, phaseCorr2, _, _ = commonPhaseOffset(
                    self.commonPhaseFileName,
                    self.commonPhaseDistance / 100,
                    self.modFreq1,
                    cx,
                    cy,
                    self.commonPhaseFileName1,
                    self.modFreq2,
                    chipset=self.depthCamera.chipset())
            except Exception, e:
                print(e)
                ret = False
            if ret:
                self.paramsText += "phase_corr_1 = %d\n phase_corr_2 = %d\n" % (
                    phaseCorr1, phaseCorr2)
                self.calibrationWizard.calibParams['phase_corr1'] = phaseCorr1
                self.calibrationWizard.calibParams['phase_corr2'] = phaseCorr2
            else:
                QtGui.QMessageBox.critical(self, "Check Data",
                                           "Can't get the coefficients")
示例#2
0
    def initializePage(self, *args, **kwargs):
        self.depthCamera = self.calibrationWizard.depthCamera
        self.calibrations = []
        for calibration in self.calibrationWizard.calibs:
            if self.calibrationWizard.calibs[
                    calibration] == CalibrationDataCapturePage.STRATEGY:
                self.calibrations.append(calibration)
        if 'lens' in self.calibrations:
            self.calibrationToDo = 'lens'
            self.buttonTexts = CalibrationDataCapturePage.BUTTON_TEXTS['lens']
            self.messageTexts = CalibrationDataCapturePage.MESSAGE_TEXTS[
                'lens']
        elif 'commonPhase' in self.calibrations:
            self.calibrationToDo = 'commonPhase'
            self.buttonTexts = CalibrationDataCapturePage.BUTTON_TEXTS[
                'commonPhase']
            self.messageTexts = CalibrationDataCapturePage.MESSAGE_TEXTS[
                'commonPhase']
        elif 'perPixel' in self.calibrations:
            self.calibrationToDo = 'perPixel'
            self.buttonTexts = CalibrationDataCapturePage.BUTTON_TEXTS[
                'perPixel']
            self.messageTexts = CalibrationDataCapturePage.MESSAGE_TEXTS[
                'perPixel']
        self.captureButton.setText(self.buttonTexts[0])
        self.message.setText(self.messageTexts[0])

        if not self.hlayout:
            self.hlayout = QtGui.QHBoxLayout()
            self.numberOfFrames = 100
            self.hlayout.addWidget(
                QtGui.QLabel('Number of frames to capture: '))
            self.frameCount = QtGui.QSpinBox()
            self.frameCount.setRange(50, 1000)
            self.frameCount.setValue(self.numberOfFrames)
            self.frameCount.valueChanged.connect(self.setNumberOfFrames)
            self.hlayout.addWidget(self.frameCount)
            self.hlayout.addStretch()

            self.hlayout.addWidget(QtGui.QLabel(' Distance from flat wall: '))

            self.distance = QtGui.QDoubleSpinBox()
            self.distance.setRange(0.1, 5000)
            self.distance.setSingleStep(0.1)
            self.distance.setValue(25.0)
            self.distanceValue = self.distance.value()
            self.distance.setDecimals(1)
            self.distance.valueChanged.connect(self.setDistance)
            self.hlayout.addWidget(self.distance)
            self.hlayout.addWidget(QtGui.QLabel('cm'))
            self.layout.addLayout(self.hlayout)
        if not self.dataEngine:
            self.depthCameraController = DepthCameraStreamController(
                self.calibrationWizard.cameraSystem,
                self.calibrationWizard.depthCamera)
            self.dataEngine = DataEngine(self.depthCameraController)
            self.dataEngine.disableStatistics()

        self.captureRunning = False
        self.dataEngine.connectData("phase", self.captureData,
                                    QtCore.Qt.QueuedConnection)
        if not self.dataView:
            self.dataView = DataViewContainer(self.dataEngine,
                                              'phase',
                                              shouldLinkViewBox=False,
                                              showFormatMenu=True)
            self.layout.addWidget(self.dataView)

        if not self.progressBar:
            self.progressBar = QtGui.QProgressBar()
            self.currentProgressValue = 0
            self.progressBar.setValue(0)

            self.layout.addWidget(self.progressBar)
        if not self.progressText:
            self.progressText = QtGui.QLabel()
            self.layout.addWidget(self.progressText)

        if not self.paramsGroupBox:
            self.paramsGroupBox = QtGui.QGroupBox("Calibration Parameters")
        vlayout = QtGui.QVBoxLayout()
        self.paramsGroupBox.setLayout(vlayout)
        self.paramsLabel = QtGui.QLabel()
        vlayout.addWidget(self.paramsLabel)
        self.paramsGroupBox.hide()
        self.layout.addWidget(self.paramsGroupBox)

        self.depthCameraController.start()
    def __init__(self,
                 cameraSystem,
                 depthCamera,
                 captureFileName,
                 captureType,
                 numberOfFrames,
                 parent=None,
                 flags=0):
        super(CalibrationDataCaptureDialog, self).__init__(parent, flags)

        self.captureTypes = {
            'phase': ['Phase', self.captureData],
            'amplitude': ['Amplitude', self.captureAmplitude]
        }

        self.setMinimumHeight(500)
        self.setMinimumWidth(600)

        self.depthCamera = depthCamera

        self.depthCameraController = DepthCameraStreamController(
            cameraSystem, depthCamera)
        self.dataEngine = DataEngine(self.depthCameraController)
        self.dataEngine.disableStatistics()

        self.captureType = captureType

        if not self.captureTypes.has_key(
                self.captureType) or not self.depthCamera:
            return

        self.captureFileName = captureFileName
        self.numberOfFrames = numberOfFrames

        layout = QtGui.QVBoxLayout()

        self.setWindowTitle('Data Capture')

        self.dataView = DataViewContainer(self.dataEngine,
                                          self.captureType,
                                          shouldLinkViewBox=False,
                                          showFormatMenu=True)

        layout.addWidget(self.dataView)

        hlayout = QtGui.QHBoxLayout()
        hlayout.addWidget(QtGui.QLabel('Filename: '))
        nameEdit = QtGui.QLineEdit()
        hlayout.addWidget(nameEdit)
        nameEdit.setText(self.captureFileName)
        nameEdit.setDisabled(True)

        layout.addLayout(hlayout)

        self.progressBar = QtGui.QProgressBar()
        self.progressBar.setValue(0)

        layout.addWidget(self.progressBar)

        hlayout = QtGui.QHBoxLayout()
        hlayout.addWidget(QtGui.QLabel('Number of frames to capture: '))
        self.frameCount = QtGui.QSpinBox()
        self.frameCount.setRange(50, 500)
        self.frameCount.setValue(self.numberOfFrames)
        self.frameCount.valueChanged.connect(self.setNumberOfFrames)
        hlayout.addWidget(self.frameCount)

        self.captureButton = QtGui.QPushButton('Capture')
        self.captureButton.pressed.connect(self.startCapture)
        hlayout.addWidget(self.captureButton)

        self.cancelButton = QtGui.QPushButton('Cancel')
        self.cancelButton.pressed.connect(self.reject)
        hlayout.addWidget(self.cancelButton)

        layout.addLayout(hlayout)

        self.setLayout(layout)

        self.depthCameraController.start()
        self.captureFrames = False
        self.currentFrameCount = 0
        self.average = None
        self.data = None

        self.dataEngine.connectData(self.captureType,
                                    self.captureTypes[self.captureType][1],
                                    QtCore.Qt.QueuedConnection)
class CalibrationDataCaptureDialog(QtGui.QDialog):
    """ Shows the dialog for capturing data used in the wizard

    This code is used to show and capture data. It can save VXL files or png files. 
    
    Args:
        cameraSystem: The camera system used. Generally, Voxel.CameraSystem() for TI cameras
        depthCamera: The depth camera connected to the device
        captureFileName: The name of the file where the captured data is stored
        numberOfFrames: Frames to capture
        
    Returns:
        Data (png or vxl or npy) file containing information about the captured data


  """
    def __init__(self,
                 cameraSystem,
                 depthCamera,
                 captureFileName,
                 captureType,
                 numberOfFrames,
                 parent=None,
                 flags=0):
        super(CalibrationDataCaptureDialog, self).__init__(parent, flags)

        self.captureTypes = {
            'phase': ['Phase', self.captureData],
            'amplitude': ['Amplitude', self.captureAmplitude]
        }

        self.setMinimumHeight(500)
        self.setMinimumWidth(600)

        self.depthCamera = depthCamera

        self.depthCameraController = DepthCameraStreamController(
            cameraSystem, depthCamera)
        self.dataEngine = DataEngine(self.depthCameraController)
        self.dataEngine.disableStatistics()

        self.captureType = captureType

        if not self.captureTypes.has_key(
                self.captureType) or not self.depthCamera:
            return

        self.captureFileName = captureFileName
        self.numberOfFrames = numberOfFrames

        layout = QtGui.QVBoxLayout()

        self.setWindowTitle('Data Capture')

        self.dataView = DataViewContainer(self.dataEngine,
                                          self.captureType,
                                          shouldLinkViewBox=False,
                                          showFormatMenu=True)

        layout.addWidget(self.dataView)

        hlayout = QtGui.QHBoxLayout()
        hlayout.addWidget(QtGui.QLabel('Filename: '))
        nameEdit = QtGui.QLineEdit()
        hlayout.addWidget(nameEdit)
        nameEdit.setText(self.captureFileName)
        nameEdit.setDisabled(True)

        layout.addLayout(hlayout)

        self.progressBar = QtGui.QProgressBar()
        self.progressBar.setValue(0)

        layout.addWidget(self.progressBar)

        hlayout = QtGui.QHBoxLayout()
        hlayout.addWidget(QtGui.QLabel('Number of frames to capture: '))
        self.frameCount = QtGui.QSpinBox()
        self.frameCount.setRange(50, 500)
        self.frameCount.setValue(self.numberOfFrames)
        self.frameCount.valueChanged.connect(self.setNumberOfFrames)
        hlayout.addWidget(self.frameCount)

        self.captureButton = QtGui.QPushButton('Capture')
        self.captureButton.pressed.connect(self.startCapture)
        hlayout.addWidget(self.captureButton)

        self.cancelButton = QtGui.QPushButton('Cancel')
        self.cancelButton.pressed.connect(self.reject)
        hlayout.addWidget(self.cancelButton)

        layout.addLayout(hlayout)

        self.setLayout(layout)

        self.depthCameraController.start()
        self.captureFrames = False
        self.currentFrameCount = 0
        self.average = None
        self.data = None

        self.dataEngine.connectData(self.captureType,
                                    self.captureTypes[self.captureType][1],
                                    QtCore.Qt.QueuedConnection)

    def setNumberOfFrames(self, value):
        self.numberOfFrames = value

    @QtCore.Slot(object, object, object)
    def capturePhase(self, id, timestamp, frame):
        if not self.captureFrames:
            return

        if self.average is None:
            self.average = np.zeros(self.dataEngine.data['phase'].shape,
                                    dtype='complex')

        phase = self.dataEngine.data['phase'] * np.pi / 2048
        self.average += self.dataEngine.data['amplitude'] * (
            np.cos(phase) + 1j * np.sin(phase))
        self.currentFrameCount += 1
        self.progressBar.setValue(self.currentFrameCount * 100 /
                                  self.numberOfFrames)

        if self.currentFrameCount == self.numberOfFrames:
            self.average /= self.currentFrameCount
            self.average = np.angle(self.average) * (4096 / (2 * math.pi))
            np.save(self.captureFileName, self.average.T)
            self.stopCapture()

    @QtCore.Slot(object, object, object)
    def captureAmplitude(self, id, timestamp, frame):
        if not self.captureFrames:
            return

        if self.average is None:
            self.average = np.array(frame, copy=True).astype(float)
        else:
            self.average += np.array(frame)

        self.currentFrameCount += 1
        self.progressBar.setValue(self.currentFrameCount * 100 /
                                  self.numberOfFrames)

        if self.currentFrameCount == self.numberOfFrames:
            self.average /= self.currentFrameCount
            self.average.clip(0, 255, out=self.average)

            cv2.imwrite(self.captureFileName,
                        self.average.transpose().astype(np.uint8))

            self.stopCapture()

    def captureData(self, id, timestamp, frame):
        if not self.captureFrames:
            return
        if self.depthCamera.isSavingFrameStream():
            self.currentFrameCount += 1
            self.progressBar.setValue(self.currentFrameCount * 100 /
                                      self.numberOfFrames)
        if self.currentFrameCount > self.numberOfFrames:
            self.stopCapture()
        else:
            if not self.depthCamera.saveFrameStream(self.captureFileName):
                QtGui.QMessageBox.critical(self, "Can't save stream",
                                           "Cannot save the current stream")

    def startCapture(self):
        self.captureButton.setDisabled(True)
        self.frameCount.setDisabled(True)
        self.average = None
        self.captureFrames = True

    def stopCapture(self):
        if self.average is not None:
            self.data = self.average
        self.average = None
        if self.depthCameraController.isRunning():
            self.depthCameraController.stop()
            self.accept()

        self.dataEngine.stop()

    # static method to create the dialog and return (date, time, accepted)
    @staticmethod
    def showDialog(cameraSystem,
                   depthCamera,
                   captureFileName,
                   captureType,
                   numberOfFrames,
                   parent=None):
        dialog = CalibrationDataCaptureDialog(cameraSystem, depthCamera,
                                              captureFileName, captureType,
                                              numberOfFrames, parent)
        result = dialog.exec_()

        dialog.stopCapture()

        if result == QtGui.QDialog.Accepted:
            return dialog.data
        else:
            return None
示例#5
0
class CalibrationDataCapturePage(CalibrationPage):
    #Capture Data Only for FlatWall Method: Captures Phase Data Only
    STATE_START = 0
    STATE_NEAR_CAPTURED = 1
    STATE_FAR_CAPTURED = 2

    DISTANCE_READONLY = [False, True, True]
    DISTANCE_ENABLED = [True, False, False]
    BUTTON_TEXTS = ["Capture Near View", "Capture Far View", "Calibrate"]
    MESSAGE_TEXTS = {
        'lens': [
            "Step 1: Point the depth camera flat to a wall. Make sure you only see the flat wall in the camera view below and nothing else. This will be for near view capture (usually about 25cm). Later you'll need to capture far view. Now, for near view click on the capture button to the right.",
            "Step 2: Maintain the depth camera orientation to the flat wall but move it farther away by about 35cm (usually to about 60cm). Make sure you only see the flat wall in the camera view below and nothing else. Now, click on the capture button to the right.",
            "Step 3: Great! You've got both near and far views captured. Now, click on the calibrate button to the right to compute the calibration coefficents."
        ],
        'commonPhase':
        "Point the depth camera flat to a wall. Make sure you only see the flat wall in the camera view below and nothing else.",
        'perPixel':
        'Point the camera to a vxl and capture data for calculating per pixel offsets.'
    }

    def __init__(self, calibrationWizard):
        super(CalibrationDataCapturePage, self).__init__()
        self.calibrationWizard = calibrationWizard
        self.setMinimumHeight(200)
        self.setSizePolicy(QtGui.QSizePolicy.Preferred,
                           QtGui.QSizePolicy.Expanding)
        self.setTitle("Capture Data For Calibration")
        self.setSubTitle("Follow the instructions to save data")
        self.layout = QtGui.QVBoxLayout(self)
        self.nearPhase = None
        self.farPhase = None
        self.currentFrameCount = 0
        self.phaseAverage = None
        self.phaseData = None
        self.captureFrames = False

    def setState(self, state):
        self.state = state
        self.calibrateButton.setText(
            CalibrationDataCapturePage.BUTTON_TEXTS[state])
        self.message.setText(
            CalibrationDataCapturePage.MESSAGE_TEXTS['lens'][state])
        if self.depthCamera.chipset(
        ) == self.calibrationWizard.CHIPSET_CALCULUS:
            self.distance.setReadOnly(
                CalibrationDataCapturePage.DISTANCE_READONLY[state])
            self.distance.setEnabled(
                CalibrationDataCapturePage.DISTANCE_ENABLED[state])

    def setCenterPoint(self, value=0):
        self.center = [self.cx.value(), self.cy.value()]

    def setNumberOfFrames(self, value):
        self.numberOfFrames = value

    def getMousePosition(self, pos):
        p = self.dataView.dataView.imageItem.mapFromScene(pos)

        if self.frameSize is not None and self.phaseData is not None:
            if p.x() < 0 or p.x() >= self.frameSize[1] or p.y() < 0 or p.y(
            ) >= self.frameSize[0]:
                return

            x = int(p.x())
            y = int(p.y())

            self.progressText.setText('Current point (%d, %d). Phase = %d' %
                                      (x, y, self.phaseData[x, y]))

    def reset(self):
        self.setState(CalibrationDataCapturePage.STATE_START)
        #         self.calibrationPage.resetCalibrationParameters()
        if self.depthCamera.chipset(
        ) == self.calibrationWizard.CHIPSET_CALCULUS:
            self.distance.setValue(25.0)

    def calibrate(self):
        if self.state == CalibrationDataCapturePage.STATE_START:
            if not self.setExtraPhaseOffsetInCamera():
                QtGui.QMessageBox.critical(self, 'Extra Phase Offset',
                                           'Failed to set extra phase offset')
                return

        if self.state == CalibrationDataCapturePage.STATE_START or self.state == CalibrationDataCapturePage.STATE_NEAR_CAPTURED:  # Capture first and second view
            self.captureRunning = True
            self.currentFrameCount = 0
        elif self.state == CalibrationDataCapturePage.STATE_FAR_CAPTURED:

            if self.nearPhase is None or self.farPhase is None:
                QtGui.QMessageBox.critical(
                    self, 'Invalid state',
                    'Cannot calibrate with near and far views captured.')
                return

    def captureData(self, id, timestamp, frame):
        if not self.captureFrames:
            return
        if self.depthCamera.isSavingFrameStream():
            self.currentFrameCount += 1
            self.progressBar.setValue(self.currentFrameCount * 100 /
                                      self.numberOfFrames)
        if self.currentFrameCount > self.numberOfFrames:
            self.stopCapture()
            self.currentFrameCount = 0
        else:
            if not self.depthCamera.saveFrameStream(self.captureFileName):
                QtGui.QMessageBox.critical(self, "Can't save stream",
                                           "Cannot save the current stream")

    def initializePage(self, *args, **kwargs):
        hlayout = QtGui.QHBoxLayout()
        hlayout.setAlignment(QtCore.Qt.AlignTop)
        self.message = QtGui.QLabel()
        self.message.setSizePolicy(QtGui.QSizePolicy.Expanding,
                                   QtGui.QSizePolicy.Preferred)
        self.message.setWordWrap(True)
        self.message.setAlignment(QtCore.Qt.AlignTop)
        hlayout.addWidget(self.message)
        self.layout.addLayout(hlayout)
        self.depthCamera = self.calibrationWizard.depthCamera
        self.lensPath = self.calibrationWizard.profilePath + os.sep + self.depthCamera.name(
        ) + os.sep + self.depthCamera.id().split(':')[-1].split(')')[0]
        if not self.lensPath:
            os.makedirs(self.lensPath)
        hlayout = QtGui.QHBoxLayout()
        self.resetButton = QtGui.QPushButton('&Reset')
        self.resetButton.setShortcut('Alt+R')
        self.resetButton.pressed.connect(self.reset)
        self.calibrateButton = QtGui.QPushButton('&Calibrate')
        self.calibrateButton.pressed.connect(self.calibrate)
        self.calibrateButton.setShortcut('Alt+C')
        vlayout = QtGui.QVBoxLayout()
        vlayout.setAlignment(QtCore.Qt.AlignRight)
        vlayout.addWidget(self.calibrateButton)
        vlayout.addWidget(self.resetButton)
        hlayout.addLayout(vlayout)

        self.layout.addLayout(hlayout)

        r, size = self.calibrationWizard.depthCamera.getMaximumFrameSize()

        if not r:
            QtGui.QMessageBox.warning(self, 'Maximum Frame Size',
                                      'Could not get maximum frame size')
            width = 1920
            height = 1080
        else:
            width = size.width
            height = size.height

        hlayout = QtGui.QHBoxLayout()

        hlayout.addWidget(QtGui.QLabel('Center pixel: cx = '))
        self.cx = QtGui.QSpinBox()
        self.cx.setRange(0, width - 1)
        self.cx.setValue(width / 2)
        hlayout.addWidget(self.cx)
        self.cx.valueChanged.connect(self.setCenterPoint)
        hlayout.addWidget(QtGui.QLabel(', cy = '))
        self.cy = QtGui.QSpinBox()
        self.cy.setRange(0, height - 1)
        self.cy.setValue(height / 2)
        self.cy.valueChanged.connect(self.setCenterPoint)
        hlayout.addWidget(self.cy)
        hlayout.addStretch()

        self.setCenterPoint()

        self.extraPhaseOffset = 0
        hlayout.addWidget(QtGui.QLabel('Introduce Phase Offset: '))
        self.phaseOffsetBox = QtGui.QSpinBox()
        self.phaseOffsetBox.setRange(-4095, 4095)
        self.phaseOffsetBox.setValue(self.extraPhaseOffset)
        self.phaseOffsetBox.valueChanged.connect(self.setExtraPhaseOffset)
        hlayout.addWidget(self.phaseOffsetBox)

        self.layout.addLayout(hlayout)

        hlayout = QtGui.QHBoxLayout()
        self.numberOfFrames = 1000
        hlayout.addWidget(QtGui.QLabel('Number of frames to capture: '))
        self.frameCount = QtGui.QSpinBox()
        self.frameCount.setRange(50, 1000)
        self.frameCount.setValue(self.numberOfFrames)
        self.frameCount.valueChanged.connect(self.setNumberOfFrames)
        hlayout.addWidget(self.frameCount)
        hlayout.addStretch()

        r, self.dealiasingEnabled = self.depthCamera.getb('dealias_en')
        r1, self.commonPhaseAdditive = self.depthCamera.getb('phase_corr_add')

        if self.depthCamera.chipset() == self.calibrationWizard.CHIPSET_CALCULUS or \
            r and not self.dealiasingEnabled:
            self.updateCommonPhase = True
            hlayout.addWidget(
                QtGui.QLabel('Camera Distance (near view) from flat wall: '))

            self.distance = QtGui.QDoubleSpinBox()
            self.distance.setRange(0.1, 5000)
            self.distance.setSingleStep(0.1)
            self.distance.setValue(25.0)
            self.distance.setDecimals(1)
            hlayout.addWidget(self.distance)
            hlayout.addWidget(QtGui.QLabel('cm'))
        else:
            self.updateCommonPhase = False
        self.layout.addLayout(hlayout)

        self.depthCameraController = DepthCameraStreamController(
            self.calibrationWizard.cameraSystem,
            self.calibrationWizard.depthCamera)
        self.dataEngine = DataEngine(self.depthCameraController)
        self.dataEngine.disableStatistics()

        self.captureRunning = False
        self.dataEngine.connectData("phase", self.captureData,
                                    QtCore.Qt.QueuedConnection)

        self.dataView = DataViewContainer(self.dataEngine,
                                          'phase',
                                          shouldLinkViewBox=False,
                                          showFormatMenu=True)

        self.layout.addWidget(self.dataView)

        self.progressBar = QtGui.QProgressBar()
        self.currentProgressValue = 0
        self.progressBar.setValue(0)

        self.layout.addWidget(self.progressBar)

        self.progressText = QtGui.QLabel()
        self.layout.addWidget(self.progressText)

        self.dataView.dataView.graphicsWidget.scene().sigMouseMoved.connect(
            self.getMousePosition)

        self.setState(CalibrationDataCapturePage.STATE_START)
        self.depthCameraController.start()

    def setExtraPhaseOffset(self, value):
        self.extraPhaseOffset = value

    def setExtraPhaseOffsetInCamera(self):
        return self.depthCamera.setb('disable_offset_corr', False) and \
            self.depthCamera.seti('phase_corr_1', self.extraPhaseOffset)
示例#6
0
    def initializePage(self, *args, **kwargs):
        hlayout = QtGui.QHBoxLayout()
        hlayout.setAlignment(QtCore.Qt.AlignTop)
        self.message = QtGui.QLabel()
        self.message.setSizePolicy(QtGui.QSizePolicy.Expanding,
                                   QtGui.QSizePolicy.Preferred)
        self.message.setWordWrap(True)
        self.message.setAlignment(QtCore.Qt.AlignTop)
        hlayout.addWidget(self.message)
        self.layout.addLayout(hlayout)
        self.depthCamera = self.calibrationWizard.depthCamera
        self.lensPath = self.calibrationWizard.profilePath + os.sep + self.depthCamera.name(
        ) + os.sep + self.depthCamera.id().split(':')[-1].split(')')[0]
        if not self.lensPath:
            os.makedirs(self.lensPath)
        hlayout = QtGui.QHBoxLayout()
        self.resetButton = QtGui.QPushButton('&Reset')
        self.resetButton.setShortcut('Alt+R')
        self.resetButton.pressed.connect(self.reset)
        self.calibrateButton = QtGui.QPushButton('&Calibrate')
        self.calibrateButton.pressed.connect(self.calibrate)
        self.calibrateButton.setShortcut('Alt+C')
        vlayout = QtGui.QVBoxLayout()
        vlayout.setAlignment(QtCore.Qt.AlignRight)
        vlayout.addWidget(self.calibrateButton)
        vlayout.addWidget(self.resetButton)
        hlayout.addLayout(vlayout)

        self.layout.addLayout(hlayout)

        r, size = self.calibrationWizard.depthCamera.getMaximumFrameSize()

        if not r:
            QtGui.QMessageBox.warning(self, 'Maximum Frame Size',
                                      'Could not get maximum frame size')
            width = 1920
            height = 1080
        else:
            width = size.width
            height = size.height

        hlayout = QtGui.QHBoxLayout()

        hlayout.addWidget(QtGui.QLabel('Center pixel: cx = '))
        self.cx = QtGui.QSpinBox()
        self.cx.setRange(0, width - 1)
        self.cx.setValue(width / 2)
        hlayout.addWidget(self.cx)
        self.cx.valueChanged.connect(self.setCenterPoint)
        hlayout.addWidget(QtGui.QLabel(', cy = '))
        self.cy = QtGui.QSpinBox()
        self.cy.setRange(0, height - 1)
        self.cy.setValue(height / 2)
        self.cy.valueChanged.connect(self.setCenterPoint)
        hlayout.addWidget(self.cy)
        hlayout.addStretch()

        self.setCenterPoint()

        self.extraPhaseOffset = 0
        hlayout.addWidget(QtGui.QLabel('Introduce Phase Offset: '))
        self.phaseOffsetBox = QtGui.QSpinBox()
        self.phaseOffsetBox.setRange(-4095, 4095)
        self.phaseOffsetBox.setValue(self.extraPhaseOffset)
        self.phaseOffsetBox.valueChanged.connect(self.setExtraPhaseOffset)
        hlayout.addWidget(self.phaseOffsetBox)

        self.layout.addLayout(hlayout)

        hlayout = QtGui.QHBoxLayout()
        self.numberOfFrames = 1000
        hlayout.addWidget(QtGui.QLabel('Number of frames to capture: '))
        self.frameCount = QtGui.QSpinBox()
        self.frameCount.setRange(50, 1000)
        self.frameCount.setValue(self.numberOfFrames)
        self.frameCount.valueChanged.connect(self.setNumberOfFrames)
        hlayout.addWidget(self.frameCount)
        hlayout.addStretch()

        r, self.dealiasingEnabled = self.depthCamera.getb('dealias_en')
        r1, self.commonPhaseAdditive = self.depthCamera.getb('phase_corr_add')

        if self.depthCamera.chipset() == self.calibrationWizard.CHIPSET_CALCULUS or \
            r and not self.dealiasingEnabled:
            self.updateCommonPhase = True
            hlayout.addWidget(
                QtGui.QLabel('Camera Distance (near view) from flat wall: '))

            self.distance = QtGui.QDoubleSpinBox()
            self.distance.setRange(0.1, 5000)
            self.distance.setSingleStep(0.1)
            self.distance.setValue(25.0)
            self.distance.setDecimals(1)
            hlayout.addWidget(self.distance)
            hlayout.addWidget(QtGui.QLabel('cm'))
        else:
            self.updateCommonPhase = False
        self.layout.addLayout(hlayout)

        self.depthCameraController = DepthCameraStreamController(
            self.calibrationWizard.cameraSystem,
            self.calibrationWizard.depthCamera)
        self.dataEngine = DataEngine(self.depthCameraController)
        self.dataEngine.disableStatistics()

        self.captureRunning = False
        self.dataEngine.connectData("phase", self.captureData,
                                    QtCore.Qt.QueuedConnection)

        self.dataView = DataViewContainer(self.dataEngine,
                                          'phase',
                                          shouldLinkViewBox=False,
                                          showFormatMenu=True)

        self.layout.addWidget(self.dataView)

        self.progressBar = QtGui.QProgressBar()
        self.currentProgressValue = 0
        self.progressBar.setValue(0)

        self.layout.addWidget(self.progressBar)

        self.progressText = QtGui.QLabel()
        self.layout.addWidget(self.progressText)

        self.dataView.dataView.graphicsWidget.scene().sigMouseMoved.connect(
            self.getMousePosition)

        self.setState(CalibrationDataCapturePage.STATE_START)
        self.depthCameraController.start()