WIP Mutations ranking

This commit is contained in:
Mark Hoekveen
2018-08-11 11:05:03 +02:00
parent 34ba693e1c
commit 1de3961ece
5 changed files with 82 additions and 32 deletions
+55 -27
View File
@@ -20,6 +20,7 @@ DescriptorManager::DescriptorManager(const fs::path & image_path) {
descriptor_types[0] = new averageColor();
mutators[0] = new Grayscale();
mutators[1] = new Flip();
}
//No need to call destructor.. yet?
@@ -100,7 +101,6 @@ bool DescriptorManager::createMutations() {
//foreach mutation
for (int i = 0; i < numMutations; i++) {
fs::path mutationBase(img_path / mutators[i]->ToPath());
fs::path descriptorBase("meta" / descriptor_types[i]->ToPath());
if (!fs::is_directory(mutationBase))
if (fs::create_directory(mutationBase)) {}
@@ -121,35 +121,19 @@ bool DescriptorManager::createMutations() {
for (int j = 0; j < numDescriptors; j++) {
//folder to store descriptors; create if not exists
fs::path descriptorBase("meta" / descriptor_types[j]->ToPath() / mutators[i]->ToPath());
//cout << "Doing mutated descriptors for " << descriptorBase << endl;
if (!fs::is_directory(descriptorBase))
if (fs::create_directory(descriptorBase)) {}
fs::path featurePath(descriptorBase / itr->path().stem());
cout << featurePath << endl;
//cout << featurePath << endl;
string image_name = itr->path().filename().generic_string();
if (fs::exists(featurePath)) { //feature already calculated
ifs.open(featurePath, fs::fstream::binary); //in binary mode
unsigned long long n;
ifs.read(reinterpret_cast<char*>(&n), sizeof(n));
ifs.close();
bitset<64> b1(n);
//descriptors[j][image_name] = b1;
}
else { //feature not yet calculated
Image* image; //only load image from disk if not loaded before
if (images.count(image_name)) { //if image exists in map
image = images[image_name];
}
else {
image = new Image;
image->read(itr->path().string());
images[image_name] = image;
}
if (!fs::exists(featurePath)) { //feature not yet calculated
//cout << "Calculating " << image_name << endl;
bitset<64> b1 = descriptor_types[i]->feature(image);
//descriptor_types[i]->distance(b1, b1);
//descriptors[i][image_name] = b1;
bitset<64> b1 = descriptor_types[j]->feature(mutated);
unsigned long long n = b1.to_ullong();
cout << "Writing " << b1 << "(" << n << ")" << " to " << featurePath << endl;
ofs.open(featurePath, fs::fstream::binary); //in binary mode; only open file after calculations have been done
ofs.write(reinterpret_cast<const char*>(&n), sizeof(n));
ofs.close();
@@ -174,9 +158,10 @@ void DescriptorManager::similarImages(string image_name, int method, int numImag
image->read(image_name); //assuming http url
target = descriptor_types[method]->feature(image);
} //TODO: Add support for uploaded images
//cout << "HEY?!" << endl;
//first we need all distances to this image.. and insert them in a sorted way
multiset<pair<unsigned long long, string> > distances; //multiset is probably not needed, but who knows..
//multiset<pair<unsigned long long, string> > distances; //multiset is probably not needed, but who knows..
//distances.clear(); //start fresh
for (map<string, bitset<64>>::iterator it = descriptors[method].begin(); it != descriptors[method].end(); ++it) {
//cout << "ho " << endl;
unsigned long long d = descriptor_types[method]->distance(target, it->second);
@@ -188,19 +173,62 @@ void DescriptorManager::similarImages(string image_name, int method, int numImag
for (std::multiset<pair<unsigned long long, string>>::iterator it = distances.begin(); it != distances.end() && i < numImages; ++it) {
//cout << "hey " << endl;
if (true || it->second != image_name) {
cout << it->second << endl;
//cout << "(" << it->first << ", " << it->second << ")" << endl;
i++;
}
}
}
//inserts the descriptors of the mutated versions of the image.
//Assumes the distances array is filled with the normal distances
void DescriptorManager::insertMutations(string image_name, int method) {
fs::ifstream ifs;
bitset<64> target;
if (descriptors[method].count(image_name)) { //if image exists in map
target = descriptors[method][image_name];
}
cout << "Finding hits for " << image_name << " which has feature-number " << target.to_ullong() << endl;
for (int i = 0; i < numMutations; i++) {
fs::path descriptorBase("meta" / descriptor_types[method]->ToPath() / mutators[i]->ToPath());
fs::path imagePath(image_name);
fs::path featurePath(descriptorBase / imagePath.stem());
cout << "Finding mutation for this image at: " << featurePath << endl;
if (fs::exists(featurePath)) { //feature already calculated
ifs.open(featurePath, fs::fstream::binary); //in binary mode
unsigned long long n;
ifs.read(reinterpret_cast<char*>(&n), sizeof(n));
ifs.close();
cout << "Number: " << n << endl;
bitset<64> b1(n);
unsigned long long d = descriptor_types[method]->distance(target, b1);
pair<unsigned long long, string> p(d, mutators[i]->ToString());
distances.insert(p);
}
else {
cout << "data not set properly" << endl;
}
}
cout << "..." << endl;
for (int i = 0; i < numMutations; i++) {
int j = 0;
for (std::multiset<pair<unsigned long long, string>>::iterator it = distances.begin(); it != distances.end(); ++it) {
//cout << "hey " << endl;
if (it->second == mutators[i]->ToString()) {
cout << "Hit. On the " << j << "th position: " << endl;
cout << "(" << it->first << ", " << it->second << ")" << endl;
}
j++;
}
}
}
//outputs the gathered data to iostream
//can be called directly or is called by its overload
//assumes a < numDescriptors
void DescriptorManager::outputMap(int a) {
if (a != 7) return;
if (a != 2) return;
cout << "Outputting map " << descriptor_types[a]->ToString() << endl;
map<string, bitset<64>> a_map = descriptors[a];
if (a_map.begin() == a_map.end()) {