-
Notifications
You must be signed in to change notification settings - Fork 0
/
realsense_capture.py
147 lines (115 loc) · 4.46 KB
/
realsense_capture.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
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
135
136
137
138
139
140
141
142
143
144
145
146
147
import argparse
import ast
import shutil
import time
import uuid
from pathlib import Path
import cv2
import numpy as np
import pyaudio
import pyrealsense2 as rs
import wave
from video_utils import replace_audio
CHUNK = 1024
SAMPLE_FORMAT = pyaudio.paInt16
CHANNELS = 1
SAMPLE_RATE = 16000
WARM_UP_TIME = 1
INFRARED_TMP = '/tmp/infrared.mp4'
COLOUR_TMP = '/tmp/colour.mp4'
AUDIO_PATH = '/tmp/audio.wav'
def main(args):
print(args.phrases)
output_directory = Path(args.output_directory)
if args.redo and output_directory.exists():
shutil.rmtree(output_directory)
output_directory.mkdir(exist_ok=True)
config = rs.config()
config.enable_stream(rs.stream.infrared)
config.enable_stream(rs.stream.color)
for i, phrase in enumerate(args.phrases):
pipeline = rs.pipeline()
pipeline_profile = pipeline.start(config)
# disable dot laser pattern
device = pipeline_profile.get_device()
depth_sensor = device.query_sensors()[0]
depth_sensor.set_option(rs.option.emitter_enabled, 0)
print(f'Recording {i+1}:', phrase)
phrase = phrase.replace('\'', '').replace(' ', '').lower()
infrared_video = cv2.VideoWriter(INFRARED_TMP, cv2.VideoWriter_fourcc(*'mp4v'), 15, (848, 480))
colour_video = cv2.VideoWriter(COLOUR_TMP, cv2.VideoWriter_fourcc(*'mp4v'), 15, (640, 480))
p = pyaudio.PyAudio()
audio_stream = p.open(format=SAMPLE_FORMAT,
channels=CHANNELS,
rate=SAMPLE_RATE,
frames_per_buffer=CHUNK,
input=True)
audio_frames = []
warmed_up = False
warm_up_start = None
try:
while True:
audio_data = audio_stream.read(CHUNK)
# capture frames
frames = pipeline.wait_for_frames()
infrared_frame = cv2.cvtColor(np.asanyarray(frames.get_infrared_frame().get_data()), cv2.COLOR_GRAY2BGR)
colour_frame = cv2.cvtColor(np.asanyarray(frames.get_color_frame().get_data()), cv2.COLOR_RGB2BGR)
if not warmed_up:
if warm_up_start is None:
warm_up_start = time.time()
current_time = time.time()
if current_time - warm_up_start > WARM_UP_TIME:
print('Warmed up...')
warmed_up = True
continue
# save audio
audio_frames.append(audio_data)
# save frames
infrared_video.write(infrared_frame)
colour_video.write(colour_frame)
if args.debug:
cv2.imshow('Infrared', infrared_frame)
cv2.imshow('Colour', colour_frame)
if cv2.waitKey(33) == ord('q'):
raise KeyboardInterrupt()
except KeyboardInterrupt:
audio_stream.stop_stream()
audio_stream.close()
p.terminate()
pipeline.stop()
cv2.destroyAllWindows()
infrared_video.release()
colour_video.release()
# save audio to file
wf = wave.open(AUDIO_PATH, 'wb')
wf.setnchannels(CHANNELS)
wf.setsampwidth(p.get_sample_size(SAMPLE_FORMAT))
wf.setframerate(SAMPLE_RATE)
wf.writeframes(b''.join(audio_frames))
wf.close()
uuid_str = str(uuid.uuid4())
replace_audio(
video_path=INFRARED_TMP,
audio_path=AUDIO_PATH,
output_video_path=output_directory.joinpath(f'{phrase}_infrared_{uuid_str}.mp4')
)
replace_audio(
video_path=COLOUR_TMP,
audio_path=AUDIO_PATH,
output_video_path=output_directory.joinpath(f'{phrase}_colour_{uuid_str}.mp4')
)
if args.pause_every and (i + 1) % args.pause_every == 0:
print('Pausing...')
try:
while True:
time.sleep(10)
except KeyboardInterrupt:
print('Continuing...')
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('phrases', type=ast.literal_eval)
parser.add_argument('output_directory')
parser.add_argument('--pause_every', type=int)
parser.add_argument('--redo', action='store_true')
parser.add_argument('--debug', action='store_true')
main(parser.parse_args())