-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_multi_camera.py
More file actions
325 lines (257 loc) Β· 10.6 KB
/
example_multi_camera.py
File metadata and controls
325 lines (257 loc) Β· 10.6 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
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
"""
Multi-Camera GoPro Control Example
Controls 3 GoPro cameras simultaneously via USB
"""
from gopro_usb import GoProUSB
import time
import threading
from concurrent.futures import ThreadPoolExecutor, as_completed
# GoPro Serial Numbers
SN1 = "C3504224696431"
SN2 = "C3504224677229"
SN3 = "C3504224682139"
CAMERAS = {
"Camera 1": SN1,
"Camera 2": SN2,
"Camera 3": SN3,
}
def power_on_camera(name: str, gopro: GoProUSB) -> bool:
"""Power on a single camera"""
try:
print(f"π [{name}] Powering on...")
result = gopro.power_on()
if result:
print(f"β
[{name}] Powered on successfully")
else:
print(f"β [{name}] Failed to power on")
return result
except Exception as e:
print(f"β [{name}] Error: {e}")
return False
def configure_camera(name: str, gopro: GoProUSB) -> bool:
"""Configure a single camera (4K @ 120 FPS, Linear)"""
try:
print(f"βοΈ [{name}] Configuring...")
gopro.mode_video()
time.sleep(1)
gopro.set_resolution_4k()
time.sleep(1)
gopro.set_fps_120()
time.sleep(1)
gopro.set_lens_linear()
time.sleep(1)
# Verify configuration was applied
state = gopro.get_state()
actual_fps = gopro._get_fps_name(state['settings'].get('3', 'N/A'))
actual_res = gopro._get_resolution_name(state['settings'].get('2', 'N/A'))
actual_lens = gopro._get_lens_name(state['settings'].get('121', 'N/A'))
print(f"β
[{name}] Configured: {actual_res} @ {actual_fps}, {actual_lens}")
return True
except Exception as e:
print(f"β [{name}] Configuration error: {e}")
return False
def start_recording_camera(name: str, gopro: GoProUSB) -> bool:
"""Start recording on a single camera"""
try:
print(f"π΄ [{name}] Starting recording...")
result = gopro.record_start()
if result:
print(f"β
[{name}] Recording started")
return result
except Exception as e:
print(f"β [{name}] Recording start error: {e}")
return False
def stop_recording_camera(name: str, gopro: GoProUSB) -> bool:
"""Stop recording on a single camera"""
try:
print(f"βΉοΈ [{name}] Stopping recording...")
result = gopro.record_stop()
if result:
print(f"β
[{name}] Recording stopped")
return result
except Exception as e:
print(f"β [{name}] Recording stop error: {e}")
return False
def power_off_camera(name: str, gopro: GoProUSB) -> bool:
"""Power off a single camera"""
try:
print(f"π [{name}] Powering off...")
result = gopro.power_off()
if result:
print(f"β
[{name}] Powered off")
return result
except Exception as e:
print(f"β [{name}] Power off error: {e}")
return False
def get_camera_status(name: str, gopro: GoProUSB) -> dict:
"""Get status of a single camera"""
try:
state = gopro.get_state()
status = {
"name": name,
"battery": state['status'].get('70', 'N/A'),
"free_space": state['status'].get('54', 'N/A'),
"recording": state['status'].get('8', 0) == 1,
"resolution": gopro._get_resolution_name(state['settings'].get('2', 'N/A')),
"fps": gopro._get_fps_name(state['settings'].get('3', 'N/A')),
"lens": gopro._get_lens_name(state['settings'].get('121', 'N/A')),
}
return status
except Exception as e:
return {"name": name, "error": str(e)}
def display_all_status(cameras_gopro: dict):
"""Display status for all cameras in a formatted table"""
print("\n" + "=" * 80)
print("π CAMERA STATUS")
print("=" * 80)
print(f"{'Camera':<12} {'Battery':<10} {'Free Space':<12} {'Recording':<12} {'Resolution':<12} {'FPS':<8} {'Lens':<10}")
print("-" * 80)
with ThreadPoolExecutor(max_workers=3) as executor:
futures = {executor.submit(get_camera_status, name, gopro): name
for name, gopro in cameras_gopro.items()}
statuses = []
for future in as_completed(futures):
statuses.append(future.result())
# Sort by camera name
statuses.sort(key=lambda x: x.get('name', ''))
for status in statuses:
if 'error' in status:
print(f"{status['name']:<12} β Error: {status['error']}")
else:
rec_icon = "π΄ Yes" if status['recording'] else "βͺ No"
print(f"{status['name']:<12} {status['battery']}%{'':<6} {status['free_space']} MB{'':<4} {rec_icon:<12} {status['resolution']:<12} {status['fps']:<8} {status['lens']:<10}")
print("=" * 80)
def main():
print("=" * 80)
print("π₯ MULTI-CAMERA GOPRO CONTROL")
print(" Controlling 3 cameras simultaneously")
print("=" * 80)
# Initialize all cameras
cameras_gopro = {}
for name, sn in CAMERAS.items():
cameras_gopro[name] = GoProUSB(sn)
print(f"π· {name}: SN {sn}")
try:
# 1. POWER ON all cameras simultaneously
print("\n" + "=" * 80)
print("π STEP 1: Powering on all cameras")
print("=" * 80)
with ThreadPoolExecutor(max_workers=3) as executor:
futures = {executor.submit(power_on_camera, name, gopro): name
for name, gopro in cameras_gopro.items()}
for future in as_completed(futures):
pass # Results already printed in the function
time.sleep(3)
# 2. CONFIGURE all cameras SEQUENTIALLY (avoid USB conflicts)
print("\n" + "=" * 80)
print("π STEP 2: Configuring all cameras (4K @ 120 FPS, Linear)")
print(" β οΈ Configuring one at a time to avoid USB conflicts")
print("=" * 80)
for name, gopro in cameras_gopro.items():
configure_camera(name, gopro)
time.sleep(2) # Wait between cameras
time.sleep(2)
# 3. Display initial status
print("\n" + "=" * 80)
print("π STEP 3: Initial status")
display_all_status(cameras_gopro)
# 4. START RECORDING on all cameras simultaneously
print("\n" + "=" * 80)
print("π STEP 4: Starting recording on all cameras")
print("=" * 80)
# Use barrier to sync start time
start_time = time.time() + 1 # Start in 1 second
print(f"β±οΈ Synchronized start in 1 second...")
def sync_start_recording(name, gopro, target_time):
wait_time = target_time - time.time()
if wait_time > 0:
time.sleep(wait_time)
return start_recording_camera(name, gopro)
with ThreadPoolExecutor(max_workers=3) as executor:
futures = {executor.submit(sync_start_recording, name, gopro, start_time): name
for name, gopro in cameras_gopro.items()}
for future in as_completed(futures):
pass
# 5. Monitor during recording
print("\n" + "=" * 80)
print("π STEP 5: Recording in progress (10 seconds)")
print("=" * 80)
for i in range(5):
time.sleep(2)
display_all_status(cameras_gopro)
# 6. STOP RECORDING on all cameras simultaneously
print("\n" + "=" * 80)
print("π STEP 6: Stopping recording on all cameras")
print("=" * 80)
with ThreadPoolExecutor(max_workers=3) as executor:
futures = {executor.submit(stop_recording_camera, name, gopro): name
for name, gopro in cameras_gopro.items()}
for future in as_completed(futures):
pass
time.sleep(3)
# 7. Final status
print("\n" + "=" * 80)
print("π STEP 7: Final status")
display_all_status(cameras_gopro)
# 8. POWER OFF all cameras
print("\n" + "=" * 80)
print("π STEP 8: Powering off all cameras")
print("=" * 80)
with ThreadPoolExecutor(max_workers=3) as executor:
futures = {executor.submit(power_off_camera, name, gopro): name
for name, gopro in cameras_gopro.items()}
for future in as_completed(futures):
pass
print("\n" + "=" * 80)
print("β
MULTI-CAMERA DEMO COMPLETED SUCCESSFULLY!")
print("=" * 80)
except KeyboardInterrupt:
print("\n\nβ οΈ User interruption - stopping all cameras...")
with ThreadPoolExecutor(max_workers=3) as executor:
# Stop recording
futures = [executor.submit(stop_recording_camera, name, gopro)
for name, gopro in cameras_gopro.items()]
time.sleep(2)
# Power off
futures = [executor.submit(power_off_camera, name, gopro)
for name, gopro in cameras_gopro.items()]
print("π All cameras stopped")
except Exception as e:
print(f"\nβ Error: {e}")
print("Attempting to stop all cameras...")
for name, gopro in cameras_gopro.items():
try:
gopro.record_stop()
gopro.power_off()
except:
pass
def demo_continuous_monitoring():
"""
Continuously monitor all 3 cameras.
Press Ctrl+C to stop.
"""
print("π₯ Multi-Camera Continuous Monitoring")
print("Press Ctrl+C to stop\n")
cameras_gopro = {}
for name, sn in CAMERAS.items():
cameras_gopro[name] = GoProUSB(sn)
try:
# Power on all
with ThreadPoolExecutor(max_workers=3) as executor:
futures = [executor.submit(power_on_camera, name, gopro)
for name, gopro in cameras_gopro.items()]
time.sleep(3)
# Continuous monitoring
while True:
display_all_status(cameras_gopro)
time.sleep(2)
except KeyboardInterrupt:
print("\n\nπ Monitoring stopped")
with ThreadPoolExecutor(max_workers=3) as executor:
futures = [executor.submit(power_off_camera, name, gopro)
for name, gopro in cameras_gopro.items()]
if __name__ == "__main__":
# Full multi-camera demo
main()
# For continuous monitoring only:
# demo_continuous_monitoring()