def from_file(cls, filepath): """ Return a new ConfigBlock object based on the given configuration file :raises MaptkBaseException: Error occurred in reading from configuration file path given. :param filepath: Path to a configuration file :type filepath: str :return: New ConfigBlock instance :rtype: ConfigBlock """ cb_read = cls.MAPTK_LIB.maptk_config_block_file_read cb_read.argtypes = [ctypes.c_char_p, MaptkErrorHandle.C_TYPE_PTR] cb_read.restype = cls.C_TYPE_PTR with MaptkErrorHandle() as eh: eh.set_exception_map({ -1: MaptkConfigBlockIoException, 1: MaptkConfigBlockIoFileNotFoundException, 2: MaptkConfigBlockIoFileNotReadException, 3: MaptkConfigBlockIoFileNotParsed }) return cls.from_c_pointer(cb_read(filepath, eh))
def get_value_bool(self, key, default=None): """ Get the boolean value for a key :raises MaptkConfigBlockNoSuchValueException: the given key doesn't exist in the configuration and no default was provided. :param key: The index of the configuration value to retrieve. :type key: str :param default: Optional default value that will be returned if the given is not found in this configuration. :type default: bool :return: The boolean value stored within the configuration :rtype: bool """ cb_get_bool_argtypes = [self.C_TYPE_PTR, ctypes.c_char_p] cb_get_bool_args = [self, key] if default is None: cb_get_bool = self.MAPTK_LIB['maptk_config_block_' 'get_value_bool'] else: cb_get_bool = self.MAPTK_LIB['maptk_config_block_' 'get_value_default_bool'] cb_get_bool_argtypes.append(ctypes.c_bool) cb_get_bool_args.append(default) cb_get_bool_argtypes.append(MaptkErrorHandle.C_TYPE_PTR) cb_get_bool.argtypes = cb_get_bool_argtypes cb_get_bool.restype = ctypes.c_bool with MaptkErrorHandle() as eh: eh.set_exception_map({-1: MaptkConfigBlockNoSuchValueException}) cb_get_bool_args.append(eh) return cb_get_bool(*cb_get_bool_args)
def get_config(self, cb=None): """ Return this algorithm's current configuration. As algorithms are named, the configuration returned will be contained inside a sub-block whose name matches this algorithm's name in order to separate :param cb: An optional existing ConfigBlock instance to add this algorithm's configuration to. :type cb: ConfigBlock :return: This named algorithm's current configuration. If a config block was given to this method, the same config block is returned. :rtype: maptk.ConfigBlock """ algo_gnac = \ self.MAPTK_LIB['maptk_algorithm_get_nested_algo_configuration'] algo_gnac.argtypes = [ ctypes.c_char_p, ctypes.c_char_p, ConfigBlock.C_TYPE_PTR, self.C_TYPE_PTR, MaptkErrorHandle.C_TYPE_PTR ] algo_gnac.restype = ConfigBlock.C_TYPE_PTR if cb is None: cb = ConfigBlock() with MaptkErrorHandle() as eh: algo_gnac(self.type_name(), self.name, cb, self, eh) return cb
def set_config(self, cb): """ Set this algorithm's configuration based on the given config block If there is no configuration for this algorithm in the given block, or if the recorded implementation type is invalid, this algorithm remains unchanged. NOTE: Setting an algorithm configuration with a valid implementation type, the underlying instance is reset (deleted and reconstructed). This is important to note for algorithms that are state-based as their states would implicitly reset due to instance reconstruction. :param cb: Configuration block to draw this algorithm's configuration from. :type cb: ConfigBlock """ algo_snac = \ self.MAPTK_LIB['maptk_algorithm_set_nested_algo_configuration'] algo_snac.argtypes = [ ctypes.c_char_p, ctypes.c_char_p, ConfigBlock.C_TYPE_PTR, ctypes.POINTER(self.C_TYPE_PTR), MaptkErrorHandle.C_TYPE_PTR ] with MaptkErrorHandle() as eh: algo_snac(self.type_name(), self.name, cb, ctypes.byref(self.c_pointer), eh)
def create(cls, algo_name, impl_name): """ Create a new algorithm instance of the derived type, initialized with the given implementation. :raises RuntimeError: The implementation name given does not match a registered implementation name. :param algo_name: Name to be assigned to this algorithm instance. :type algo_name: str :param impl_name: Name of the implementation. This should be one of the registered implementation names (check <type>.registered_names()). :type impl_name: str :return: New instance of derived type initialized to the given implementation. :rtype: cls """ algo_create = cls.MAPTK_LIB['maptk_algorithm_create'] algo_create.argtypes = [ ctypes.c_char_p, ctypes.c_char_p, MaptkErrorHandle.C_TYPE_PTR ] algo_create.restype = cls.C_TYPE_PTR with MaptkErrorHandle() as eh: inst_ptr = algo_create(cls.type_name(), impl_name, eh) if not bool(inst_ptr): raise MaptkNullPointerException( "Failed to construct algorithm instance") return cls.from_c_pointer(inst_ptr, name=algo_name)
def registered_names(cls): """ :return: list of string implementation names currently registered """ algo_reg_names = cls.MAPTK_LIB['maptk_algorithm_registered_names'] algo_reg_names.argtypes = [ ctypes.c_char_p, ctypes.POINTER(ctypes.c_uint), ctypes.POINTER(ctypes.POINTER(ctypes.c_char_p)), MaptkErrorHandle.C_TYPE_PTR ] sl_free = cls.MAPTK_LIB.maptk_common_free_string_list sl_free.argtypes = [ctypes.c_uint, ctypes.POINTER(ctypes.c_char_p)] length = ctypes.c_uint(0) names = ctypes.POINTER(ctypes.c_char_p)() with MaptkErrorHandle() as eh: algo_reg_names(cls.type_name(), ctypes.byref(length), ctypes.byref(names), eh) # Constructing return array r = [] for i in xrange(length.value): r.append(names[i]) # Free allocated key listing sl_free(length, names) return r
def _destroy(self): """ Delete instance through C API """ if self._inst_ptr: cam_del = self.MAPTK_LIB.maptk_camera_destroy cam_del.argtypes = [self.C_TYPE_PTR, MaptkErrorHandle.C_TYPE_PTR] with MaptkErrorHandle() as eh: cam_del(self, eh)
def size(self): """ :return: Number of elements in this mapping. :rtype: int """ cm_size = self.MAPTK_LIB.maptk_camera_map_size cm_size.argtypes = [self.C_TYPE_PTR, MaptkErrorHandle.C_TYPE_PTR] cm_size.restype = ctypes.c_size_t with MaptkErrorHandle() as eh: return cm_size(self, eh)
def __len__(self): """ :return: The number of states in this track :rtype: int """ t_size = self.MAPTK_LIB['maptk_track_size'] t_size.argtypes = [self.C_TYPE_PTR, MaptkErrorHandle.C_TYPE_PTR] t_size.restype = ctypes.c_size_t with MaptkErrorHandle() as eh: return t_size(self, eh)
def is_empty(self): """ :return: If this track has no track states or not :rtype: bool """ t_empty = self.MAPTK_LIB['maptk_track_empty'] t_empty.argtypes = [self.C_TYPE_PTR, MaptkErrorHandle.C_TYPE_PTR] t_empty.restype = ctypes.c_bool with MaptkErrorHandle() as eh: return t_empty(self, eh)
def from_krtd_file(cls, filepath): """ :return: New Camera instance from a KRTD format file :rtype: Camera """ cam_read_krtd = cls.MAPTK_LIB.maptk_camera_read_krtd_file cam_read_krtd.argtypes = [ctypes.c_char_p, MaptkErrorHandle.C_TYPE_PTR] cam_read_krtd.restype = cls.C_TYPE_PTR with MaptkErrorHandle() as eh: return cls.from_c_pointer(cam_read_krtd(filepath, eh))
def _destroy(self): """ Destroy internal instance """ if self._inst_ptr: algo_destroy = self.MAPTK_LIB["maptk_algorithm_destroy"] algo_destroy.argtypes = [ self.C_TYPE_PTR, MaptkErrorHandle.C_TYPE_PTR ] with MaptkErrorHandle() as eh: algo_destroy(self, eh)
def clone(self, new_name=None): """ :param new_name: An optional new instance name for the cloned algorithm :type new_name: str :return: A new copy of this algorithm :rtype: MaptkAlgorithm """ algo_clone = self.MAPTK_LIB['maptk_algorithm_clone'] algo_clone.argtypes = [self.C_TYPE_PTR, MaptkErrorHandle.C_TYPE_PTR] algo_clone.restype = self.C_TYPE_PTR with MaptkErrorHandle() as eh: return self.from_c_pointer(algo_clone(self, eh), name=(new_name or self.name))
def write_krtd_file(self, filepath): """ Write camera object in KRTD format to the specified file. :param filepath: Path to the file to write to. :type filepath: str """ cam_write_krtd = self.MAPTK_LIB.maptk_camera_write_krtd_file cam_write_krtd.argtypes = [ self.C_TYPE_PTR, ctypes.c_char_p, MaptkErrorHandle.C_TYPE_PTR ] with MaptkErrorHandle() as eh: cam_write_krtd(self, filepath, eh)
def save(self, image_container, filepath): """ Save an image to the specified path :param image_container: ImageContainer containing the image to save :type image_container: ImageContainer :param filepath: The path to save the image to :type filepath: str """ iio_save = self.MAPTK_LIB.maptk_algorithm_image_io_save iio_save.argtypes = [self.C_TYPE_PTR, ctypes.c_char_p, ImageContainer.C_TYPE_PTR, MaptkErrorHandle.C_TYPE_PTR] with MaptkErrorHandle() as eh: iio_save(self, filepath, image_container, eh)
def convert(self, image_container): """ :param image_container: The image container with image data to convert :type image_container: ImageContainer :return: A new ImageContainer with the converted underlying data :rtype: ImageContainer """ ci_convert = self.MAPTK_LIB['maptk_algorithm_convert_image_convert'] ci_convert.argtypes = [self.C_TYPE_PTR, ImageContainer.C_TYPE_PTR, MaptkErrorHandle.C_TYPE_PTR] ci_convert.restype = ImageContainer.C_TYPE_PTR with MaptkErrorHandle() as eh: return ImageContainer.from_c_pointer( ci_convert(self, image_container, eh) )
def load(self, filepath): """ Load an image into a ImageContainer :param filepath: Path to the image to load :type filepath: str :return: New ImageContainer containing the loaded image :rtype: ImageContainer """ iio_load = self.MAPTK_LIB.maptk_algorithm_image_io_load iio_load.argtypes = [self.C_TYPE_PTR, ctypes.c_char_p, MaptkErrorHandle.C_TYPE_PTR] iio_load.restype = ImageContainer.C_TYPE_PTR with MaptkErrorHandle() as eh: ic_ptr = iio_load(self, filepath, eh) return ImageContainer.from_c_pointer(ic_ptr)
def impl_name(self): """ :return: String name for the current implementation, or None if this algorithm is not initialized to an implementation :rtype: str or None """ if self._inst_ptr: algo_impl_name = self.MAPTK_LIB['maptk_algorithm_impl_name'] algo_impl_name.argtypes = [ self.C_TYPE_PTR, MaptkErrorHandle.C_TYPE_PTR ] algo_impl_name.restype = self.MST_TYPE_PTR with MaptkErrorHandle() as eh: s_ptr = algo_impl_name(self, eh) s = s_ptr.contents.str self.MST_FREE(s_ptr) return s else: return None
def description(self): """ :return: Optional descriptive string about an implementation. An empty string is returned if there is no available description. None is returned if this algorithm has not been initialized yet. :rtype: str or None """ if self._inst_ptr: algo_descr = self.MAPTK_LIB['maptk_algorithm_description'] algo_descr.argtypes = [ self.C_TYPE_PTR, MaptkErrorHandle.C_TYPE_PTR ] algo_descr.restype = self.MST_TYPE_PTR with MaptkErrorHandle() as eh: s_ptr = algo_descr(self, eh) s = s_ptr.contents.str self.MST_FREE(s_ptr) return s else: return None
def check_config(self, cb): """ Check the given configuration block for valid algorithm configuration :param cb: Configuration to check. :type cb: ConfigBlock :return: True if the given configuration block contains a valid configuration for this algorithm type and implementation. :rtype: bool """ algo_cnac = \ self.MAPTK_LIB['maptk_algorithm_check_nested_algo_configuration'] algo_cnac.argtypes = [ ctypes.c_char_p, ctypes.c_char_p, ConfigBlock.C_TYPE_PTR, MaptkErrorHandle.C_TYPE_PTR ] algo_cnac.restype = ctypes.c_bool with MaptkErrorHandle() as eh: return algo_cnac(self.type_name(), self.name, cb, eh)
def write(self, filepath): """ Output this configuration to the specified file path :raises MaptkBaseException: Error occurred in writing configuration to the given file path. :param filepath: Output file path. """ cb_write = self.MAPTK_LIB.maptk_config_block_file_write cb_write.argtypes = [ self.C_TYPE_PTR, ctypes.c_char_p, MaptkErrorHandle.C_TYPE_PTR ] with MaptkErrorHandle() as eh: eh.set_exception_map({ -1: MaptkConfigBlockIoException, 1: MaptkConfigBlockIoFileWriteException }) cb_write(self, filepath, eh)
def track(self, prev_tracks, frame_num, image, mask=None): """ Extend a previous set of tracks using the given image. An optional mask image may be provided, where positive valued regions indicate regions of the input image to consider for feature tracking. The mask image must be of the same dimensions as the input image, other wise an exception is raised. :param prev_tracks: :param frame_num: :param image: :param mask: :return: """ tf_track_argtypes = [ self.C_TYPE_PTR, TrackSet.C_TYPE_PTR, ctypes.c_uint, ImageContainer.C_TYPE_PTR ] tf_track_args = [self, prev_tracks, frame_num, image] tf_track_restype = TrackSet.C_TYPE_PTR if mask: tf_track = self.MAPTK_LIB['maptk_algorithm_track_features_' 'track_with_mask'] tf_track_argtypes.append(ImageContainer.C_TYPE_PTR) tf_track_args.append(mask) else: tf_track = self.MAPTK_LIB['maptk_algorithm_track_features_track'] tf_track_argtypes.append(MaptkErrorHandle.C_TYPE_PTR) tf_track.argtypes = tf_track_argtypes tf_track.restype = tf_track_restype with MaptkErrorHandle() as eh: tf_track_args.append(eh) return TrackSet.from_c_pointer(tf_track(*tf_track_args))
def _destroy(self): t_del = self.MAPTK_LIB['maptk_track_destroy'] t_del.argtypes = [self.C_TYPE_PTR, MaptkErrorHandle.C_TYPE_PTR] with MaptkErrorHandle() as eh: t_del(self, eh)
def _destroy(self): cm_del = self.MAPTK_LIB.maptk_camera_map_destroy cm_del.argtypes = [self.C_TYPE_PTR, MaptkErrorHandle.C_TYPE_PTR] with MaptkErrorHandle() as eh: cm_del(self, eh)
def _destroy(self): imgc_del = self.MAPTK_LIB.maptk_image_container_destroy imgc_del.argtypes = [self.C_TYPE_PTR, MaptkErrorHandle.C_TYPE_PTR] with MaptkErrorHandle() as eh: imgc_del(self, eh)