分割网络
from openvino.inference_engine import IECore import numpy as np import time import cv2 as cv def ssd_video_demo(): ie = IECore() for device in ie.available_devices: print(device) model_xml = "/home/bhc/BHC/model/intel/semantic-segmentation-adas-0001/FP16/semantic-segmentation-adas-0001.xml" model_bin = "/home/bhc/BHC/model/intel/semantic-segmentation-adas-0001/FP16/semantic-segmentation-adas-0001.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("2.mp4") exec_net = ie.load_network(network=net, device_name="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 res = res[out_blob] #(1, 1, 1024, 2048) 1:每一个像素的类别的标签 res = np.squeeze(res, 0) res = res.transpose(1, 2, 0) # res = np.squeeze(res, 2) hh, ww, c = res.shape res = res.reshape((hh, ww)) mask = np.zeros((hh, ww, 3), dtype=np.uint8) for i in range(20): #20个类别,每个对象进行颜色变更 mask[np.where(res > i)] = (np.random.randint(0, 255), np.random.randint(0, 255), np.random.randint(0, 255)) mask = cv.resize(mask, (frame.shape[1], frame.shape[0])) result = cv.addWeighted(frame, 0.5, mask, 0.5, 0) #进行融合 cv.putText(result, "infer time(ms): %.3f, FPS: %.2f"%(inf_end*1000, 1/(inf_end+0.0001)), (10, 50), cv.FONT_HERSHEY_SIMPLEX, 1.0, (255, 0, 255), 2, 8) cv.imshow("semantic segmentation Demo", result) c = cv.waitKey(1) if c == 27: break cv.waitKey(0) cv.destroyAllWindows() if __name__ == "__main__": ssd_video_demo()
天道酬勤 循序渐进 技压群雄