-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.py
More file actions
72 lines (53 loc) · 2.21 KB
/
example.py
File metadata and controls
72 lines (53 loc) · 2.21 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
import threading
import cv2
from numpy import concatenate
from pynaneye import Camera, NanEyeSensorType, SensorChannel
from pynaneye.frame import NanEyeFrame
from pynaneye.frame_queue import FrameQueue
ESCAPE_KEY = 27
SENSOR_CHANNEL = SensorChannel.BOTH # Or SensorChannel.CH1, SensorChannel.CH2
def display_frames(input_queue: FrameQueue, window_name: str = "NanEye Viewer. Press 'q' or 'esc' to quit."):
"""Displays frames from the queue in an openCV window."""
cv2.namedWindow(window_name, cv2.WINDOW_AUTOSIZE)
while True:
try:
frame_data = input_queue.get(timeout=1)
except Exception:
break
if SENSOR_CHANNEL == SensorChannel.BOTH:
left_frame_dict, right_frame_dict = frame_data
left_frame = NanEyeFrame(**left_frame_dict)
right_frame = NanEyeFrame(**right_frame_dict)
left_image = left_frame.as_array()
right_image = right_frame.as_array()
combined_image = concatenate((left_image, right_image), axis=1)
cv2.imshow(window_name, combined_image)
else:
frame = NanEyeFrame(**frame_data)
image = frame.as_array()
cv2.imshow(window_name, image)
key = cv2.waitKey(1) & 0xFF
if (key == ord('q')) or (key == ESCAPE_KEY):
break
cv2.destroyAllWindows()
def run_example():
try:
print("Initializing Camera...")
camera = Camera(NanEyeSensorType.NanEyeM, SENSOR_CHANNEL)
print("Camera initialized.")
frame_queue = FrameQueue(SENSOR_CHANNEL)
camera.SubscribeToImageProcessedEvent(frame_queue.put)
display_thread = threading.Thread(target=display_frames, args=(frame_queue,))
display_thread.start()
print("Starting capture...")
camera.StartCapture()
print("Capture started. Press 'q' or 'esc' to quit.")
display_thread.join()
print("Stopping capture...")
camera.StopCapture()
print("Capture stopped.")
except Exception as e:
print(f"An error occurred: {e}")
print("Please ensure the C# DLL is compiled and located correctly, and the camera is connected.")
if __name__ == "__main__":
run_example()