示例#1
0
def test_hashing(read_asset, config_injector):
    config_injector({
        'elasticsearch': {
            'host': 'localhost',
            'port': 9200,
            'index': 'szurubooru_test',
        },
    })
    image_hash.purge()
    image_hash.add_image('test', read_asset('jpeg.jpg'))

    paths = image_hash.get_all_paths()
    results_exact = image_hash.search_by_image(read_asset('jpeg.jpg'))
    results_similar = image_hash.search_by_image(
        read_asset('jpeg-similar.jpg'))

    assert len(paths) == 1
    assert len(results_exact) == 1
    assert len(results_similar) == 1
    assert results_exact[0].path == 'test'
    assert results_exact[0].score == 63
    assert results_exact[0].distance == 0
    assert results_similar[0].path == 'test'
    assert results_similar[0].score == 17
    assert abs(results_similar[0].distance - 0.20599895341812172) < 1e-8
示例#2
0
def _sync_post_content(post: model.Post) -> None:
    regenerate_thumb = False

    if hasattr(post, '__content'):
        content = getattr(post, '__content')
        files.save(get_post_content_path(post), content)
        delattr(post, '__content')
        regenerate_thumb = True
        if post.post_id and post.type in (
                model.Post.TYPE_IMAGE, model.Post.TYPE_ANIMATION):
            image_hash.delete_image(post.post_id)
            image_hash.add_image(post.post_id, content)

    if hasattr(post, '__thumbnail'):
        if getattr(post, '__thumbnail'):
            files.save(
                get_post_thumbnail_backup_path(post),
                getattr(post, '__thumbnail'))
        else:
            files.delete(get_post_thumbnail_backup_path(post))
        delattr(post, '__thumbnail')
        regenerate_thumb = True

    if regenerate_thumb:
        generate_post_thumbnail(post)
示例#3
0
文件: posts.py 项目: rr-/szurubooru
def _sync_post_content(post: model.Post) -> None:
    regenerate_thumb = False

    if hasattr(post, '__content'):
        content = getattr(post, '__content')
        files.save(get_post_content_path(post), content)
        delattr(post, '__content')
        regenerate_thumb = True
        if post.post_id and post.type in (
                model.Post.TYPE_IMAGE, model.Post.TYPE_ANIMATION):
            image_hash.delete_image(post.post_id)
            image_hash.add_image(post.post_id, content)

    if hasattr(post, '__thumbnail'):
        if getattr(post, '__thumbnail'):
            files.save(
                get_post_thumbnail_backup_path(post),
                getattr(post, '__thumbnail'))
        else:
            files.delete(get_post_thumbnail_backup_path(post))
        delattr(post, '__thumbnail')
        regenerate_thumb = True

    if regenerate_thumb:
        generate_post_thumbnail(post)
示例#4
0
def test_hashing(read_asset, config_injector):
    config_injector({
        'elasticsearch': {
            'host': 'localhost',
            'port': 9200,
            'index': 'szurubooru_test',
            'user': '******',
            'pass': None,
        },
    })

    if not image_hash.get_session().ping():
        pytest.xfail('Unable to connect to ElasticSearch, '
                     'perhaps it is not available for this test?')

    image_hash.purge()
    image_hash.add_image('test', read_asset('jpeg.jpg'))

    paths = image_hash.get_all_paths()
    results_exact = image_hash.search_by_image(read_asset('jpeg.jpg'))
    results_similar = image_hash.search_by_image(
        read_asset('jpeg-similar.jpg'))

    assert len(paths) == 1
    assert len(results_exact) == 1
    assert len(results_similar) == 1
    assert results_exact[0].path == 'test'
    assert results_exact[0].score == 63
    assert results_exact[0].distance == 0
    assert results_similar[0].path == 'test'
    assert results_similar[0].score == 17
    assert abs(results_similar[0].distance - 0.20599895341812172) < 1e-8
示例#5
0
def populate_reverse_search() -> None:
    excluded_post_ids = image_hash.get_all_paths()

    post_ids_to_hash = (db.session.query(model.Post.post_id).filter(
        (model.Post.type == model.Post.TYPE_IMAGE)
        | (model.Post.type == model.Post.TYPE_ANIMATION)).filter(
            ~model.Post.post_id.in_(excluded_post_ids)).order_by(
                model.Post.post_id.asc()).all())

    for post_ids_chunk in util.chunks(post_ids_to_hash, 100):
        posts_chunk = (db.session.query(model.Post).filter(
            model.Post.post_id.in_(post_ids_chunk)).all())
        for post in posts_chunk:
            content_path = get_post_content_path(post)
            if files.has(content_path):
                image_hash.add_image(post.post_id, files.get(content_path))
示例#6
0
def populate_reverse_search():
    excluded_post_ids = image_hash.get_all_paths()

    post_ids_to_hash = (db.session
        .query(db.Post.post_id)
        .filter(
            (db.Post.type == db.Post.TYPE_IMAGE) |
            (db.Post.type == db.Post.TYPE_ANIMATION))
        .filter(~db.Post.post_id.in_(excluded_post_ids))
        .order_by(db.Post.post_id.asc())
        .all())

    for post_ids_chunk in util.chunks(post_ids_to_hash, 100):
        posts_chunk = (db.session
            .query(db.Post)
            .filter(db.Post.post_id.in_(post_ids_chunk))
            .all())
        for post in posts_chunk:
            content_path = get_post_content_path(post)
            if files.has(content_path):
                image_hash.add_image(post.post_id, files.get(content_path))