Exemplo n.º 1
0
    def test_save_calibrations():
        """
        Test save_calibrations.

        Loading calibs was already tested above, so can rely on it.

        CSV FORMAT
            - delimiter: ', '
            - no floats, round and convert to int using known precision
            - strings in double quotes

            e.g. "varname", var[0], var[1], ...\\n
        """
        fn_cp = calibsdir + '/camera_params.csv'
        if not os_exists(fn_cp):
            raise RuntimeError('tests/camera_params.csv could not be found')
        # set not to tests/ (`load_calibrations` will do that)
        #   don't want to overwrite test data
        calib1 = CalibratePSEye()
        calib1.init_chessboard(p, o)
        try:
            # Load known calibs and then save
            cp = calib1.calibpath
            debug('%s' % calib1.calibpath)
            calib1.load_calibrations(fn_cp)
            calib1.calibpath = cp
            calib1.save_calibrations()
            if not os_exists(calib1.calibpath):
                raise RuntimeError('Failed to create calib path \'%s\''
                                   % calib1.calib_path)
            # Compare saving
            with open(fn_cp, 'r') as f:
                f1 = f.read()
            with open(cp+'/camera_params.csv', 'r') as f:
                f2 = f.read()
            if f1 != f2:
                raise RuntimeError('Camera parameter csvs did not match')

            # Compare loading
            calib2 = CalibratePSEye()
            calib2.load_calibrations(calib1.calibpath+'/camera_params.csv')
            paramlist = ('cameraMatrix', 'distCoeffs')
            for k in paramlist:
                k1 = getattr(calib1, k)
                k2 = getattr(calib2, k)
                if not array_equal(k1, k2):
                    raise RuntimeError(
                        'param \'%s\' does not match between calib1 and calib2' % k
                    )
                debug('\'%s\' matched' % k)
        finally:
            calib1.removepath()
Exemplo n.º 2
0
    def test_compute_calibrations():
        """
        Test computation to the 4th decimal place on known calibration data.
        """
        # Test Asserts
        calib = CalibratePSEye()
        calib.corners_arr = [1, 2]
        calib.objpoints = [1, 2]
        calib.w = 320
        calib.h = 240

        calib.corners_arr = []
        try:
            calib.compute_calibrations()
        except RuntimeError:
            pass
        else:
            raise RuntimeError('Failed to catch len(corners_arr)==0')

        calib.corners_arr = [1, 2]
        calib.objpoints = []
        try:
            calib.compute_calibrations()
        except RuntimeError:
            pass
        else:
            raise RuntimeError('Failed to catch len(objpoints)==0')

        calib.objpoints = [1, 2]
        calib.w = None
        try:
            calib.compute_calibrations()
        except RuntimeError:
            pass
        else:
            raise RuntimeError('Failed to catch _w is None')

        calib.h = None
        calib.w = 320
        try:
            calib.compute_calibrations()
        except RuntimeError:
            pass
        else:
            raise RuntimeError('Failed to catch h is None')

        # Test Math
        global cpdict
        imgpath = testdir + '/raw'
        if not os_exists(imgpath):
            raise RuntimeError('Could not find imgpath')
        calib = CalibratePSEye()
        calib.init_chessboard(p, o)
        calib.load_calib_imgs(imgpath)
        try:
            calib.compute_calibrations()
            calib.save_calibrations()
            for k in cpdict.keys():
                k1 = cpdict[k]  # already rounded b/c loaded from file
                k2 = around(getattr(calib, k), decimals=4)
                if not array_equal(k1, k2):
                    raise RuntimeError('\'%s\' did not match' % k)
                debug('\'%s\' matched' % k)
                # print(getattr(calib, k))
        finally:
            calib.removepath()