def test_throws_when_type_conversion_fails(self):
     with self.assertRaises(mpf_util.ModelTypeConversionError) as err:
         (mpf_util.ModelsIniParser(
             self._plugin_models_dir).register_int_field(
                 'string_field').build_class()('test model',
                                               self._common_models_dir))
     self.assertEqual(mpf.DetectionError.COULD_NOT_READ_DATAFILE,
                      err.exception.error_code)
    def test_can_handle_missing_fields_when_optional(self):
        model_settings = (mpf_util.ModelsIniParser(
            self._plugin_models_dir
        ).register_optional_field('string_field').register_optional_int_field(
            'int_field',
            100).register_optional_path_field('path_field').build_class()(
                'missing fields model', self._common_models_dir))

        self.assertEqual('hello world', model_settings.string_field)
        self.assertEqual(100, model_settings.int_field)
        self.assertEqual(None, model_settings.path_field)
    def setUp(self):
        self._plugin_dir = tempfile.mkdtemp()
        self._plugin_models_dir = os.path.join(self._plugin_dir, 'models')
        os.mkdir(self._plugin_models_dir)
        with open(os.path.join(self._plugin_models_dir, 'models.ini'),
                  'w') as f:
            f.write(MODELS_INI)

        self._ModelSettings = (mpf_util.ModelsIniParser(
            self._plugin_models_dir).register_field(
                'string_field').register_int_field('int_field').
                               register_path_field('path_field').build_class())

        self._root_common_models_dir = tempfile.mkdtemp()
        self._common_models_dir = os.path.join(self._root_common_models_dir,
                                               'test_component')
    def test_uses_values_from_ini_file_when_field_is_optional_and_in_ini_file(
            self):
        test_file_path = os.path.join(self._plugin_models_dir, 'test_file.txt')
        with open(os.path.join(test_file_path), 'w') as f:
            f.write('test')

        model_settings = (mpf_util.ModelsIniParser(
            self._plugin_models_dir
        ).register_optional_field('string_field').register_optional_int_field(
            'int_field',
            100).register_optional_path_field('path_field').build_class()(
                'test model', self._common_models_dir))

        self.assertEqual('hello world', model_settings.string_field)
        self.assertEqual(567, model_settings.int_field)
        self.assertEqual(test_file_path, model_settings.path_field)
    def test_validates_default_value_for_optional_path(self):
        ModelSettings = (mpf_util.ModelsIniParser(
            self._plugin_models_dir).register_optional_field(
                'string_field', 'asdf').register_optional_int_field(
                    'int_field', 100).register_optional_path_field(
                        'path_field', 'test_file.txt').build_class())

        with self.assertRaises(mpf_util.ModelFileNotFoundError):
            ModelSettings('missing fields model', self._common_models_dir)

        test_file_path = os.path.join(self._plugin_models_dir, 'test_file.txt')
        with open(os.path.join(test_file_path), 'w') as f:
            f.write('test')

        model_settings = ModelSettings('missing fields model',
                                       self._common_models_dir)
        self.assertEqual('hello world', model_settings.string_field)
        self.assertEqual(100, model_settings.int_field)
        self.assertEqual(test_file_path, model_settings.path_field)
 def test_throws_when_type_conversion_fails(self):
     with self.assertRaises(mpf_util.ModelTypeConversionError):
         (mpf_util.ModelsIniParser(
             self._plugin_models_dir).register_int_field(
                 'string_field').build_class()('test model',
                                               self._common_models_dir))
示例#7
0
        last_frame_read = 0
        for idx, frame in enumerate(video_capture):
            last_frame_read = idx
            last_il = mpf.ImageLocation(
                0, 0, min(width - 1, last_il.width + expand_rate),
                min(height - 1, last_il.height + expand_rate))
            detections[idx] = last_il

        if not detections:
            return ()
        return [mpf.VideoTrack(0, last_frame_read, frame_locations=detections)]


ModelSettings = (mpf_util.ModelsIniParser(
    pkg_resources.resource_filename(
        __name__,
        'models')).register_path_field('network').register_path_field(
            'names').register_int_field('num_classes').build_class())


def get_model(job):
    model_name = job.job_properties.get('MODEL_NAME', 'animal model')
    models_dir_path = os.path.join(
        job.job_properties.get('MODELS_DIR_PATH', '.'), 'PythonOcvComponent')
    model_settings = ModelSettings(model_name, models_dir_path)
    logger.info(
        '[%s] Successfully retrieved settings file for the "%s" model: '
        '{ network = "%s", names = "%s", num_classes = %s }', job.job_name,
        model_name, model_settings.network, model_settings.names,
        model_settings.num_classes)
    return load_model(model_settings)