ssd对象检测

  1 from openvino.inference_engine import IECore
  2 import numpy as np
  3 import time
  4 import cv2 as cv
  5 
  6 # 图像识别
  7 def ssd_image_demo():
  8     ie = IECore()
  9     for device in ie.available_devices:
 10         print(device)
 11 
 12     with open('object_detection_classes_coco.txt') as f:
 13         labels = [line.strip() for line in f.readlines()]
 14 
 15     model_xml = "ssdv2_graph.xml"
 16     model_bin = "ssdv2_graph.bin"
 17 
 18     net = ie.read_network(model=model_xml, weights=model_bin)
 19     input_blob = next(iter(net.input_info))
 20     out_blob = next(iter(net.outputs))
 21 
 22     n, c, h, w = net.input_info[input_blob].input_data.shape            #输入BCHW
 23     print(n, c, h, w)
 24 
 25     src = cv.imread("001.jpg")
 26     image = cv.resize(src, (w, h))
 27     image = image.transpose(2, 0, 1)                                    #HWC->CHW
 28 
 29     exec_net = ie.load_network(network=net, device_name="CPU")
 30     res = exec_net.infer(inputs={input_blob:[image]})
 31 
 32     ih, iw, ic = src.shape
 33     res = res[out_blob]                                                 #输出(1, 1, N, 7),
 34     for obj in res[0][0]:
 35         if obj[2] > 0.25:                                               #[image_id, label, conf, x_min, y_min, x_max, y_max]
 36             index = int(obj[1])-1
 37             print(index)
 38             xmin = int(obj[3] * iw)
 39             ymin = int(obj[4] * ih)
 40             xmax = int(obj[5] * iw)
 41             ymax = int(obj[6] * ih)
 42             print(labels[index])
 43             cv.rectangle(src, (xmin, ymin), (xmax, ymax), (0, 255, 255), 2, 8)
 44             cv.putText(src, labels[index] + str(obj[2]), (xmin, ymin), cv.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 1, 8)
 45     cv.imshow("SSD Object Detection", src)
 46     cv.waitKey(0)
 47 
 48 # 视频识别
 49 def ssd_video_demo():
 50     ie = IECore()
 51     for device in ie.available_devices:
 52         print(device)
 53 
 54     with open('object_detection_classes_coco.txt') as f:
 55         labels = [line.strip() for line in f.readlines()]
 56 
 57     model_xml = "ssdv2_graph.xml"
 58     model_bin = "ssdv2_graph.bin"
 59 
 60     net = ie.read_network(model=model_xml,weights=model_bin)
 61     input_blob = next(iter(net.input_info))
 62     out_blob = next(iter(net.outputs))
 63 
 64     n, c, h, w = net.input_info[input_blob].input_data.shape
 65     print(n, c, h, w)
 66 
 67     cap = cv.VideoCapture("1.mp4")
 68     exec_net = ie.load_network(network=net, device_name="CPU")
 69 
 70     while True:
 71         ret, frame = cap.read()
 72         if ret is not True:
 73             break
 74         image = cv.resize(frame, (w, h))
 75         image = image.transpose(2, 0, 1)
 76         inf_start = time.time()
 77         res = exec_net.infer(inputs={input_blob:[image]})
 78         inf_end = time.time() - inf_start
 79         print("infer time(ms):%.3f"%(inf_end*1000))            #推理时间
 80         ih, iw, ic = frame.shape
 81         res = res[out_blob]
 82         for obj in res[0][0]:
 83             if obj[2] > 0.25:
 84                 index = int(obj[1])-1
 85                 xmin = int(obj[3] * iw)
 86                 ymin = int(obj[4] * ih)
 87                 xmax = int(obj[5] * iw)
 88                 ymax = int(obj[6] * ih)
 89                 print(labels[index])
 90                 cv.rectangle(frame, (xmin, ymin), (xmax, ymax), (0, 255, 255), 2, 8)
 91                 cv.putText(frame, "infer time(ms): %.3f"%(inf_end*1000), (50, 50), cv.FONT_HERSHEY_SIMPLEX, 1.0, (255, 0, 255),
 92                            2, 8)
 93                 cv.putText(frame, labels[index] + str(obj[2]), (xmin, ymin), cv.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 1, 8)
 94         cv.imshow("SSD Object Detection", frame)
 95         c = cv.waitKey(1)
 96         if c == 27:
 97             break
 98 
 99 
100 if __name__ == "__main__":
101     ssd_video_demo()

 

posted @ 2022-02-23 13:39  wuyuan2011woaini  阅读(112)  评论(0编辑  收藏  举报