Exemplo n.º 1
0
void detectSat(HOGDescriptor& hog, const double hitThreshold, Mat& imageData, CircleData& cercle)
{
    vector<Rect> found;
    Size padding(Size(4, 4));
    Size winStride(Size(2, 2));
    hog.detectMultiScale(imageData, found, hitThreshold, winStride, padding);
    showDetections(found, imageData, cercle);
}
void PedestrianDetector::runDetection() {
    // open the video stream
    VideoStream *vid = read_video_stream(config["dataset"]);
    vid->open();

    std::vector<BoundingBox> candidates;
    double hit_threshold = config["detector_opts"]["hit_threshold"].asDouble();

    std::ofstream out_file;
    if (config["output"]["save_log"].asBool()) {
        //std::cout << "It's going to save a log file in " << config.logFilename << std::endl;
        out_file.open(config["output"]["out_filename"].asString());
        if (out_file.fail()) {
            std::cerr << "open failure: " << strerror(errno) << '\n';
        }
    }

    while (!vid->has_ended()) {
        cv::Mat frame = vid->get_next_frame();

        // resize frame if needed.
        if (config["detector_opts"]["resize_image"].asDouble() != 1.0) {
            double f = config["detector_opts"]["resize_image"].asDouble();
            cv::resize(frame, frame, cv::Size(), f, f);
        }

        // begin timer
        clock_t frame_start = clock();

        std::vector<BoundingBox> detections;
        // if calibration is required and the candidates were not build yet
        if (config["detector_opts"]["use_calibration"].asBool()) {
            if (candidates.size() == 0) {
                candidates = generateCandidatesWCalibration(frame.rows, frame.cols, NULL);
                std::cout << "Number of candidates: " << candidates.size() << std::endl; 
            }

            // std::cout << "Its going to debug..." << std::endl;
            
            std::vector<cv::Mat> pyramid_images;
            std::vector<float> pyramid_scales;
            pyramid_images = computeImagePyramid(frame, pyramid_scales, 1.05);
            associateScaleToCandidates(candidates, pyramid_scales, frame.rows);

            // debugCandidates(frame, candidates, pyramid_scales);

            detections = detectWCandidates(candidates, pyramid_images, pyramid_scales, hit_threshold);

            std::cout << "Number of detections: " << detections.size() << std::endl;
            
        }
        else {
            std::vector<cv::Mat> pyramid_images;
            std::vector<float> pyramid_scales;
            pyramid_images = computeImagePyramid(frame, pyramid_scales, 1.05);

            detections = detectBaseline(pyramid_images, pyramid_scales, hit_threshold);
        }

        showDetections(frame, detections, cv::Scalar(0,200,0));
        detections = nonMaxSuppression(detections);
        showDetections(frame, detections, cv::Scalar(0,0,200));

        clock_t frame_end = clock();
        std::cout << "TIME: " << (double(frame_end - frame_start) / CLOCKS_PER_SEC) << std::endl;
        // end timer
        
        int f = vid->get_current_frame_number();
        if (config["output"]["save_frames"].asBool()) {
            std::stringstream ss;
            ss << config["output"]["out_folder"].asString() << format_int(f, 4) << ".jpg";
            std::cout << "Saving frame " << ss.str() << std::endl;
            cv::imwrite(ss.str(), frame);

        }

        if (config["output"]["save_log"].asBool()) {
            for (int i = 0; i < detections.size(); ++i) {
                out_file << f << " " << detections[i].bb.x << " " 
                         << detections[i].bb.y << " "  
                         << detections[i].bb.height << " "
                         << detections[i].bb.width << " "
                         << detections[i].score << std::endl;
            }
        }
    }
}