异步推理-SSD

from openvino.inference_engine import IECore
import cv2 as cv

def ssd_video_demo():
    ie = IECore()
    for device in ie.available_devices:
        print(device)

    with open('object_detection_classes_coco.txt') as f:
        labels = [line.strip() for line in f.readlines()]

    model_xml = "/home/bhc/BHC/model/intel/face-detection-0200/FP16/face-detection-0200.xml"
    model_bin = "/home/bhc/BHC/model/intel/face-detection-0200/FP16/face-detection-0200.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("1.mp4")
    exec_net = ie.load_network(network=net, device_name="CPU", num_requests=2)      #指定两个request
    ret, frame = cap.read()
    curr_request_id = 0
    next_request_id = 1

    while True:
        next_ret, next_frame = cap.read()
        if next_ret is not True:
            break
        image = cv.resize(frame, (w, h))
        image = image.transpose(2, 0, 1)
        # res = exec_net.infer(inputs={input_blob:[image]})
        exec_net.start_async(request_id=next_request_id, inputs={input_blob:[image]})       #异步推理

        # 根据状态检查
        if exec_net.requests[curr_request_id].wait(-1) == 0:                                #等待request推理的状态
            res = exec_net.requests[curr_request_id].output_blobs[out_blob].buffer
            ih, iw, ic = frame.shape
            for obj in res[0][0]:
                if obj[2] > 0.25:
                    index = int(obj[1])-1
                    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, labels[index] + str(obj[2]), (xmin, ymin), cv.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 1, 8)

        # 显示
        cv.imshow("SSD Object Detection Async", frame)
        c = cv.waitKey(1)
        if c == 27:
            break

        # 交换数据
        frame = next_frame
        curr_request_id, next_request_id = next_request_id, curr_request_id                 #相当于一直判断这两个request的推理结果


if __name__ == "__main__":
    ssd_video_demo()

 

posted @ 2022-02-24 16:41  wuyuan2011woaini  阅读(26)  评论(0编辑  收藏  举报