#include #include #include #include #include #include #include #include // OpenCV headers #include #include #include #include #include #include #include #include #include #include #include #include using std::cout; using std::endl; PluginManager plugin_manager_; bool myfunction (std::map i, std::map j) { if (i["FPR"] == j["FPR"]) { return (i["TPR"] < j["TPR"]); } else { return (i["FPR"] < j["FPR"]); } } int main(int argc, char *argv[]) { std::string xml_dir = ""; int c; int create_roc_file = 0; int output_file_set = 0; std::string output_file = ""; std::string output_dir = "./"; while ((c = getopt (argc, argv, "f:o:d:")) != -1) { switch (c) { //case 'a': // aflag = 1; // break; //case 'b': // bflag = 1; // break; case 'o': output_dir = std::string(optarg); create_roc_file = 1; break; case 'f': output_file = std::string(optarg); output_file_set = 1; break; case 'd': xml_dir = std::string(optarg); break; case '?': if (optopt == 'd') { fprintf (stderr, "Option -%c requires an argument.\n", optopt); } else if (isprint (optopt)) { fprintf (stderr, "Unknown option `-%c'.\n", optopt); } else { fprintf (stderr, "Unknown option character `\\x%x'.\n", optopt); } return 1; default: abort (); } } // Check existence of xml directory if ( !boost::filesystem::exists( fs::path(xml_dir) ) ) { cout << "Error: XML Directory doesn't exist." << endl; return -1; } // Open each xml file and count metrics std::vector file_paths; syllo::get_files_with_ext(fs::path(xml_dir), ".xml", file_paths,true); std::vector< std::map > metrics_vector; int TP = 0, TN = 0, FP = 0, FN = 0; for (std::vector::iterator it = file_paths.begin(); it != file_paths.end(); it++) { cout << "File: " << it->string() << endl; AnnotationParser parser; int retcode = parser.ParseFile(it->string()); if (retcode != 0) { cout << "Failed to parse: " << it->string(); continue; } std::map metrics = parser.get_metrics(); metrics_vector.push_back(metrics); } std::sort(metrics_vector.begin(), metrics_vector.end(), myfunction); std::string roc_fn; if (output_file_set == 1) { roc_fn = output_dir + "/" + output_file; } else { // time string generation time_t rawtime; struct tm * timeinfo; char buffer [80]; time (&rawtime); timeinfo = localtime (&rawtime); strftime (buffer,80,"%Y-%m-%d-%H-%M-%S",timeinfo); std::string time_str = std::string(buffer); roc_fn = output_dir + "/" + time_str + "_ROC.csv"; } std::ofstream roc_stream; if (create_roc_file) { cout << "ROC Output file: " << roc_fn << endl; roc_stream.open (roc_fn.c_str(), std::ofstream::out); roc_stream << "# FPR,TPR,thresh,FP,FN,TP,TN," << endl; } for(std::vector< std::map >::iterator it = metrics_vector.begin(); it != metrics_vector.end(); it++) { // 0 for threshold place holder for now: TODO roc_stream << (*it)["FPR"] <<","<< (*it)["TPR"] << ",0," << (*it)["FP"] <<","<< (*it)["FN"] <<"," << (*it)["TP"] <<","<< (*it)["TN"] <<"," << endl; TP += (*it)["TP"]; TN += (*it)["TN"]; FP += (*it)["FP"]; FN += (*it)["FN"]; } roc_stream.close(); double TPR = 0, FPR = 0; TPR = (double)TP / (double)(TP + FN) ; FPR = (double)FP / (double)(FP + TN) ; // Display results syllo::fill_line("+"); cout << "Totals... " << endl; cout << "True Positives: " << TP << endl; cout << "True Negatives: " << TN << endl; cout << "False Positives: " << FP << endl; cout << "False Negatives: " << FN << endl; cout << "True Positive Rate: " << TPR << endl; cout << "False Positive Rate: " << FPR << endl; syllo::fill_line("+"); return 0; }