Skip to content

Instantly share code, notes, and snippets.

@kashimAstro
Last active March 2, 2017 16:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kashimAstro/ebc68f9b7c87576dcb25e111715b63c9 to your computer and use it in GitHub Desktop.
Save kashimAstro/ebc68f9b7c87576dcb25e111715b63c9 to your computer and use it in GitHub Desktop.
dlib imglab command line, tool for train machine learning with dlib
/*
http://dlib.net/train_object_detector.cpp.html
Usage:
first step create positive train:
./bin/imglab_commandline positive/ "30,20,520,520" "person"
second step create negative train in append positive train previously created:
./bin/imglab_commandline negative/ "30,20,520,520"
and result train.xml create .svm:
./bin/train_object_detector -tv train.xml
test:
./bin/train_object_detector test/*.jpg
*/
#include <iostream>
#include <vector>
#include <dirent.h>
#include <fstream>
#include <stdlib.h>
std::vector<std::string> split(const std::string &text, char sep)
{
std::vector<std::string> tokens;
std::size_t start = 0, end = 0;
while ((end = text.find(sep, start)) != std::string::npos) {
tokens.push_back(text.substr(start, end - start));
start = end + 1;
}
tokens.push_back(text.substr(start));
return tokens;
}
std::string replaced(std::string subject, const std::string& search, const std::string& replace)
{
size_t pos = 0;
while ((pos = subject.find(search, pos)) != std::string::npos) {
subject.replace(pos, search.length(), replace);
pos += replace.length();
}
return subject;
}
std::string read_file(std::string p)
{
std::string str;
std::ifstream t(p.c_str());
if(t.good())
{
str.assign((std::istreambuf_iterator<char>(t)),std::istreambuf_iterator<char>());
}
return str;
}
void write_file(std::string p, std::string d)
{
std::ofstream file;
file.open(p.c_str());
file << d;
file.close();
}
int main(int argc, char** argv)
{
if(argc > 2)
{
bool opened = false;
std::string buf;
std::string path = argv[1];
std::vector<std::string> pbox = split(argv[2],',');
if(pbox.size()<3)
{
std::cout<<"Error: parsing size box!";
exit(0);
}
std::string t = pbox[0];
std::string l = pbox[1];
std::string w = pbox[2];
std::string h = pbox[3];
std::string label;
if(argc <= 3)
{
buf = "<?xml version='1.0' encoding='ISO-8859-1'?>\n";
buf+= "<?xml-stylesheet type='text/xsl' href='image_metadata_stylesheet.xsl'?>\n";
buf+= "<dataset>\n";
buf+= "<name>Dataset Image for dlib</name>\n";
buf+= "<comment>generator xml list for image training</comment>\n";
buf+= "<images>\n";
std::string reader = read_file("train.xml");
buf+= reader;
label = "";
opened = true;
}
else
{
label = argv[3];
}
struct dirent **namelist;
int n;
n = scandir(path.c_str(), &namelist, 0, alphasort);
if(n < 0)
std::cout<<"error scandir\n";
else
while(n--)
{
std::string name = namelist[n]->d_name;
if(name != "." && name != "..")
{
std::cout<< name<<"\n";
buf+= "<image file='"+path+name+"'>\n";
buf+= "\t<box top='"+t+"' left='"+l+"' width='"+w+"' height='"+h+"'>\n";
buf+= "\t<label>"+label+"</label>\n";
buf+= "\t</box>\n";
buf+= "</image>\n";
}
}
if(!opened)
{
write_file("train.xml", buf);
}
else
{
buf+= "</images>\n";
buf+= "</dataset>\n";
write_file("train.xml", buf);
}
}
else
{
std::cout<<"Error: parameter[ \"folder\" \"top,left,width,height\" \"label\" ]\n\nPositive Example:\n \timglab_commadline \"person_image/\" \"10,10,50,100\" \"personbody\"\n\nNegative Example:\n \timglab_commadline \"noperson_image/\" \"10,10,50,100\"\n";
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment