void growFlowers() { boxes(); LRforward(1); backward(8); RLforward(5); boxes(); }
void do_nms_sort(float *detections, int total, float score_thresh, float nms_thresh) { std::vector<Rect2f> boxes(total); std::vector<float> scores(total); for (int i = 0; i < total; ++i) { Rect2f &b = boxes[i]; int box_index = i * (classes + coords + 1); b.width = detections[box_index + 2]; b.height = detections[box_index + 3]; b.x = detections[box_index + 0] - b.width / 2; b.y = detections[box_index + 1] - b.height / 2; } std::vector<int> indices; for (int k = 0; k < classes; ++k) { for (int i = 0; i < total; ++i) { int box_index = i * (classes + coords + 1); int class_index = box_index + 5; scores[i] = detections[class_index + k]; detections[class_index + k] = 0; } NMSFast_(boxes, scores, score_thresh, nms_thresh, 1, 0, indices, rectOverlap); for (int i = 0, n = indices.size(); i < n; ++i) { int box_index = indices[i] * (classes + coords + 1); int class_index = box_index + 5; detections[class_index + k] = scores[indices[i]]; } } }
// Get potential bounding boxes for all test images void Objectness::getObjBndBoxesForTests(vector<vector<Vec4i>> &_boxesTests, int numDetPerSize) { const int TestNum = _voc.testSet.size(); vecM imgs3u(TestNum); vector<ValStructVec<float, Vec4i>> boxesTests; boxesTests.resize(TestNum); #pragma omp parallel for for (int i = 0; i < TestNum; i++){ //imgs3u[i] = imread(format(_S(_voc.imgPathW), _S(_voc.testSet[i]))); imgs3u[i] = imread("C:/Users/TerryChen/Desktop/M.jpg"); boxesTests[i].reserve(10000); } int scales[3] = {1, 3, 5}; for (int clr = MAXBGR; clr <= G; clr++){ setColorSpace(clr); trainObjectness(numDetPerSize); loadTrainedModel(); CmTimer tm("Predict"); tm.Start(); #pragma omp parallel for for (int i = 0; i < TestNum; i++){ ValStructVec<float, Vec4i> boxes; getObjBndBoxes(imgs3u[i], boxes, numDetPerSize); boxesTests[i].append(boxes, scales[clr]); } tm.Stop(); printf("Average time for predicting an image (%s) is %gs\n", _clrName[_Clr], tm.TimeInSeconds()/TestNum); } _boxesTests.resize(TestNum); CmFile::MkDir(_bbResDir); #pragma omp parallel for for (int i = 0; i < TestNum; i++){ CStr fName = _bbResDir + _voc.testSet[i]; ValStructVec<float, Vec4i> &boxes = boxesTests[i]; FILE *f = fopen(_S(fName + ".txt"), "w"); fprintf(f, "%d\n", boxes.size()); for (size_t k = 0; k < boxes.size(); k++) fprintf(f, "%g, %s\n", boxes(k), _S(strVec4i(boxes[k]))); fclose(f); _boxesTests[i].resize(boxesTests[i].size()); for (int j = 0; j < boxesTests[i].size(); j++) _boxesTests[i][j] = boxesTests[i][j]; } evaluatePerImgRecall(_boxesTests, "PerImgAllNS.m", 5000); #pragma omp parallel for for (int i = 0; i < TestNum; i++){ boxesTests[i].sort(false); for (int j = 0; j < boxesTests[i].size(); j++) _boxesTests[i][j] = boxesTests[i][j]; } evaluatePerImgRecall(_boxesTests, "PerImgAllS.m", 5000); }
int main(void) { vga_init(); /* Create virtual screen. */ vbuf = malloc(VWIDTH * VHEIGHT); gl_setcontextvirtual(VWIDTH, VHEIGHT, 1, 8, vbuf); /* Set Mode X-style 320x240x256. */ vga_setmode(G320x240x256); gl_setrgbpalette(); vga_clear(); boxes(); demo1(); demo2(); vga_setmode(G320x200x256); /* Set linear 320x200x256. */ gl_setrgbpalette(); demo3(); vga_setmode(TEXT); exit(0); }
virtual void Body() { int valid[MAX_BOX]; while (1) { // printf("Reading...\n"); in_mix.Read(); FiveBoxesInARow& boxes = in_mix.Content().GetBoxes(); int top = FiveBoxesInARow::GetMaxBoxes(); assert (top<=MAX_BOX); int count = 0; trackers.BeginObservations(); for (int i=0; i<top; i++) { valid[i] = 0; CBox2Send& src = boxes(i); if (src.valid) { valid[i] = 1; Box b; b.Set(src.xmin,src.ymin,src.xmax,src.ymax); trackers.Observe(b); count++; } } trackers.face_prev.CastCopy(in_mix.Content().GetImage()); trackers.EndObservations(); if (count>0) { // printf("Received %d boxes\n", count); } // printf("Computation complete.\n"); } }
void do_geo(DisjointBoxLayout& a_dbl, EBISLayout & a_ebisl, Box & a_domain, Real & a_dx ) { //define grids int len = 32; int mid = len/2; Real xdom = 1.0; a_dx = xdom/len; const IntVect hi = (len-1)*IntVect::Unit; a_domain= Box(IntVect::Zero, hi); Vector<Box> boxes(4); Vector<int> procs(4); boxes[0] = Box(IntVect(D_DECL(0 , 0, 0)), IntVect(D_DECL(mid-1, mid-1, len-1))); boxes[1] = Box(IntVect(D_DECL(0 ,mid, 0)), IntVect(D_DECL(mid-1, len-1, len-1))); boxes[2] = Box(IntVect(D_DECL(mid, 0, 0)), IntVect(D_DECL(len-1, mid-1, len-1))); boxes[3] = Box(IntVect(D_DECL(mid,mid, 0)), IntVect(D_DECL(len-1, len-1, len-1))); LoadBalance(procs, boxes); /** int loProc = 0; int hiProc = numProc() -1; procs[0] = loProc; procs[1] = hiProc; procs[2] = loProc; procs[3] = hiProc; **/ a_dbl = DisjointBoxLayout(boxes, procs); //define geometry RealVect rampNormal = RealVect::Zero; rampNormal[0] = -0.5; rampNormal[1] = 0.866025404; Real rampAlpha = -0.0625; RealVect rampPoint = RealVect::Zero; rampPoint[0] = rampAlpha / rampNormal[0]; bool inside = true; PlaneIF ramp(rampNormal,rampPoint,inside); RealVect vectDx = RealVect::Unit; vectDx *= a_dx; GeometryShop mygeom( ramp, 0, vectDx ); RealVect origin = RealVect::Zero; EBIndexSpace *ebisPtr = Chombo_EBIS::instance(); ebisPtr->define(a_domain, origin, a_dx, mygeom); //fill layout ebisPtr->fillEBISLayout(a_ebisl, a_dbl, a_domain, 4 ); }
const QList<Mailbox*>& QMailAccount::mailboxes() const { if (_sortMailboxes) { _sortMailboxes = false; QList<Mailbox*>& boxes(const_cast<QList<Mailbox*>&>(_mailboxes)); qSort(boxes.begin(), boxes.end(), &compareMailboxes); } return _mailboxes; }
MGRadBndry::MGRadBndry(const BoxArray& _grids, const int _ngroups, const Geometry& _geom) : NGBndry(_grids,_ngroups,_geom) { if (first) init(_ngroups); first = 0; const BoxArray& grids = boxes(); const Box& domain = geom.Domain(); // const Real* dx = geom.CellSize(); // const Real* xlo = geom.ProbLo(); // It is desirable that the type array be set up after static init. // This is part of the reason this step is not in NGBndry. for (OrientationIter fi; fi; ++fi) { Orientation face = fi(); if (bcflag[face] == 2) { bctypearray[face].resize(grids.size()); for (FabSetIter bi(bndry[face]); bi.isValid(); ++bi) { int igrid = bi.index(); if (domain[face] == boxes()[igrid][face] && !geom.isPeriodic(face.coordDir())) { const Box& face_box = bndry[face][bi].box(); bctypearray[face].set(igrid, new BaseFab<int>(face_box)); // We don't care about the bndry values here, only the type array. #if 0 FORT_RADBNDRY2(bndry[face][bi].dataPtr(), dimlist(face_box), bctypearray[face][igrid].dataPtr(), dimlist(domain), dx, xlo, time); #endif } } } } }
ErrorCode Tree::find_all_trees( Range& results ) { Tag tag = get_box_tag(); ErrorCode rval = moab()->get_entities_by_type_and_tag( 0, MBENTITYSET, &tag, 0, 1, results ); if (MB_SUCCESS != rval || results.empty()) return rval; std::vector<BoundBox> boxes(results.size()); rval = moab()->tag_get_data(tag, results, &boxes[0]); if (MB_SUCCESS != rval) return rval; for (std::vector<BoundBox>::iterator vit = boxes.begin(); vit != boxes.end(); ++vit) boundBox.update(*vit); if (results.size() == 1) myRoot = *results.begin(); return MB_SUCCESS; }
Teuchos::Array<BoundingBox> GeometryManager<Geometry,GlobalOrdinal>::boundingBoxes() const { Teuchos::Array<BoundingBox> boxes( d_geometry.size() ); Teuchos::Array<BoundingBox>::iterator box_iterator; typename Teuchos::ArrayRCP<Geometry>::const_iterator geom_iterator; for ( geom_iterator = d_geometry.begin(), box_iterator = boxes.begin(); geom_iterator != d_geometry.end(); ++geom_iterator, ++box_iterator ) { *box_iterator = GT::boundingBox( *geom_iterator ); } return boxes; }
void RayTraceRenderPass::UploadToGPU(const Camera &cam) const { const auto primitivesUB = UniformBuffers::Get().Primitives(); size_t offset = 0; primitivesUB.rectangles(mRectangles, offset); offset += mRectangles.size() * sizeof(RTRectangle); primitivesUB.boxes(mBoxes, offset); offset += mBoxes.size() * sizeof(RTBox); primitivesUB.spheres(mSpheres, offset); offset += mSpheres.size() * sizeof(RTSphere); primitivesUB.lights(mLights, offset); offset += mLights.size() * sizeof(RTLight); if (offset > PrimitivesUB::MAX_SIZE) { throw exception("PrimitivesUB size larger then max: " + offset); } }
std::list<Geometry::Point2f> pointToConvex(const std::list<Geometry::Point2f>& points, const float sizeStep) { Geometry::Point2f pt_min, pt_max; Geometry::getVerticalBorderPoint(points, &pt_min, &pt_max); float f_n_step = (pt_max.x - pt_min.x)/sizeStep; int n_step = static_cast<int>(f_n_step); if ( f_n_step-n_step > 0 ) ++n_step; std::vector< std::list<Geometry::Point2f> > boxes(n_step); // sort to boxes for ( std::list<Geometry::Point2f>::const_iterator ptIt = points.begin(); ptIt != points.end(); ++ptIt ) { int step = static_cast<int>(ptIt->x/sizeStep); std::cout << "pt.x : " << ptIt->x << " step : " << step << std::endl; boxes[step].push_back(*ptIt); } std::list<Geometry::Point2f> maxBoxPoints; std::list<Geometry::Point2f> minBoxPoints; for ( std::vector< std::list<Geometry::Point2f> >::iterator boxIt = boxes.begin(); boxIt != boxes.end(); ++boxIt ) { Geometry::Point2f box_pt_min, box_pt_max; Geometry::getHorizontalBorderPoint(*boxIt, &box_pt_min, &box_pt_max); minBoxPoints.push_back(box_pt_min); maxBoxPoints.push_back(box_pt_max); } std::list<Geometry::Point2f> polinom; polinom.push_back(pt_min); polinom.insert(polinom.end(), minBoxPoints.begin(), minBoxPoints.end()); polinom.push_back(pt_max); polinom.insert(polinom.end(), maxBoxPoints.begin(), maxBoxPoints.end()); return polinom; }
bool Run(Node *node) { const Tensor *loc_tensor = node->GetInputTensor(0); const Tensor *conf_tensor = node->GetInputTensor(1); const Tensor *priorbox_tensor = node->GetInputTensor(2); Tensor *output_tensor = node->GetOutputTensor(0); DetectionOutput *detect_op = dynamic_cast<DetectionOutput *>(node->GetOp()); DetectionOutputParam *param_ = detect_op->GetParam(); //location [b,num_prior*4,1,1] float *location = (float *)get_tensor_mem(loc_tensor); //confidence [b,num_prior*21,1,1] float *confidence = (float *)get_tensor_mem(conf_tensor); //priorbox [b,2,num_prior*4,1] float *priorbox = (float *)get_tensor_mem(priorbox_tensor); const std::vector<int>& dims=priorbox_tensor->GetShape().GetDim(); const int num_priorx4=dims[2]; const int num_prior =num_priorx4/4; const int num_classes = param_->num_classes; //const int batch=dims[0]; // only support for batch=1 //for(int b=0;b<batch;b++) //{ int b=0; float* loc_ptr=location+b*num_priorx4; float* conf_ptr=confidence+b*num_prior*num_classes; float* prior_ptr=priorbox+b*num_priorx4*2; std::vector<Box> boxes(num_prior); get_boxes(boxes, num_prior,loc_ptr,prior_ptr); std::vector< std::vector<Box> > all_class_bbox_rects; all_class_bbox_rects.resize(num_classes); // start from 1 to ignore background class for(int i=1;i<num_classes;i++) { std::vector<Box> class_box; for(int j=0;j<num_prior;j++) { float score= conf_ptr[j*num_classes +i]; if(score > param_->confidence_threshold) { boxes[j].score=score; boxes[j].class_idx=i; class_box.push_back(boxes[j]); } } //sort std::sort(class_box.begin(),class_box.end(), [](const Box& a, const Box& b) {return a.score > b.score;}); // keep nms_top_k if (param_->nms_top_k < (int)class_box.size()) { class_box.resize(param_->nms_top_k); } // apply nms std::vector<int> picked; nms_sorted_bboxes(class_box, picked, param_->nms_threshold); // select for (int j = 0; j < (int)picked.size(); j++) { int z = picked[j]; all_class_bbox_rects[i].push_back(class_box[z]); } } // gather all class std::vector< Box> bbox_rects; for (int i = 0; i < num_classes; i++) { const std::vector<Box>& class_bbox_rects = all_class_bbox_rects[i]; bbox_rects.insert(bbox_rects.end(), class_bbox_rects.begin(), class_bbox_rects.end()); } // global sort inplace std::sort(bbox_rects.begin(),bbox_rects.end(), [](const Box& a, const Box& b) {return a.score > b.score;}); // keep_top_k if (param_->keep_top_k < (int)bbox_rects.size()) { bbox_rects.resize(param_->keep_top_k); } // output [b,num,6,1] int num_detected = bbox_rects.size(); int total_size=num_detected*6*4; // alloc mem void * mem_addr=mem_alloc(total_size); set_tensor_mem(output_tensor,mem_addr,total_size,mem_free); float *output = (float *)get_tensor_mem(output_tensor); TShape &out_shape = output_tensor->GetShape(); std::vector<int> outdim={1,num_detected,6,1}; out_shape.SetDim(outdim); for (int i = 0; i < num_detected; i++) { const Box& r = bbox_rects[i]; float* outptr = output+i*6; outptr[0] = r.class_idx; outptr[1] = r.score; outptr[2] = r.x0; outptr[3] = r.y0; outptr[4] = r.x1; outptr[5] = r.y1; } return true; }
void detection_thread<T>::execute() { try { bool display = false; #ifdef __GUI__ 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"); bool display_states = conf.exists_true("display_states"); uint wid = 0; // window id uint wid_states = 0; // window id #endif uint display_sleep = conf.try_get_uint("display_sleep", 0); // if (!display && save_video) { // // we still want to output images but not show them // display = true; // #ifdef __GUI__ // set_gui_silent(); // #endif // } // load network and weights in a forward-only parameter parameter<T> 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<T> *ans = create_answer<T,T,T>(conf, classes.dim(0)); uint noutputs = ans->get_nfeatures(); intg thick = -1; module_1_1<T> *net = create_network<T>(theparam, conf, thick, noutputs, "arch", this->_id); // 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")); mout << "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<T>*)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); } } DEBUGMEM_PRETTY("before detection"); // detector detector<T> detect(*net, sclasses, ans, NULL, NULL, mout, merr); init_detector(detect, conf, outdir, silent); // keep pointer to detector pdetect = &detect; bootstrapping<T> 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, mout, merr); 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); // gui #ifdef __GUI__ uint display_wmax = conf.try_get_uint("display_max_width", 3000); T display_min = (T) conf.try_get_double("display_min", -1.7); T display_max = (T) conf.try_get_double("display_max", 1.7); T display_in_max = (T) conf.try_get_double("display_in_max", 255); T display_in_min = (T) 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: "; title += _name; if (display) { wid = new_window(title.c_str()); mout << "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<T> 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"), (T) conf.try_get_double("mask_threshold", 0)); #endif // timing variables timer tpass, toverall; long ms; // loop toverall.start(); // we're ready bavailable = true; while(!this->_stop) { // wait until a new image is made available while (!in_updated && !_stop) { millisleep(1); } tpass.restart(); if (_stop) break ; // we got a new frame, reset new frame flag in_updated = false; // no need to lock mutex // check if this frame should be skipped if (boot.skip_frame(frame_name)) { skip_frame(); continue ; } else if (!frame_loaded) { uframe = load_image<ubyte>(frame_fullname); mout << "loaded image " << frame_fullname << std::endl; } if (!silent) mout << "processing " << frame_name << std::endl; // check frame is correctly allocated, if not, allocate. if (frame.order() != uframe.order()) frame = idx<T>(uframe.get_idxdim()); else if (frame.get_idxdim() != uframe.get_idxdim()) frame.resize(uframe.get_idxdim()); // copy frame idx_copy(uframe, frame); // run detector if (!display) { // fprop without display if (precomputed_boxes) { try { 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); } catch(eblexception &e) { #ifdef __NOEXCEPTIONS__ merr << "exception" << std::endl; #else merr << e << std::endl; #endif } } else { try { mout << "starting processing of frame " << frame_name << std::endl; bboxes &bb = detect.fprop(frame, frame_name.c_str(), frame_id); copy_bboxes(bb); // make a copy of bounding boxes } catch(ebl::eblexception &e) { // detection failed #ifdef __NOEXCEPTIONS__ eblwarn("detection failed"); #else eblwarn("detection failed: " << e); #endif clear_bboxes(); } } } #ifdef __GUI__ else { // fprop and display if (precomputed_boxes) eblerror("not implemented for nms only (TODO)"); disable_window_updates(); select_window(wid); clear_window(); std::string title = _name; title << ": " << frame_name; set_window_title(title.c_str()); // clear_resize_window(); try { if (mindisplay) { bboxes &bb = dgui.display(detect, frame, frame_name.c_str(), frame_id, 0, 0, zoom, display_min, display_max, wid, _name.c_str(), display_transp); copy_bboxes(bb); // make a copy of bounding boxes } else { // 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, _name.c_str(), display_in_min, display_in_max, display_transp, display_wmax); // make a copy of bounding boxes copy_bboxes(bb); } } catch(ebl::eblexception &e) { // detection failed eblwarn("detection failed: " << e); clear_bboxes(); } enable_window_updates(); } if (display_states) { dgui.display_current(detect, frame, wid_states, NULL, zoom); select_window(wid); } if (save_video && display) { std::string fname = viddir; fname += frame_name; save_window(fname.c_str()); if (!silent) mout << "saved " << fname << std::endl; } #endif if (!silent) mout << "processing done for frame " << frame_name << std::endl; // bootstrapping if (conf.exists_true("bootstrapping")) { boot.fprop(detect, frame_name); // add multiple scales if positives and scales are defined if (conf.exists("gt_scales") && boot.extract_positives()) { std::vector<double> scales = string_to_doublevector(conf.get_cstring("gt_scales")); for (uint s = 0; s < scales.size(); ++s) { double f = scales[s]; // downsample input by f detect.set_resolution(f); detect.init(frame.get_idxdim(), frame_name.c_str()); detect.fprop(frame, frame_name.c_str(), frame_id); boot.fprop(detect, frame_name, false, f); } detect.set_scaling_original(); detect.init(frame.get_idxdim(), frame_name.c_str()); } copy_bootstrapping(boot.get_all(), boot.get_bball()); #ifdef __GUI__ // display groundtruth if (conf.exists_true("display_bootstrapping")) dgui.display_groundtruth(detect, frame, boot.get_gtall(), boot.get_gtclean(), boot.get_gtrest(), boot.get_bbpos(), boot.get_bbneg(), boot.get_pos(), boot.get_neg(), 0, 0, zoom, display_min, display_max); #endif } total_saved = detect.get_total_saved(); ms = tpass.elapsed_milliseconds(); if (!silent) { mout << bbs.pretty_short(detect.get_labels()); mout << "processing=" << ms << " ms (" << tpass.elapsed() << ")" << std::endl; } DEBUGMEM_PRETTY("after detection"); // switch 'updated' flag on to warn we just added new data set_out_updated(); // display sleep if (display_sleep > 0) { mout << "sleeping for " << display_sleep << "ms." << std::endl; millisleep(display_sleep); } if (conf.exists("save_max") && detect.get_total_saved() > conf.get_uint("save_max")) break ; // limit number of detection saves } mout << "detection finished. Execution time: " << toverall.elapsed()<<std::endl; // free variables if (net) delete net; if (ans) delete ans; } eblcatcherror(); }
int main(int argc, char** argv) { //This test is an attempt to test linearization //of eb data holders directly int eekflag=0; #ifdef CH_MPI MPI_Init(&argc, &argv); #endif //begin forever present scoping trick { //registerDebugger(); // Set up some geometry. Box domain; Real dx; do_geo( domain, dx ); Vector<Box> boxes(1, domain); Vector<int> procs(1, 0); // Make a layout for the space. DisjointBoxLayout dbl(boxes, procs); // Fill in the layout with the geometrical info. EBISLayout ebisl; EBIndexSpace *ebisPtr = Chombo_EBIS::instance(); ebisPtr->fillEBISLayout(ebisl, dbl, domain, 1 ); // Define the leveldata for my one and only level. DataIterator dit = dbl.dataIterator(); for ( dit.begin(); dit.ok(); ++dit ) { eekflag = testIVFAB(ebisl[dit()], dbl.get(dit())); if (eekflag != 0) { pout() << "IVFAB linearization test failed " << endl; return eekflag; } eekflag = testIFFAB(ebisl[dit()], dbl.get(dit())); if (eekflag != 0) { pout() << "IFFAB linearization test failed " << endl; return eekflag; } eekflag = testEBCellFAB(ebisl[dit()], dbl.get(dit())); if (eekflag != 0) { pout() << "EBCellFAB linearization test failed " << endl; return eekflag; } eekflag = testEBFaceFAB(ebisl[dit()], dbl.get(dit())); if (eekflag != 0) { pout() << "EBFaceFAB linearization test failed " << endl; return eekflag; } eekflag = testEBFluxFAB(ebisl[dit()], dbl.get(dit())); if (eekflag != 0) { pout() << "EBFluxFAB linearization test failed " << endl; return eekflag; } } ebisPtr->clear(); }//end scoping trick pout() << "linearization tests passed "<< endl; #ifdef CH_MPI MPI_Finalize(); #endif return eekflag; }
for (auto&& yi : c_cheb) { *ri *= unit_polar(_M_ * (K.phase(t_center, yi) - K.phase(p_center, yi))); ++ri; } // Multiply M_AB_i += LgM_ij rhs_j prod(LgM, rhs, M_AB); } } }; auto M2L = [&](int L) { // M2L and check the result int s_idx = -1; for (source_box_type sbox : boxes(L_max - L, source_tree)) { ++s_idx; const point_type& s_center = sbox.center(); const point_type& s_extent = sbox.extents(); // Define the source box Chebyshev grid std::array<point_type,pow_(Q,D)> s_cheb; { auto si = s_cheb.begin(); for (auto&& i : TensorIndexGridRange<D,Q>()) { for (unsigned d = 0; d < D; ++d) (*si)[d] = s_center[d] + Chebyshev<double,Q>::x[i[d]] * s_extent[d]; ++si; }
void MGRadBndry::setBndryConds(const BCRec& bc, const Geometry& geom, IntVect& ratio) { // NOTE: ALL BCLOC VALUES ARE NOW DEFINED AS A LENGTH IN PHYSICAL DIMENSIONS // *RELATIVE* TO THE FACE, NOT IN ABSOLUTE PHYSICAL SPACE const BoxArray& grids = boxes(); int ngrds = grids.size(); const Real* dx = geom.CellSize(); const Box& domain = geom.Domain(); for (OrientationIter fi; fi; ++fi) { Orientation face(fi()); Array<Real> &bloc = bcloc[face]; Array<RadBoundCond> &bctag = bcond[face]; int dir = face.coordDir(); Real delta = dx[dir]*ratio[dir]; int p_bc = (face.isLow() ? bc.lo(dir) : bc.hi(dir)); for (int i = 0; i < ngrds; i++) { const Box& grd = grids[i]; if (domain[face] == grd[face] && !geom.isPeriodic(dir)) { /* // All physical bc values are located on face if (p_bc == EXT_DIR) { bctag[i] = LO_DIRICHLET; bloc[i] = 0.; } else if (p_bc == EXTRAP || p_bc == HOEXTRAP || p_bc == REFLECT_EVEN) { bctag[i] = LO_NEUMANN; bloc[i] = 0.; } else if (p_bc == REFLECT_ODD) { bctag[i] = LO_REFLECT_ODD; bloc[i] = 0.; } */ if (p_bc == LO_DIRICHLET || p_bc == LO_NEUMANN || p_bc == LO_REFLECT_ODD) { bctag[i] = p_bc; bloc[i] = 0.; } else if (p_bc == LO_MARSHAK || p_bc == LO_SANCHEZ_POMRANING) { bctag[i] = p_bc; //gives asymmetric, second-order version of Marshak b.c. // (worked for bbmg, works with nonsymmetric hypre solvers): bloc[i] = 0.; //gives symmetric version of Marshak b.c. //(hypre symmetric solvers ignore bloc and do this automatically): //bloc[i] = -0.5 * dx[dir]; } else { cerr << "MGRadBndry---Not a recognized boundary condition" << endl; exit(1); } } else { // internal bndry bctag[i] = LO_DIRICHLET; bloc[i] = 0.5*delta; } } } }
void IntVectSet::printBoxes(std::ostream& a_ostrm) const { Vector<Box> b = boxes(); for (int i=0; i<b.size(); ++i) a_ostrm<<b[i]<<std::endl; }
void contractor_parallel_all::prune(box & b, SMTConfig & config) { DREAL_LOG_DEBUG << "contractor_parallel_all::prune"; DREAL_LOG_FATAL << "-------------------------------------------------------------"; // TODO(soonhok): implement this if (m_vec.size() == 0) { // Do nothing for empty vec return; } // 1. Make n copies of box b vector<box> boxes(m_vec.size(), b); vector<pruning_thread_status> statuses(m_vec.size(), pruning_thread_status::READY); m_index = -1; // DREAL_LOG_FATAL << "parallel: Boxes are copied"; // 2. Trigger execution with each contractor and a copied box vector<interruptible_thread> threads; atomic_int tasks_to_run(m_vec.size()); // DREAL_LOG_FATAL << "parallel: tasks to run = " << tasks_to_run.load(); for (unsigned i = 0; i < m_vec.size(); ++i) { DREAL_LOG_FATAL << "parallel : thread " << i << " / " << (tasks_to_run.load() - 1) << " spawning..."; threads.emplace_back(parallel_helper_fn, i, m_vec[i], boxes[i], config, statuses[i], m_mutex, m_cv, m_index, tasks_to_run); DREAL_LOG_FATAL << "parallel : thread " << i << " / " << (tasks_to_run.load() - 1) << " spawned..."; } DREAL_LOG_FATAL << "parallel : " << m_vec.size() << " thread(s) got created"; while (true) { DREAL_LOG_FATAL << "parallel: waiting for the lock"; unique_lock<mutex> lk(m_mutex); DREAL_LOG_FATAL << "parallel: get a lock. " << tasks_to_run.load() << " tasks to go"; if (tasks_to_run.load() == 0) { break; } DREAL_LOG_FATAL << "parallel: WAIT for CV." << tasks_to_run.load() << " tasks to go";; m_index = -1; m_cv.wait(lk, [&]() { return m_index != -1; }); DREAL_LOG_FATAL << "parallel: wake up" << tasks_to_run.load(); pruning_thread_status const & s = statuses[m_index]; // DREAL_LOG_FATAL << "parallel: thread " << m_index << " " << s; if (s == pruning_thread_status::UNSAT || s == pruning_thread_status::EXCEPTION) { // Interrupt all the rest threads for (unsigned i = 0; i < statuses.size(); i++) { if (i - m_index != 0 && (statuses[i] == pruning_thread_status::READY || statuses[i] == pruning_thread_status::RUNNING)) { threads[i].interrupt(); } } if (s == pruning_thread_status::UNSAT) { DREAL_LOG_FATAL << "parallel: " << m_index << " got UNSAT"; b.set_empty(); m_input.union_with(m_vec[m_index].input()); m_output.union_with(m_vec[m_index].output()); unordered_set<shared_ptr<constraint>> const & used_ctrs = m_vec[m_index].used_constraints(); m_used_constraints.insert(used_ctrs.begin(), used_ctrs.end()); lk.unlock(); for (unsigned i = 0; i < m_vec.size(); i++) { threads[i].join(); } DREAL_LOG_FATAL << "parallel: return UNSAT"; return; } if (s == pruning_thread_status::EXCEPTION) { DREAL_LOG_FATAL << "parallel: " << m_index << " got EXCEPTION"; lk.unlock(); for (unsigned i = 0; i < m_vec.size(); i++) { threads[i].join(); } DREAL_LOG_FATAL << "parallel: throw exception"; throw contractor_exception("exception during parallel contraction"); } } else { // if (s != pruning_thread_status::SAT) { // // DREAL_LOG_FATAL << "parallel: " << m_index << " got " << s; // // DREAL_LOG_FATAL << "parallel: " << m_index << " got " << statuses[m_index]; assert(s == pruning_thread_status::SAT); // } // if (threads[m_index].joinable()) { // threads[m_index].join(); // } // DREAL_LOG_FATAL << "parallel: " << m_index << " got SAT"; // Why? // - Not READY/RUNNING: It's a job already done. // - Not UNSAT/EXCEPTION: already handled above. // - Not KILLED: There must be one which kill the killed // job, and this loop stops after handling // the first one } } // Assertion: All of them got SAT // for (pruning_thread_status const & s : statuses) { // assert(s == pruning_thread_status::SAT); // } // DREAL_LOG_FATAL << "All of them are SAT"; b = boxes[0]; for (unsigned i = 0; i < m_vec.size(); i++) { contractor const & c = m_vec[i]; b.intersect(boxes[i]); m_input.union_with(c.input()); m_output.union_with(c.output()); unordered_set<shared_ptr<constraint>> const & used_ctrs = c.used_constraints(); m_used_constraints.insert(used_ctrs.begin(), used_ctrs.end()); if (b.is_empty()) { // DREAL_LOG_FATAL << "Found an empty while intersecting..."; for (unsigned i = 0; i < m_vec.size(); i++) { // if (threads[i].joinable()) { // DREAL_LOG_FATAL << "Try to join " << i << "..."; threads[i].join(); // DREAL_LOG_FATAL << "Try to join " << i << "... done"; // } } // DREAL_LOG_FATAL << "parallel: return UNSAT"; return; } } // DREAL_LOG_FATAL << "Intersection is nonempty exiting..."; for (unsigned i = 0; i < m_vec.size(); i++) { // if (threads[i].joinable()) { threads[i].join(); // } } // DREAL_LOG_FATAL << "parallel: return SAT"; return; }
void Physics::SetUpTutorial1() { //Add a plane PxTransform pose = PxTransform(PxVec3(0.0f, -50, 0.0f), PxQuat(PxHalfPi*1.0f, PxVec3(0.0f, 0.0f, 1.0f))); PxRigidStatic* plane = PxCreateStatic(*g_Physics, pose, PxPlaneGeometry(), *g_PhysicsMaterial); //Add it to the physx scene g_PhysicsScene->addActor(*plane); PxArticulation* ragDollArticulation; ragDollArticulation = makeRagdoll(g_Physics, ragdollData, PxTransform(PxVec3(0, 0, 0)), 0.1f, g_PhysicsMaterial); g_PhysicsScene->addArticulation(*ragDollArticulation); PxBoxGeometry box(1, 1, 10); for (int j = -25; j < 0; j++) { PxTransform transform(PxVec3((j*-2) - 4, (j*2)-9, 8)); PxRigidStatic* staticActor = PxCreateStatic(*g_Physics, transform, box, *g_PhysicsMaterial); //add it to the physx scene g_PhysicsScene->addActor(*staticActor); } for (int j = -25; j < 0; j++) { PxTransform transform(PxVec3((j*-2) - 3, (j * 2) - 10, 8)); PxRigidStatic* staticActor = PxCreateStatic(*g_Physics, transform, box, *g_PhysicsMaterial); //add it to the physx scene g_PhysicsScene->addActor(*staticActor); } PxBoxGeometry boxS(10, 1, 1); for (int j = -25; j < 0; j++) { PxTransform transform(PxVec3(8, (j * 2) - 9, (j*-2) - 4)); PxRigidStatic* staticActor = PxCreateStatic(*g_Physics, transform, boxS, *g_PhysicsMaterial); //add it to the physx scene g_PhysicsScene->addActor(*staticActor); } for (int j = -25; j < 0; j++) { PxTransform transform(PxVec3(8, (j * 2) - 10, (j*-2) - 3)); PxRigidStatic* staticActor = PxCreateStatic(*g_Physics, transform, boxS, *g_PhysicsMaterial); //add it to the physx scene g_PhysicsScene->addActor(*staticActor); } float density = 50; PxBoxGeometry boxes(1, 1, 1); for (int i = 0; i < 2; i++) { for (int j = 30; j < 50; j++) { for (int k = 0; k < 2; k++) { PxTransform transform(PxVec3(2*i, 2*j+1, 2*k)); PxRigidDynamic* dynamicActor = PxCreateDynamic(*g_Physics, transform, boxes, *g_PhysicsMaterial, density); //add it to the physx scene g_PhysicsScene->addActor(*dynamicActor); //dynamicActor->putToSleep(); } } } }
int spheroidsToSTL(const string& out, const shared_ptr<DemField>& dem, Real tol, const string& solid, int mask, bool append, bool clipCell, bool merge){ if(tol==0 || isnan(tol)) throw std::runtime_error("tol must be non-zero."); #ifndef WOO_GTS if(merge) throw std::runtime_error("woo.triangulated.spheroidsToSTL: merge=True only possible in builds with the 'gts' feature."); #endif // first traversal to find reference radius auto particleOk=[&](const shared_ptr<Particle>&p){ return (mask==0 || (p->mask & mask)) && (p->shape->isA<Sphere>() || p->shape->isA<Ellipsoid>() || p->shape->isA<Capsule>()); }; int numTri=0; if(tol<0){ LOG_DEBUG("tolerance is negative, taken as relative to minimum radius."); Real minRad=Inf; for(const auto& p: *dem->particles){ if(particleOk(p)) minRad=min(minRad,p->shape->equivRadius()); } if(isinf(minRad) || isnan(minRad)) throw std::runtime_error("Minimum radius not found (relative tolerance specified); no matching particles?"); tol=-minRad*tol; LOG_DEBUG("Minimum radius "<<minRad<<"."); } LOG_DEBUG("Triangulation tolerance is "<<tol); std::ofstream stl(out,append?(std::ofstream::app|std::ofstream::binary):std::ofstream::binary); // binary better, anyway if(!stl.good()) throw std::runtime_error("Failed to open output file "+out+" for writing."); Scene* scene=dem->scene; if(!scene) throw std::logic_error("DEM field has not associated scene?"); // periodicity, cache that for later use AlignedBox3r cell; /* wasteful memory-wise, but we need to store the whole triangulation in case *merge* is in effect, when it is only an intermediary result and will not be output as-is */ vector<vector<Vector3r>> ppts; vector<vector<Vector3i>> ttri; vector<Particle::id_t> iid; for(const auto& p: *dem->particles){ if(!particleOk(p)) continue; const auto sphere=dynamic_cast<Sphere*>(p->shape.get()); const auto ellipsoid=dynamic_cast<Ellipsoid*>(p->shape.get()); const auto capsule=dynamic_cast<Capsule*>(p->shape.get()); vector<Vector3r> pts; vector<Vector3i> tri; if(sphere || ellipsoid){ Real r=sphere?sphere->radius:ellipsoid->semiAxes.minCoeff(); // 1 is for icosahedron int tess=ceil(M_PI/(5*acos(1-tol/r))); LOG_DEBUG("Tesselation level for #"<<p->id<<": "<<tess); tess=max(tess,0); auto uSphTri(CompUtils::unitSphereTri20(/*0 for icosahedron*/max(tess-1,0))); const auto& uPts=std::get<0>(uSphTri); // unit sphere point coords pts.resize(uPts.size()); const auto& node=(p->shape->nodes[0]); Vector3r scale=(sphere?sphere->radius*Vector3r::Ones():ellipsoid->semiAxes); for(size_t i=0; i<uPts.size(); i++){ pts[i]=node->loc2glob(uPts[i].cwiseProduct(scale)); } tri=std::get<1>(uSphTri); // this makes a copy, but we need out own for capsules } if(capsule){ #ifdef WOO_VTK int subdiv=max(4.,ceil(M_PI/(acos(1-tol/capsule->radius)))); std::tie(pts,tri)=VtkExport::triangulateCapsule(static_pointer_cast<Capsule>(p->shape),subdiv); #else throw std::runtime_error("Triangulation of capsules is (for internal and entirely fixable reasons) only available when compiled with the 'vtk' features."); #endif } // do not write out directly, store first for later ppts.push_back(pts); ttri.push_back(tri); LOG_TRACE("#"<<p->id<<" triangulated: "<<tri.size()<<","<<pts.size()<<" faces,vertices."); if(scene->isPeriodic){ // make sure we have aabb, in skewed coords and such if(!p->shape->bound){ // this is a bit ugly, but should do the trick; otherwise we would recompute all that ourselves here if(sphere) Bo1_Sphere_Aabb().go(p->shape); else if(ellipsoid) Bo1_Ellipsoid_Aabb().go(p->shape); else if(capsule) Bo1_Capsule_Aabb().go(p->shape); } assert(p->shape->bound); const AlignedBox3r& box(p->shape->bound->box); AlignedBox3r cell(Vector3r::Zero(),scene->cell->getSize()); // possibly in skewed coords // central offset Vector3i off0; scene->cell->canonicalizePt(p->shape->nodes[0]->pos,off0); // computes off0 Vector3i off; // offset from the original cell //cerr<<"#"<<p->id<<" at "<<p->shape->nodes[0]->pos.transpose()<<", off0="<<off0<<endl; for(off[0]=off0[0]-1; off[0]<=off0[0]+1; off[0]++) for(off[1]=off0[1]-1; off[1]<=off0[1]+1; off[1]++) for(off[2]=off0[2]-1; off[2]<=off0[2]+1; off[2]++){ Vector3r dx=scene->cell->intrShiftPos(off); //cerr<<" off="<<off.transpose()<<", dx="<<dx.transpose()<<endl; AlignedBox3r boxOff(box); boxOff.translate(dx); //cerr<<" boxOff="<<boxOff.min()<<";"<<boxOff.max()<<" | cell="<<cell.min()<<";"<<cell.max()<<endl; if(boxOff.intersection(cell).isEmpty()) continue; // copy the entire triangulation, offset by dx vector<Vector3r> pts2(pts); for(auto& p: pts2) p+=dx; vector<Vector3i> tri2(tri); // same topology ppts.push_back(pts2); ttri.push_back(tri2); LOG_TRACE(" offset "<<off.transpose()<<": #"<<p->id<<": "<<tri2.size()<<","<<pts2.size()<<" faces,vertices."); } } } if(!merge){ LOG_DEBUG("Will export (unmerged) "<<ppts.size()<<" particles to STL."); stl<<"solid "<<solid<<"\n"; for(size_t i=0; i<ppts.size(); i++){ const auto& pts(ppts[i]); const auto& tri(ttri[i]); LOG_TRACE("Exporting "<<i<<" with "<<tri.size()<<" faces."); for(const Vector3i& t: tri){ Vector3r pp[]={pts[t[0]],pts[t[1]],pts[t[2]]}; // skip triangles which are entirely out of the canonical periodic cell if(scene->isPeriodic && clipCell && (!scene->cell->isCanonical(pp[0]) && !scene->cell->isCanonical(pp[1]) && !scene->cell->isCanonical(pp[2]))) continue; numTri++; Vector3r n=(pp[1]-pp[0]).cross(pp[2]-pp[1]).normalized(); stl<<" facet normal "<<n.x()<<" "<<n.y()<<" "<<n.z()<<"\n"; stl<<" outer loop\n"; for(auto p: {pp[0],pp[1],pp[2]}){ stl<<" vertex "<<p[0]<<" "<<p[1]<<" "<<p[2]<<"\n"; } stl<<" endloop\n"; stl<<" endfacet\n"; } } stl<<"endsolid "<<solid<<"\n"; stl.close(); return numTri; } #if WOO_GTS /***** Convert all triangulation to GTS surfaces, find their distances, isolate connected components, merge these components incrementally and write to STL *****/ // total number of points const size_t N(ppts.size()); // bounds for collision detection struct Bound{ Bound(Real _coord, int _id, bool _isMin): coord(_coord), id(_id), isMin(_isMin){}; Bound(): coord(NaN), id(-1), isMin(false){}; // just for allocation Real coord; int id; bool isMin; bool operator<(const Bound& b) const { return coord<b.coord; } }; vector<Bound> bounds[3]={vector<Bound>(2*N),vector<Bound>(2*N),vector<Bound>(2*N)}; /* construct GTS surface objects; all objects must be deleted explicitly! */ vector<GtsSurface*> ssurf(N); vector<vector<GtsVertex*>> vvert(N); vector<vector<GtsEdge*>> eedge(N); vector<AlignedBox3r> boxes(N); for(size_t i=0; i<N; i++){ LOG_TRACE("** Creating GTS surface for #"<<i<<", with "<<ttri[i].size()<<" faces, "<<ppts[i].size()<<" vertices."); AlignedBox3r box; // new surface object ssurf[i]=gts_surface_new(gts_surface_class(),gts_face_class(),gts_edge_class(),gts_vertex_class()); // copy over all vertices vvert[i].reserve(ppts[i].size()); eedge[i].reserve(size_t(1.5*ttri[i].size())); // each triangle consumes 1.5 edges, for closed surfs for(size_t v=0; v<ppts[i].size(); v++){ vvert[i].push_back(gts_vertex_new(gts_vertex_class(),ppts[i][v][0],ppts[i][v][1],ppts[i][v][2])); box.extend(ppts[i][v]); } // create faces, and create edges on the fly as needed std::map<std::pair<int,int>,int> edgeIndices; for(size_t t=0; t<ttri[i].size(); t++){ //const Vector3i& t(ttri[i][t]); //LOG_TRACE("Face with vertices "<<ttri[i][t][0]<<","<<ttri[i][t][1]<<","<<ttri[i][t][2]); Vector3i eIxs; for(int a:{0,1,2}){ int A(ttri[i][t][a]), B(ttri[i][t][(a+1)%3]); auto AB=std::make_pair(min(A,B),max(A,B)); auto ABI=edgeIndices.find(AB); if(ABI==edgeIndices.end()){ // this edge not created yet edgeIndices[AB]=eedge[i].size(); // last index eIxs[a]=eedge[i].size(); //LOG_TRACE(" New edge #"<<eIxs[a]<<": "<<A<<"--"<<B<<" (length "<<(ppts[i][A]-ppts[i][B]).norm()<<")"); eedge[i].push_back(gts_edge_new(gts_edge_class(),vvert[i][A],vvert[i][B])); } else { eIxs[a]=ABI->second; //LOG_TRACE(" Found edge #"<<ABI->second<<" for "<<A<<"--"<<B); } } //LOG_TRACE(" New face: edges "<<eIxs[0]<<"--"<<eIxs[1]<<"--"<<eIxs[2]); GtsFace* face=gts_face_new(gts_face_class(),eedge[i][eIxs[0]],eedge[i][eIxs[1]],eedge[i][eIxs[2]]); gts_surface_add_face(ssurf[i],face); } // make sure the surface is OK if(!gts_surface_is_orientable(ssurf[i])) LOG_ERROR("Surface of #"+to_string(iid[i])+" is not orientable (expect troubles)."); if(!gts_surface_is_closed(ssurf[i])) LOG_ERROR("Surface of #"+to_string(iid[i])+" is not closed (expect troubles)."); assert(!gts_surface_is_self_intersecting(ssurf[i])); // copy bounds LOG_TRACE("Setting bounds of surf #"<<i); boxes[i]=box; for(int ax:{0,1,2}){ bounds[ax][2*i+0]=Bound(box.min()[ax],/*id*/i,/*isMin*/true); bounds[ax][2*i+1]=Bound(box.max()[ax],/*id*/i,/*isMin*/false); } } /* broad-phase collision detection between GTS surfaces only those will be probed with exact algorithms below and merged if needed */ for(int ax:{0,1,2}) std::sort(bounds[ax].begin(),bounds[ax].end()); vector<Bound>& bb(bounds[0]); // run the search along x-axis, does not matter really std::list<std::pair<int,int>> int0; // broad-phase intersections for(size_t i=0; i<2*N; i++){ if(!bb[i].isMin) continue; // only start with lower bound // go up to the upper bound, but handle overflow safely (no idea why it would happen here) as well for(size_t j=i+1; j<2*N && bb[j].id!=bb[i].id; j++){ if(bb[j].isMin) continue; // this is handled by symmetry #if EIGEN_VERSION_AT_LEAST(3,2,5) if(!boxes[bb[i].id].intersects(boxes[bb[j].id])) continue; // no intersection along all axes #else // old, less elegant if(boxes[bb[i].id].intersection(boxes[bb[j].id]).isEmpty()) continue; #endif int0.push_back(std::make_pair(min(bb[i].id,bb[j].id),max(bb[i].id,bb[j].id))); LOG_TRACE("Broad-phase collision "<<int0.back().first<<"+"<<int0.back().second); } } /* narrow-phase collision detection between GTS surface this must be done via gts_surface_inter_new, since gts_surface_distance always succeeds */ std::list<std::pair<int,int>> int1; for(const std::pair<int,int> ij: int0){ LOG_TRACE("Testing narrow-phase collision "<<ij.first<<"+"<<ij.second); #if 0 GtsRange gr1, gr2; gts_surface_distance(ssurf[ij.first],ssurf[ij.second],/*delta ??*/(gfloat).2,&gr1,&gr2); if(gr1.min>0 && gr2.min>0) continue; LOG_TRACE(" GTS reports collision "<<ij.first<<"+"<<ij.second<<" (min. distances "<<gr1.min<<", "<<gr2.min); #else GtsSurface *s1(ssurf[ij.first]), *s2(ssurf[ij.second]); GNode* t1=gts_bb_tree_surface(s1); GNode* t2=gts_bb_tree_surface(s2); GtsSurfaceInter* I=gts_surface_inter_new(gts_surface_inter_class(),s1,s2,t1,t2,/*is_open_1*/false,/*is_open_2*/false); GSList* l=gts_surface_intersection(s1,s2,t1,t2); // list of edges describing intersection int n1=g_slist_length(l); // extra check by looking at number of faces of the intersected surface #if 1 GtsSurface* s12=gts_surface_new(gts_surface_class(),gts_face_class(),gts_edge_class(),gts_vertex_class()); gts_surface_inter_boolean(I,s12,GTS_1_OUT_2); gts_surface_inter_boolean(I,s12,GTS_2_OUT_1); int n2=gts_surface_face_number(s12); gts_object_destroy(GTS_OBJECT(s12)); #endif gts_bb_tree_destroy(t1,TRUE); gts_bb_tree_destroy(t2,TRUE); gts_object_destroy(GTS_OBJECT(I)); g_slist_free(l); if(n1==0) continue; #if 1 if(n2==0){ LOG_ERROR("n1==0 but n2=="<<n2<<" (no narrow-phase collision)"); continue; } #endif LOG_TRACE(" GTS reports collision "<<ij.first<<"+"<<ij.second<<" ("<<n<<" edges describe the intersection)"); #endif int1.push_back(ij); } /* connected components on the graph: graph nodes are 0…(N-1), graph edges are in int1 see http://stackoverflow.com/a/37195784/761090 */ typedef boost::subgraph<boost::adjacency_list<boost::vecS,boost::vecS,boost::undirectedS,boost::property<boost::vertex_index_t,int>,boost::property<boost::edge_index_t,int>>> Graph; Graph graph(N); for(const auto& ij: int1) boost::add_edge(ij.first,ij.second,graph); vector<size_t> clusters(boost::num_vertices(graph)); size_t numClusters=boost::connected_components(graph,clusters.data()); for(size_t n=0; n<numClusters; n++){ // beginning cluster #n // first, count how many surfaces are in this cluster; if 1, things are easier int numThisCluster=0; int cluster1st=-1; for(size_t i=0; i<N; i++){ if(clusters[i]!=n) continue; numThisCluster++; if(cluster1st<0) cluster1st=(int)i; } GtsSurface* clusterSurf=NULL; LOG_DEBUG("Cluster "<<n<<" has "<<numThisCluster<<" surfaces."); if(numThisCluster==1){ clusterSurf=ssurf[cluster1st]; } else { clusterSurf=ssurf[cluster1st]; // surface of the cluster itself LOG_TRACE(" Initial cluster surface from "<<cluster1st<<"."); /* composed surface */ for(size_t i=0; i<N; i++){ if(clusters[i]!=n || ((int)i)==cluster1st) continue; LOG_TRACE(" Adding "<<i<<" to the cluster"); // ssurf[i] now belongs to cluster #n // trees need to be rebuild every time anyway, since the merged surface keeps changing in every cycle //if(gts_surface_face_number(clusterSurf)==0) LOG_ERROR("clusterSurf has 0 faces."); //if(gts_surface_face_number(ssurf[i])==0) LOG_ERROR("Surface #"<<i<<" has 0 faces."); GNode* t1=gts_bb_tree_surface(clusterSurf); GNode* t2=gts_bb_tree_surface(ssurf[i]); GtsSurfaceInter* I=gts_surface_inter_new(gts_surface_inter_class(),clusterSurf,ssurf[i],t1,t2,/*is_open_1*/false,/*is_open_2*/false); GtsSurface* merged=gts_surface_new(gts_surface_class(),gts_face_class(),gts_edge_class(),gts_vertex_class()); gts_surface_inter_boolean(I,merged,GTS_1_OUT_2); gts_surface_inter_boolean(I,merged,GTS_2_OUT_1); gts_object_destroy(GTS_OBJECT(I)); gts_bb_tree_destroy(t1,TRUE); gts_bb_tree_destroy(t2,TRUE); if(gts_surface_face_number(merged)==0){ LOG_ERROR("Cluster #"<<n<<": 0 faces after fusing #"<<i<<" (why?), adding #"<<i<<" separately!"); // this will cause an extra 1-particle cluster to be created clusters[i]=numClusters; numClusters+=1; } else { // not from global vectors (cleanup at the end), explicit delete! if(clusterSurf!=ssurf[cluster1st]) gts_object_destroy(GTS_OBJECT(clusterSurf)); clusterSurf=merged; } } } #if 0 LOG_TRACE(" GTS surface cleanups..."); pygts_vertex_cleanup(clusterSurf,.1*tol); // cleanup 10× smaller than tolerance pygts_edge_cleanup(clusterSurf); pygts_face_cleanup(clusterSurf); #endif LOG_TRACE(" STL: cluster "<<n<<" output"); stl<<"solid "<<solid<<"_"<<n<<"\n"; /* output cluster to STL here */ _gts_face_to_stl_data data(stl,scene,clipCell,numTri); gts_surface_foreach_face(clusterSurf,(GtsFunc)_gts_face_to_stl,(gpointer)&data); stl<<"endsolid\n"; if(clusterSurf!=ssurf[cluster1st]) gts_object_destroy(GTS_OBJECT(clusterSurf)); } // this deallocates also edges and vertices for(size_t i=0; i<ssurf.size(); i++) gts_object_destroy(GTS_OBJECT(ssurf[i])); return numTri; #endif /* WOO_GTS */ }
// Get potential bounding boxes for all test images void Objectness::getObjBndBoxesForTestsFast(vector<vector<Vec4i> > &_boxesTests, int numDetPerSize, bool preloadModel, bool preloadImages) { //setColorSpace(HSV); if (!preloadModel) trainObjectness(numDetPerSize); loadTrainedModel(); illustrate(); const int TestNum = _voc.testSet.size(); vecM imgs3u; if (preloadImages) imgs3u.resize(TestNum); vector<ValStructVec<float, Vec4i> > boxesTests; boxesTests.resize(TestNum); //Reading all images beforehand needs a lot of memory. //The timing results are affected based on when the image is loaded. if (preloadImages) { #pragma omp parallel for for (int i = 0; i < TestNum; i++) { imgs3u[i] = imread(format(_S(_voc.imgPathW), _S(_voc.testSet[i]))); boxesTests[i].reserve(10000); } } printf("Start predicting\n"); CmTimer tm("Predict"); tm.Start(); #pragma omp parallel for for (int i = 0; i < TestNum; i++) { if (!preloadImages) { Mat img = imread(format(_S(_voc.imgPathW), _S(_voc.testSet[i]))); getObjBndBoxes(img, boxesTests[i], numDetPerSize); } else { getObjBndBoxes(imgs3u[i], boxesTests[i], numDetPerSize); } } tm.Stop(); if (!preloadImages) { printf("Average time for loading and predicting an image (%s) is %gs\n", _clrName[_Clr], tm.TimeInSeconds()/TestNum); } else { printf("Average time for predicting an image (%s) is %gs\n", _clrName[_Clr], tm.TimeInSeconds()/TestNum); } _boxesTests.resize(TestNum); CmFile::MkDir(_bbResDir); #pragma omp parallel for for (int i = 0; i < TestNum; i++) { CStr fName = _bbResDir + _voc.testSet[i]; ValStructVec<float, Vec4i> &boxes = boxesTests[i]; FILE *f = fopen(_S(fName + ".txt"), "w"); fprintf(f, "%d\n", boxes.size()); for (size_t k = 0; k < boxes.size(); k++) fprintf(f, "%g, %s\n", boxes(k), _S(strVec4i(boxes[k]))); fclose(f); _boxesTests[i].resize(boxesTests[i].size()); for (int j = 0; j < boxesTests[i].size(); j++) _boxesTests[i][j] = boxesTests[i][j]; } evaluatePerImgRecall(_boxesTests, "PerImgAll.m", 5000); }
void MGRadBndry::setBndryFluxConds(const BCRec& bc, const BC_Mode phys_bc_mode) { const BoxArray& grids = boxes(); // int ngrds = grids.size(); const Real* dx = geom.CellSize(); const Real* xlo = geom.ProbLo(); const Box& domain = geom.Domain(); // variables for multigroup Array<Real> value_nu; value_nu.resize(ngroups); for (OrientationIter fi; fi; ++fi) { Orientation face(fi()); // Array<Real> &bloc = bcloc[face]; // Array<RadBoundCond> &bctag = bcond[face]; int dir = face.coordDir(); int p_bc = (face.isLow() ? bc.lo(dir) : bc.hi(dir)); int p_bcflag = (phys_bc_mode == Inhomogeneous_BC && !correction) ? bcflag[face] : 0; if(phys_bc_mode == Inhomogeneous_BC && !correction) { for(int igroup = 0; igroup < ngroups; igroup++) { value_nu[igroup] = bcval[face][igroup]; } } else { for(int igroup = 0; igroup < ngroups; igroup++) { value_nu[igroup] = 0.0; } } for (FabSetIter bi(bndry[face]); bi.isValid(); ++bi) { int i = bi.index(); const Box& grd = grids[i]; if (domain[face] == grd[face] && !geom.isPeriodic(dir)) { if (bcflag[face] <= 1) { if (p_bc == LO_MARSHAK || p_bc == LO_SANCHEZ_POMRANING || p_bc == LO_DIRICHLET || p_bc == LO_NEUMANN) { if (p_bcflag == 0) { for(int igroup = 0; igroup < ngroups; igroup++) { bndry[face][bi].setVal(value_nu[igroup], igroup); } } else { Fab& bnd_fab = bndry[face][bi]; const Box& bnd_box = bnd_fab.box(); int iface = face.isLow() ? 0 : 1; FORT_RADBNDRY(bnd_fab.dataPtr(), dimlist(bnd_box), dimlist(domain), dx, xlo, time, dir, iface); } } } else { if(ParallelDescriptor::IOProcessor()) { cout << "MGRadBndry ERROR 2: feature not implemented" << endl; } exit(2); #if 0 Fab& bnd_fab = bndry[face][bi]; const Box& bnd_box = bnd_fab.box(); BaseFab<int>& tfab = bctypearray[face][i]; FORT_RADBNDRY2(bnd_fab.dataPtr(), dimlist(bnd_box), tfab.dataPtr(), dimlist(domain), dx, xlo, time); #endif if (p_bcflag == 0) { // Homogeneous case. We called RADBNDRY2 only to set tfab right. //setValue(face, i, value); } } } } } }
bool OCRTess::detectAndRecog() { UMat grey = UMat::zeros(this->img.rows + 2, this->img.cols + 2, CV_8UC1); cvtColor(this->img.clone(), grey, COLOR_RGB2GRAY); vector<UMat> channels; channels.clear(); channels.push_back(grey); Mat m = 255 - grey.getMat(ACCESS_READ | ACCESS_WRITE); channels.push_back(m.getUMat(ACCESS_READ)); vector<vector<ERStat>> regions(2); regions[0].clear(); regions[1].clear(); switch (this->REGION) { case REG_CSER: { parallel_for_(Range(0, (int) channels.size()), Parallel_extractCSER(channels, regions, this->erf1, this->erf2)); break; } case REG_MSER: { vector<vector<Point> > contours; vector<Rect> bboxes; Ptr<MSER> mser = MSER::create(21, (int) (0.00002 * grey.cols * grey.rows), (int) (0.05 * grey.cols * grey.rows), 1, 0.7); mser->detectRegions(grey, contours, bboxes); if (contours.size() > 0) MSERsToERStats(grey, contours, regions); break; } default: { break; } } /*Text Recognition (OCR)*/ vector<vector<Vec2i> > nm_region_groups; vector<Rect> nm_boxes; switch (this->GROUP) { case 0: erGrouping(this->img, channels, regions, nm_region_groups, nm_boxes, ERGROUPING_ORIENTATION_HORIZ); break; case 1: default: erGrouping(this->img, channels, regions, nm_region_groups, nm_boxes, ERGROUPING_ORIENTATION_ANY, DIR + TR_GRP, 0.5); break; } if (!nm_boxes.size() || nm_boxes.size() > 1) return false; vector<string> words_detection; float min_confidence1 = 51.f, min_confidence2 = 60.f; vector<UMat> detections; for (int i = 0; i < (int) nm_boxes.size(); i++) { // rectangle(this->out, nm_boxes[i].tl(), nm_boxes[i].br(), Scalar(255, 255, 0), 3); UMat group_img = UMat::zeros(this->img.rows + 2, this->img.cols + 2, CV_8UC1); er_draw(channels, regions, nm_region_groups[i], group_img); group_img = group_img(nm_boxes[i]); copyMakeBorder(group_img.clone(), group_img, 15, 15, 15, 15, BORDER_CONSTANT, Scalar(0)); detections.push_back(group_img); } vector<string> outputs((int) detections.size()); vector<vector<Rect> > boxes((int) detections.size()); vector<vector<string> > words((int) detections.size()); vector<vector<float> > confidences((int) detections.size()); if (!detections.size() || detections.size() > 1) return false; for (int i = 0; i < (int) detections.size(); i = i + this->num) { Range r; if (i + this->num <= (int) detections.size()) r = Range(i, i + this->num); else r = Range(i, (int) detections.size()); parallel_for_(r, Parallel_OCR<OCRTesseract>(detections, outputs, boxes, words, confidences, this->ocrs)); } for (int i = 0; i < (int) detections.size(); i++) { outputs[i].erase(remove(outputs[i].begin(), outputs[i].end(), '\n'), outputs[i].end()); if (outputs[i].size() < 3) { continue; } for (int j = 0; j < (int) boxes[i].size(); j++) { boxes[i][j].x += nm_boxes[i].x - 15; boxes[i][j].y += nm_boxes[i].y - 15; if ((words[i][j].size() < 2) || (confidences[i][j] < min_confidence1) || ((words[i][j].size() == 2) && (words[i][j][0] == words[i][j][1])) || ((words[i][j].size() < 4) && (confidences[i][j] < min_confidence2)) || isRepetitive(words[i][j])) continue; words_detection.push_back(words[i][j]); // rectangle(this->out, boxes[i][j].tl(), boxes[i][j].br(), Scalar(255, 0, 255), 3); // Size word_size = getTextSize(words[i][j], FONT_HERSHEY_SIMPLEX, (double) scale_font, (int) (3 * scale_font), NULL); // rectangle(this->out, boxes[i][j].tl() - Point(3, word_size.height + 3), boxes[i][j].tl() + Point(word_size.width, 0), Scalar(255, 0, 255), -1); // putText(this->out, words[i][j], boxes[i][j].tl() - Point(1, 1), FONT_HERSHEY_SIMPLEX, scale_font, Scalar(255, 255, 255), (int) (3 * scale_font)); } } if (!words_detection.size() || words_detection.size() > 1) return false; return (words_detection[0].compare(WORD) == 0); }
/* * Improved q-intersection algorithm. */ IntervalVector qinter2(const Array<IntervalVector>& _boxes, int q) { assert(q>0); assert(_boxes.size()>0); unsigned int n = _boxes[0].size(); /* Remove the empty boxes from the list */ int p=0; for (int i=0; i<_boxes.size(); i++) { if (!_boxes[i].is_empty()) p++; } if (p==0) return IntervalVector::empty(n); Array<IntervalVector> boxes(p); int j=0; for (int i=0; i<_boxes.size(); i++) { if (!_boxes[i].is_empty()) boxes.set_ref(j++,_boxes[i]); } /* Create the sets of available boxes for each direction */ IntStack ***dirboxes = (IntStack ***)malloc(n*sizeof(IntStack **)); for (int i=0; i<n; i++) { dirboxes[i] = (IntStack **)malloc(2*sizeof(IntStack *)); for (int j=0; j<2;j++) { dirboxes[i][j] = new IntStack(0,p-1,true); } } /* Create the original intersection graph */ KCoreGraph *origin = new KCoreGraph(p,q-1,true); /* Add edges */ for (int i=0; i<p; i++) { for (int j=i+1; j<p; j++) { if (boxes[i].intersects(boxes[j])) origin->add_edge(i,j); } } /* Initialize the data structures */ vector<BitSet *> nogoods; nogoods.reserve(2*n); BitSet *curr_set = new BitSet(0,p-1,BitSet::empt); IntervalVector curr_qinter(n); curr_qinter.set_empty(); IntervalVector hull_qinter(n); hull_qinter.set_empty(); vector<IntervalVector *> neighboxes; neighboxes.reserve(p); vector<int> n_indices; n_indices.reserve(p); int b,b2,nboxes; pair<double,int> x[p]; bool first_pass = true; bool ng; /* Compute the q-inter hull */ for (int i=0; i<n; i++) { /* ######################################################## */ /* ##### ##### */ /* ##### Left ----> Right ##### */ /* ##### ##### */ /* ######################################################## */ nboxes = dirboxes[i][0]->size; /* Sort the boxes by increasing order of their left bounds */ b = dirboxes[i][0]->head(); for (int k=0; k<nboxes; k++) { x[k] = make_pair(boxes[b][i].lb(), b); b = dirboxes[i][0]->next(b); } sort(x,x+nboxes,leftpaircomp); /* For each box, look for a (q-1)-inter in its left neighbourhood */ for (int k=q-1; k<nboxes; k++) { curr_set->clear(); b = x[k].second; curr_set->add(b); /* Find the left neighbors */ neighboxes.clear(); n_indices.clear(); for (int l=0; l<k; l++) { b2 = x[l].second; if (origin->is_edge(b,b2)) { neighboxes.push_back(&(boxes[b2])); n_indices.push_back(b2); curr_set->add(b2); } } if (neighboxes.size() < q-1) continue; /* Check if it's a nogood */ ng = false; for (int z=0; z<nogoods.size(); z++) { if (nogoods.at(z)->includes(curr_set)) { ng = true; break; } } if (ng) continue; /* Call to the existence procedure */ curr_qinter = qinterex_cliquer(neighboxes, n_indices, q-1, origin); curr_qinter = curr_qinter & boxes[b]; if (curr_qinter.is_empty()) continue; /* Optimal q-inter found : extend the q-inter hull and propagate the bounds */ hull_qinter = hull_qinter | curr_qinter; propagate(boxes, dirboxes, i, true, curr_qinter, nogoods); break; } if (first_pass && curr_qinter.is_empty()) { /* No q-inter on the whole problem. No need to process the other dimensions. */ break; } first_pass = false; if (curr_qinter.is_empty()) { /* A face of the q-inter hull has been proved optimal, but has not been updated. * There is still some information we can propagate. */ propagate_no_ub(boxes, dirboxes, i, true, hull_qinter, nogoods); } /* ######################################################## */ /* ##### ##### */ /* ##### Right ----> Left ##### */ /* ##### ##### */ /* ######################################################## */ nboxes = dirboxes[i][1]->size; /* Sort the boxes by decreasing order of their right bounds */ b = dirboxes[i][1]->head(); for (int k=0; k<nboxes; k++) { x[k] = make_pair(boxes[b][i].ub(), b); b = dirboxes[i][1]->next(b); } sort(x,x+nboxes,rightpaircomp); /* For each box, look for a (q-1)-inter in its right neighbourhood */ for (int k=q-1; k<nboxes; k++) { curr_set->clear(); b = x[k].second; curr_set->add(b); /* Find the right neighbors */ neighboxes.clear(); n_indices.clear(); for (int l=0; l<k; l++) { b2 = x[l].second; if (origin->is_edge(b,b2)) { neighboxes.push_back(&(boxes[b2])); n_indices.push_back(b2); curr_set->add(b2); } } if (neighboxes.size() < q-1) continue; /* Check if it's a nogood */ ng = false; for (int z=0; z<nogoods.size(); z++) { if (nogoods.at(z)->includes(curr_set)) { ng = true; break; } } if (ng) continue; /* Call to the existence procedure */ curr_qinter = qinterex_cliquer(neighboxes, n_indices, q-1, origin); curr_qinter = curr_qinter & boxes[b]; if (curr_qinter.is_empty()) continue; /* Optimal q-inter found : extend the q-inter hull and propagate the bounds */ hull_qinter = hull_qinter | curr_qinter; if (i!=n) propagate(boxes, dirboxes, i, false, curr_qinter, nogoods); break; } if (curr_qinter.is_empty() && (i!=n)) { /* A face of the q-inter hull has been proved optimal, but has not been updated. * There is still some information we can propagate. */ propagate_no_ub(boxes, dirboxes, i, false, hull_qinter, nogoods); } } /* Cleanup */ for (int i=0; i<n; i++) { for (int j=0; j<2;j++) { delete(dirboxes[i][j]); } free(dirboxes[i]); } free(dirboxes); delete(origin); delete(curr_set); for (int i=0; i<nogoods.size(); i++) delete(nogoods.at(i)); return hull_qinter; }
MAIN_QTHREAD(int, argc, char **, argv) { // macro to enable multithreaded gui #else int main(int argc, char **argv) { // regular main without gui #endif try { // check input parameters if ((argc != 2) && (argc != 3) ) { cerr << "warning: wrong number of parameters." << endl; cerr << "usage: detect <config file> [directory or file]" << endl; // return -1; } #ifdef __LINUX__ feenableexcept(FE_DIVBYZERO | FE_INVALID); // enable float exceptions #endif // load configuration 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(); // output synchronization bool sync = conf.exists_true("sync_outputs"); mutex out_mutex; mutex_ostream mutout(std::cout, &out_mutex, "Thread M"); mutex_ostream muterr(std::cerr, &out_mutex, "Thread M"); ostream &mout = sync ? mutout : cout; ostream &merr = sync ? muterr : cerr; bootstrapping<t_net> boot(conf); // output dir string outdir = detection_thread<t_net>::get_output_directory(conf); mout << "Saving outputs to " << outdir << endl; // save conf to output dir string cname = outdir; cname << filename(argv[1]); if (conf.write(cname.c_str())) mout << "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) { merr << "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, mout, merr); uint ipp_cores = 1; if (conf.exists("ipp_cores")) ipp_cores = conf.get_uint("ipp_cores"); ipp_init(ipp_cores); // limit IPP (if available) to 1 core 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"); mout << "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; #ifdef __LINUX__ // default camera for linux if not defined cam_type = "v4l2"; #endif if (conf.exists("camera")) cam_type = conf.get_string("camera"); // allocate threads uint nthreads = 1; bool updated = false; idx<ubyte> detframe; // frame returned by detection thread uint frame_id = 0; svector<midx<t_net> > all_samples, samples; // extracted samples bboxes all_bbsamples, bbsamples; // boxes corresponding to samples if (conf.exists("nthreads")) nthreads = (std::max)((uint) 1, conf.get_uint("nthreads")); list<detection_thread<t_net>*> threads; list<detection_thread<t_net>*>::iterator ithreads; idx<uint> total_saved(nthreads); idx_clear(total_saved); mout << "Initializing " << nthreads << " detection threads." << endl; for (uint i = 0; i < nthreads; ++i) { detection_thread<t_net> *dt = new detection_thread<t_net>(conf, &out_mutex, NULL, NULL, sync); threads.push_back(dt); dt->start(); } // 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, *cam2 = 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, mout, merr, fpattern, &files); } else // given directory only cam = new camera_directory<ubyte>(dir.c_str(), height, width, input_random, npasses, mout, merr, fpattern, &files); } else if (!strcmp(cam_type.c_str(), "opencv")) cam = new camera_opencv<ubyte>(-1, height, width); #ifdef __LINUX__ else if (!strcmp(cam_type.c_str(), "v4l2")) cam = new camera_v4l2<ubyte>(conf.get_cstring("device"), height, width, conf.exists_true("camera_grayscale"), conf.exists_true("camera_rgb")); else if (!strcmp(cam_type.c_str(), "mac")) cam = new camera_mac<ubyte>(conf.get_cstring("device"), height, width, conf.exists_true("camera_grayscale"), conf.exists_true("camera_rgb")); else if (!strcmp(cam_type.c_str(), "mcams")) { vector<string> devices = conf.get_all_strings("device"); cam = new camera_mcams<ubyte>(conf, devices, height, width, conf.exists_true("camera_grayscale"), conf.exists_true("camera_rgb")); } #endif #ifdef __KINECT__ else if (!strcmp(cam_type.c_str(), "kinect")) cam = new camera_kinect<ubyte>(height, width); #endif else if (!strcmp(cam_type.c_str(), "shmem")) cam = new camera_shmem<ubyte>("shared-mem", height, width); else if (!strcmp(cam_type.c_str(), "video")) { if (argc >= 3) cam = new camera_video<ubyte> (argv[2], height, width, conf.get_uint("input_video_sstep"), conf.get_uint("input_video_max_duration")); else eblerror("expected 2nd argument"); } else if (!strcmp(cam_type.c_str(), "datasource")) { cam = new camera_datasource<ubyte,int>(conf); } else eblerror("unknown camera type, set \"camera\" in your .conf"); // a camera directory may be used first, then switching to regular cam if (conf.exists_true("precamera")) cam2 = new camera_directory<ubyte>(conf.get_cstring("precamdir"), height, width, input_random, npasses, mout, merr, fpattern); if (conf.exists_true("camera_grayscale")) cam->set_grayscale(); if (conf.exists_true("silent")) cam->set_silent(); // answer variables & initializations bboxes bb; // gui #ifdef __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")); #endif // timing variables timer tpass, toverall, tstop; uint cnt = 0; bool stop = false, finished = false; // loop toverall.start(); while (!finished) { // check for results and send new image for each thread uint i = 0; finished = true; for (ithreads = threads.begin(); ithreads != threads.end(); ++ithreads, ++i) { // do nothing if thread is finished already if ((*ithreads)->finished()) continue ; finished = false; // a thread is not finished string processed_fname; uint processed_id = 0; // retrieve new data if present bool skipped = false; updated = (*ithreads)->get_data (bb, detframe, *(total_saved.idx_ptr() + i), processed_fname, &processed_id, &samples, &bbsamples, &skipped); if (skipped) cnt++; // a new skipped frame was received // save bounding boxes if (updated) { idxdim d(detframe); if (boot.activated()) bb.clear(); if (bbsaving != bbox_none) { if (!silent) mout << "Adding " << bb.size() << " boxes into new group: " << processed_fname << " with id " << processed_id << endl; boxes.new_group(d, &processed_fname, processed_id); boxes.add(bb, d, &processed_fname, processed_id); if (cnt % save_bbox_period == 0) boxes.save(); // avoid sample accumulation if not using bootstrapping if (boot.activated()) mout << "Received " << samples.size() << " bootstrapping samples." << endl; } if (conf.exists_true("bootstrapping_save")) { all_samples.push_back_new(samples); all_bbsamples.push_back_new(bbsamples); } // datasource mode, check and log answers if (dynamic_cast<camera_datasource<ubyte,int>*>(cam)) { camera_datasource<ubyte,int>* dscam = (camera_datasource<ubyte,int>*) cam; dscam->log_answers(bb); } cnt++; // display processed frame #ifdef __GUI__ if (display) { select_window(wid); disable_window_updates(); clear_resize_window(); set_window_title(processed_fname.c_str()); uint h = 0, w = 0; // display frame with resulting boxes dgui.display_minimal (detframe, bb, ((*ithreads)->pdetect ? (*ithreads)->pdetect->get_labels() : sclasses), h, w, 1, 0, 255, wid, show_parts, display_transp, bbox_show_class, bbox_show_conf, &bbsamples); // display extracted samples if (boot.activated()) { dgui.display_preprocessed (samples, bbsamples, ((*ithreads)->pdetect ? (*ithreads)->pdetect->get_labels() : sclasses), h, w, 1, -1, 1); } enable_window_updates(); if (save_video && display) { string fname; fname << viddir << processed_fname; save_window(fname.c_str()); if (!silent) mout << "saved " << fname << endl; } } // sleep display if (display_sleep > 0) { mout << "sleeping for " << display_sleep << "ms." << endl; millisleep(display_sleep); } #endif if (!silent) { // output info uint k = cnt, tot = cam->size() - cnt; // progress variables if (conf.exists("save_max")) tot = conf.get_uint("save_max"); if (!silent) { if (save_detections) { mout << "total_saved=" << idx_sum(total_saved); if (conf.exists("save_max")) mout << " / " << tot; mout << endl; } } if (boot.activated()) mout << "total_bootstrapping=" << all_samples.size() << endl; mout << "remaining=" << (cam->size() - cnt) << " elapsed=" << toverall.elapsed(); if (cam->size() > 0) mout << " ETA=" << toverall.eta(cnt, cam->size()); if (conf.exists("save_max") && save_detections) { k = idx_sum(total_saved); mout << " save_max_ETA=" << toverall.eta(k, tot); } mout << endl; mout << "i=" << cnt << " processing: " << tpass.elapsed_ms() << " fps: " << cam->fps() << endl; // save progress if (!conf.exists_false("save_progress")) job::write_progress(k, tot); } } // check if ready if ((*ithreads)->available()) { if (stop) (*ithreads)->ask_stop(); // stop but let thread finish else { // grab a new frame if available if (cam->empty()) { stop = true; tstop.start(); // start countdown timer (*ithreads)->ask_stop(); // ask this thread to stop millisleep(50); } else { #ifdef __GUI__ int key = gui.pop_key_pressed(); // if thread has already received data, wait for next key if ((*ithreads)->fed() && next_on_key) { if ((int)next_on_key != key && (int)next_on_key != key + 32) { if (!bkey_msg) mout << "Press " << next_on_key << " to process next frame." << endl; bkey_msg = true; continue ; // pause until key is pressed } else { mout << "Key pressed (" << key << ") allowing next frame to process." << endl; bkey_msg = false; tpass.restart(); } } #endif bool frame_grabbed = false; frame_id = cam->frame_id(); // if the pre-camera is defined use it until empty if (cam2 && !cam2->empty()) frame = cam2->grab(); else { // empty pre-camera, use regular camera if (skip_frames > 0) cam->skip(skip_frames); // skip frames if skip_frames > 0 if (cam->empty()) continue ; if (precomputed_boxes && !save_video) cam->next(); // move to next frame but without grabbing else if (dynamic_cast<camera_directory<ubyte>*>(cam)) { cam->grab_filename(); // just get the filename, no data } else { // actually grab the frame frame = cam->grab(); frame_grabbed = true; // cropping if (crop.nelements() > crop.order()) { cout << "cropping frame from " << frame; for (uint i = 0; i < crop.order(); ++i) if (crop.dim(i) > 1) frame = frame.narrow(i, crop.dim(i), 0); cout << " to " << frame << endl; } } } // send new frame to this thread string ffname = cam->frame_fullname(); string fname = cam->frame_name(); if (frame_grabbed) { while (!(*ithreads)->set_data(frame, ffname, fname, frame_id)) millisleep(5); } else { while (!(*ithreads)->set_data(ffname, fname, frame_id)) millisleep(5); } // we just sent a new frame tpass.restart(); } } } } if ((conf.exists("save_max") && !stop && idx_sum(total_saved) > conf.get_uint("save_max")) || (boot.activated() && (intg) all_samples.size() > boot.max_size())) { mout << "Reached max number of detections, exiting." << endl; stop = true; // limit number of detection saves tstop.start(); // start countdown timer } // sleep a bit between each iteration millisleep(5); // check if stop countdown reached 0 if (stop && tstop.elapsed_minutes() >= 20) { cerr << "threads did not all return 20 min after request, stopping" << endl; break ; // program too long to stop, force exit } } // saving boxes if (bbsaving != bbox_none) boxes.save(); mout << "Execution time: " << toverall.elapsed() << endl; if (save_video) cam->stop_recording(conf.exists_bool("use_original_fps") ? cam->fps() : conf.get_uint("save_video_fps"), outdir.c_str()); // saving bootstrapping if (conf.exists_true("bootstrapping_save") && boot.activated()) boot.save_dataset(all_samples, all_bbsamples, outdir, classes); // free variables if (cam) delete cam; for (ithreads = threads.begin(); ithreads != threads.end(); ++ithreads) { if (!(*ithreads)->finished()) (*ithreads)->stop(); // stop thread without waiting delete *ithreads; } #ifdef __GUI__ if (!conf.exists_true("no_gui_quit") && !conf.exists("next_on_key")) { mout << "Closing windows..." << endl; quit_gui(); // close all windows mout << "Windows closed." << endl; } #endif job::write_finished(); // declare job finished mout << "Detection finished." << endl; // evaluation of bbox if (conf.exists_true("evaluate") && conf.exists("evaluate_cmd")) { string cmd; cmd << "cd " << outdir << " && " << conf.get_string("evaluate_cmd"); int res = std::system(cmd.c_str()); if (res != 0) cerr << "bbox evaluation failed with command " << cmd << endl; } } eblcatcherror(); return 0; }