example5 - denoise.py
Go to the documentation of this file.
1 import pyrealsense2 as rs
2 import numpy as np
3 import cv2
4 from tensorflow import keras
5 import time, sys
6 
7 # Configure depth and color streams
8 pipeline = rs.pipeline()
9 config = rs.config()
10 config.enable_stream(rs.stream.depth, 848, 480, rs.format.z16, 30)
11 config.enable_stream(rs.stream.infrared, 1, 848, 480, rs.format.y8, 30) # 1 for left frame
12 # Start streaming
13 pipeline.start(config)
14 
15 channels = 2
16 cropped_w, cropped_h = 480, 480
17 
18 test_model_name = ""
19 if (len(sys.argv) > 1):
20  test_model_name = str(sys.argv[1])
21 
22 t1 = time.perf_counter()
23 model = keras.models.load_model(test_model_name)
24 t2 = time.perf_counter()
25 print('model loading : ', t2 - t1, 'seconds')
26 
27 def predict(noisy_image, ir_image):
28  t1 = time.perf_counter()
29  ir_image = np.array(ir_image).astype("uint16")
30  cropped_ir , cropped_noisy = [], []
31  width, height = 848, 480
32  w, h = cropped_w, cropped_h
33  for col_i in range(0, width, w):
34  for row_i in range(0, height, h):
35  cropped_ir.append(ir_image[row_i:row_i+h, col_i:col_i+w])
36  cropped_noisy.append(noisy_image[row_i:row_i+h, col_i:col_i+w])
37 
38  # fill with zero to get size 480x480 for both images
39  fill = np.zeros((h, w - cropped_ir[-1].shape[1]), dtype="uint16")
40  cropped_ir[-1] = np.hstack((cropped_ir[-1], fill))
41  cropped_noisy[-1] = np.hstack((cropped_noisy[-1], fill))
42  t2 = time.perf_counter()
43  print('image cropping : ', t2 - t1, 'seconds')
44 
45  cropped_image_offsets = [(0,0), (0,480)]
46  whole_image = np.zeros((height, width, channels), dtype="float32")
47 
48  for i in range(len(cropped_ir)):
49  t1 = time.perf_counter()
50  noisy_images_plt = cropped_noisy[i].reshape(1, cropped_w, cropped_h, 1)
51  ir_images_plt = cropped_ir[i].reshape(1, cropped_w, cropped_h, 1)
52  im_and_ir = np.stack((noisy_images_plt, ir_images_plt), axis=3)
53  im_and_ir = im_and_ir.reshape(1, cropped_w, cropped_h, channels)
54  img = np.array(im_and_ir)
55  # Parse numbers as floats
56  img = img.astype('float32')
57 
58  # Normalize data : remove average then devide by standard deviation
59  img = img / 65535
60  sample = img
61  row, col = cropped_image_offsets[i]
62  t2 = time.perf_counter()
63  print('image channeling : ', t2 - t1, 'seconds')
64 
65  t1 = time.perf_counter()
66  denoised_image = model.predict(sample)
67  t2 = time.perf_counter()
68  print('prediction only : ', t2 - t1, 'seconds')
69  row_end = row + cropped_h
70  col_end = col + cropped_w
71  denoised_row = cropped_h
72  denoised_col = cropped_w
73  if row + cropped_h >= height:
74  row_end = height - 1
75  denoised_row = abs(row - row_end)
76  if col + cropped_w >= width:
77  col_end = width - 1
78  denoised_col = abs(col - col_end)
79  # combine tested images
80  whole_image[row:row_end, col:col_end] = denoised_image[:, 0:denoised_row, 0:denoised_col, :]
81  return whole_image[:, :, 0]
82 
83 #=============================================================================================================
85  m = np.min(i)
86  M = np.max(i)
87  i = np.divide(i, np.array([M - m], dtype=np.float)).astype(np.float)
88  i = (i - m).astype(np.float)
89  i8 = (i * 255.0).astype(np.uint8)
90  if i8.ndim == 3:
91  i8 = cv2.cvtColor(i8, cv2.COLOR_BGRA2GRAY)
92  i8 = cv2.equalizeHist(i8)
93  colorized = cv2.applyColorMap(i8, cv2.COLORMAP_JET)
94  colorized[i8 == int(m)] = 0
95  font = cv2.FONT_HERSHEY_SIMPLEX
96  m = float("{:.2f}".format(m))
97  M = float("{:.2f}".format(M))
98  colorized = cv2.putText(colorized, str(m) + " .. " + str(M) + "[m]", (20, 50), font, 1, (255, 255, 255), 2, cv2.LINE_AA)
99  return colorized
100 
101 
102 try:
103  c = rs.colorizer()
104  while True:
105  print("==============================================================")
106  t0 = time.perf_counter()
107  # Wait for a coherent pair of frames: depth and ir
108  t1 = time.perf_counter()
109  frames = pipeline.wait_for_frames()
110  depth_frame = frames.get_depth_frame()
111  ir_frame = frames.get_infrared_frame()
112  t2 = time.perf_counter()
113  print('getting depth + ir frames : ', t2 - t1, 'seconds')
114 
115  if not depth_frame or not ir_frame:
116  continue
117 
118  # Convert images to numpy arrays
119  t1 = time.perf_counter()
120  depth_image = np.asanyarray(depth_frame.get_data())
121  ir_image = np.asanyarray(ir_frame.get_data())
122  t2 = time.perf_counter()
123  print('convert frames to numpy arrays : ', t2 - t1, 'seconds')
124 
125  t1 = time.perf_counter()
126  predicted_image = predict(depth_image, ir_image)
127  t2 = time.perf_counter()
128  print('processing + prediction : ', t2 - t1, 'seconds')
129 
130  # Stack both images horizontally
131  # depth_image = convert_image(depth_image)
132  t1 = time.perf_counter()
133  depth_image = np.asanyarray(c.process(depth_frame).get_data())
134  predicted_image = convert_image(predicted_image)
135 
136  red = depth_image[:, :, 2].copy()
137  blue = depth_image[:, :, 0].copy()
138  depth_image[:, :, 0] = red
139  depth_image[:, :, 2] = blue
140  images = np.hstack((depth_image, predicted_image))
141 
142  # Show images
143  cv2.namedWindow('RealSense', cv2.WINDOW_AUTOSIZE)
144  cv2.imshow('RealSense', images)
145  cv2.waitKey(1)
146  t2 = time.perf_counter()
147  print('show image : ', t2 - t1, 'seconds')
148  print('TOTAL TIME : ', t2 - t0, 'seconds')
149 
150 finally:
151 
152  # Stop streaming
153  pipeline.stop()
def predict(noisy_image, ir_image)
void reshape(GLFWwindow *window, int w, int h)
Definition: boing.c:215
static std::string print(const transformation &tf)
void copy(void *dst, void const *src, size_t size)
Definition: types.cpp:836


librealsense2
Author(s): Sergey Dorodnicov , Doron Hirshberg , Mark Horn , Reagan Lopez , Itay Carpis
autogenerated on Mon May 3 2021 02:47:14