-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathextraction.py
More file actions
188 lines (134 loc) · 5.07 KB
/
extraction.py
File metadata and controls
188 lines (134 loc) · 5.07 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
"""Adapted from https://github.com/alliedvision/VimbaPython/blob/master/Examples/asynchronous_grab_opencv.py"""
import threading
import sys
import cv2
import time
from pathlib import Path
import numpy as np
from datetime import datetime
from typing import Optional
from vimba import *
def print_preamble():
print()
print("///////////////////////////////////////////////////////")
print("/// PLD CAMERA IMAGE CAPTURE API ///")
print("///////////////////////////////////////////////////////\n")
def print_usage():
print("Usage:")
print(" python image_capture.py [moss|rheed]")
print(" python image_capture.py [/h] [-h]")
print()
def abort(reason: str, return_code: int = 1, usage: bool = False):
print(reason + "\n")
if usage:
print_usage()
sys.exit(return_code)
def parse_args() -> Optional[str]:
args = sys.argv[1:]
argc = len(args)
for arg in args:
if arg in ("/h", "-h"):
print_usage()
sys.exit(0)
if argc > 1:
abort(reason="Invalid number of arguments. Abort.", return_code=2, usage=True)
return None if argc == 0 else args[0]
def get_camera(camera_id: Optional[str]) -> Camera:
with Vimba.get_instance() as vimba:
if camera_id:
try:
return vimba.get_camera_by_id(camera_id)
except VimbaCameraError:
abort("Failed to access Camera '{}'. Abort.".format(camera_id))
else:
cams = vimba.get_all_cameras()
if not cams:
abort("No Cameras accessible. Abort.")
return cams[0]
def setup_camera(cam: Camera):
with cam:
# Enable auto exposure time setting if camera supports it
# try:
# cam.ExposureAuto.set('Continuous')
#
# except (AttributeError, VimbaFeatureError):
# pass
# Set exposure time hard to 10ms
try:
cam.ExposureAuto.set("Off")
cam.ExposureTimeAbs.set(0.01)
except (AttributeError, VimbaFeatureError):
pass
# Enable white balancing if camera supports it
try:
cam.BalanceWhiteAuto.set("Continuous")
except (AttributeError, VimbaFeatureError):
pass
# Try to adjust GeV packet size. This Feature is only available for GigE - Cameras.
try:
cam.GVSPAdjustPacketSize.run()
while not cam.GVSPAdjustPacketSize.is_done():
pass
except (AttributeError, VimbaFeatureError):
pass
# Query available, open_cv compatible pixel formats
# prefer color formats over monochrome formats
cv_fmts = intersect_pixel_formats(cam.get_pixel_formats(), OPENCV_PIXEL_FORMATS)
color_fmts = intersect_pixel_formats(cv_fmts, COLOR_PIXEL_FORMATS)
if color_fmts:
cam.set_pixel_format(color_fmts[0])
else:
mono_fmts = intersect_pixel_formats(cv_fmts, MONO_PIXEL_FORMATS)
if mono_fmts:
cam.set_pixel_format(mono_fmts[0])
else:
abort(
"Camera does not support a OpenCV compatible format natively. Abort."
)
TIME_FORMAT = "%Y%m%d_%H%M%S%f"
class Handler:
def __init__(self):
self.shutdown_event = threading.Event()
timestamp = time.strftime(TIME_FORMAT[:-2])
self.output_path = Path(timestamp)
self.output_path.mkdir()
def __call__(self, cam: Camera, frame: Frame):
ENTER_KEY_CODE = 13
key = cv2.waitKey(1)
if key == ENTER_KEY_CODE:
self.shutdown_event.set()
return
elif frame.get_status() == FrameStatus.Complete:
timestamp = datetime.now().strftime(TIME_FORMAT)[:-3]
print("{} acquired {} at {}".format(cam, frame, timestamp), flush=True)
msg = "Stream from '{}'. Press <Enter> to stop stream."
cv2.imshow(msg.format(cam.get_name()), frame.as_opencv_image())
# filename = f"{timestamp}_frame.jpg"
# cv2.imwrite(filename, frame.as_opencv_image())
data = frame.as_numpy_ndarray()
filepath = self.output_path / f"{timestamp}_frame.npy"
np.save(filepath, data.reshape(data.shape[:-1]))
cam.queue_frame(frame)
CAMERA_IDS = {
"moss": "DEV_000F3102C6CA",
"rheed": "DEV_000F314F7A84",
}
def main():
print_preamble()
cam = parse_args()
if not (cam_id := CAMERA_IDS.get(cam)):
print(f"Available cameras: 'moss', 'rheed'; got {cam}")
return
with Vimba.get_instance():
with get_camera(cam_id) as cam:
# Start Streaming, wait for five seconds, stop streaming
setup_camera(cam)
handler = Handler()
try:
# Start Streaming with a custom buffer of 10 Frames (defaults to 5)
cam.start_streaming(handler=handler, buffer_count=10)
handler.shutdown_event.wait()
finally:
cam.stop_streaming()
if __name__ == "__main__":
main()