def dump_objects(path, *objects):

        logger.info('filename: {}'.format(path))
        try:
            with gzip.open(path, 'wb') as f:
                for obj in objects:
                    cPickle.dump(obj, f, -1)
            return True
        except Exception as e:
            logger.error('dump objects fail: {}'.format(e))
        return False
 def work(self):
     q = self.params['queue']
     s.settimeout(5)
     while self.run_state:
         try:
             data = s.recv(8192)
             logger.info('thread {} working ...'.format(self.name))
             logger.info('self.run_state: {}'.format(self.run_state))
             msg = q.get(block=True, timeout=7)
             logger.info('get msg: {}'.format(msg))
             logger.info('self.run_state: {}'.format(self.run_state))
             time.sleep(1)
         except (queue.Empty, socket.timeout) as e:
             logger.exception('work fail: {}'.format(e))
示例#3
0
print(os.environ.get('DYNACONF_SETTINGS'), os.environ.get('REDIS_URL'))
print('OSSURL_INTERNAL:', settings.OSSURL_INTERNAL,
      'DEDUP_TEM_INCRE_REDIS_CONFIG:', settings.DEDUP_TEM_INCRE_REDIS_CONFIG)

# 支持返回结果显示中文
app.config['JSON_AS_ASCII'] = False

image_filter = ImageFilterConstructor.instance()

ImageFilterView.init_version()
ImageFilterView.init_filter(image_filter)

app.add_url_rule('/image/filter',
                 view_func=ImageFilterView.as_view('image_filter'))


@app.route('/', endpoint='index')
def hello_world():
    return 'Hello World!'


@app.before_first_request
def daemon_thread():
    start_daemon_thread()


if __name__ == "__main__":
    logger.info('image filter detect service start ...')
    app.run(host="0.0.0.0", port=9003, threaded=True)
def pick_request_url(url):
    if settings.OSSURL_INTERNAL and 'oss-cn-hangzhou-internal' not in url:
        url = url.replace('oss-cn-hangzhou', 'oss-cn-hangzhou-internal')
        logger.info('url: %s', url)
    return url
 def stop(self):
     self.run_state = False
     print('over')
     logger.info('self.run_state: {}'.format(self.run_state))
 def run(self):
     logger.info('thread {} start ...'.format(self.name))
     self.run_state = True
     self.work()
     logger.info('thread {} stop ...'.format(self.name))
 def __del__(self):
     # 仅提供 gc 前的 log,不影响delete 语句
     logger.info('{} destroy, release some resource'.format(self.name))
    def dispatch_request(self):
        result = {
            'server_name': hostname,
            'filter_result': False,
            'duplicate_or_not': duplicate,
            'filter_model_version': self.version.get_filter_model_version(),
        }
        base_tag_list: List[str] = []
        face_info_list: List[Dict] = []
        filter_tag_list: Dict[str, Any] = dict()
        result['filter_tags'] = filter_tag_list
        filter_tag_list['base_tags'] = base_tag_list
        filter_tag_list['face_infos'] = face_info_list
        try:
            json_data = request.get_json()
            logger.info('request params: {}'.format(json_data))
            url = json_data['imageUrl']
            image_source = json_data['image_source']
            result['url'] = url
            # 图片载入
            detect = QualityDetect(self.image_filter, pick_request_url(url), image_source)
            detect.load_image_cv()
            # 基本信息
            base_tags, resolution_tag = detect.tag_base_info()

            logger.info('base info tags:{}, resolution: {}'.format(base_tags, resolution_tag))
            [base_tag_list.append(tag) for tag in base_tags]
            filter_tag_list['blur-resolution'] = resolution_tag
            # 人脸过滤
            is_face_ok, fail_tag, face_normal_positions, face_num = detect.tag_faces_info()

            logger.info('face info is_ok: {}, fail_tags: {}, face_normal_positions: {}'.
                        format(is_face_ok, fail_tag, face_normal_positions))
            # 记录面部信息
            for index, pos in enumerate(face_normal_positions):
                face_info_list.append({"face_index": index, "position": pos})

            # 黑人图片去除
            if face_num > 0:
                black_face, face_complexion_list = detect.get_face_complexion()
                # 记录每张脸的肤色信息
                for i, face in enumerate(face_info_list):
                    face['complexion'] = face_complexion_list[i]
                if black_face:
                    base_tag_list.append('filter-face-black')
            # 衣物过滤
            is_ok, msg, clothes_detect_results = detect.test_img_clothes()
            filter_tag_list['clothes_infos'] = clothes_detect_results
            # 文本过滤
            text_exist = detect.tag_text_info()

            # 去重检测
            is_dup = False
            if duplicate:
                hash_result, dups = detect.filter_dedup_image(url)
                is_dup = True if dups else False
                result['duplicated'] = is_dup
                result['duplicated_features'] = dups
                result['feature_hash']: int = hash_result[1]

            clothes_only_ok = is_ok and not text_exist
            if not is_face_ok:
                base_tag_list.append(fail_tag)
            if not is_ok:
                base_tag_list.append(msg)
            if text_exist:
                base_tag_list.append('text_exist')

            quality_fail = base_tag_list

            if not quality_fail:
                base_tag_list.append('filter-quality-ok')
                result['filter_result'] = True 
            result['filter_result_V2'] = clothes_only_ok
            logger.info('result: {}'.format(result))
            return make_response(jsonify(result), 200)

        except Exception as e:
            logger.error('image filter fail: {}'.format(e))
            logger.error(''.join(traceback.format_exception(*sys.exc_info())[-2:]))
            result['error'] = '{}'.format(e)
            return make_response(jsonify(result), 500)