<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          OpenVINO開發(fā)教程之八 – 道路分割

          共 10852字,需瀏覽 22分鐘

           ·

          2021-05-25 16:28

          點擊上方小白學(xué)視覺”,選擇加"星標"或“置頂

          重磅干貨,第一時間送達

          本文轉(zhuǎn)自:opencv學(xué)堂


          模型介紹


          基于OpenVINO預(yù)訓(xùn)練模塊中的道路分割模型,實現(xiàn)像素級別的圖像分割,把像素劃分為如下四個類別

          • 背景

          • 道路

          • 車道線

          • 標志


          輸入數(shù)據(jù)

          要求輸入圖像BGR通道順序的彩色圖像,blob的大小為

          BCHW = Nx3x512x896,其中

          • B表示批次數(shù)目

          • C表示圖像通道

          • H表示圖像高度

          • W表示圖像寬度


          輸出數(shù)據(jù)

          輸出數(shù)據(jù)是四通道blob對象,格式為BCHW

          其中C=4表示輸出的四個分類得分,H表示feature map的高度, W表示featuremap的寬度,對輸出blob進行解析可以得到輸出的分割mask,對mask進行配色之后,得到最終的輸出結(jié)果。


          代碼實現(xiàn)

          1. 基于OpenVINO SDK開發(fā)完成演示程序,模型加載與創(chuàng)建推斷請求的代碼如下:

          C++版本

              // 加載道路分割網(wǎng)絡(luò)
              CNNNetReader network_reader;
              network_reader.ReadNetwork(model_xml);
              network_reader.ReadWeights(model_bin);

              // 請求網(wǎng)絡(luò)輸入與輸出信息
              auto network = network_reader.getNetwork();
              InferenceEngine::InputsDataMap input_info(network.getInputsInfo());
              InferenceEngine::OutputsDataMap output_info(network.getOutputsInfo());

              // 設(shè)置輸入精度
              InputInfo::Ptr& input = input_info.begin()->second;
              auto inputName = input_info.begin()->first;
              input->setPrecision(Precision::U8);
              input->getInputData()->setLayout(Layout::NCHW);

              /** 設(shè)置輸出精度與內(nèi)容**/
              DataPtr& output = output_info.begin()->second;
              auto outputName = output_info.begin()->first;
              const SizeVector outputDims = output->getTensorDesc().getDims();
              output->setPrecision(Precision::FP32);
              output->setLayout(Layout::NCHW);

              size_t N = outputDims[0];
              size_t C = outputDims[1];
              size_t H = outputDims[2];
              size_t W = outputDims[3];
              size_t image_stride = W*H*C;

              // 創(chuàng)建可執(zhí)行網(wǎng)絡(luò)對象
              auto executable_network = plugin.LoadNetwork(network, {});

              // 請求推斷圖
              InferRequest::Ptr async_infer_request_next = executable_network.CreateInferRequestPtr();
              InferRequest::Ptr async_infer_request_curr = executable_network.CreateInferRequestPtr();


          Python版本

          # 加載IR
          log.info("Reading IR...")
          net = IENetwork(model=model_xml, weights=model_bin)

          # 獲取輸入輸出層
          input_blob = next(iter(net.inputs))
          out_blob = next(iter(net.outputs))
          log.info("Loading IR to the plugin...")

          # 創(chuàng)建可執(zhí)行網(wǎng)絡(luò)
          exec_net = plugin.load(network=net, num_requests=2)
          # Read and pre-process input image
          n, c, h, w = net.inputs[input_blob].shape
          del net

          # 開始視頻文件或者攝像頭
          cap = cv2.VideoCapture("D:/images/video/CarsDrivingUnderBridge.mp4")
          # cap = cv2.VideoCapture(0)

          cur_request_id = 0
          next_request_id = 1

          log.info("Starting inference in async mode...")
          log.info("To switch between sync and async modes press Tab button")
          log.info("To stop the demo execution press Esc button")
          is_async_mode = True
          render_time = 0

          # 讀取視頻流
          ret, frame = cap.read()
          initial_w = cap.get(3)
          initial_h = cap.get(4)


          2. 檢查異步返回與解析輸出數(shù)據(jù)的代碼如下

          C++版本

          if (OK == async_infer_request_curr->Wait(IInferRequest::WaitMode::RESULT_READY)) {
              const Blob::Ptr output_blob = async_infer_request_curr->GetBlob(outputName);
              const float* output_data = output_blob->buffer().as<float*>();

              Mat result = Mat::zeros(Size(W, H), CV_8UC3);
              for (size_t h = 0; h < H; ++h) {
                  for (size_t w = 0; w < W; ++w) {
                      int index = 0;
                      float max = -100;
                      for (size_t ch = 0; ch < C; ++ch) {
                          float data = output_data[W * H * ch + W * h + w];
                          if (data > max) {
                              index = ch;
                              max = data;
                          }
                      }
                      result.at<Vec3b>(h, w) = lut[index];
                  }
              }

              // 計算FPS
              auto t1 = std::chrono::high_resolution_clock::now();
              ms dtime = std::chrono::duration_cast<ms>(t1 - t0);
              std::ostringstream out;
              out << "Detection time  : " << std::fixed << std::setprecision(2) << dtime.count()
                  << " ms (" << 1000.f / dtime.count() << " fps)";
              resize(result, result, curr_frame.size());
              putText(curr_frame, out.str(), Point(2020), FONT_HERSHEY_SIMPLEX, 0.75, Scalar(00255), 28);
              addWeighted(result, 0.2, curr_frame, 0.80, curr_frame);
          }


          Python版本

          if exec_net.requests[cur_request_id].wait(-1) == 0:
              # 解析mask輸出
              res = exec_net.requests[cur_request_id].outputs[out_blob]
              # 降維
              res = np.squeeze(res, 0)
              # 矩陣轉(zhuǎn)置
              res = res.transpose((120))
              # 獲取類別 index,
              # 0 - 表示背景,
              # 1 - 道路,
              # 2 - 車道線 ,
              # 3 - 交通標志
              res = np.argmax(res, 2)
              hh, ww = res.shape
              mask = np.zeros((hh, ww, 3), dtype=np.uint8)
              mask[np.where(res > 0)] = (0255255)
              mask[np.where(res > 1)] = (2550255)

              # 顯示mask
              cv2.imshow("segmentation mask", mask)
              mask = cv2.resize(mask, dsize=(frame.shape[1], frame.shape[0]))
              # print("final shape : ", res.shape)
              frame = cv2.addWeighted(mask, 0.4, frame, 0.60)
              inf_end = time.time()
              det_time = inf_end - inf_start

              # 顯示繪制文本
              inf_time_message = "Inference time: {:.3f} ms, FPS:{:.3f}".format(det_time * 10001000 / (det_time * 1000 + 0.1))
              render_time_message = "OpenCV rendering time: {:.3f} ms".format(render_time * 1000)
              async_mode_message = "Async mode is on. Processing request {}".format(cur_request_id) if is_async_mode else \
                  "Async mode is off. Processing request {}".format(cur_request_id)

              cv2.putText(frame, inf_time_message, (1515), cv2.FONT_HERSHEY_COMPLEX, 0.5, (2552550), 1)
              cv2.putText(frame, render_time_message, (1530), cv2.FONT_HERSHEY_COMPLEX, 0.5, (1010200), 1)
              cv2.putText(frame, async_mode_message, (10int(initial_h - 20)), cv2.FONT_HERSHEY_COMPLEX, 0.5,
                          (1010200), 1)

          運行效果

          輸入視頻幀

          道路分割模型輸出mask

          最終顯示效果



          下載1:OpenCV-Contrib擴展模塊中文版教程
          在「小白學(xué)視覺」公眾號后臺回復(fù):擴展模塊中文教程即可下載全網(wǎng)第一份OpenCV擴展模塊教程中文版,涵蓋擴展模塊安裝、SFM算法、立體視覺、目標跟蹤、生物視覺、超分辨率處理等二十多章內(nèi)容。

          下載2:Python視覺實戰(zhàn)項目52講
          小白學(xué)視覺公眾號后臺回復(fù):Python視覺實戰(zhàn)項目即可下載包括圖像分割、口罩檢測、車道線檢測、車輛計數(shù)、添加眼線、車牌識別、字符識別、情緒檢測、文本內(nèi)容提取、面部識別等31個視覺實戰(zhàn)項目,助力快速學(xué)校計算機視覺。

          下載3:OpenCV實戰(zhàn)項目20講
          小白學(xué)視覺公眾號后臺回復(fù):OpenCV實戰(zhàn)項目20講即可下載含有20個基于OpenCV實現(xiàn)20個實戰(zhàn)項目,實現(xiàn)OpenCV學(xué)習(xí)進階。

          交流群


          歡迎加入公眾號讀者群一起和同行交流,目前有SLAM、三維視覺、傳感器、自動駕駛、計算攝影、檢測、分割、識別、醫(yī)學(xué)影像、GAN、算法競賽等微信群(以后會逐漸細分),請掃描下面微信號加群,備注:”昵稱+學(xué)校/公司+研究方向“,例如:”張三 + 上海交大 + 視覺SLAM“。請按照格式備注,否則不予通過。添加成功后會根據(jù)研究方向邀請進入相關(guān)微信群。請勿在群內(nèi)發(fā)送廣告,否則會請出群,謝謝理解~


          瀏覽 36
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  AⅤ一本大道电影 | 国産精品久久久久久久 | 三级在线视频观看 | 波多野久久 | 亚洲天堂网中文字幕 |