前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >系列 | OpenVINO视觉加速库使用四

系列 | OpenVINO视觉加速库使用四

作者头像
OpenCV学堂
发布2019-04-29 17:08:45
1.3K1
发布2019-04-29 17:08:45
举报

基本思路

使用对象检测网络MobileNet SSD V2版本实现车辆与车牌检测,对得到车辆与车牌ROI对象,分别送到后续的车辆属性识别网络与车牌识别网络中,实现对车辆属性(颜色与车辆类型)识别输出与车牌识别输出。图示如下:

其中车辆属性识别模型输入大小为72x72,支持的颜色与车辆类别如下:

车牌识别网络支持中国所有的省市车牌,模型使用1165张中国各个省份数据作为验证集验证,准确率高达95%以上,能很好识别旋转/错切的车牌。支持输入车牌HXW=24X94

代码实现与运行

加载插件

注意可以使用多个插件,对不同的网络使用不同的插件作为计算后台,OpenVINO是支持这种方式,因为我的机器只有CPU,所以就加载了CPU插件,代码如下:

代码语言:javascript
复制
// 创建IE插件
InferenceEnginePluginPtr engine_ptr = PluginDispatcher(dirs).getSuitablePlugin(TargetDevice::eCPU);
InferencePlugin plugin(engine_ptr);

// 加载CPU扩展库支持
plugin.AddExtension(std::make_shared<Extensions::Cpu::CpuExtensions>());

加载检测与识别网络

需要预先加载对象检测与识别网络,加载好的网络信息,存储到如下结构体中:

代码语言:javascript
复制
struct MyDetectionNet {
    ExecutableNetwork net;
    std::string inputName;
    std::string sencondOutputName;
    std::string outputName;
};

加载网络的代码如下:

代码语言:javascript
复制
// 加载车辆与车牌检测网络
loadVehiclePlateNetWork(plugin);

// 加载车辆属性识别网络
loadVehicleAttributesNetWork(plugin);

// 加载车牌识别网络
loadVehicleLicenseNetWork(plugin);

输入图像数据,开始执行网络预测推断,这个部分代码实现如下:

代码语言:javascript
复制
/** Getting input blob **/
auto input = vehicleplateDetectorinfer.GetBlob(vehicleDetector.inputName);
size_t num_channels = input->getTensorDesc().getDims()[1];
size_t h = input->getTensorDesc().getDims()[2];
size_t w = input->getTensorDesc().getDims()[3];
size_t image_size = h*w;
Mat blob_image;
resize(src, blob_image, Size(h, w));

// NCHW
unsigned char* data = static_cast<unsigned char*>(input->buffer());
for (size_t row = 0; row < h; row++) {
    for (size_t col = 0; col < w; col++) {
        for (size_t ch = 0; ch < num_channels; ch++) {
            data[image_size*ch + row*w + col] = blob_image.at<Vec3b>(row, col)[ch];
        }
    }
}

// 执行预测
vehicleplateDetectorinfer.Infer();

解析输出实现车辆属性识别与车牌识别

车牌识别

代码语言:javascript
复制
Rect roi;
roi.x = rect.x - padding;
roi.y = rect.y - padding;
roi.width = rect.width + padding*2;
roi.height = rect.height + padding*2;
LicensePlateObject lpo;
lpo.location = roi;
fetchLicenseText(src(roi), lpo);
putText(src,  lpo.text.c_str(), Point(roi.x-40, roi.y - 10), FONT_HERSHEY_SIMPLEX, 0.8, Scalar(0, 0, 255), 2, 8);

车辆属性识别

代码语言:javascript
复制
VehicleObject vo;
vo.location = rect;
fetchVehicleAttributes(src(rect), vo);
putText(src, format("vehicle color: %s", vo.color.c_str()), Point(rect.x, rect.y - 20), FONT_HERSHEY_SIMPLEX, 1.0, Scalar(255, 0, 255), 2, 8);
putText(src, format("vehicle type: %s", vo.type.c_str()), Point(rect.x, rect.y - 40), FONT_HERSHEY_SIMPLEX, 1.0, Scalar(255, 0, 255), 2, 8);

车辆属性检测网络,输入,推断与解析输出的函数实现代码如下:

代码语言:javascript
复制
// 设置输入数据
Blob::Ptr inputBlob = vehicleAttrDetectorInfer.GetBlob(attrDetector.inputName);
matU8ToBlob<uint8_t>(blob, inputBlob);

// 执行推断图
vehicleAttrDetectorInfer.Infer();

// 获取输出结果
auto colorsValues = vehicleAttrDetectorInfer.GetBlob(attrDetector.outputName)->buffer().as<float*>();
auto typesValues = vehicleAttrDetectorInfer.GetBlob(attrDetector.sencondOutputName)->buffer().as<float*>();

// 获取最大可能行
const auto color_id = max_element(colorsValues, colorsValues + 7) - colorsValues;
const auto type_id = max_element(typesValues, typesValues + 4) - typesValues;

info.color = colors[color_id];
info.type = types[type_id];

车牌识别网络的输入,推断与解析输出的函数实现代码如下:

代码语言:javascript
复制
// 执行推断
plateLicenseRecognizerInfer.Infer();

// 解析输出结果
const auto data = plateLicenseRecognizerInfer.GetBlob(PLRDetector.outputName)->buffer().as<float*>();
string result;
for (int i = 0; i < maxSequenceSizePerPlate; i++) {
    if (data[i] == -1)
        break;
    result += items[data[i]];
}
info.text = result;

程序执行结果

行驶中抓拍车辆属性与车牌识别

静止状态下车辆属性与车牌识别

请给我一个【在看】

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2019-04-08,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 OpenCV学堂 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
汽车相关识别
汽车相关识别(Vehicle Optical Character Recognition,Vehicle OCR)基于行业前沿的深度学习技术,提供驾驶证识别、行驶证识别、车牌识别、车辆 VIN 码识别等多种服务,支持将图片上的文字内容,智能识别为结构化的文本,应用于车主身份认证、ETC 出行、违章识别、停车管理等多种场景,大幅提升信息处理效率。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档