<commit_before>/******************************************************************************
 * Copyright 2018 The Apollo Authors. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *****************************************************************************/

#include "modules/perception/obstacle/camera/lane_post_process/cc_lane_post_processor/cc_lane_post_processor.h"

#include <memory>
#include <string>
#include <vector>

#include "gtest/gtest.h"
#include "opencv2/opencv.hpp"

#include "modules/common/log.h"
#include "modules/perception/common/perception_gflags.h"
#include "modules/perception/lib/config_manager/config_manager.h"

using std::shared_ptr;
using std::string;
using std::vector;

namespace apollo {
namespace perception {

DEFINE_string(test_dir,
              "/apollo/modules/perception/data/cc_lane_post_processor_test/",
              "test data directory");

static const cv::Scalar lane_mask_color(0, 255, 255);  // yellow

template<typename T = float>
T GetPolyValue(T a, T b, T c, T d, T x) {
  T y = d;
  T v = x;
  y += (c * v);
  v *= x;
  y += (b * v);
  v *= x;
  y += (a * v);
  return y;
}

class CCLanePostProcessorTest : public ::testing::Test {
 protected:
  CCLanePostProcessorTest() {}
  ~CCLanePostProcessorTest() {}
  void SetUp() override {
    cc_lane_post_processor_.reset(new CCLanePostProcessor());
  }

