Exemple #1
0
bool TestText_1()
{
    // Create new model
    std::unique_ptr<ModelManager> model_manager(new ModelManager("Model1"));

    /*    // Add shell object to the model
        auto shell_id = model_manager->GenerateShellObject(nullptr, "Shell");
        ShellObject& shell_object = model_manager->GetShellObject(shell_id);

        {
            // Shell data
            std::vector<glm::vec3> points;
            std::vector<glm::ivec3> indices;
            GenerateSphere(glm::vec3(0, 0, 0), 2.4, points, indices);
            std::vector<glm::vec4> colors(points.size(), glm::vec4(0.1, 1.0, 0.0, 1.0));

            // Load data to previous created object
            shell_object.AppendData(points, colors, indices);
        }
    */
    // Add text object to the model
    auto text_id = model_manager->GenerateTextObject(nullptr, "Text");
    TextObject& text_object = model_manager->GetTextObject(text_id);

    text_object.SetFontNameAndFontSize(get_fonts_resources_path() + "DejaVuSansMono-Bold.ttf", 45);
    unsigned int s_text_id = text_object.AddStaticText(L"(-: StaticText test :-)", glm::vec4(1, 0, 0, 0), 50, 200);

    text_object.SetFontNameAndFontSize(get_fonts_resources_path() + "/FreeSerifItalic.ttf", 24);
    unsigned int d_text_id = text_object.AddDynamicText(L"(-: DynamicText test :-)", glm::vec4(0, 0, 1, 0), 10, 30);

    // Create view and attach model to it
    return RunWithinTestEnv(model_manager->GetModel(), std::string(__FUNCTION__));
}
int main(int argc, const char *argv[])
{
    try {
        po::options_description desc("Allowed options");
        desc.add_options()
            ("help,h", "print this help message")
            ("config,c", po::value<std::string>()->required(), "path to config file")
            ("model,m", po::value<std::string>()->required(), "path to model file")
            ("input,i", po::value<std::string>()->required(), "path to image file")
        ;

        po::variables_map vm;
        po::store(po::parse_command_line(argc, argv, desc), vm);

        if (vm.count("help")) {
            std::cout << desc << std::endl;
            return 0; 
        }

        po::notify(vm);

        std::shared_ptr<TextDetector::ConfigurationManager> config(
        	new TextDetector::ConfigurationManager(
        		vm["config"].as<std::string>()));
        TextDetector::ConfigurationManager::set_instance(config);

        float threshold = config->get_threshold();

        srand(config->get_random_seed());

        // creates the models
        TextDetector::ModelManager model_manager(config);

        if (config->verbose())
            std::cout << "Read inputs" << std::endl;

        std::string input(vm["input"].as<std::string>());
        if (!fs::exists(input)) {
            std::cerr << "Input image not available" << std::endl;
            return 1;
        }

        TextDetector::MserDetector detector(config);

        TextDetector::AdaboostClassifier clf(vm["model"].as<std::string>());

        cv::Mat image = cv::imread(input);
        cv::Mat mask;

        clf.detect(image, mask);
        cv::Mat result_image;
        std::vector<cv::Rect> words = detector(image, result_image, mask > (255 * threshold));

        for (size_t i = 0; i < words.size(); i++) {
            cv::Rect r = words[i];
            cv::rectangle(result_image, r.tl(), r.br(), cv::Scalar(0, 0, 255), 4);
            std::cout << r.x << "," << r.y << "," << r.width << "," << r.height << std::endl;
        }
        cv::imshow("MASK", mask); 
        cv::imshow("RESULT", result_image); cv::waitKey(0);


    } catch (const std::exception &e) {
        std::cerr << "Error: " << e.what() << std::endl;
        return 1;
    }
    
    return 0;
}
int main(int argc, const char *argv[])
{
    signal(SIGINT, terminate);
    try {
        po::options_description desc("Allowed options");
        desc.add_options()
            ("help,h", "print this help message")
            ("config,c", po::value<std::string>()->required(), "path to config file")
        ;

        po::variables_map vm;
        po::store(po::parse_command_line(argc, argv, desc), vm);

        if (vm.count("help")) {
            std::cout << desc << std::endl;
            return 0;
        }

        po::notify(vm);

        std::shared_ptr<TextDetector::ConfigurationManager> config(
        	new TextDetector::ConfigurationManager(
        		vm["config"].as<std::string>()));
        TextDetector::ConfigurationManager::set_instance(config);


        float threshold = config->get_threshold();
        if (config->has_cache()) {
            CacheManager::set_instance(
                std::shared_ptr<CacheManager>((new CacheManager(config->get_cache_directory()))));
        }

        srand(config->get_random_seed());

        // creates the models
        TextDetector::ModelManager model_manager(config);

        if (config->verbose())
            std::cout << "Read inputs" << std::endl;

        if (!fs::is_directory(config->get_input_directory()) ||
            !fs::is_directory(config->get_responses_directory())) {
            std::cerr << "Error: response or input directory does not exist" << std::endl;
            return 1;
        }

        std::vector<fs::path> directories;
        std::copy(fs::directory_iterator(
            config->get_input_directory()),
            fs::directory_iterator(), 
            std::back_inserter(directories));
        std::sort(directories.begin(), directories.end());

        TextDetector::MserDetector detector(config);
        for (auto it = directories.begin(); it != directories.end(); ++it) {
            fs::path p(*it);
            //p = "../train_icdar_2005/332.jpg";
            //p = "../test_icdar_2005/214.jpg";

            if (p.extension() != ".jpg" && p.extension() != ".png") {
                std::cout << "Skipping: " << p << std::endl;
                continue;
            }

            fs::path number = p.stem();

            std::string response_name = number.generic_string() + "_response.png";
            fs::path response = config->get_responses_directory();
            response += "/";
            response += response_name;

            if (!fs::exists(response) && 
                !config->ignore_responses()) {
                std::cout << "Error, " << response
                		  << " does not exist -> skipping!" << std::endl;
                continue;
            }

            std::cout << "Processing: " << p.filename() << " " 
                      << number << " " << response << std::endl;

            if (CacheManager::instance()) {
                CacheManager::instance()->load_image(number.generic_string());
            }
            
            cv::Mat response_image;
            if (fs::exists(response)) {
                response_image = cv::imread(response.generic_string());
                if (response_image.channels() > 1) {
                    cv::cvtColor(response_image, response_image, cv::COLOR_RGB2GRAY);
                }
            }
            cv::Mat train_image = cv::imread(p.generic_string());
            cv::Mat mask;

            if (!response_image.empty()) 
                mask = response_image > (threshold * 255);

            if (config->ignore_responses()) {
                mask = cv::Mat(train_image.rows, train_image.cols, CV_8UC1, cv::Scalar(255));
            }

            cv::Mat gt_mask;
            if (config->include_binary_masks()) {
                std::string mask_path = config->get_input_directory() + number.generic_string() + "_mask.png";
                if (!fs::exists(mask_path)) {
                    std::cout << "Skipping: " << mask_path << " due to missing mask!" << std::endl;
                    continue;
                }
                gt_mask = cv::imread(mask_path);
            }
            cv::Mat result_image;
            std::vector<cv::Rect> words = detector(train_image, result_image, mask, gt_mask);

            fs::path out_name(config->get_responses_directory());
            std::string box_name = number.generic_string() + "_boxes.txt";
            out_name += "/"; out_name += box_name;
            std::ofstream ofs(out_name.generic_string());
//            cv::Mat result_img = show_groups_color(
//                groups,
//                cv::Size(train_image.cols, train_image.rows),
//                all_probs, false);
            for (size_t i = 0; i < words.size(); i++) {
                cv::Rect r = words[i];
                cv::rectangle(result_image, r.tl(), r.br(), cv::Scalar(0, 0, 255), 4);
                ofs << r.x << "," << r.y << "," << r.width << "," << r.height << std::endl;
            }
            ofs.close();

            std::string box_img_name = number.generic_string() + "_boxes.png";
            fs::path out_img_name(config->get_responses_directory());
            out_img_name += "/"; out_img_name += box_img_name;
            cv::imwrite(out_img_name.generic_string(), result_image);

            if (CacheManager::instance())
                CacheManager::instance()->save_image(number.generic_string());
        }
        
    } catch (const std::exception &e) {
        std::cerr << "Error: " << e.what() << std::endl;
        return 1;
    }

    return 0;
}