OpenVINO中的ATSS与FCOS人脸检测模型代码演示

小o 更新于 7月前

模型输入与输出

OpenVINO 2020R04版本的官方模型库中有两个人脸检测模型标号分别为:
face-detection-0105 – MobileNetv2 + FCOS
face-detection-0106 – RestNet152 + ATSS

这里需要注意一下,FCOS与ATSS模型检测头输出跟SSD模型不同,官方支持的IR文件有两个输出数据分别是:
boxes: [Nx5],
labels:[N]

其中N表示得到预测框个数,5表示预测框的左上角与右下角坐标,加上分类得分-5个元素为:[x_min, y_min, x_max, y_max, conf]。这里需要特别注意的是,跟SSD与FasterRCNN对象检测网络不同,它输出的是基于输入图像大小的真实坐标信息,不是0~1之间的值。输入格式是:NCHW=[1xNxHxW],图像通道顺序BGR。

下载这两个模型,只需要执行下面的脚本语句即可:

就可以完成下载!

代码实现

代码实现基于OpenVINO Python SDK,以FCOS为例!首先加载模型,代码如下:

from __future__ import print_function
import cv2
import time
import logging as log
from openvino.inference_engine import IECore
model_xml = "D:/project***odels/face-detection-0105/FP32/face-detection-0105.xml"
model_bin = "D:/project***odels/face-detection-0105/FP32/face-detection-0105.bin"
log.info("Creating Inference Engine")
ie = IECore()
# Read IR
net = ie.read_network(model=model_xml, weight***odel_bin)

获取输入与输出格式化信息

log.info("Preparing input blob*****r>input_it = iter(net.input_info)
input_blob = next(input_it)
print(input_blob)
output_it = iter(net.output****r>out_labels = next(output_it)
out_boxes = next(output_it)
# Read and pre-process input image***r>print(net.input_info[input_blob].input_data.shape)

对输入数据实现转换为NCHW格式

image = cv2.imread("D:/images/selfie.jpg")
ih, iw, ic = image.shape
rh = ih / 416
rw = iw / 416
print("rate of height:",rh, "rate of widht:",rw)
image_blob = cv2.resize(image, (416, 416))
image_blob = image_blob.transpose(2, 0, 1) # Change data layout from HWC to CHW
# Loading model to the plugin
exec_net = ie.load_network(network=net, device_name="CPU")

执行推理


# Start sync inference
log.info("Starting inference in synchronou***ode")
inf_start1 = time.time()
res = exec_net.infer(inputs={input_blob:[image_blob]})
inf_end1 = time.time() - inf_start1
print("inference time(ms) : %.3f" % (inf_end1 * 1000))

解析输出


# Processing output blob
log.info("Processing output blob")
res = res[out_boxe****r>face_cnt = 0
for obj in res:
if float(obj[4]) > 0.25:
print(obj)
face_cnt += 1
xmin = int(obj[0]*rw)
ymin = int(obj[1]*rh)
xmax = int(obj[2]*rw)
ymax = int(obj[3]*rh)
cv2.rectangle(image, (xmin, ymin), (xmax, ymax), (0, 0, 255), 2, 8, 0)
cv2.putText(image, "Total Detected Face: %d"%face_cnt, (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 1.0, (0, 0, 255), 2)
cv2.imshow("FOCS Face detection", image)
print("total faces : ", face_cnt)
cv2.imwrite("D:/result_fcos.png", image)
cv2.waitKey(0)
cv2.destroyAllWindow******lockquote>
运行截图如下:

0个评论