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

模型输入与输出

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:/projects/models/face-detection-0105/FP32/face-detection-0105.xml"
model_bin = "D:/projects/models/face-detection-0105/FP32/face-detection-0105.bin"

log.info("Creating Inference Engine")
ie = IECore()
# Read IR
net = ie.read_network(model=model_xml, weights=model_bin)

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

log.info("Preparing input blobs")
input_it = iter(net.input_info)
input_blob = next(input_it)
print(input_blob)
output_it = iter(net.outputs)
out_labels = next(output_it)
out_boxes = next(output_it)

# Read and pre-process input images
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 synchronous mode")
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_boxes]
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.destroyAllWindows()

运行截图如下:
在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值