工具:pycharm,opencv,NCS2计算棒
语言:python
这代码是我从openvino中文社区学习来的,大家要是想直接用,就可以直接用,想明白代码是怎么回事,建议去B站的openvino中文社区里找视频学习,我只是一个搬运工。
如有侵权,联系删除
from openvino.inference_engine import IECore
import time
import cv2 as cv
def face_detect_demo():
ie = IECore()
for device in ie.available_devices:
print(device)
model_xml = "D:/APP/pycharm/project/face-detection-0102/FP32/face-detection-0102.xml"
model_bin = "D:/APP/pycharm/project/face-detection-0102/FP32/face-detection-0102.bin"
net = ie.read_network(model=model_xml, weights=model_bin)
input_blob = next(iter(net.input_info))
out_blob = next(iter(net.outputs))
n, c, h, w = net.input_info[input_blob].input_data.shape
print(n, c, h, w)
cap = cv.VideoCapture("D:/APP/pycharm/project/11.mp4")//将视频文件改成自己的文件
exec_net = ie.load_network(network=net, device_name="MYRIAD")//使用NCS2计算棒
#exec_net = ie.load_network(network=net, device_name="CPU")//使用电脑的CPU
while True:
ret, frame = cap.read()
if ret is not True:
break
image = cv.resize(frame, (w, h))
image = image.transpose(2, 0, 1)
inf_start = time.time()
res = exec_net.infer(inputs={input_blob: [image]})
inf_end = time.time() - inf_start
print("infer time(ms):%.3f"%(inf_end*1000))
ih, iw, ic = frame.shape
res = res[out_blob]
for obj in res[0][0]:
if obj[2] > 0.75:
xmin = int(obj[3] * iw)
ymin = int(obj[4] * ih)
xmax = int(obj[5] * iw)
ymax = int(obj[6] * ih)
cv.rectangle(frame, (xmin, ymin), (xmax, ymax), (0, 255, 255), 2, 8)
cv.putText(frame, "infer time(ms): %.3f" % (inf_end * 1000), (50, 50), cv.FONT_HERSHEY_SIMPLEX, 1.0,
(255, 0, 255),
2, 8)
cv.imshow("Face Detection", frame)
c = cv.waitKey(1)
if c == 27:
break
if __name__=="__main__":
face_detect_demo()