OpenVINO開發(fā)教程之八 – 道路分割
點擊上方“小白學(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é)果。
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(20, 20), FONT_HERSHEY_SIMPLEX, 0.75, Scalar(0, 0, 255), 2, 8);
addWeighted(result, 0.2, curr_frame, 0.8, 0, 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((1, 2, 0))
# 獲取類別 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)] = (0, 255, 255)
mask[np.where(res > 1)] = (255, 0, 255)
# 顯示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.6, 0)
inf_end = time.time()
det_time = inf_end - inf_start
# 顯示繪制文本
inf_time_message = "Inference time: {:.3f} ms, FPS:{:.3f}".format(det_time * 1000, 1000 / (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, (15, 15), cv2.FONT_HERSHEY_COMPLEX, 0.5, (255, 255, 0), 1)
cv2.putText(frame, render_time_message, (15, 30), cv2.FONT_HERSHEY_COMPLEX, 0.5, (10, 10, 200), 1)
cv2.putText(frame, async_mode_message, (10, int(initial_h - 20)), cv2.FONT_HERSHEY_COMPLEX, 0.5,
(10, 10, 200), 1)
輸入視頻幀

道路分割模型輸出mask

最終顯示效果

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

