-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcamera_lib.py
More file actions
206 lines (173 loc) · 7.16 KB
/
camera_lib.py
File metadata and controls
206 lines (173 loc) · 7.16 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
#! python3.8
import argparse
import os
import sys
import time
# import datetime
import zwoasi as asi
__author__ = 'Steve Marple'
__version__ = '0.0.22'
__license__ = 'MIT'
def save_control_values(filename, settings):
dir_name = os.path.dirname(filename)
base_name = os.path.basename(filename)
filename = os.path.join(dir_name, base_name+'.txt')
with open(filename, 'w') as f:
for k in sorted(settings.keys()):
f.write('%s: %s\n' % (k, str(settings[k])))
# print('Camera settings saved to %s' % filename)
def init_camera():
# ZWO_ASI_LIB = r'C:\Program Files (x86)\ZWO Design\ZWO_USB_Cameras_driver'
env_filename = os.getenv('ZWO_ASI_LIB') #
print(env_filename)
parser = argparse.ArgumentParser(description='Process and save images from a camera')
parser.add_argument('filename',
nargs='?',
help='SDK library filename')
args = parser.parse_args()
# print(args)
## Initialize zwoasi with the name of the SDK library
# if args.filename:
# asi.init(args.filename)
# elif env_filename:
# asi.init(env_filename)
# else:
# print('The filename of the SDK library is required (or set ZWO_ASI_LIB environment variable with the filename)')
# sys.exit(1)
# asi.init(r'C:\Users\ideabanny\Downloads\buffer\lib\x64\ASICamera2.dll')
# C:\local_coding\image_data\camera_lib\lib\x64
asi.init(r'C:\local_coding\image_data\camera_lib\lib\x64\ASICamera2.dll')
# asi.init(r'camera_lib\lib\x64\ASICamera2.dll')
num_cameras = asi.get_num_cameras()
if num_cameras == 0:
print('No cameras found')
sys.exit(0)
cameras_found = asi.list_cameras() # Models names of the connected cameras
if num_cameras == 1:
camera_id = 0
print('Found one camera: %s' % cameras_found[0])
else:
print('Found %d cameras' % num_cameras)
for n in range(num_cameras):
print(' %d: %s' % (n, cameras_found[n]))
# TO DO: allow user to select a camera
camera_id = 0
print('Using #%d: %s' % (camera_id, cameras_found[camera_id]))
camera = asi.Camera(camera_id)
camera_info = camera.get_camera_property()
# Get all of the camera controls
print('')
print('Camera controls:')
controls = camera.get_controls()
# for cn in sorted(controls.keys()):
# print(' %s:' % cn)
# for k in sorted(controls[cn].keys()):
# print(' %s: %s' % (k, repr(controls[cn][k])))
# Use minimum USB bandwidth permitted
camera.set_control_value(asi.ASI_BANDWIDTHOVERLOAD, camera.get_controls()['BandWidth']['MinValue'])
# Set some sensible defaults. They will need adjusting depending upon
# the sensitivity, lens and lighting conditions used.
camera.disable_dark_subtract()
camera.set_control_value(asi.ASI_GAIN, 200)
camera.set_control_value(asi.ASI_EXPOSURE, 25000) # in micro second
camera.set_control_value(asi.ASI_WB_B, 99)
camera.set_control_value(asi.ASI_WB_R, 75)
camera.set_control_value(asi.ASI_GAMMA, 50)
camera.set_control_value(asi.ASI_BRIGHTNESS, 100)
camera.set_control_value(asi.ASI_FLIP, 0)
camera.set_image_type(asi.ASI_IMG_RAW16)
return camera
# print('Enabling stills mode')
# try:
# # Force any single exposure to be halted
# camera.stop_video_capture()
# camera.stop_exposure()
# except (KeyboardInterrupt, SystemExit):
# raise
# except:
# pass
# print('Capturing a single 8-bit mono image')
# filename = 'image_mono.jpg'
# camera.set_image_type(asi.ASI_IMG_RAW8)
# camera.capture(filename=filename)
# print('Saved to %s' % filename)
# save_control_values(filename, camera.get_control_values())
# if camera_info['IsColorCam']:
# filename = 'image_color.jpg'
# camera.set_image_type(asi.ASI_IMG_RGB24)
# print('Capturing a single, color image')
# camera.capture(filename=filename)
# print('Saved to %s' % filename)
# save_control_values(filename, camera.get_control_values())
# else:
# print('Color image not available with this camera')
# # Enable video mode
# try:
# # Force any single exposure to be halted
# camera.stop_exposure()
# except (KeyboardInterrupt, SystemExit):
# raise
# except:
# pass
# print('Enabling video mode')
# camera.start_video_capture()
# # Restore all controls to default values except USB bandwidth
# for c in controls:
# if controls[c]['ControlType'] == asi.ASI_BANDWIDTHOVERLOAD:
# continue
# camera.set_control_value(controls[c]['ControlType'], controls[c]['DefaultValue'])
# # Can autoexposure be used?
# k = 'Exposure'
# if 'Exposure' in controls and controls['Exposure']['IsAutoSupported']:
# print('Enabling auto-exposure mode')
# camera.set_control_value(asi.ASI_EXPOSURE,
# controls['Exposure']['DefaultValue'],
# auto=True)
# if 'Gain' in controls and controls['Gain']['IsAutoSupported']:
# print('Enabling automatic gain setting')
# camera.set_control_value(asi.ASI_GAIN,
# controls['Gain']['DefaultValue'],
# auto=True)
# # Keep max gain to the default but allow exposure to be increased to its maximum value if necessary
# camera.set_control_value(controls['AutoExpMaxExp']['ControlType'], controls['AutoExpMaxExp']['MaxValue'])
# print('Waiting for auto-exposure to compute correct settings ...')
# sleep_interval = 0.100
# df_last = None
# gain_last = None
# exposure_last = None
# matches = 0
# while True:
# time.sleep(sleep_interval)
# settings = camera.get_control_values()
# df = camera.get_dropped_frames()
# gain = settings['Gain']
# exposure = settings['Exposure']
# if df != df_last:
# print(' Gain {gain:d} Exposure: {exposure:f} Dropped frames: {df:d}'
# .format(gain=settings['Gain'],
# exposure=settings['Exposure'],
# df=df))
# if gain == gain_last and exposure == exposure_last:
# matches += 1
# else:
# matches = 0
# if matches >= 5:
# break
# df_last = df
# gain_last = gain
# exposure_last = exposure
# # Set the timeout, units are ms
# timeout = (camera.get_control_value(asi.ASI_EXPOSURE)[0] / 1000) * 2 + 500
# camera.default_timeout = timeout
# if camera_info['IsColorCam']:
# print('Capturing a single color frame')
# filename = 'image_video_color.jpg'
# camera.set_image_type(asi.ASI_IMG_RGB24)
# camera.capture_video_frame(filename=filename)
# else:
# print('Capturing a single 8-bit mono frame')
# filename = 'image_video_mono.jpg'
# camera.set_image_type(asi.ASI_IMG_RAW8)
# camera.capture_video_frame(filename=filename)
# print('Saved to %s' % filename)
# save_control_values(filename, camera.get_control_values())