-
Notifications
You must be signed in to change notification settings - Fork 3
/
openvino_test_CPU.py
99 lines (81 loc) · 3.05 KB
/
openvino_test_CPU.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/usr/bin/env python
import sys
import cv2
import numpy as np
from PIL import Image
import time
from openvino.inference_engine import IENetwork, IEPlugin
model_xml='lrmodels/FP32/semantic-segmentation-adas-0001.xml'
model_bin='lrmodels/FP32/semantic-segmentation-adas-0001.bin'
net = IENetwork.from_ir(model=model_xml, weights=model_bin)
seg_image = Image.open("data/input/009649.png")
palette = seg_image.getpalette() # Get a color palette
camera_width = 320
camera_height = 240
fps = ""
framepos = 0
frame_count = 0
vidfps = 0
elapsedTime = 0
#plugin = IEPlugin(device="HETERO:MYRIAD,CPU")
#plugin.set_config({"TARGET_FALLBACK": "HETERO:MYRIAD,CPU"})
#plugin.set_initial_affinity(net)
#plugin = IEPlugin(device="MYRIAD")
#plugin = IEPlugin(device="GPU")
plugin = IEPlugin(device="CPU")
plugin.add_cpu_extension("lib/libcpu_extension.so")
exec_net = plugin.load(network=net)
input_blob = next(iter(net.inputs)) #input_blob = "data"
out_blob = next(iter(net.outputs)) #out_blob = "argmax"
print("input_blob =", input_blob)
print("out_blob =", out_blob)
n, c, h, w = net.inputs[input_blob].shape #n, c, h, w = 1, 3, 1024, 2048
del net
#cap = cv2.VideoCapture(0)
#cap.set(cv2.CAP_PROP_FPS, 10)
#cap.set(cv2.CAP_PROP_FRAME_WIDTH, camera_width)
#cap.set(cv2.CAP_PROP_FRAME_HEIGHT, camera_height)
cap = cv2.VideoCapture("data/input/testvideo.mp4")
camera_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
camera_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
frame_count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
vidfps = int(cap.get(cv2.CAP_PROP_FPS))
print("videosFrameCount =", str(frame_count))
print("videosFPS =", str(vidfps))
time.sleep(1)
while cap.isOpened():
t1 = time.time()
cap.set(cv2.CAP_PROP_POS_FRAMES, framepos)
ret, frame = cap.read()
if not ret:
break
#frame = cv2.imread('data/input/000003.jpg')
prepimg = frame[:, :, ::-1].copy()
prepimg = Image.fromarray(prepimg)
prepimg = prepimg.resize((2048, 1024), Image.ANTIALIAS)
prepimg = np.asarray(prepimg)
prepimg = prepimg.transpose((2, 0, 1)).reshape((1, c, h, w))
t2 = time.perf_counter()
exec_net.start_async(request_id=0, inputs={input_blob: prepimg})
if exec_net.requests[0].wait(-1) == 0:
outputs = exec_net.requests[0].outputs[out_blob] # (1, 1, 1024, 2048)
print("SegmentationTime = {:.7f}".format(time.perf_counter() - t2))
outputs = outputs[0][0]
print(outputs.shape)
outputs = cv2.resize(outputs, (camera_width, camera_height))
# View
image = Image.fromarray(np.uint8(outputs), mode="P")
image.putpalette(palette)
image = image.convert("RGB")
image = np.asarray(image)
image = cv2.addWeighted(frame, 1, image, 0.9, 0)
cv2.putText(image, fps, (camera_width-180,15), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (38,0,255), 1, cv2.LINE_AA)
cv2.imshow("Result", image)
if cv2.waitKey(1)&0xFF == ord('q'):
break
elapsedTime = time.time() - t1
fps = "(Playback) {:.1f} FPS".format(1/elapsedTime)
framepos += vidfps
cv2.destroyAllWindows()
del exec_net
del plugin