 protected:
  shared_ptr<CCLanePostProcessor> cc_lane_post_processor_;
};

TEST_F(CCLanePostProcessorTest, test_lane_post_process_vis) {
  FLAGS_work_root = "modules/perception";
  FLAGS_config_manager_path = "conf/config_manager.config";
  CHECK(ConfigManager::instance()->Init()) << "failed to Init ConfigManager";

  cc_lane_post_processor_->set_vis(true);
  EXPECT_TRUE(cc_lane_post_processor_->Init());

  const string input_test_image_file = FLAGS_test_dir + "test_image.jpg";
  AINFO << "test original image file: " << input_test_image_file;
  cv::Mat test_image_ori =
      cv::imread(input_test_image_file, CV_LOAD_IMAGE_COLOR);

  const string input_lane_map_file = FLAGS_test_dir + "lane_map.jpg";
  AINFO << "input lane map file: " << input_lane_map_file;
  cv::Mat lane_map_ori =
      cv::imread(input_lane_map_file, CV_LOAD_IMAGE_GRAYSCALE);

  string input_lane_map_file_test = FLAGS_test_dir + "test_lane_map.jpg";
  cv::imwrite(input_lane_map_file_test, lane_map_ori);

  cv::Mat lane_map;
  lane_map_ori.convertTo(lane_map, CV_32FC1, 1.0f / 255.0f, 0.0f);


  ConfigManager *config_manager = ConfigManager::instance();
  const ModelConfig *model_config
      = config_manager->GetModelConfig(cc_lane_post_processor_->name());
  //<< "not found model: " << cc_lane_post_processor_->name();

  float lane_map_conf_thresh = 0.5f;
  CHECK(model_config->GetValue("lane_map_confidence_thresh",
                               &lane_map_conf_thresh))
  << "the confidence threshold of label map not found.";

  LaneObjectsPtr lane_objects = std::make_shared<LaneObjects>();
  CameraLanePostProcessOptions lane_post_process_options;
  cc_lane_post_processor_->Process(
    lane_map, lane_post_process_options, &lane_objects);

  AINFO << "#lane objects = " << lane_objects->size();

  cv::Mat vis_lane_objects_image;
  test_image_ori.copyTo(vis_lane_objects_image);
  for (int h = 0; h < lane_map.rows; ++h) {
    for (int w = 0; w < lane_map.cols; ++w) {
      if (lane_map.at<float>(h, w) >= lane_map_conf_thresh) {
        vis_lane_objects_image.at<cv::Vec3b>(h, w)[0] =
            static_cast<unsigned char>(lane_mask_color[0]);
        vis_lane_objects_image.at<cv::Vec3b>(h, w)[1] =
            static_cast<unsigned char>(lane_mask_color[1]);
        vis_lane_objects_image.at<cv::Vec3b>(h, w)[2] =
            static_cast<unsigned char>(lane_mask_color[2]);
      }
    }
  }<commit_msg>fix bug with GetModelConfig() in CCLanePostProcessor uni-test<commit_after>/******************************************************************************
 * Copyright 2018 The Apollo Authors. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *****************************************************************************/

#include "modules/perception/obstacle/camera/lane_post_process/cc_lane_post_processor/cc_lane_post_processor.h"

#include <memory>
#include <string>
#include <vector>

#include "gtest/gtest.h"
#include "opencv2/opencv.hpp"

#include "modules/common/log.h"
#include "modules/perception/common/perception_gflags.h"
#include "modules/perception/lib/config_manager/config_manager.h"

using std::shared_ptr;
using std::string;
using std::vector;

namespace apollo {
namespace perception {

DEFINE_string(test_dir,
              "/apollo/modules/perception/data/cc_lane_post_processor_test/",
              "test data directory");

static const cv::Scalar lane_mask_color(0, 255, 255);  // yellow

template<typename T = float>
T GetPolyValue(T a, T b, T c, T d, T x) {
  T y = d;
  T v = x;
  y += (c * v);
  v *= x;
  y += (b * v);
  v *= x;
  y += (a * v);
  return y;
}

class CCLanePostProcessorTest : public ::testing::Test {
 protected:
  CCLanePostProcessorTest() {}
  ~CCLanePostProcessorTest() {}
  void SetUp() override {
    cc_lane_post_processor_.reset(new CCLanePostProcessor());
  }

 protected:
  shared_ptr<CCLanePostProcessor> cc_lane_post_processor_;
};

TEST_F(CCLanePostProcessorTest, test_lane_post_process_vis) {
  FLAGS_work_root = "modules/perception";
  FLAGS_config_manager_path = "conf/config_manager.config";
  CHECK(ConfigManager::instance()->Init()) << "failed to Init ConfigManager";

  cc_lane_post_processor_->set_vis(true);
  EXPECT_TRUE(cc_lane_post_processor_->Init());

  const string input_test_image_file = FLAGS_test_dir + "test_image.jpg";
  AINFO << "test original image file: " << input_test_image_file;
  cv::Mat test_image_ori =
      cv::imread(input_test_image_file, CV_LOAD_IMAGE_COLOR);

  const string input_lane_map_file = FLAGS_test_dir + "lane_map.jpg";
  AINFO << "input lane map file: " << input_lane_map_file;
  cv::Mat lane_map_ori =
      cv::imread(input_lane_map_file, CV_LOAD_IMAGE_GRAYSCALE);

  string input_lane_map_file_test = FLAGS_test_dir + "test_lane_map.jpg";
  cv::imwrite(input_lane_map_file_test, lane_map_ori);

  cv::Mat lane_map;
  lane_map_ori.convertTo(lane_map, CV_32FC1, 1.0f / 255.0f, 0.0f);


  ConfigManager *config_manager = ConfigManager::instance();
  const ModelConfig *model_config
      = config_manager->GetModelConfig(cc_lane_post_processor_->name());

  float lane_map_conf_thresh = 0.5f;
  CHECK(model_config->GetValue("lane_map_confidence_thresh",
                               &lane_map_conf_thresh))
  << "the confidence threshold of label map not found.";
  AINFO << "lane_map_conf_thresh = " << lane_map_conf_thresh;

  LaneObjectsPtr lane_objects = std::make_shared<LaneObjects>();
  CameraLanePostProcessOptions lane_post_process_options;
  cc_lane_post_processor_->Process(
    lane_map, lane_post_process_options, &lane_objects);

  AINFO << "#lane objects = " << lane_objects->size();

  cv::Mat vis_lane_objects_image;
  test_image_ori.copyTo(vis_lane_objects_image);
  for (int h = 0; h < lane_map.rows; ++h) {
    for (int w = 0; w < lane_map.cols; ++w) {
      if (lane_map.at<float>(h, w) >= lane_map_conf_thresh) {
        vis_lane_objects_image.at<cv::Vec3b>(h, w)[0] =
            static_cast<unsigned char>(lane_mask_color[0]);
        vis_lane_objects_image.at<cv::Vec3b>(h, w)[1] =
            static_cast<unsigned char>(lane_mask_color[1]);
        vis_lane_objects_image.at<cv::Vec3b>(h, w)[2] =
            static_cast<unsigned char>(lane_mask_color[2]);
      }
    }
  }
{'fraction_non_alphanumeric': 0.14951861732977612, 'fraction_numerical': 0.009279665932026447, 'mean_word_length': 4.713717693836978, 'pattern_counts': {'":': 0, '<': 73, '<?xml version=': 0, '>': 48, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3234423', 'n_tokens_mistral': 2910, 'n_tokens_neox': 2804, 'n_words': 611}