1 import pyrealsense2
as rs
4 from tensorflow
import keras
8 pipeline = rs.pipeline()
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)
13 pipeline.start(config)
16 cropped_w, cropped_h = 480, 480
19 if (len(sys.argv) > 1):
20 test_model_name =
str(sys.argv[1])
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')
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])
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')
45 cropped_image_offsets = [(0,0), (0,480)]
46 whole_image = np.zeros((height, width, channels), dtype=
"float32")
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)
56 img = img.astype(
'float32')
61 row, col = cropped_image_offsets[i]
62 t2 = time.perf_counter()
63 print(
'image channeling : ', t2 - t1,
'seconds')
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:
75 denoised_row = abs(row - row_end)
76 if col + cropped_w >= width:
78 denoised_col = abs(col - col_end)
80 whole_image[row:row_end, col:col_end] = denoised_image[:, 0:denoised_row, 0:denoised_col, :]
81 return whole_image[:, :, 0]
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)
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)
105 print(
"==============================================================")
106 t0 = time.perf_counter()
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')
115 if not depth_frame
or not ir_frame:
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')
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')
132 t1 = time.perf_counter()
133 depth_image = np.asanyarray(c.process(depth_frame).get_data())
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))
143 cv2.namedWindow(
'RealSense', cv2.WINDOW_AUTOSIZE)
144 cv2.imshow(
'RealSense', images)
146 t2 = time.perf_counter()
147 print(
'show image : ', t2 - t1,
'seconds')
148 print(
'TOTAL TIME : ', t2 - t0,
'seconds')
def predict(noisy_image, ir_image)
void reshape(GLFWwindow *window, int w, int h)
static std::string print(const transformation &tf)
void copy(void *dst, void const *src, size_t size)