aboutsummaryrefslogtreecommitdiff
path: root/qdeduper/sigdb_qt.cpp
blob: 6bfa6421af9edbfaf25ecd367083565999db9c7d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
//Chris Xiong 2022
//License: MPL-2.0
#include "sigdb_qt.hpp"
#include "signature_db.hpp"

#include <algorithm>

signature_config cfg_full =
{
    9,      //slices
    3,      //blur_window
    2,      //min_window
    true,   //crop
    true,   //comp
    0.5,    //pr
    1./128, //noise_threshold
    0.05,   //contrast_threshold
    0.25    //max_cropping
};

signature_config cfg_subslice =
{
    4,      //slices
    16,     //blur_window
    2,      //min_window
    false,  //crop
    true,   //comp
    0.5,    //pr
    1./64,  //noise_threshold
    0.05,   //contrast_threshold
    0.25    //max_cropping
};

SignatureDB::SignatureDB() : QObject(nullptr)
{
    sdb = new signature_db();
    cfg = {
        3,
        3,
        cfg_full,
        cfg_subslice,
        0.3,
        nullptr,
        1
    };
}

SignatureDB::SignatureDB(const fs::path &dbpath) : QObject(nullptr)
{
    sdb = new signature_db(dbpath);
    create_priv_struct();
}

SignatureDB::~SignatureDB()
{
    delete sdb;
}

void SignatureDB::create_priv_struct()
{
    if (!valid()) return;
    fmap.clear();
    frmap.clear();
    distmap.clear();
    this->groups.clear();
    auto ids = sdb->get_image_ids();
    sdb->batch_get_signature_begin();
    for (auto &id : ids)
    {
        fs::path p;
        std::tie(p, std::ignore) = sdb->get_signature(id);
        fmap[id] = p;
        frmap[p] = id;
    }
    sdb->batch_get_signature_end();

    auto dupes = sdb->dupe_pairs();
    for (auto &dupe : dupes)
        distmap[std::make_pair(dupe.id1, dupe.id2)] = dupe.distance;

    auto gps = sdb->groups_get();
    //gps.erase(std::remove_if(gps.begin(), gps.end(), [](std::vector<size_t> v){ return v.size() < 2; }), gps.end());
    this->groups = std::move(gps);
}

bool SignatureDB::valid()
{
    return sdb->valid();
}

bool SignatureDB::is_dirty()
{
    return sdb->is_dirty();
}

populate_cfg_t SignatureDB::get_sig_config()
{
    return cfg;
}

void SignatureDB::set_sig_config(populate_cfg_t cfg)
{
    this->cfg = cfg;
}

void SignatureDB::scan_files(const std::vector<fs::path> &files)
{
    populate_cfg_t pcfg = cfg;
    pcfg.callback = [this](size_t c,int){Q_EMIT image_scanned(c);};

    sdb->populate(files, pcfg);

    Q_EMIT image_scanned(~size_t(0));

    sdb->group_similar();

    create_priv_struct();
}
void SignatureDB::interrupt_scan()
{
    if (sdb)
        sdb->populate_interrupt();
}

std::vector<std::pair<size_t, double>> SignatureDB::search_file(const fs::path &files)
{
    if(sdb)
        return sdb->search_image(files, cfg, false);
    return {};
}

size_t SignatureDB::num_groups()
{
    return groups.size();
}

std::vector<size_t> SignatureDB::get_group(size_t gid)
{
    if (gid >= groups.size()) return {};
    return groups[gid];
}

std::map<std::pair<size_t, size_t>, double> SignatureDB::group_distances(size_t gid)
{
    std::map<std::pair<size_t, size_t>, double> ret;
    auto g = get_group(gid);
    for (size_t i = 0; i < g.size(); ++i)
        for (size_t j = i + 1; j < g.size(); ++j)
        {
            size_t x = g[i], y = g[j];
            if (distmap.find(std::make_pair(x, y)) != distmap.end())
                ret[std::make_pair(x, y)] = distmap[std::make_pair(x, y)];
            else if (distmap.find(std::make_pair(y, x)) != distmap.end())
                ret[std::make_pair(x, y)] = distmap[std::make_pair(y, x)];
        }
    return ret;
}

std::vector<size_t> SignatureDB::get_image_ids()
{
    std::vector<size_t> ret;
    for (auto &f : fmap)
        ret.push_back(f.first);
    return ret;
}

fs::path SignatureDB::get_image_path(size_t id)
{
    if (fmap.find(id) == fmap.end())
        return fs::path();
    else return fmap[id];
}

size_t SignatureDB::get_path_id(const fs::path& p)
{
    if (frmap.find(p) == frmap.end())
        return ~size_t(0);
    else return frmap[p];
}

bool SignatureDB::load(const fs::path &p)
{
    bool ret = sdb->from_db_file(p);
    create_priv_struct();
    return ret;
}

bool SignatureDB::save(const fs::path &p)
{
    return sdb->to_db_file(p);
}

int64_t SignatureDB::db_memory_usage()
{
    return sdb->db_memory_usage();
}