forked from leeesangwon/ICNet-tensorflow
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathopenvino_icnet_test.py
94 lines (78 loc) · 3.14 KB
/
openvino_icnet_test.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
import sys
import cv2
import numpy as np
from PIL import Image
import time
from openvino.inference_engine import IENetwork, IEPlugin
model_xml='/opt/intel/computer_vision_sdk_2018.4.420/deployment_tools/model_optimizer/10_lrmodels/ICNet/FP32/semanticsegmentation_ICNet.xml'
model_bin='/opt/intel/computer_vision_sdk_2018.4.420/deployment_tools/model_optimizer/10_lrmodels/ICNet/FP32/semanticsegmentation_ICNet.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
index_void = 2 # Define index_void Back Ground
camera_width = 320
camera_height = 240
fps = ""
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="CPU")
plugin.add_cpu_extension("/home/alpha/inference_engine_samples_build/intel64/Release/lib/libcpu_extension.so")
exec_net = plugin.load(network=net)
input_blob = next(iter(net.inputs)) #input_blob = 'input'
out_blob = next(iter(net.outputs)) #out_blob = 'ResizeBilinear_19'
print(net.inputs[input_blob].shape)
h, w, c = net.inputs[input_blob].shape #h, w, c = 256, 512, 3
del net
cap = cv2.VideoCapture(0)
cap.set(cv2.CAP_PROP_FPS, 30)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, camera_width)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, camera_height)
time.sleep(1)
while cap.isOpened():
t1 = time.time()
#ret, frame = cap.read()
#if not ret:
# break
frame = cv2.imread('data/input/000003.jpg')
prepimg = frame[:, :, ::-1].copy()
prepimg = frame
prepimg = Image.fromarray(prepimg)
prepimg = prepimg.resize((512, 256), Image.ANTIALIAS)
if prepimg.mode == "RGBA":
prepimg = prepimg.convert("RGB")
#prepimg = np.asarray(prepimg) / 255.0
#print(np.asarray(prepimg).shape)
#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, 19, 256, 512)
print(outputs[0].shape)
print("SegmentationTime = {:.7f}".format(time.perf_counter() - t2))
outputs = outputs[0] # (256, 512, 19)
outputs = np.argmax(outputs, axis=0) # (256, 512)
print(outputs.shape)
# View
image = Image.fromarray(np.uint8(outputs), mode="P")
image.putpalette(palette)
image = image.convert("RGB")
#image = image.resize((camera_width, camera_height)) # (320, 240)
image = image.resize((512, 256)) # (512, 256)
image.save("2.jpg")
image = np.asarray(image)
print("frame =", frame.shape)
print("image =", image.shape)
#image = 0.5 * frame + 0.5 * image
sys.exit(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)
cv2.destroyAllWindows()
del exec_net
del plugin