Commit 06e0182f466c906d909eb525c56f939bc29c5bfc

Authored by Hu Chunming
1 parent 6ec23cbb

修复src图片alignWidth为0导致崩溃的问题;

完善road_seg返回结果
src/PicAnalysis.cpp
... ... @@ -182,23 +182,31 @@ vector<AnalysisResult> PicAnalysis::analysis_img(vector<sy_img> vec_img){
182 182 }
183 183  
184 184 va_result* result = m_vehicle_analysis.detect(vec_img);
185   -
186 185 if (result) {
187   -
188 186 vec_result = va_result2AnalysisResult(result, batch_size);
189 187 m_vehicle_analysis.release_result(result, vec_img.size());
190 188 }
191 189  
192   - m_road_seg_algorithm.detect(vec_img);
  190 + std::vector<RoadInfo> vec_road = m_road_seg_algorithm.detect(vec_img);
  191 + if (vec_road.size() == batch_size && vec_result.size() == batch_size) {
  192 + for (size_t i = 0; i < batch_size; i++) {
  193 + vec_result[i].vec_line = vec_road[i].vec_line;
  194 + vec_result[i].vec_road = vec_road[i].vec_road;
  195 + }
  196 + }
193 197  
194 198 for (int b = 0; b < vec_result.size(); b++)
195 199 {
196 200 vector<VehicleInfo>& vec_info = vec_result[b].info;
197 201  
198 202 sy_img img = vec_img[b];
  203 +
199 204 ImageData src;
200 205 src.width = img.w_;
201 206 src.height = img.h_;
  207 + src.alignWidth = ALIGN_UP16(img.w_);
  208 + src.alignHeight = ALIGN_UP2(img.h_);
  209 + src.size = YUV420SP_SIZE(src.alignWidth, src.alignHeight);
202 210 src.data_naked = img.data_;
203 211 for(int c=0;c<vec_info.size();c++)
204 212 {
... ... @@ -322,7 +330,7 @@ vector&lt;AnalysisResult&gt; PicAnalysis::analysis_img(vector&lt;sy_img&gt; vec_img){
322 330 // head_tail_result.clear();
323 331 // }
324 332  
325   - LOG_INFO("analysis_sync finished!");
  333 + LOG_INFO("analysis finished!");
326 334  
327 335 return vec_result;
328 336 }
... ...
src/PicAnalysis.h
... ... @@ -23,7 +23,6 @@ public:
23 23  
24 24 int init(VillageParam param);
25 25  
26   - // todo 接口定义是二进制流的,这个地方有待修改
27 26 vector<AnalysisResult> analysis_file(vector<string> file_path);
28 27  
29 28 vector<AnalysisResult> analysis_img(vector<sy_img> vec_img);
... ...
src/ai_engine_module/RoadSegAnalysis.cpp
1 1 #include "RoadSegAnalysis.h"
2   -#include "road_seg.h"
  2 +
  3 +uint8_t seg_colors[][3] = { {0, 0, 0}, {0, 255, 255}, {128, 255, 0}, {255, 128, 0}, {128, 0, 255}, {255, 0, 128}, {0, 128, 255}, {0, 255, 128}, {128, 255, 255}};
  4 +uint8_t lane_colors[][3] = { {0, 0, 0}, {255, 0, 0}, {0, 255, 0}, {0, 0, 255}, {255, 255, 0}, {255, 0, 255}, {0, 255, 255}, {128, 255, 0}, {255, 128, 0}};
  5 +
  6 +void lanes_process(const rs_lane* lanes, int lane_count, std::vector<std::pair<std::vector<cv::Point>, int>>& combined, float scale_w = 1.0, float scale_h = 1.0) {
  7 + std::vector<std::vector<cv::Point> > lanes_xys;
  8 + std::vector<int> lanes_cls;
  9 + for (int i = 0; i < lane_count; i++) {
  10 + std::vector<cv::Point> xys;
  11 + for (int j = 0; j < lanes[i].num_points; j++) {
  12 + int x = static_cast<int>(lanes[i].points[j].x_ * scale_w);
  13 + int y = static_cast<int>(lanes[i].points[j].y_ * scale_h);
  14 + if (x > 0 && y > 0) {
  15 + xys.emplace_back(x, y);
  16 + }
  17 + }
  18 + if (!xys.empty()) {
  19 + lanes_xys.push_back(xys);
  20 + lanes_cls.push_back(lanes[i].cls);
  21 + }
  22 + }
  23 +
  24 + for (size_t i = 0; i < lanes_xys.size(); ++i) {
  25 + combined.push_back(std::make_pair(lanes_xys[i], lanes_cls[i]));
  26 + }
  27 + if (!combined.empty()) {
  28 + //按车道线起点坐标排序,相应的类别顺序也会变化以保证标签对齐
  29 + std::sort(combined.begin(), combined.end(), [](const std::pair<std::vector<cv::Point>, int>& a, const std::pair<std::vector<cv::Point>, int>& b) {
  30 + return a.first[0].x < b.first[0].x;
  31 + });
  32 + }
  33 +}
3 34  
4 35 RoadSegAnalysis::RoadSegAnalysis(/* args */)
5 36 {
... ... @@ -29,9 +60,9 @@ int RoadSegAnalysis::init(int devId){
29 60 return SY_SUCCESS;
30 61 }
31 62  
32   -int RoadSegAnalysis::detect(vector<sy_img> vec_img){
  63 +std::vector<RoadInfo> RoadSegAnalysis::detect(vector<sy_img> vec_img){
33 64  
34   - ACL_CALL(aclrtSetCurrentContext(ctx), SY_SUCCESS, SY_FAILED);
  65 + std::vector<RoadInfo> vec_road;
35 66  
36 67 const int batchsize = vec_img.size();
37 68 rs_result results[batchsize];
... ... @@ -45,13 +76,38 @@ int RoadSegAnalysis::detect(vector&lt;sy_img&gt; vec_img){
45 76  
46 77 do
47 78 {
  79 + ret = aclrtSetCurrentContext(ctx);
  80 + if (SY_SUCCESS != ret) {
  81 + printf("aclrtSetCurrentContext failed!");
  82 + break;
  83 + }
  84 +
48 85 ret = rs_batch(m_handle, vec_img.data(), batchsize, results);
49 86 if (SY_SUCCESS != ret) {
50   - printf("mrc_batch failed!");
  87 + printf("rs_batch failed!");
51 88 break;
52 89 }
53 90  
54   - // todo 结果处理
  91 + for (int b = 0; b < batchsize; b++) {
  92 + auto one_result = results[b];
  93 + RoadInfo one_road;
  94 + for (size_t i = 0; i < one_result.lane_count; i++)
  95 + {
  96 + auto one_lane = one_result.reg_array[i];
  97 + LineInfo info;
  98 + for (size_t j = 0; j < one_lane.num_points; j++)
  99 + {
  100 + info.vec_pt.push_back(one_lane.points[j]);
  101 + }
  102 + info.line_type = one_lane.cls;
  103 + one_road.vec_line.push_back(info);
  104 + }
  105 +
  106 + one_road.vec_road = parse_seg(one_result, vec_img[b]);
  107 +
  108 + vec_road.push_back(one_road);
  109 + }
  110 +
55 111 } while (0);
56 112  
57 113 for (int b = 0; b < batchsize; b++) {
... ... @@ -61,7 +117,7 @@ int RoadSegAnalysis::detect(vector&lt;sy_img&gt; vec_img){
61 117 results[b].direct_seg = NULL;
62 118 }
63 119  
64   - return ret;
  120 + return vec_road;
65 121 }
66 122  
67 123 int RoadSegAnalysis::release() {
... ... @@ -78,4 +134,48 @@ int RoadSegAnalysis::release() {
78 134 }
79 135  
80 136 return SY_SUCCESS;
  137 +}
  138 +
  139 +std::vector<SegInfo> RoadSegAnalysis::parse_seg(rs_result one_result, sy_img src) {
  140 + int w = 640;
  141 + int h = 360;
  142 + float alpha = 0.75;
  143 + cv::Mat overlayed_img(cv::Size(w,h), CV_8UC3, cv::Scalar(0, 0, 0));
  144 + // 将车道线标签转换为彩色图像
  145 + for (int i = 0; i < h; ++i) {
  146 + for (int j = 0; j < w; ++j) {
  147 + int idx = one_result.seg_array[(i * w + j)];
  148 + overlayed_img.at<cv::Vec3b>(i, j)[0] = seg_colors[idx][0]; // R通道;
  149 + overlayed_img.at<cv::Vec3b>(i, j)[1] = seg_colors[idx][1]; // G通道
  150 + overlayed_img.at<cv::Vec3b>(i, j)[2] = seg_colors[idx][2]; // B通道
  151 + }
  152 + }
  153 +
  154 + cv::resize(overlayed_img, overlayed_img, cv::Size(src.w_,src.h_), 0, 0, cv::INTER_LINEAR);
  155 + // 将原始图像和彩色车道线图进行混合
  156 + // cv::addWeighted(cvImg, alpha, overlayed_img, 1 - alpha, 0, overlayed_img);
  157 +
  158 + float scale_w = src.w_ / 640.0;
  159 + float scale_h = src.h_ / 360.0;
  160 + std::vector<std::pair<std::vector<cv::Point>, int>> combined;
  161 + lanes_process(one_result.reg_array, one_result.lane_count, combined, scale_w, scale_h);
  162 +
  163 + std::vector<SegInfo> vec_seg;
  164 + for (const auto& lane_info : combined) {
  165 + const auto& xys = lane_info.first;
  166 + int cls = lane_info.second;
  167 + SegInfo info;
  168 + info.seg_type = cls;
  169 + // cv::Scalar color(lane_colors[cls][0],lane_colors[cls][1],lane_colors[cls][2]);
  170 + for (size_t i = 1; i < xys.size(); ++i) {
  171 + // cv::line(overlayed_img, xys[i - 1], xys[i], color, 4);
  172 + sy_point pt;
  173 + pt.x_ = xys[i].x;
  174 + pt.y_ = xys[i].y;
  175 + info.vec_pt.push_back(pt);
  176 + }
  177 + vec_seg.push_back(info);
  178 + }
  179 +
  180 + return vec_seg;
81 181 }
82 182 \ No newline at end of file
... ...
src/ai_engine_module/RoadSegAnalysis.h
1 1 #include "include.h"
  2 +#include "../village_inc.h"
  3 +#include "road_seg.h"
  4 +
  5 +#include <vector>
  6 +
  7 +using namespace std;
  8 +
  9 +struct RoadInfo{
  10 + std::vector<LineInfo> vec_line;
  11 + std::vector<SegInfo> vec_road;
  12 +};
2 13  
3 14 class RoadSegAnalysis
4 15 {
... ... @@ -8,11 +19,13 @@ public:
8 19  
9 20 int init(int devId);
10 21  
11   - int detect(vector<sy_img> vec_img);
  22 + std::vector<RoadInfo> detect(vector<sy_img> vec_img);
12 23  
13 24 private:
14 25 int release();
15 26  
  27 + std::vector<SegInfo> parse_seg(rs_result one_result, sy_img src);
  28 +
16 29 private:
17 30 void* m_handle{nullptr};
18 31 aclrtContext ctx{nullptr};
... ...
src/demo/main.cpp
... ... @@ -13,7 +13,7 @@ int main() {
13 13 vector<string> vec_path;
14 14 for (size_t i = 0; i < 1; i++)
15 15 {
16   - vec_path.push_back("./test_all.jpg");
  16 + vec_path.push_back("./img/test_road1.jpg");
17 17 }
18 18  
19 19 pic_analysis.analysis_file(vec_path);
... ...
src/village_inc.h
... ... @@ -45,8 +45,20 @@ typedef struct VehicleInfo {
45 45  
46 46 } VehicleInfo;
47 47  
  48 +struct LineInfo{
  49 + int line_type;
  50 + std::vector<sy_point> vec_pt;
  51 +};
  52 +
  53 +struct SegInfo {
  54 + int seg_type;
  55 + std::vector<sy_point> vec_pt;
  56 +};
  57 +
48 58 typedef struct AnalysisResult {
49   - std::vector<VehicleInfo> info;
  59 + std::vector<VehicleInfo> info;
  60 + std::vector<LineInfo> vec_line;
  61 + std::vector<SegInfo> vec_road;
50 62 } AnalysisResult;
51 63  
52 64 #endif // __VILLAGE_INC_H__
53 65 \ No newline at end of file
... ...