|
| 1 | +import time |
| 2 | + |
| 3 | +import cv2 |
| 4 | +from rich import print # noqa: A004 |
| 5 | + |
| 6 | +from pupil_labs.realtime_api.simple import discover_one_device |
| 7 | + |
| 8 | + |
| 9 | +def set_state_1(device, camera_state): |
| 10 | + print("Updating camera to state 1") |
| 11 | + device.set_camera_state( |
| 12 | + ae_mode="manual", |
| 13 | + man_exp=500, |
| 14 | + gain=64, |
| 15 | + brightness=0, |
| 16 | + contrast=32, |
| 17 | + gamma=300, |
| 18 | + validate_with_state=camera_state, |
| 19 | + ) |
| 20 | + |
| 21 | + |
| 22 | +def set_state_2(device, camera_state): |
| 23 | + print("Updating camera to state 2") |
| 24 | + device.set_camera_state( |
| 25 | + ae_mode="manual", |
| 26 | + man_exp=1000, |
| 27 | + gain=75, |
| 28 | + brightness=-20, |
| 29 | + contrast=50, |
| 30 | + gamma=200, |
| 31 | + validate_with_state=camera_state, |
| 32 | + ) |
| 33 | + |
| 34 | + |
| 35 | +def main(): |
| 36 | + print("Looking for the next best device...") |
| 37 | + device = discover_one_device(max_search_duration_seconds=10) |
| 38 | + if device is None: |
| 39 | + print("No device found.") |
| 40 | + raise SystemExit(-1) |
| 41 | + |
| 42 | + # Initiate video stream before querying camera state to ensure camera is active |
| 43 | + # You could instead simply open the scene video preview in the companion app |
| 44 | + device.receive_scene_video_frame() |
| 45 | + |
| 46 | + print("Retrieving camera state...") |
| 47 | + camera_state = device.get_camera_state() |
| 48 | + print(f"Current camera state: {camera_state}") |
| 49 | + |
| 50 | + set_state_1(device, camera_state) |
| 51 | + last_state = 1 |
| 52 | + last_tick_time = time.time() |
| 53 | + while True: |
| 54 | + bgr_pixels, frame_datetime = device.receive_scene_video_frame() |
| 55 | + draw_time(bgr_pixels, frame_datetime) |
| 56 | + cv2.imshow("Scene Camera - Press ESC to quit", bgr_pixels) |
| 57 | + |
| 58 | + if time.time() - last_tick_time > 4: |
| 59 | + if last_state == 1: |
| 60 | + set_state_2(device, camera_state) |
| 61 | + last_state = 2 |
| 62 | + else: |
| 63 | + set_state_1(device, camera_state) |
| 64 | + last_state = 1 |
| 65 | + last_tick_time = time.time() |
| 66 | + |
| 67 | + if cv2.waitKey(1) & 0xFF == 27: |
| 68 | + break |
| 69 | + |
| 70 | + device.close() |
| 71 | + |
| 72 | + |
| 73 | +def draw_time(frame, timestamp): |
| 74 | + frame_txt_font_name = cv2.FONT_HERSHEY_SIMPLEX |
| 75 | + frame_txt_font_scale = 1.0 |
| 76 | + frame_txt_thickness = 1 |
| 77 | + |
| 78 | + # first line: frame index |
| 79 | + frame_txt = str(timestamp) |
| 80 | + |
| 81 | + cv2.putText( |
| 82 | + frame, |
| 83 | + frame_txt, |
| 84 | + (20, 50), |
| 85 | + frame_txt_font_name, |
| 86 | + frame_txt_font_scale, |
| 87 | + (255, 255, 255), |
| 88 | + thickness=frame_txt_thickness, |
| 89 | + lineType=cv2.LINE_8, |
| 90 | + ) |
| 91 | + |
| 92 | + |
| 93 | +if __name__ == "__main__": |
| 94 | + main() |
0 commit comments