Pavan Kunchala commited on
Commit
b534665
1 Parent(s): 88c9e9e
.DS_Store ADDED
Binary file (6.15 kB). View file
 
demo.mp4 ADDED
Binary file (3.23 MB). View file
 
depth_estim_app.py ADDED
@@ -0,0 +1,219 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import sys
2
+ import time
3
+ from pathlib import Path
4
+
5
+ import cv2
6
+ from openvino.inference_engine import IECore
7
+ import matplotlib.cm
8
+ import matplotlib.pyplot as plt
9
+ import numpy as np
10
+ import streamlit as st
11
+ from PIL import Image
12
+ import tempfile
13
+
14
+
15
+ DEMO_IMAGE = 'dog-new.jpg'
16
+
17
+ DEMO_VIDEO = 'demo.mp4'
18
+
19
+
20
+ @st.cache
21
+ def normalize_minmax(data):
22
+
23
+ return (data - data.min()) / (data.max() - data.min())
24
+
25
+ @st.cache
26
+ def convert_result_to_image(result, colormap="inferno"):
27
+
28
+ cmap = matplotlib.cm.get_cmap(colormap)
29
+ result = result.squeeze(0)
30
+ result = normalize_minmax(result)
31
+ result = cmap(result)[:, :, :3] * 255
32
+ result = result.astype(np.uint8)
33
+ return result
34
+
35
+ @st.cache
36
+ def to_rgb(image_data) -> np.ndarray:
37
+
38
+ return cv2.cvtColor(image_data, cv2.COLOR_BGR2RGB)
39
+
40
+
41
+ st.title("Depth Estimation App")
42
+ st.sidebar.title('Depth Estimation')
43
+ st.sidebar.subheader('Parameters')
44
+
45
+ DEVICE = "CPU"
46
+ MODEL_FILE = "models/MiDaS_small.xml"
47
+
48
+ model_xml_path = Path(MODEL_FILE)
49
+
50
+
51
+ ie = IECore()
52
+ net = ie.read_network(model=model_xml_path, weights=model_xml_path.with_suffix(".bin"))
53
+ exec_net = ie.load_network(network=net, device_name=DEVICE)
54
+
55
+ input_key = list(exec_net.input_info)[0]
56
+ output_key = list(exec_net.outputs.keys())[0]
57
+
58
+ network_input_shape = exec_net.input_info[input_key].tensor_desc.dims
59
+ network_image_height, network_image_width = network_input_shape[2:]
60
+
61
+
62
+ app_mode = st.sidebar.selectbox('Choose the App mode',
63
+ ['Run on Image','Run on Video'],index = 0)
64
+
65
+
66
+ if app_mode == "Run on Image":
67
+
68
+
69
+ st.markdown('Running on Image')
70
+
71
+ st.sidebar.text('Params for Image')
72
+ st.markdown(
73
+ """
74
+ <style>
75
+ [data-testid="stSidebar"][aria-expanded="true"] > div:first-child {
76
+ width: 400px;
77
+ }
78
+ [data-testid="stSidebar"][aria-expanded="false"] > div:first-child {
79
+ width: 400px;
80
+ margin-left: -400px;
81
+ }
82
+ </style>
83
+ """,
84
+ unsafe_allow_html=True,
85
+ )
86
+
87
+ img_file_buffer = st.sidebar.file_uploader("Upload an image", type=[ "jpg", "jpeg",'png'])
88
+
89
+ if img_file_buffer is not None:
90
+ image = np.array(Image.open(img_file_buffer))
91
+
92
+ else:
93
+ demo_image = DEMO_IMAGE
94
+ image = np.array(Image.open(demo_image))
95
+
96
+ st.sidebar.text('Original Image')
97
+ st.sidebar.image(image)
98
+ resized_image = cv2.resize(src=image, dsize=(network_image_height, network_image_width))
99
+ # reshape image to network input shape NCHW
100
+ input_image = np.expand_dims(np.transpose(resized_image, (2, 0, 1)), 0)
101
+
102
+
103
+ result = exec_net.infer(inputs={input_key: input_image})[output_key]
104
+ # convert network result of disparity map to an image that shows
105
+ # distance as colors
106
+ result_image = convert_result_to_image(result=result)
107
+ # resize back to original image shape. cv2.resize expects shape
108
+ # in (width, height), [::-1] reverses the (height, width) shape to match this.
109
+ result_image = cv2.resize(result_image, image.shape[:2][::-1])
110
+
111
+
112
+ st.subheader('Output Image')
113
+
114
+ st.image(result_image,use_column_width= True)
115
+
116
+ if app_mode =='Run on Video':
117
+
118
+ st.markdown('Running on Video')
119
+
120
+ use_webcam = st.sidebar.button('Use Webcam')
121
+
122
+ video_file_buffer = st.sidebar.file_uploader("Upload a video", type=[ "mp4", "mov",'avi','asf', 'm4v' ])
123
+
124
+ tfflie = tempfile.NamedTemporaryFile(delete=False)
125
+
126
+ stop_button = st.sidebar.button('Stop Processing')
127
+
128
+ if stop_button:
129
+ st.stop()
130
+
131
+
132
+
133
+ if not video_file_buffer:
134
+ if use_webcam:
135
+ vid = cv2.VideoCapture(0)
136
+
137
+ else:
138
+ vid = cv2.VideoCapture(DEMO_VIDEO)
139
+ tfflie.name = DEMO_VIDEO
140
+
141
+
142
+
143
+ else:
144
+ tfflie.write(video_file_buffer.read())
145
+ vid = cv2.VideoCapture(tfflie.name)
146
+
147
+
148
+
149
+
150
+
151
+
152
+
153
+ width = int(vid.get(cv2.CAP_PROP_FRAME_WIDTH))
154
+ height = int(vid.get(cv2.CAP_PROP_FRAME_HEIGHT))
155
+ fps = int(vid.get(cv2.CAP_PROP_FPS))#codec = cv2.VideoWriter_fourcc(*FLAGS.output_format)
156
+ codec = cv2.VideoWriter_fourcc('X','V','I','D')
157
+ out = cv2.VideoWriter('output_depth.mp4', codec, fps, (width, height))
158
+
159
+ start_time = time.perf_counter()
160
+ total_inference_duration = 0
161
+ stframe = st.empty()
162
+ SCALE_OUTPUT = 1
163
+ st.markdown("**Frame Rate**")
164
+ kpi1_text = st.markdown("0")
165
+ save_video = st.checkbox('Save video')
166
+
167
+ while vid.isOpened():
168
+ ret, image = vid.read()
169
+ new_time = time.time()
170
+ input_video_frame_height, input_video_frame_width = image.shape[:2]
171
+ target_frame_height = int(input_video_frame_height * SCALE_OUTPUT)
172
+ target_frame_width = int(input_video_frame_width * SCALE_OUTPUT)
173
+
174
+
175
+ if not ret:
176
+ vid.release()
177
+ break
178
+ resized_image = cv2.resize(src=image, dsize=(network_image_height, network_image_width))
179
+ # reshape image to network input shape NCHW
180
+ input_image = np.expand_dims(np.transpose(resized_image, (2, 0, 1)), 0)
181
+
182
+ inference_start_time = time.perf_counter()
183
+ result = exec_net.infer(inputs={input_key: input_image})[output_key]
184
+ inference_stop_time = time.perf_counter()
185
+ inference_duration = inference_stop_time - inference_start_time
186
+ total_inference_duration += inference_duration
187
+
188
+
189
+ result_frame = to_rgb(convert_result_to_image(result))
190
+ # Resize image and result to target frame shape
191
+ result_frame = cv2.resize(result_frame, (target_frame_width, target_frame_height))
192
+ image = cv2.resize(image, (target_frame_width, target_frame_height))
193
+ # Put image and result side by side
194
+ stacked_frame = np.vstack((image, result_frame))
195
+ if save_video:
196
+ out.write(stacked_frame)
197
+
198
+ stframe.image(stacked_frame,channels = 'BGR',use_column_width=True)
199
+ fps = 1.0/(time.time() - new_time)
200
+ kpi1_text.write(f"<h1 style='text-align: center; color: red;'>{'{:.1f}'.format(fps)}</h1>", unsafe_allow_html=True)
201
+
202
+
203
+
204
+ vid.release()
205
+ out.release()
206
+ cv2.destroyAllWindows()
207
+ st.success('Video is Processed')
208
+ st.stop()
209
+
210
+
211
+
212
+
213
+
214
+
215
+
216
+
217
+
218
+
219
+
dog-new.jpg ADDED
models/.DS_Store ADDED
Binary file (6.15 kB). View file
 
models/MiDaS_small.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:a040001d3a6e4087d6ac52a79b164118e807f0ea1ac245beaed201ab11832d75
3
+ size 33127812
models/MiDaS_small.xml ADDED
The diff for this file is too large to render. See raw diff
 
requirements.txt ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ moviepy==1.0.3
2
+ openvino==2021.3.0
3
+ numpy==1.18.5
4
+ streamlit
5
+ matplotlib==3.4.3
6
+ opencv_python_headless==4.5.2.54
7
+ Pillow==8.3.2