forked from F2Wang/ObjectDatasetTools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrecord-astra.py
More file actions
134 lines (102 loc) · 3.8 KB
/
Copy pathrecord-astra.py
File metadata and controls
134 lines (102 loc) · 3.8 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
"""
record.py
---------------
Main Function for recording a video sequence into cad (color-aligned-to-depth)
images and depth images
This code is compatible with legacy camera models supported on librealsense SDK v1
and use 3rd party python wrapper https://github.com/toinsson/pyrealsense
For the newer D series cameras, please use record2.py
"""
# record for 30s after a 5s count down
# or exit the recording earlier by pressing q
RECORD_LENGTH = 30
import png
import json
import logging
logging.basicConfig(level=logging.INFO)
import numpy as np
import cv2
from primesense import openni2
import time
import os
import sys
def make_directories(folder):
if not os.path.exists(folder+"JPEGImages/"):
os.makedirs(folder+"JPEGImages/")
if not os.path.exists(folder+"depth/"):
os.makedirs(folder+"depth/")
def print_usage():
print("Usage: record.py <foldername>")
print("foldername: path where the recorded data should be stored at")
print("e.g., record.py LINEMOD/mug")
if __name__ == "__main__":
try:
folder = sys.argv[1]+"/"
except:
print_usage()
exit()
make_directories(folder)
# save_color_intrinsics(folder)
FileName=0
openni2.initialize("/home/zyf/src/OpenNI-Linux-x64-2.3.0.66/Redist/")
dev = openni2.Device.open_any()
depth_stream = dev.create_depth_stream()
depth_stream.start()
cap = cv2.VideoCapture(0)
# Save color intrinsics to the corresponding folder
# https://github.com/Riotpiaole/rgbd
camera_parameters = {'fx': 553.797, 'fy': 553.722,
'ppx': 320, 'ppy': 240,
'height': 480, 'width': 640,
'depth_scale':0.001}
with open(folder+'intrinsics.json', 'w') as fp:
json.dump(camera_parameters, fp)
# Set frame rate
cnt = 0
last = time.time()
smoothing = 0.9
fps_smooth = 30
T_start = time.time()
while True:
cnt += 1
if (cnt % 10) == 0:
now = time.time()
dt = now - last
fps = 10/dt
fps_smooth = (fps_smooth * smoothing) + (fps * (1.0-smoothing))
last = now
_, c = cap.read()
# d = dev.dac
frame = depth_stream.read_frame()
dframe_data = np.array(frame.get_buffer_as_triplet()).reshape([480, 640, 2])
dpt1 = np.asarray(dframe_data[:, :, 0], dtype='uint16')
dpt2 = np.asarray(dframe_data[:, :, 1], dtype='uint16')
dpt2 *= 255
d = cv2.flip(dpt1 + dpt2,1)
# Visualize count down
if time.time() -T_start > 5:
filecad= folder+"JPEGImages/%s.jpg" % FileName
filedepth= folder+"depth/%s.png" % FileName
cv2.imwrite(filecad,c)
with open(filedepth, 'wb') as f:
writer = png.Writer(width=d.shape[1], height=d.shape[0],
bitdepth=16, greyscale=True)
zgray2list = d.tolist()
writer.write(f, zgray2list)
FileName+=1
if time.time() -T_start > RECORD_LENGTH + 5:
dev.close()
depth_stream.stop()
break
if time.time() -T_start < 5:
cv2.putText(c,str(5-int(time.time() -T_start)),(240,320), cv2.FONT_HERSHEY_SCRIPT_SIMPLEX, 4,(0,0,255),2,cv2.LINE_AA)
if time.time() -T_start > RECORD_LENGTH:
cv2.putText(c,str(RECORD_LENGTH+5-int(time.time()-T_start)),(240,320), cv2.FONT_HERSHEY_SCRIPT_SIMPLEX, 4,(0,0,255),2,cv2.LINE_AA)
cv2.imshow('COLOR FRAME',c)
# press q to quit the program
if cv2.waitKey(1) & 0xFF == ord('q'):
dev.close()
depth_stream.stop()
break
# Release everything if job is finished
cv2.destroyAllWindows()