def init_pipeline_plugins():
    """
    装载管道插件
    """
    # 装载配置
    _execute_path = os.path.realpath(os.path.join(
        os.path.dirname(__file__), os.path.pardir, 'search_by_image'
    ))
    RunTool.set_global_var('EXECUTE_PATH', _execute_path)

    _config = os.path.join(_execute_path, 'conf/server_jade.xml')
    _config_xml = SimpleXml(_config, encoding='utf-8')
    _server_config = _config_xml.to_dict()['server']

    RunTool.set_global_var(
        'PIPELINE_PROCESSER_PARA', _server_config['pipeline']['processer_para']
    )
    RunTool.set_global_var('PIPELINE_ROUTER_PARA', _server_config['pipeline']['router_para'])
    _plugins_path_list = _server_config['pipeline']['plugins_path'].split(',')
    for _plugins_path in _plugins_path_list:
        Pipeline.load_plugins_by_path(
            os.path.join(_execute_path, _plugins_path.strip())
        )

    _logger: Logger = None
    if 'logger' in _server_config.keys():
        _logger_config = _server_config['logger']
        if len(_logger_config['conf_file_name']) > 0 and _logger_config['conf_file_name'][0] == '.':
            # 相对路径
            _logger_config['conf_file_name'] = os.path.join(
                _execute_path, _logger_config['conf_file_name']
            )
        if len(_logger_config['logfile_path']) > 0 and _logger_config['logfile_path'][0] == '.':
            # 相对路径
            _logger_config['logfile_path'] = os.path.join(
                _execute_path, _logger_config['logfile_path']
            )
        _logger = Logger.create_logger_by_dict(_logger_config)

    # 创建空管道用于测试
    _empty_pipeline = Pipeline('empty', '{}', logger=_logger)
    RunTool.set_global_var('EMPTY_PIPELINE', _empty_pipeline)

    # 创建测试管道
    _jade_pipeline = Pipeline(
        'jade_search',
        _server_config['pipeline']['pipeline_config']['JadeSearch'],
        logger=_logger
    )
    RunTool.set_global_var('JADE_PIPELINE', _jade_pipeline)
def test_JadeTypeDetect():
    """
    测试翡翠类型处理器
    """
    _execute_path = RunTool.get_global_var('EXECUTE_PATH')
    _pipeline = RunTool.get_global_var('EMPTY_PIPELINE')
    _processer_class: PipelineProcesser = Pipeline.get_plugin('processer', 'JadeTypeDetect')
    _filelist = FileTool.get_filelist(
        os.path.join(_execute_path, os.path.pardir, 'test_data/test_pic/'),
        is_fullname=True
    )
    for _file in _filelist:
        # 遍历执行
        with open(_file, 'rb') as _fid:
            _file_bytes = _fid.read()
            _input = {
                'type': '',
                'sub_type': '',
                'image': Image.open(_file_bytes),
                'score': 0.0
            }
            _output = _processer_class.execute(_input, {}, _pipeline)

            # 输出图片和对应文字
            _image = _output['image']
            _print_str = 'type: %s\nsub_type: %s\nscore: %s' % (
                _output['type'], _output['sub_type'], str(_output['score']))
            _draw = ImageDraw.Draw(_image)  # PIL图片上打印汉字
            # 参数1:字体文件路径,参数2:字体大小;Windows系统“simhei.ttf”默认存储在路径:C:\Windows\Fonts中
            _font = ImageFont.truetype("simhei.ttf", 20, encoding="utf-8")
            _draw.text((0, 0), _print_str, (255, 0, 0), font=_font)

            plt.figure(_file)
            plt.imshow(_image)
            plt.show()
    def __init__(self):
        """
        初始化测试参数
        """
        # 获取配置
        self.execute_path = os.path.realpath(
            os.path.join(os.path.dirname(__file__), os.path.pardir,
                         'search_by_image'))
        RunTool.set_global_var('EXECUTE_PATH', self.execute_path)

        _config = os.path.join(self.execute_path, 'conf/server.xml')
        _config_xml = SimpleXml(_config, encoding='utf-8')
        self.server_config = _config_xml.to_dict()['server']

        # 装载管道插件
        RunTool.set_global_var(
            'PIPELINE_PROCESSER_PARA',
            self.server_config['pipeline']['processer_para'])
        RunTool.set_global_var('PIPELINE_ROUTER_PARA',
                               self.server_config['pipeline']['router_para'])
        _plugins_path_list = self.server_config['pipeline'][
            'plugins_path'].split(',')
        for _plugins_path in _plugins_path_list:
            Pipeline.load_plugins_by_path(
                os.path.join(self.execute_path, _plugins_path.strip()))

        # 日志对象
        self.logger: Logger = None
        if 'logger' in self.server_config.keys():
            _logger_config = self.server_config['logger']
            if len(_logger_config['conf_file_name']
                   ) > 0 and _logger_config['conf_file_name'][0] == '.':
                # 相对路径
                _logger_config['conf_file_name'] = os.path.join(
                    self.execute_path, _logger_config['conf_file_name'])
            if len(_logger_config['logfile_path']
                   ) > 0 and _logger_config['logfile_path'][0] == '.':
                # 相对路径
                _logger_config['logfile_path'] = os.path.join(
                    self.execute_path, _logger_config['logfile_path'])
            self.logger = Logger.create_logger_by_dict(_logger_config)

        # 创建搜索引擎
        self.search_engine = SearchEngine(self.server_config,
                                          logger=self.logger)
