MyFPGA Forum

 找回密码
 注册
搜索
查看: 3964|回复: 0

OpenVINO使用之行人属性识别

[复制链接]
发表于 2020-6-19 11:49:42 | 显示全部楼层 |阅读模式
本帖最后由 Nicole_Terasic 于 2020-6-19 11:51 编辑

        OpenVINO不仅通过其IE组件实现加速推理,其提供的预训练库还支持各种常见的图像检测、分割、对象识别等的计算机视觉任务。前面小编写过一系列的文章详细介绍过OpenVINO的各种应用,可以看这里回顾一下:

        这里分享一下如何通过OpenVINO提供的行人检测与行人属性识别模型实现一个实时的视频行人检测与属性识别的演示程序。先看一下效果:

图1.jpg 图1

模型

模型来自OpenVINO官方提供的预训练模型库
行人检测模型:

模型名称:
pedestrian-detection-adas-0002
输入格式:NCHW= [1x3x384x672]
输出格式:DetectionOut 类型 [1, 1, N, 7]

基于Caffe SSD MobileNet V1版本训练生成

行人属性识别模型:

模型名称:
person-attributes-recognition-crossroad-0230
输入格式:NCHW=  [1x3x160x80]
输出格式:
输出层有三个,其中输出层名称为435的输出格式为
[1, 8, 1, 1] 是八个属性
另外两个输出层456,459表示两个颜色位置

基于pytorch训练生成的分类网络模型,支持的八种属性与准确率如下:
图2.png 图2

两个模型均可在intel OpenVINO的官方网站下载即可

代码实现与演示

程序基于OpenVINO的异步推断实现了视频实时的行人检测,在行人检测得到行人ROI的基础上,调用行人属性识别模型实现行人属性识别,输出结果显示。首先需要的是加载模型与读取模型的输入与输出层,这部分的代码实现如下:

  1. # 加载MKLDNN - CPU Target
  2. log.basicConfig(format="[ %(levelname)s ] %(message)s", level=log.INFO, stream=sys.stdout)
  3. plugin = IEPlugin(device="CPU", plugin_dirs=plugin_dir)
  4. plugin.add_cpu_extension(cpu_extension)

  5. lut = [];
  6. lut.append((0, 0, 255))
  7. lut.append((255, 0, 0))
  8. lut.append((0, 255, 0))
  9. lut.append((0, 255, 255))
  10. lut.append((255, 0, 255))
  11. # 加载IR
  12. log.info("Reading IR...")
  13. net = IENetwork(model=model_xml, weights=model_bin)
  14. pedestrian_attr_net = IENetwork(model=attribute_xml, weights=attribute_bin)

  15. if plugin.device == "CPU":
  16.     supported_layers = plugin.get_supported_layers(net)
  17.     not_supported_layers = [l for l in net.layers.keys() if l not in supported_layers]
  18.     if len(not_supported_layers) != 0:
  19.         log.error("Following layers are not supported by the plugin for specified device {}:\n {}".
  20.                   format(plugin.device, ', '.join(not_supported_layers)))
  21.         log.error("Please try to specify cpu extensions library path in demo's command line parameters using -l "
  22.                   "or --cpu_extension command line argument")
  23.         sys.exit(1)
  24. assert len(net.inputs.keys()) == 1, "Demo supports only single input topologies"
  25. assert len(net.outputs) == 1, "Demo supports only single output topologies"

  26. # 获取输入输出层
  27. input_blob = next(iter(net.inputs))
  28. out_blob = next(iter(net.outputs))

  29. lm_input_blob = next(iter(pedestrian_attr_net.inputs))
  30. lm_output_blob = next(iter(pedestrian_attr_net.outputs))
  31. log.info("Loading IR to the plugin...")

  32. # 创建可执行网络
  33. exec_net = plugin.load(network=net, num_requests=2)
  34. lm_exec_net = plugin.load(network=pedestrian_attr_net)
  35. n, c, h, w = net.inputs[input_blob].shape
  36. del net

  37. # we did not need pedestrian model any more
  38. mn, mc, mh, mw = pedestrian_attr_net.inputs[lm_input_blob].shape
  39. del pedestrian_attr_net
