laneatt.cc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. #include "laneatt.hh"
  2. #include <fstream>
  3. #include <NvInferPlugin.h>
  4. #include <cuda_runtime_api.h>
  5. #include <opencv2/imgproc.hpp>
  6. #include <opencv2/highgui.hpp>
  7. #include <map>
  8. #define INPUT_H 360
  9. #define INPUT_W 640
  10. #define N_OFFSETS 72
  11. #define N_STRIPS (N_OFFSETS - 1)
  12. #define MAX_COL_BLOCKS 1000
  13. //std::vector<cv::Point2f> warped_point, origin_point;
  14. //float METER_PER_PIXEL;
  15. //float BOARD_SIDE_LENGTH = 0.5;
  16. //cv::Mat get_pers_mat(std::string pers_file)
  17. //{
  18. // cv::Point2f src[4];
  19. // cv::Point2f dst[4];
  20. // cv::FileStorage pf(pers_file, cv::FileStorage::READ);
  21. // pf["board_left_top"]>>src[0];
  22. // pf["board_left_bottom"]>>src[1];
  23. // pf["board_right_top"]>>src[2];
  24. // pf["board_right_bottom"]>>src[3];
  25. // dst[1].x = src[0].x;
  26. // dst[1].y = src[1].y;
  27. // dst[3].x = src[2].x;
  28. // dst[3].y = src[3].y;
  29. // int side_length = dst[3].x - dst[1].x;
  30. // METER_PER_PIXEL = BOARD_SIDE_LENGTH / side_length;
  31. // dst[0].x = src[0].x;
  32. // dst[0].y = dst[1].y - side_length;
  33. // dst[2].x = src[2].x;
  34. // dst[2].y = dst[3].y - side_length;
  35. // cv::Mat pers_mat = getPerspectiveTransform(dst, src);
  36. // cv::Mat pers_mat_inv = pers_mat.inv();
  37. // return pers_mat_inv;
  38. //}
  39. //float DX (float x1, float x2, float dis) {
  40. // return dis / (x2 - x1);
  41. //}
  42. //float DY (float y1, float y2, float dis) {
  43. // return dis / (x2 - x1);
  44. //}
  45. //std::string PERS_FILE = "./yaml/pers.yaml";
  46. //cv::Mat pers_mat_inv = get_pers_mat(PERS_FILE); // 得到透视变换关系
  47. LaneATT::LaneATT(const std::string& plan_path) {
  48. buffer_size_[0] = 3 * INPUT_H * INPUT_W;
  49. buffer_size_[1] = MAX_COL_BLOCKS * (5 + N_OFFSETS);
  50. cudaMalloc(&buffers_[0], buffer_size_[0] * sizeof(float));
  51. cudaMalloc(&buffers_[1], buffer_size_[1] * sizeof(float));
  52. image_data_.resize(buffer_size_[0]);
  53. detections_.resize(MAX_COL_BLOCKS);
  54. cudaStreamCreate(&stream_);
  55. LoadEngine(plan_path);
  56. }
  57. LaneATT::~LaneATT() {
  58. cudaStreamDestroy(stream_);
  59. for (auto& buffer : buffers_) {
  60. cudaFree(buffer);
  61. }
  62. if (context_ != nullptr) {
  63. context_->destroy();
  64. engine_->destroy();
  65. }
  66. }
  67. std::vector<std::vector<cv::Point2f>> LaneATT::DetectLane(const cv::Mat& raw_image) {
  68. // Preprocessing
  69. cv::Mat img_resize;
  70. cv::resize(raw_image, img_resize, cv::Size(INPUT_W, INPUT_H), cv::INTER_LINEAR);
  71. // img_resize.convertTo(img_resize, CV_32FC3, 1.0);
  72. uint8_t* data_hwc = reinterpret_cast<uint8_t*>(img_resize.data);
  73. float* data_chw = image_data_.data();
  74. for (int c = 0; c < 3; ++c) {
  75. for (unsigned j = 0, img_size = INPUT_H * INPUT_W; j < img_size; ++j) {
  76. data_chw[c * img_size + j] = data_hwc[j * 3 + c] / 255.f;
  77. }
  78. }
  79. // Do inference
  80. cudaMemcpyAsync(buffers_[0], image_data_.data(), buffer_size_[0] * sizeof(float), cudaMemcpyHostToDevice, stream_);
  81. context_->execute(1, &buffers_[0]);
  82. cudaMemcpyAsync(detections_.data(), buffers_[1], buffer_size_[1] * sizeof(float), cudaMemcpyDeviceToHost, stream_);
  83. // NMS and decoding
  84. cv::Mat rst;
  85. std::vector<std::vector<cv::Point2f>> lanes;
  86. lanes = PostProcess(img_resize, raw_image);
  87. return lanes;
  88. }
  89. void LaneATT::LoadEngine(const std::string& engine_file) {
  90. std::ifstream in_file(engine_file, std::ios::binary);
  91. if (!in_file.is_open()) {
  92. std::cerr << "Failed to open engine file: " << engine_file << std::endl;
  93. return;
  94. }
  95. in_file.seekg(0, in_file.end);
  96. int length = in_file.tellg();
  97. in_file.seekg(0, in_file.beg);
  98. std::unique_ptr<char[]> trt_model_stream(new char[length]);
  99. in_file.read(trt_model_stream.get(), length);
  100. in_file.close();
  101. // FIXME: getPluginCreator could not find plugin: ScatterND version: 1
  102. initLibNvInferPlugins(&g_logger_, "");
  103. nvinfer1::IRuntime* runtime = nvinfer1::createInferRuntime(g_logger_);
  104. assert(runtime != nullptr);
  105. engine_ = runtime->deserializeCudaEngine(trt_model_stream.get(), length, nullptr);
  106. assert(engine_ != nullptr);
  107. context_ = engine_->createExecutionContext();
  108. assert(context_ != nullptr);
  109. runtime->destroy();
  110. }
  111. float LaneIoU(const Detection& a, const Detection& b) {
  112. int start_a = static_cast<int>(a.start_y * + 0.5f);
  113. int start_b = static_cast<int>(b.start_y * + 0.5f);
  114. int start = std::max(start_a, start_b);
  115. int end_a = start_a + static_cast<int>(a.length + 0.5f) - 1;
  116. int end_b = start_b + static_cast<int>(b.length + 0.5f) - 1;
  117. int end = std::min(std::min(end_a, end_b), N_STRIPS);
  118. // if (end < start) {
  119. // return 1.0f / 0.0f;
  120. // }
  121. float dist = 0.0f;
  122. for (int i = start; i <= end; ++i) {
  123. dist += fabs(a.lane_xs[i] - b.lane_xs[i]);
  124. }
  125. dist /= static_cast<float>(end - start + 1);
  126. return dist;
  127. }
  128. std::vector<std::vector<cv::Point2f>> LaneATT::PostProcess(cv::Mat& lane_image, cv::Mat raw_image, float conf_thresh, float nms_thresh, int nms_topk) { //conf_thresh=0.4 nms_thresh=50 nms_topk=4
  129. // 1.Do NMS
  130. std::vector<Detection> candidates;
  131. std::vector<Detection> proposals;
  132. for (auto det : detections_) {
  133. if (det.score > conf_thresh) {
  134. candidates.push_back(det);
  135. }
  136. }
  137. // std::cout << candidates.size() << std::endl;
  138. std::sort(candidates.begin(), candidates.end(), [=](const Detection& a, const Detection& b) { return a.score > b.score; });
  139. for (int i = 0; i < candidates.size(); ++i) {
  140. if (candidates[i].score < 0.0f) {
  141. continue;
  142. }
  143. proposals.push_back(candidates[i]);
  144. if (proposals.size() == nms_topk) {
  145. break;
  146. }
  147. for (int j = i + 1; j < candidates.size(); ++j) {
  148. if (candidates[j].score > 0.0f && LaneIoU(candidates[j], candidates[i]) < nms_thresh) {
  149. candidates[j].score = -1.0f;
  150. }
  151. }
  152. }
  153. // 2.Decoding
  154. std::vector<float> anchor_ys;
  155. for (int i = 0; i < N_OFFSETS; ++i) {
  156. anchor_ys.push_back(1.0f - i / float(N_STRIPS));
  157. }
  158. std::vector<std::vector<cv::Point2f>> lanes;
  159. for (const auto& lane: proposals) {
  160. int start = static_cast<int>(lane.start_y * N_STRIPS + 0.5f);
  161. int end = start + static_cast<int>(lane.length + 0.5f) - 1;
  162. end = std::min(end, N_STRIPS);
  163. std::vector<cv::Point2f> points;
  164. for (int i = start; i <= end; ++i) {
  165. points.push_back(cv::Point2f(lane.lane_xs[i], anchor_ys[i] * INPUT_H));
  166. }
  167. lanes.push_back(points);
  168. }
  169. std::vector<std::vector<cv::Point2f>> lanes1;
  170. float y_ratio = float(raw_image.size().height) / float(INPUT_H);
  171. float x_ratio = float(raw_image.size().width) / float(INPUT_W);
  172. cv::Point2f pp;
  173. for (const auto& lane_points : lanes) {
  174. std::vector<cv::Point2f> points;
  175. for (int i=0; i<lane_points.size(); i++) {
  176. pp.x =float(lane_points[i].x) * x_ratio;
  177. pp.y =float(lane_points[i].y) * y_ratio;
  178. points.push_back(pp);
  179. }
  180. lanes1.push_back(points);
  181. }
  182. return lanes1;
  183. // float y_ratio = float(raw_image.size().height) / float(INPUT_H);
  184. // float x_ratio = float(raw_image.size().width) / float(INPUT_W);
  185. // // 3.PerspectiveTransform
  186. // std::vector<cv::Point2f> leftLane;
  187. // std::vector<cv::Point2f> rightLane;
  188. // std::vector<std::vector<cv::Point2f>> warped_lanes;
  189. // for (int i = 0; i < lanes.size(); i++) {
  190. // cv::perspectiveTransform(lanes[i], warped_point , pers_mat_inv); // 坐标变换(输入点,输出点,变换矩阵)
  191. // warped_lanes.push_back(warped_point);
  192. // double sum = 0.0;
  193. // for (int i = 0 ; i < 5 ; i++){
  194. // sum += warped_point[i].x;
  195. // }
  196. // double mean = sum / 5;
  197. // if (mean < INPUT_W / 2){
  198. // leftLane.push_back(cv::Point2f(mean, i));
  199. // }else{
  200. // rightLane.push_back(cv::Point2f(mean, i));
  201. // }
  202. // }
  203. // float left_min = 0;
  204. // float right_max = INPUT_W;
  205. // int left_num = -1;
  206. // int right_num = -1;
  207. // int left_left = -1;
  208. // int right_right = -1;
  209. // for (const auto& points : leftLane){
  210. // if(points.x > left_min){
  211. // left_num = (int) round(points.y);
  212. // left_min = points.x;
  213. // }
  214. // }
  215. // for (const auto& points : leftLane){
  216. // if((int)round(points.y) != left_num){
  217. // left_left = points.y;
  218. // }
  219. // }
  220. // if (left_num == -1){
  221. // std::cout<<"left lane failed"<<std::endl;
  222. // }
  223. // for (const auto& points : rightLane){
  224. // if(points.x < right_max){
  225. // right_num = (int) round(points.y);
  226. // right_max = points.x;
  227. // }
  228. // }
  229. // for (const auto& points : rightLane){
  230. // if((int)round(points.y) != right_num){
  231. // right_right = points.y;
  232. // }
  233. // }
  234. // if (right_num == -1){
  235. // std::cout<<"right lane failed"<<std::endl;
  236. // }
  237. // // 4.Visualize
  238. // cv::Point2f pp;
  239. // cv::Point3f ppp;
  240. // int flag = 0;
  241. // cv::Mat img_warp;
  242. // int DX, DY;
  243. // std::vector<std::vector<cv::Point3f>> world_lanes;
  244. // cv::warpPerspective(raw_image, img_warp, pers_mat_inv, raw_image.size()); // 图像变换(输入图像,输出图像,变换矩阵)
  245. // for (const auto& lane_points : warped_lanes) {
  246. // if(flag == left_num || flag == right_num){
  247. // std::vector<cv::Point3f> world_points;
  248. // for (const auto& point : lane_points) {
  249. // pp.x =float(point.x);
  250. // pp.y =float(point.y);
  251. // cv::circle(img_warp, pp, 1, cv::Scalar(0, 255, 0), -1);
  252. // ppp.x = (point.x - INPUT_W / 2) * DX;
  253. // ppp.y = float((INPUT_H - point.y) * DY);
  254. // ppp.z = float(0);
  255. // world_points.push_back(ppp);
  256. // }
  257. // world_lanes.push_back(world_points);
  258. // }else{
  259. // std::vector<cv::Point3f> world_points;
  260. // for (const auto& point : lane_points) {
  261. // pp.x =float(point.x);
  262. // pp.y =float(point.y);
  263. // cv::circle(img_warp, pp, 1, cv::Scalar(0, 0, 255), -1);
  264. // ppp.x = (point.x - INPUT_W / 2) * DX;
  265. // ppp.y = float((INPUT_H - point.y) * DY);
  266. // ppp.z = float(0);
  267. // world_points.push_back(ppp);
  268. // }
  269. // world_lanes.push_back(world_points);
  270. // }
  271. // flag++;
  272. // }
  273. // cv::imshow("warp",img_warp);
  274. // flag = 0;
  275. // for (const auto& lane_points : lanes) {
  276. // if(flag == left_num || flag == right_num){
  277. // for (int i=0; i<lane_points.size()-1; i++) {
  278. // pp.x =float(lane_points[i].x) * x_ratio;
  279. // pp.y =float(lane_points[i].y) * y_ratio;
  280. // cv::circle(raw_image, pp, 1, cv::Scalar(0, 255, 0), -1);
  281. // }
  282. // }else{
  283. // for (int i=0; i<lane_points.size()-1; i++) {
  284. // pp.x =float(lane_points[i].x) * x_ratio;
  285. // pp.y =float(lane_points[i].y) * y_ratio;
  286. // cv::circle(raw_image, pp, 1, cv::Scalar(0, 0, 255), -1);
  287. // }
  288. // }
  289. // flag++;
  290. // }
  291. // cv::Point2f p1, p2;
  292. // flag = 0;
  293. // for (const auto& lane_points : lanes) {
  294. // if(flag == left_num || flag == right_num){
  295. // for (int i=0; i<lane_points.size()-1; i++) {
  296. // p1.x =float(lane_points[i].x) * x_ratio;
  297. // p1.y =float(lane_points[i].y) * y_ratio;
  298. // p2.x =float(lane_points[i+1].x) * x_ratio;
  299. // p2.y =float(lane_points[i+1].y) * y_ratio;
  300. // cv::line(raw_image, p1, p2, cv::Scalar(0, 255, 0), 1);
  301. // }
  302. // }else{
  303. // for (int i=0; i<lane_points.size()-1; i++) {
  304. // p1.x =float(lane_points[i].x) * x_ratio;
  305. // p1.y =float(lane_points[i].y) * y_ratio;
  306. // p2.x =float(lane_points[i+1].x) * x_ratio;
  307. // p2.y =float(lane_points[i+1].y) * y_ratio;
  308. // cv::line(raw_image, p1, p2, cv::Scalar(0, 0, 255), 1);
  309. // }
  310. // }
  311. // flag++;
  312. // }
  313. // return raw_image;
  314. }