Beispiel #4
0
    def _get_pipeline(self, pipeline: str) -> Pipeline:
        """
        获取可用的管道对象

        @param {str} pipeline - 处理管道标识

        @returns {Pipeline} - 返回管道对象
        """
        return Pipeline(pipeline,
                        self.pipeline_config[pipeline],
                        is_asyn=False,
                        logger=self.logger)
Beispiel #5
0
    def _get_image_vertor(self,
                          image_data: bytes,
                          pipeline_obj: Pipeline,
                          init_collection: str = ''):
        """
        获取影像的特征向量

        @param {bytes} image_data - 影像内容二进制数据
        @param {Pipeline} pipeline_obj - 可用管道对象
        @param {str} init_collection='' - 指定默认的分类

        @returns {str, numpy.ndarray} - 匹配到的影像分类, 特征向量
        """
        _input = {'image': image_data, 'collection': init_collection}

        _status, _output = pipeline_obj.start(_input, {})

        if _status != 'success':
            raise RuntimeError('Pipeline run error: status [%s]!' % _status)

        _collection = self.search_config['default_collection'] if _output[
            'collection'] == '' else _output['collection']

        return _collection, _output['vertor']
def test_HistogramVetor():
    """
    测试直方图特征变量生成
    """
    _execute_path = RunTool.get_global_var('EXECUTE_PATH')
    _pipeline = RunTool.get_global_var('EMPTY_PIPELINE')
    _processer_class: PipelineProcesser = Pipeline.get_plugin('processer', 'HistogramVetor')
    _filelist = FileTool.get_filelist(
        os.path.join(_execute_path, os.path.pardir, 'test_data/test_pic/'),
        is_fullname=True
    )
    for _file in _filelist:
        # 遍历执行
        with open(_file, 'rb') as _fid:
            _file_bytes = _fid.read()
            _input = {
                'type': '',
                'sub_type': '',
                'image': Image.open(_file_bytes),
                'score': 0.0
            }
            _output = _processer_class.execute(_input, {}, _pipeline)

            print(_output['vertor'])
Beispiel #7
0
    def __init__(self, server_config: dict, app: Flask = None, **kwargs):
        """
        以图搜图服务初始化

        @param {dict} server_config - 服务配置字典
        @param {Flask} app=None - 服务
        """
        self.kwargs = kwargs
        self.debug = server_config.get('debug', True)
        self.execute_path = server_config['execute_path']
        RunTool.set_global_var('EXECUTE_PATH', self.execute_path)

        # 日志处理
        self.logger: Logger = None
        if 'logger' in server_config.keys():
            _logger_config = server_config['logger']
            if len(_logger_config['conf_file_name']
                   ) > 0 and _logger_config['conf_file_name'][0] == '.':
                # 相对路径
                _logger_config['conf_file_name'] = os.path.join(
                    self.execute_path, _logger_config['conf_file_name'])
            if len(_logger_config['logfile_path']
                   ) > 0 and _logger_config['logfile_path'][0] == '.':
                # 相对路径
                _logger_config['logfile_path'] = os.path.join(
                    self.execute_path, _logger_config['logfile_path'])
            self.logger = Logger.create_logger_by_dict(_logger_config)

        # 加载管道配置
        RunTool.set_global_var('PIPELINE_PROCESSER_PARA',
                               server_config['pipeline']['processer_para'])
        RunTool.set_global_var('PIPELINE_ROUTER_PARA',
                               server_config['pipeline']['router_para'])
        _plugins_path_list = server_config['pipeline']['plugins_path'].split(
            ',')
        for _plugins_path in _plugins_path_list:
            Pipeline.load_plugins_by_path(
                os.path.join(self.execute_path, _plugins_path.strip()))

        self.server_config = server_config
        self.app = app
        if self.app is None:
            self.app = Flask(__name__)
            CORS(self.app)

        self.app.debug = self.debug
        self.app.send_file_max_age_default = datetime.timedelta(
            seconds=1)  # 设置文件缓存1秒
        self.app.config['JSON_AS_ASCII'] = False  # 显示中文
        # 上传文件大小限制
        self.app.config['MAX_CONTENT_LENGTH'] = math.floor(
            self.server_config['max_upload_size'] * 1024 * 1024)

        # 装载搜索引擎服务
        self.search_engine = SearchEngine(self.server_config,
                                          logger=self.logger)

        # 动态加载路由
        self.api_class = [
            SearchServer,
        ]

        # 增加客户端demo访问
        if self.server_config['enable_client']:
            # 增加静态路径
            _static_path = self.server_config['static_path']
            if _static_path[0:1] == '.':
                # 相对路径
                _static_path = os.path.realpath(
                    os.path.join(self.execute_path, _static_path))

            self.app.static_folder = _static_path
            self.app.static_url_path = 'static'

            # 加入客户端主页
            self.app.url_map.add(Rule('/', endpoint='client', methods=['GET']))
            self.app.view_functions['client'] = self._client_view_function

        FlaskTool.add_route_by_class(self.app, self.api_class)
        self._log_debug(str(self.app.url_map))