复制代码
读取视频帧实现对每帧图像的行人检测与属性识别的代码如下:
  1. # 开始检测
  2. while cap.isOpened():
  3.     if is_async_mode:
  4.         ret, next_frame = cap.read()
  5.     else:
  6.         ret, frame = cap.read()
  7.     if not ret:
  8.         break

  9.     # next_frame = cv2.flip(next_frame, 1)

  10.     # 开启同步或者异步执行模式
  11.     inf_start = time.time()
  12.     if is_async_mode:
  13.         in_frame = cv2.resize(next_frame, (w, h))
  14.         in_frame = in_frame.transpose((2, 0, 1))  # Change data layout from HWC to CHW
  15.         in_frame = in_frame.reshape((n, c, h, w))
  16.         exec_net.start_async(request_id=next_request_id, inputs={input_blob: in_frame})
  17.     else:
  18.         in_frame = cv2.resize(frame, (w, h))
  19.         in_frame = in_frame.transpose((2, 0, 1))  # Change data layout from HWC to CHW
  20.         in_frame = in_frame.reshape((n, c, h, w))
  21.         exec_net.start_async(request_id=cur_request_id, inputs={input_blob: in_frame})
  22.     if exec_net.requests[cur_request_id].wait(-1) == 0:

  23.         # 解析DetectionOut
  24.         res = exec_net.requests[cur_request_id].outputs[out_blob]
  25.         for obj in res[0][0]:
  26.             # Draw only objects when probability more than specified threshold
  27.             if obj[2] > 0.5:
  28.                 xmin = int(obj[3] * initial_w)
  29.                 ymin = int(obj[4] * initial_h)
  30.                 xmax = int(obj[5] * initial_w)
  31.                 ymax = int(obj[6] * initial_h)
  32.                 class_id = int(obj[1])

  33.                 # Draw box and label\class_id
  34.                 cv2.rectangle(frame, (xmin, ymin), (xmax, ymax), (0, 0, 255), 2)
  35.                 if xmin > 0 and ymin > 0 and (xmax < initial_w) and (ymax < initial_h):
  36.                     roi = frame[ymin:ymax, xmin:xmax, :]
  37.                     pedestrian_roi = cv2.resize(roi, (mw, mh))
  38.                     pedestrian_roi = pedestrian_roi.transpose((2, 0, 1))
  39.                     pedestrian_roi = pedestrian_roi.reshape((mn, mc, mh, mw))

  40.                     # 行人属性识别
  41.                     lm_exec_net.infer(inputs={'0': pedestrian_roi})
  42.                     attr_res = lm_exec_net.requests[0].outputs[lm_output_blob]
  43.                     attr_res = np.reshape(attr_res, (8, 1))

  44.                     # 解析行人八个属性指标
  45.                     for i in range(len(attrs)):
  46.                         if attr_res[i][0] > 0.5:
  47.                             cv2.putText(frame, attrs[i] + ": " + str(1),
  48.                                         (xmin+30, ymin+20*i),
  49.                                         cv2.FONT_HERSHEY_COMPLEX, 0.5, (0, 0, 255), 1)
  50.                         else:
  51.                             cv2.putText(frame, attrs[i] + ": " + str(0),
  52.                                         (xmin + 30, ymin + 20 * i),
  53.                                         cv2.FONT_HERSHEY_COMPLEX, 0.5, (0, 0, 255), 1)
  54.                     cv2.putText(frame, "Person" + ' ' + str(round(obj[2] * 100, 1)) + ' %', (xmin, ymin - 7),
  55.                                 cv2.FONT_HERSHEY_COMPLEX, 0.5, (255, 0, 0), 1)
  56.                     cv2.imwrite("D:/reslut.png", frame)
  57.         inf_end = time.time()
  58.         det_time = inf_end - inf_start

  59.         # 显示绘制文本
  60.         inf_time_message = "Inference time: {:.3f} ms, FPS:{:.3f}".format(det_time * 1000, 1000 / (det_time * 1000 + 1))
  61.         render_time_message = "OpenCV rendering time: {:.3f} ms".format(render_time * 1000)
  62.         async_mode_message = "Async mode is on. Processing request {}".format(cur_request_id) if is_async_mode else \
  63.             "Async mode is off. Processing request {}".format(cur_request_id)

  64.         cv2.putText(frame, inf_time_message, (15, 15), cv2.FONT_HERSHEY_COMPLEX, 0.5, (255, 255, 0), 1)
  65.         cv2.putText(frame, render_time_message, (15, 30), cv2.FONT_HERSHEY_COMPLEX, 0.5, (10, 10, 200), 1)
  66.         cv2.putText(frame, async_mode_message, (10, int(initial_h - 20)), cv2.FONT_HERSHEY_COMPLEX, 0.5,
  67.                     (10, 10, 200), 1)

  68.     # 显示
  69.     render_start = time.time()
  70.     cv2.imshow("OpenVINO-face-landmark-detection@57558865", frame)
  71.     render_end = time.time()
  72.     render_time = render_end - render_start

  73.     # ready for next frame
  74.     if is_async_mode:
  75.         cur_request_id, next_request_id = next_request_id, cur_request_id
  76.         frame = next_frame

  77.     key = cv2.waitKey(50)
  78.     if key == 27:
  79.         break
复制代码
使用两段视频测试截图分别如下:
图3.jpg 图3
图4.jpg 图4

转自:https://mp.weixin.qq.com/s/sNHXY9boxMDz8-hbQAXA3w
作者:gloomyfish  OpenCV学堂

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|Archiver|MyFPGA

GMT+8, 2024-3-28 21:41 , Processed in 0.045252 second(s), 19 queries .

Powered by Discuz! X3

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表