#define __WINDOWS__ 1
#define __PTHREAD__ 1
//#define MNIST_PATH "E:\\ebLearn\\mnist"
#include "libidx.h"
#include "libeblearn.h"
#include "libeblearntools.h"
//#include "netconf.h"
#include <iostream>
#include <stdio.h>
using namespace std;
using namespace ebl;
uint dump_count = 0;
string *gl_data_dir = NULL;
string *gl_data_errmsg = NULL;
void test_face();
int main(int argc,char** argv)
{
if (strlen(DATA_PATH) > 0) gl_data_dir = new string(DATA_PATH);
gl_data_errmsg =
new string("Data directory is unknown, some tests will be ignored");
test_face();
if (gl_data_dir) delete gl_data_dir;
if (gl_data_errmsg) delete gl_data_errmsg;
return 0;
}
void test_face()
{
typedef float t_net;
string name = "nens.gif";
string confname, imagename, root, ebl;
root << *gl_data_dir << "/face/";
ebl << *gl_data_dir << "/../../";
confname << root << "best.conf";
imagename << root << name;
idx<ubyte> im = load_image<ubyte>(imagename);
configuration conf;
conf.read(confname.c_str(), false, false, true);
conf.set("display_sleep","2000");
conf.set("root2", root.c_str());
conf.set("current_dir", root.c_str());
conf.set("ebl", ebl.c_str());
conf.resolve(true);
ebl::mutex mut;
detection_thread<t_net> dt(conf, &mut, "detection thread");
bboxes bboxes;
idx<ubyte> detframe;
idx<uint> total_saved(1);
string processed_fname;
uint cnt = 0;
dt.start();
while (!dt.set_data(im, imagename, name, cnt)) millisleep(5);
bool updated = false;
while (!updated)
{
updated = dt.get_data(bboxes, detframe, *(total_saved.idx_ptr()),
processed_fname);
millisleep(5);
}
millisleep(2);
dt.stop(true);
}
实验:
#define __WINDOWS__ 1
#define __PTHREAD__ 1
//#define __GUI__ 1
//#define MNIST_PATH "E:\\ebLearn\\mnist"
#include "libidx.h"
#include "libeblearn.h"
#include "libeblearntools.h"
//#include "libidxgui.h"
//#include "netconf.h"
//#ifdef __GUI__
//#include "libeblearngui.h"
//#endif
#include <iostream>
#include <stdio.h>
using namespace std;
using namespace ebl;
uint dump_count = 0;
string *gl_data_dir = NULL;
string *gl_data_errmsg = NULL;
void test_face();
//#ifdef __GUI__
//MAIN_QTHREAD(int, argc, char**, argv) {
//#else
int main(int argc,char** argv)
{
//#endif
if (strlen(DATA_PATH) > 0) gl_data_dir = new string(DATA_PATH);
gl_data_errmsg =
new string("Data directory is unknown, some tests will be ignored");
test_face();
if (gl_data_dir) delete gl_data_dir;
if (gl_data_errmsg) delete gl_data_errmsg;
return 0;
}
void test_face()
{
typedef float t_net;
string name = "nens.gif";
string confname, frame_name, root, ebl;
root << *gl_data_dir << "/face/";
ebl << *gl_data_dir << "/../../";
confname << root << "best.conf";
frame_name << root << name;
idx<ubyte> frame = load_image<ubyte>(frame_name);
configuration conf;
conf.read(confname.c_str(), false, false, true);
conf.set("display_sleep","2000");
conf.set("root2", root.c_str());
conf.set("current_dir", root.c_str());
conf.set("ebl", ebl.c_str());
conf.resolve(true);
// load network and weights in a forward-only parameter
parameter<float> theparam;
theparam.set_forward_only();
idx<ubyte> classes(1,1);
//try { // try loading classes names but do not stop upon failure
load_matrix<ubyte>(classes, conf.get_cstring("classes"));
// } catch(std::string &err) {
// merr << "warning: " << err;
// merr << std::endl;
// }
std::vector<std::string> sclasses = ubyteidx_to_stringvector(classes);
answer_module<float> *ans = create_answer<float,float,float>(conf, classes.dim(0));
uint noutputs = ans->get_nfeatures();
intg thick = -1;
uint cnt = 0;
module_1_1<float> *net = create_network<float>(theparam, conf, thick, noutputs,
"arch",cnt);
// loading weights
if (conf.exists("weights"))
{ // manual weights
// concatenate weights if multiple ones
std::vector<std::string> w =
string_to_stringvector(conf.get_string("weights"));
cout<< "Loading weights from: " << w << std::endl;
theparam.load_x(w);
}
else
{
int seed = dynamic_init_drand();
forget_param_linear fgp(1, 0.5, seed);
net->forget(fgp);
}
detector<float> detect(*net, sclasses, ans, NULL, NULL);
bboxes &bb = detect.fprop(frame, frame_name.c_str(), cnt);
//bboxes boxes(bbox_all, NULL);
//bboxes *bb = boxes.get_group(frame_name);
// idxdim d = boxes.get_group_dims(frame_name);
// d.insert_dim(0, 1);
// bboxes pruned;
// detect.init(d);
// detect.fprop_nms(*bb, pruned);
// //copy_bboxes(pruned); // make a copy of bounding boxes
// // resize frame so that caller knows the size of the frame
// idxdim framedim = frame.get_idxdim();
// if (d.dim(1) == -1 || d.dim(2) == -1)
// eblerror("pre-computed boxes must contain full image size, "
// << "but found: " << d);
// framedim.setdim(0, d.dim(1));
// framedim.setdim(1, d.dim(2));
// frame.resize(framedim);
/*ebl::mutex mut;
detection_thread<t_net> dt(conf, &mut, "detection thread");
bboxes bboxes;
idx<ubyte> detframe;
idx<uint> total_saved(1);
string processed_fname;
uint cnt = 0;
dt.start();
while (!dt.set_data(im, imagename, name, cnt)) millisleep(5);
bool updated = false;
while (!updated)
{
updated = dt.get_data(bboxes, detframe, *(total_saved.idx_ptr()),
processed_fname);
millisleep(5);
}
millisleep(2);
dt.stop(true);*/
if(net) delete net;
}
#define __WINDOWS__ 1//一定要定义这句
#define __PTHREAD__ 1
#define __GUI__
#include <map>
#include <string>
#include <iostream>
#include <algorithm>
#include <stdlib.h>
#include <sstream>
#include <iomanip>
#include <time.h>
#include "libidx.h"
#include "libeblearn.h"
#include "libeblearntools.h"
#include "libeblearngui.h"
using namespace std;
using namespace ebl;
//typedef double t_net; // network precision
typedef float t_net; // network precision
unsigned int dump_count = 0;//一定要这句
template <typename T>
std::string get_output_directory(configuration &conf);//获得配置文件 conf 的路径
MAIN_QTHREAD(int, argc, char **, argv)
{
configuration conf(argv[1], true, true, false);
if (conf.exists_true("fixed_randomization"))
cout << "Using fixed seed: " << fixed_init_drand() << endl;
else
cout << "Using random seed: " << dynamic_init_drand(argc, argv) << endl;
if (!conf.exists("root2") || !conf.exists("current_dir"))
{
string dir;
dir << dirname(argv[1]) << "/";
cout << "Looking for trained files in: " << dir << endl;
conf.set("root2", dir.c_str());
conf.set("current_dir", dir.c_str());
}
conf.set("run_type", "detect"); // tell conf that we are in detect mode
conf.resolve(); // manual call to resolving variable
bool silent = conf.exists_true("silent");
if (conf.exists_true("show_conf") && !silent)
{
conf.pretty();
}
string outdir = get_output_directory<t_net>(conf);//outdir为输出路径
cout << "Saving outputs to " << outdir << endl;
// save conf to output dir
string cname = outdir;
cname << filename(argv[1]);
if (conf.write(cname.c_str()))
{
cout << "Wrote configuration to " << cname << endl;//写配置文件
}
// load classes of network
//导入类别信息
idx<ubyte> classes(1,1);
vector<string> sclasses;
try
{ // try loading classes names but do not stop upon failure
load_matrix<ubyte>(classes, conf.get_cstring("classes"));
}
catch(string &err)
{
cerr << "warning: " << err << endl;
}
sclasses = ubyteidx_to_stringvector(classes);
t_bbox_saving bbsaving = bbox_none;
if (conf.exists("bbox_saving"))
bbsaving = (t_bbox_saving) conf.get_int("bbox_saving");
//bboxes boxes(bbsaving, &outdir, cout, cerr);
bool save_video = conf.exists_true("save_video");
bool save_detections = conf.exists_true("save_detections");
int height = -1;
int width = -1;
if (conf.exists("input_height")) height = conf.get_int("input_height");
if (conf.exists("input_width")) width = conf.get_int("input_width");
bool input_random = conf.exists_true("input_random");
uint npasses = 1;
char next_on_key = 0;
if (conf.exists("next_on_key"))
{
next_on_key = conf.get_char("next_on_key");
cout << "Press " << next_on_key << " to process next frame." << endl;
}
uint skip_frames = conf.try_get_uint("skip_frames", 0);
if (conf.exists("input_npasses"))
npasses = conf.get_uint("input_npasses");
string viddir;
if (save_video)
{
viddir << outdir << "video/";
mkdir_full(viddir);
}
bool precomputed_boxes = conf.exists("bbox_file");
uint save_bbox_period = conf.try_get_uint("save_bbox_period", 500);
idxdim crop(1, 1, 1);
if (conf.exists("input_crop"))
{
crop = string_to_idxdim(conf.get_string("input_crop"));
}
string cam_type;
if (conf.exists("camera"))
{
cam_type = conf.get_string("camera");
}
// image search can be configured with a search pattern
const char *fpattern = IMAGE_PATTERN_MAT;
if (conf.exists("file_pattern"))
{
fpattern = conf.get_cstring("file_pattern");
}
// initialize camera (opencv, directory, shmem or video)
//idx<ubyte> frame(std::max(height, 1), std::max(width, 1), 3);
camera<ubyte> *cam = NULL;
if (!strcmp(cam_type.c_str(), "directory"))
{
string dir;
if (argc >= 3) // read input dir from command line
dir = argv[2];
else if (conf.exists("input_dir"))
dir = conf.get_string("input_dir");
// given list
list<string> files;
if (conf.exists("input_list"))
{
files = string_to_stringlist(conf.get_string("input_list"));
cam = new camera_directory<ubyte>(dir.c_str(), height, width,
input_random, npasses, cout, cerr,
fpattern, &files);
}
else // given directory only
{
cam = new camera_directory<ubyte>(dir.c_str(), height, width,
input_random, npasses, cout, cerr,
fpattern, &files);
}
}
// answer variables & initializations
//bboxes bb;
#ifdef __GUI__
//gui
bool bkey_msg = false; // display key message
//bool display = conf.exists_bool("display");
bool show_parts = conf.exists_true("show_parts");
bool bbox_show_conf = !conf.exists_false("bbox_show_conf");
bool bbox_show_class = !conf.exists_false("bbox_show_class");
// mindisplay = conf.exists_bool("minimal_display");
uint display_sleep = 0;
if (conf.exists("display_sleep"))
{
display_sleep = conf.get_uint("display_sleep");
}
// display_states = conf.exists_bool("display_states");
// uint qstep1 = 0, qheight1 = 0, qwidth1 = 0,
// qheight2 = 0, qwidth2 = 0, qstep2 = 0;
// if (conf.exists_bool("queue1")) {
// qstep1 = conf.get_uint("qstep1");
// qheight1 = conf.get_uint("qheight1");
// qwidth1 = conf.get_uint("qwidth1"); }
// if (conf.exists_bool("queue2")) {
// qstep2 = conf.get_uint("qstep2");
// qheight2 = conf.get_uint("qheight2");
// qwidth2 = conf.get_uint("qwidth2"); }
// wid_states = display_states ? new_window("network states"):0;
// night_mode();
//uint wid = display ? new_window("eblearn object recognition") : 0;//重定义了
night_mode();
float display_transp = 0.0;
if (conf.exists("display_bb_transparency"))
{
display_transp = conf.get_float("display_bb_transparency");
}
//detector_gui<t_net> dgui(conf.exists_true("show_extracted"));
//end gui
uint wid = 0; // window id
uint wid_states = 0; // window id
bool display = false;
display = conf.exists_true("display")
&& conf.exists_true("display_threads");
bool mindisplay = conf.exists_true("minimal_display");
//bool save_video = conf.exists_true("save_video");//70行有定义
bool display_states = conf.exists_true("display_states");
//uint wid = 0; // window id
//uint wid_states = 0; // window id
#endif
// load network and weights in a forward-only parameter
parameter<float> theparam;
theparam.set_forward_only();
//try { // try loading classes names but do not stop upon failure
load_matrix<ubyte>(classes, conf.get_cstring("classes"));
// } catch(std::string &err) {
// merr << "warning: " << err;
// merr << std::endl;
// }
answer_module<float> *ans = create_answer<float,float,float>(conf, classes.dim(0));
uint noutputs = ans->get_nfeatures();
intg thick = -1;
module_1_1<float> *net = create_network<float>(theparam, conf, thick, noutputs,
"arch", 0);
// loading weights
if (conf.exists("weights"))
{ // manual weights
// concatenate weights if multiple ones
std::vector<std::string> w =
string_to_stringvector(conf.get_string("weights"));
cout << "Loading weights from: " << w << std::endl;
theparam.load_x(w);
// permute weights by blocks
if (conf.exists("weights_permutation"))
{
std::string sblocks = conf.get_string("weights_blocks");
std::string spermut = conf.get_string("weights_permutation");
std::vector<intg> blocks = string_to_intgvector(sblocks.c_str());
std::vector<uint> permut = string_to_uintvector(spermut.c_str());
theparam.permute_x(blocks, permut);
}
}
else
{
if (conf.exists_true("manual_load"))
{ // manual load
eblwarn("\"weights\" variable not defined, loading manually "
<< "if manual_load defined");
manually_load_network(*((layers<float>*)net), conf);
}
else
{ // random weights
int seed = dynamic_init_drand();
eblwarn("No weights to load, randomizing weights with seed " << seed);
forget_param_linear fgp(1, 0.5, seed);
net->forget(fgp);
}
}
// detector
detector<float> detect(*net, sclasses, ans, NULL, NULL, cout, cerr);
//初始化detector
// multi-scaling parameters
double maxs = conf.try_get_double("max_scale", 2.0);
double mins = conf.try_get_double("min_scale", 1.0);
t_scaling scaling_type =
(t_scaling) conf.try_get_uint("scaling_type", SCALES_STEP);
double scaling = conf.try_get_double("scaling", 1.4);
std::vector<midxdim> scales;
switch (scaling_type)
{
case MANUAL:
if (!conf.exists("scales"))
eblerror("expected \"scales\" variable to be defined in manual mode");
scales = string_to_midxdimvector(conf.get_cstring("scales"));
detect.set_resolutions(scales);
break ;
case ORIGINAL: detect.set_scaling_original(); break ;
case SCALES_STEP:
detect.set_resolutions(scaling, maxs, mins);
break ;
case SCALES_STEP_UP:
detect.set_resolutions(scaling, maxs, mins);
detect.set_scaling_type(scaling_type);
break ;
default:
detect.set_scaling_type(scaling_type);
}
// remove pads from target scales if requested
if (conf.exists_true("scaling_remove_pad")) detect.set_scaling_rpad(true);
// optimize memory usage by using only 2 buffers for entire flow
state<float> input(1, 1, 1), output(1, 1, 1);
if (!conf.exists_false("mem_optimization"))
detect.set_mem_optimization(input, output, true);
// TODO: always keep inputs, otherwise detection doesnt work. fix this.
// conf.exists_true("save_detections") ||
// (display && !mindisplay));
// zero padding
float hzpad = conf.try_get_float("hzpad", 0);
float wzpad = conf.try_get_float("wzpad", 0);
detect.set_zpads(hzpad, wzpad);
if (conf.exists("input_min")) // limit inputs size
detect.set_min_resolution(conf.get_uint("input_min"));
if (conf.exists("input_max")) // limit inputs size
detect.set_max_resolution(conf.get_uint("input_max"));
if (silent) detect.set_silent();
if (conf.exists_bool("save_detections"))
{
std::string detdir = outdir;
detdir += "detections";
uint nsave = conf.try_get_uint("save_max_per_frame", 0);
bool diverse = conf.exists_true("save_diverse");
detdir = detect.set_save(detdir, nsave, diverse);
}
detect.set_scaler_mode(conf.exists_true("scaler_mode"));
if (conf.exists("bbox_decision"))
detect.set_bbox_decision(conf.get_uint("bbox_decision"));
if (conf.exists("bbox_scalings"))
{
mfidxdim scalings =
string_to_fidxdimvector(conf.get_cstring("bbox_scalings"));
detect.set_bbox_scalings(scalings);
}
// nms configuration //
t_nms nms_type = (t_nms) conf.try_get_uint("nms", 0);
float pre_threshold = conf.try_get_float("pre_threshold", 0.0);
float post_threshold = conf.try_get_float("post_threshold", 0.0);
float pre_hfact = conf.try_get_float("pre_hfact", 1.0);
float pre_wfact = conf.try_get_float("pre_wfact", 1.0);
float post_hfact = conf.try_get_float("post_hfact", 1.0);
float post_wfact = conf.try_get_float("post_wfact", 1.0);
float woverh = conf.try_get_float("woverh", 1.0);
float max_overlap = conf.try_get_float("max_overlap", 0.0);
float max_hcenter_dist = conf.try_get_float("max_hcenter_dist", 0.0);
float max_wcenter_dist = conf.try_get_float("max_wcenter_dist", 0.0);
float vote_max_overlap = conf.try_get_float("vote_max_overlap", 0.0);
float vote_mhd = conf.try_get_float("vote_max_hcenter_dist", 0.0);
float vote_mwd = conf.try_get_float("vote_max_wcenter_dist", 0.0);
detect.set_nms(nms_type, pre_threshold, post_threshold, pre_hfact,
pre_wfact, post_hfact, post_wfact, woverh, max_overlap,
max_hcenter_dist, max_wcenter_dist, vote_max_overlap,
vote_mhd, vote_mwd);
if (conf.exists("raw_thresholds"))
{
std::string srt = conf.get_string("raw_thresholds");
std::vector<float> rt = string_to_floatvector(srt.c_str());
detect.set_raw_thresholds(rt);
}
if (conf.exists("outputs_threshold"))
detect.set_outputs_threshold(conf.get_double("outputs_threshold"),
conf.try_get_double("outputs_threshold_val",
-1));
///
if (conf.exists("netdims"))
{
idxdim d = string_to_idxdim(conf.get_string("netdims"));
detect.set_netdim(d);
}
if (conf.exists("smoothing"))
{
idxdim ker;
if (conf.exists("smoothing_kernel"))
ker = string_to_idxdim(conf.get_string("smoothing_kernel"));
detect.set_smoothing(conf.get_uint("smoothing"),
conf.try_get_double("smoothing_sigma", 1),
&ker,
conf.try_get_double("smoothing_sigma_scale", 1));
}
if (conf.exists("background_name"))
detect.set_bgclass(conf.get_cstring("background_name"));
if (conf.exists_true("bbox_ignore_outsiders"))
detect.set_ignore_outsiders();
if (conf.exists("corners_inference"))
detect.set_corners_inference(conf.get_uint("corners_inference"));
if (conf.exists("input_gain"))
detect.set_input_gain(conf.get_double("input_gain"));
if (conf.exists_true("dump_outputs"))
{
std::string fname;
fname << outdir << "/dump/detect_out";
detect.set_outputs_dumping(fname.c_str());
}
//初始化结束
bootstrapping<float> boot(conf);
// when a bbox file is given, ignore the processing, load the pre-computed
// bboxes and feed them to the nms (non-maximum suppression).
bboxes boxes(bbox_all, NULL, cout, cerr);//注意这里与前面的boxes重复
boxes.print_saving_type(); // inform user how we save boxes
//bool precomputed_boxes = false;
if (conf.exists("bbox_file"))
{
precomputed_boxes = true;
std::string bbfile = conf.get_string("bbox_file");
boxes.load_eblearn(bbfile);
}
bool bmask_class = false;
if (conf.exists("mask_class"))
bmask_class = detect.set_mask_class(conf.get_cstring("mask_class"));
//std::string viddir = outdir;
//viddir += "video/";
//mkdir_full(viddir);
#ifdef __GUI__
//定义GUI相关的
uint display_wmax = conf.try_get_uint("display_max_width", 3000);
float display_min = (float) conf.try_get_double("display_min", -1.7);
float display_max = (float) conf.try_get_double("display_max", 1.7);
float display_in_max = (float) conf.try_get_double("display_in_max", 255);
float display_in_min = (float) conf.try_get_double("display_in_min", 0);
//float display_transp = conf.try_get_float("display_bb_transparency", 0);
uint qstep1 = conf.try_get_uint("qstep1", 0);
uint qheight1 = conf.try_get_uint("qheight1", 0);
uint qwidth1 = conf.try_get_uint("qwidth1", 0);
uint qstep2 = conf.try_get_uint("qstep2", 0);
uint qheight2 = conf.try_get_uint("qheight2", 0);
uint qwidth2 = conf.try_get_uint("qwidth2", 0);
module_1_1_gui netgui;
wid_states = display_states ? new_window("network states"):0;
night_mode();
std::string title = "EBLearn detector: ";
if (display)
{
wid = new_window(title.c_str());
cout << "displaying in window " << wid << std::endl;
night_mode();
}
float zoom = conf.try_get_float("display_zoom", 1);
//bool bbox_show_conf = !conf.exists_false("bbox_show_conf");
//bool bbox_show_class = !conf.exists_false("bbox_show_class");
detector_gui<float>
dgui(conf.try_get_uint("show_extracted", 0),
conf.exists_bool("queue1"), qstep1, qheight1,
qwidth1, conf.exists_bool("queue2"), qstep2, qheight2, qwidth2,
bbox_show_class, bbox_show_conf);
if (bmask_class)
dgui.set_mask_class(conf.get_cstring("mask_class"),
(float) conf.try_get_double("mask_threshold", 0));
//结束定义
#endif
idx<ubyte> frame;// frame returned by detection thread
for(int i=0;i<cam->size();i++)
{
uint frame_id = 0;
//bool frame_grabbed = false;
frame_id = cam->frame_id();
frame = cam->grab();
//frame_grabbed = true;
// send new frame to this thread
string frame_fullname= cam->frame_fullname();
string frame_name= cam->frame_name();
// fprop and display
if (precomputed_boxes) eblerror("not implemented for nms only (TODO)");
disable_window_updates();
select_window(wid);
clear_window();
set_window_title(title.c_str());
// extract & display boxes
bboxes bb =
dgui.display_inputs_outputs(detect, frame, frame_name.c_str(),
frame_id, 0, 0, zoom,
display_min, display_max, wid,
outdir.c_str(),
display_in_min, display_in_max,
display_transp, display_wmax);
// make a copy of bounding boxes
if (display_sleep > 0)
{
cout << "sleeping for " << display_sleep << "ms." << std::endl;
millisleep(display_sleep);
}
cam->next();
}
//gui
// free variables
if (net) delete net;
if (ans) delete ans;
if (cam) delete cam;
return 0;
}
template <typename T>
std::string get_output_directory(configuration &conf)
{
std::string s;
if (conf.exists("output_dir")) s << conf.get_string("output_dir");
else s << conf.get_string("current_dir");
if (conf.exists("detections_dir"))
s << conf.get_string("detections_dir");
else
{
s << "/detections";
if (conf.exists_true("nms")) s << "_" << tstamp();
}
s << "/";
mkdir_full(s);
return s;
}