New Init WIP
This commit is contained in:
+99
-6
@@ -34,6 +34,89 @@ DescriptorManager::~DescriptorManager() {
|
||||
//Realistically, when this gets destroyed the application is done anyway.
|
||||
}
|
||||
|
||||
//Loads all base images, calculates their descriptors,
|
||||
//calculates all image mutations and stores their descriptors
|
||||
//on disk.
|
||||
void DescriptorManager::Init() {
|
||||
//First load all images and store descriptors in cache and disk
|
||||
if (!fs::exists(img_path)) {
|
||||
cout << "Error. Data folder not found." << endl;
|
||||
return;
|
||||
}
|
||||
fs::ifstream ifs;
|
||||
fs::ofstream ofs;
|
||||
//foreach image in directory (non-recursive)
|
||||
for (fs::directory_iterator itr(img_path); itr != fs::directory_iterator(); ++itr) {
|
||||
if (!itr->path().has_extension()) continue; //skip directories and such
|
||||
string image_name = itr->path().filename().generic_string();
|
||||
Image* image = new Image;
|
||||
image->read(itr->path().string());
|
||||
//foreach descriptor
|
||||
for (int i = 0; i < numDescriptors; i++) {
|
||||
//folder to store descriptors; create if not exists
|
||||
fs::path descriptorBase("meta" / descriptor_types[i]->ToPath());
|
||||
if (!fs::is_directory(descriptorBase))
|
||||
fs::create_directory(descriptorBase);
|
||||
|
||||
fs::path featurePath(descriptorBase / itr->path().stem());
|
||||
bitset<64>* b1;
|
||||
if (fs::exists(featurePath)) { //feature on disk, load it in
|
||||
ifs.open(featurePath, fs::fstream::binary);
|
||||
unsigned long long n;
|
||||
ifs.read(reinterpret_cast<char*>(&n), sizeof(n));
|
||||
ifs.close();
|
||||
b1 = new bitset<64>(n);
|
||||
}
|
||||
else { //not on disk, calculate it
|
||||
b1 = &descriptor_types[i]->feature(image);
|
||||
unsigned long long n = b1->to_ullong();
|
||||
ofs.open(featurePath, fs::fstream::binary);
|
||||
ofs.write(reinterpret_cast<const char*>(&n), sizeof(n));
|
||||
ofs.close();
|
||||
}
|
||||
descriptors[i][image_name] = *b1;
|
||||
delete b1;
|
||||
}
|
||||
for (int i = 0; i < numMutations; i++) {
|
||||
//Check if we have somewhere to store the mutation
|
||||
fs::path mutationBase(img_path / mutators[i]->ToPath());
|
||||
if (!fs::is_directory(mutationBase))
|
||||
fs::create_directory(mutationBase);
|
||||
fs::path mutPath(mutationBase / itr->path().filename().generic_string());
|
||||
|
||||
Image* mutated = mutators[i]->getMutated(image);
|
||||
mutated->write(mutPath.generic_string());
|
||||
|
||||
//now calculate all the features for the mutated images as well
|
||||
//foreach descriptor
|
||||
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;
|
||||
string image_name = itr->path().filename().generic_string();
|
||||
if (!fs::exists(featurePath)) { //feature not yet calculated
|
||||
//cout << "Calculating " << image_name << endl;
|
||||
//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();
|
||||
}*/
|
||||
}
|
||||
delete mutated;
|
||||
}
|
||||
delete image;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
//Loads all images and determines the feature vectors
|
||||
bool DescriptorManager::loadDescriptors() {
|
||||
@@ -87,11 +170,7 @@ bool DescriptorManager::loadDescriptors() {
|
||||
}
|
||||
}
|
||||
}
|
||||
//cout << "ma qualle ide!" << endl;
|
||||
/*for (map<string, Image*>::iterator it = images.begin(); it != images.end(); ++it) {
|
||||
cout << "sup?" << endl;
|
||||
delete it->second; //free willy
|
||||
}*/
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -187,13 +266,19 @@ void DescriptorManager::similarImages(string image_name, int method, int numImag
|
||||
//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) {
|
||||
multiset<pair<unsigned long long, string> > local_distance(distances);
|
||||
multiset<pair<unsigned long long, string> > local_distance;
|
||||
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 (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);
|
||||
pair<unsigned long long, string> p(d, it->first);
|
||||
local_distance.insert(p); //pairs are compared by their first element..
|
||||
}
|
||||
for (int i = 0; i < numMutations; i++) {
|
||||
fs::path descriptorBase("meta" / descriptor_types[method]->ToPath() / mutators[i]->ToPath());
|
||||
fs::path imagePath(image_name);
|
||||
@@ -230,6 +315,14 @@ void DescriptorManager::insertMutations(string image_name, int method) {
|
||||
}
|
||||
}
|
||||
|
||||
void DescriptorManager::checkImages(int method) {
|
||||
fs::directory_iterator end_itr;
|
||||
for (fs::directory_iterator itr(img_path); itr != end_itr; ++itr) {
|
||||
if (!itr->path().has_extension()) continue; //skip directories
|
||||
insertMutations(itr->path().string(), method);
|
||||
}
|
||||
}
|
||||
|
||||
//outputs the gathered data to iostream
|
||||
//can be called directly or is called by its overload
|
||||
//assumes a < numDescriptors
|
||||
|
||||
Reference in New Issue
Block a user