-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
161 lines (127 loc) · 6.62 KB
/
app.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import sys
import pyrealsense2 as rs
from PySide6.QtWidgets import QApplication, QMainWindow, QStackedWidget, QMessageBox
from page1 import Page1
from page2 import Page2
from page3 import Page3
import argparse
from typing import Optional
class MainWindow(QMainWindow):
def __init__(self, args: argparse.Namespace) -> None:
self.args = args
if args.bag:
self.bag_file = args.bag
else:
self.bag_file = None
if not args.dir:
args.dir = None
if not args.depth_from_markers:
args.depth_from_markers = False
super().__init__()
self.stacked_widget = QStackedWidget()
self.setCentralWidget(self.stacked_widget)
self.pipeline: Optional[rs.pipeline] = None
# Create instances of pages
self.page2 = Page2(self, self.goto_page3, self.exit_application, args.auto_progress)
self.page3 = Page3(self, self.exit_application, self.pipeline, args.screen, args.dir, args.auto_progress, args.depth_from_markers, args.ir_low_exposure)
self.stacked_widget.addWidget(self.page2)
self.stacked_widget.addWidget(self.page3)
if self.bag_file is None:
self.page1 = Page1(self, self.init_pipeline, self.exit_application)
self.stacked_widget.addWidget(self.page1)
self.stacked_widget.setCurrentWidget(self.page1)
else:
self.init_pipeline()
self.showFullScreen()
def init_pipeline(self) -> None:
# Initialize the RealSense pipeline
self.pipeline = rs.pipeline()
config = rs.config()
try:
motion_support = False
if self.bag_file:
config.enable_device_from_file(self.bag_file)
print(f"Streaming from bag file: {self.bag_file}")
self.pipeline_profile = self.pipeline.start(config)
else:
# Check if any RealSense devices are connected
context = rs.context()
devices = context.query_devices()
if len(devices) == 0:
raise Exception("No RealSense device detected.")
for dev in devices:
for sensor in dev.query_sensors():
if sensor.is_motion_sensor():
motion_support = True
config.enable_stream(rs.stream.color, 848, 480, rs.format.bgr8, 30)
config.enable_stream(rs.stream.depth, 848, 480, rs.format.z16, 30)
config.enable_stream(rs.stream.infrared, 1, 848, 480, rs.format.y8, 30)
if motion_support:
config.enable_stream(rs.stream.accel)
config.enable_stream(rs.stream.gyro)
print("Streaming from RealSense camera.")
self.pipeline_profile = self.pipeline.start(config)
# Set fixed exposure and disable auto exposure for depth stream
depth_sensor = self.pipeline_profile.get_device().first_depth_sensor()
if depth_sensor:
depth_sensor.set_option(rs.option.enable_auto_exposure, 0)
depth_sensor.set_option(rs.option.exposure, self.args.ir_high_exposure)
depth_sensor.set_option(rs.option.laser_power, self.args.laser_power)
color_sensor = self.pipeline_profile.get_device().first_color_sensor()
if color_sensor:
color_sensor.set_option(rs.option.enable_auto_exposure, 0)
color_sensor.set_option(rs.option.exposure, self.args.rgb_exposure)
# Create and configure a temporal filter
self.temporal_filter = rs.temporal_filter()
self.temporal_filter.set_option(rs.option.filter_smooth_alpha, .05)
self.temporal_filter.set_option(rs.option.filter_smooth_delta, 20)
self.hole_filter = rs.hole_filling_filter()
self.hole_filter.set_option(rs.option.holes_fill, 2)
self.align = rs.align(rs.stream.depth)
# Pass the pipeline and filter to Page3
self.page3.pipeline = self.pipeline
self.page3.pipeline_profile = self.pipeline_profile
self.page3.temporal_filter = self.temporal_filter
self.page3.hole_filter = self.hole_filter
self.page3.align = self.align
self.page3.motion_support = motion_support
# Pass the pipeline to Page2
self.page2.pipeline = self.pipeline
self.goto_page2()
except Exception as e:
QMessageBox.critical(self, "Error", str(e))
self.exit_application()
def goto_page3(self) -> None:
self.page3.start()
self.stacked_widget.setCurrentWidget(self.page3)
def goto_page2(self) -> None:
self.page2.start_steps()
self.stacked_widget.setCurrentWidget(self.page2)
def exit_application(self) -> None:
if hasattr(self, 'pipeline') and self.pipeline:
self.pipeline.stop()
sys.exit()
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='RealSense Camera GUI')
parser.add_argument('--display', type=int, default=0, help='Display index to use')
parser.add_argument('--bag', type=str, help='Bag file to stream')
parser.add_argument('--screen', type=int, default=0, help='Screen to save the calibration file for')
parser.add_argument('--dir', type=str, help='Output directory for calibration file')
parser.add_argument('--auto-progress', default=False, action="store_true", help='Enable auto-progress mode')
parser.add_argument('--depth-from-markers', default=False, action="store_true", help='Use depth from markers instead of from screen')
parser.add_argument('--ir-high-exposure', default=1500, type=float, help='IR camera exposure to use when capturing the screen')
parser.add_argument('--ir-low-exposure', default=100, type=float, help='IR camera exposure to use when capturing the markers')
parser.add_argument('--rgb-exposure', default=1500, type=float, help='RGB camera exposure')
parser.add_argument('--laser-power', default=150, type=float, help='Laser dot grid projector power (0-360)')
args = parser.parse_args()
app = QApplication(sys.argv)
screens = QApplication.screens()
if args.display < 0 or args.display >= len(screens):
print(f"Display index {args.display} is out of range. Using default display 0.")
args.display = 0
screen = screens[args.display]
window = MainWindow(args)
window.setGeometry(screen.availableGeometry())
window.showFullScreen()
window.windowHandle().setScreen(screen)
sys.exit(app.exec())