-
Notifications
You must be signed in to change notification settings - Fork 0
/
kstars_camera.py
executable file
·208 lines (148 loc) · 4.83 KB
/
kstars_camera.py
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
#!/usr/bin/python
import sys
import time
import select
import os
import random
import math
import cv2
import numpy as np
import io
import logging
log = logging.getLogger()
def apply_gamma(img, gamma):
lut = np.fromiter( ( (x / 255.0)**gamma * 65535.0 for x in xrange(256)), dtype=np.uint16 )
return np.take(lut, img)
class Camera_test_kstars:
def __init__(self, status, go_ra, go_dec, focuser):
self.status = status
self.go_ra = go_ra
self.go_dec = go_dec
self.focuser = focuser
self.status['exp-sec'] = 60
self.status['test-exp-sec'] = 1
self.e_ra = 70
self.e_dec = 65
self.status['exp_in_progress'] = False
self.t0 = time.time()
self.hyst = 2
self.bahtinov = False
self.i = 0
import gobject
gobject.threads_init()
from dbus import glib
glib.init_threads()
# Create a session bus.
import dbus
bus = dbus.SessionBus()
remote_object = bus.get_object("org.kde.kstars", # Connection name
"/KStars" # Object's path
)
self.iface = dbus.Interface(remote_object, 'org.kde.kstars')
self.capture()
def cmd(self, cmd):
log.info("camera: %s", cmd)
if cmd.startswith('exp-sec-'):
self.status['exp-sec'] = float(cmd[len('exp-sec-'):])
if cmd == "f-1" and self.hyst > 0:
self.hyst -= 1
return
if cmd == "f+1" and self.hyst < 5:
self.hyst += 1
return
if cmd in ["f-3", "f-2", "f-1", "f+3", "f+2", "f+1"]:
self.focuser.cmd(cmd)
if cmd == 'z1':
self.bahtinov = True
if cmd == 'z0':
self.bahtinov = False
def capture(self):
self.e_ra = 70 + np.random.normal(0, 0.002) + 0.001 * (time.time() - self.t0) + np.sin((time.time() - self.t0) / 3.0) * 0.002
self.e_dec = 65 + np.random.normal(0, 0.002) - 0.0005 * (time.time() - self.t0)
ra = -(self.go_ra.recent_avg() - self.go_ra.recent_avg(0.001)) / 3600.0 * 60.0 + self.e_ra
dec = (self.go_dec.recent_avg() - self.go_dec.recent_avg(0.001)) / 3600.0 * 60.0 + self.e_dec
while True:
log.info("set ra, dec %f,%f" % (ra,dec))
self.iface.setRaDec(ra / 360.0 * 24.0, dec)
time.sleep(0.4)
self.iface.exportImage("/tmp/kstars.jpg") #,2000,2000, signature='sii')
time.sleep(0.1)
im = cv2.imread("/tmp/kstars.jpg")
if im is not None:
break
self.im = apply_gamma(im, 2.2)
h, w, c = self.im.shape
im = np.rot90(self.im[:, 0:w/2])
if self.bahtinov:
im = cv2.imread("ba_test/test%d.jpg" % (self.i % 300 + 264))
im = apply_gamma(im, 2.2)
self.i += 1
log.info("focuser %d" % self.focuser.pos)
bl = np.abs(self.focuser.pos / 150.0)**2 + 1
ibl = int(bl + 1)
#im = cv2.blur(im, (ibl, ibl))
#im = cv2.blur(im, (ibl, ibl))
im = cv2.GaussianBlur(im, (51, 51), bl)
im = cv2.add(im, np.random.normal(3, 3, im.shape), dtype = cv2.CV_16UC3)
return im, time.time()
def capture_bulb(self, test = False, callback_start = None, callback_end = None):
if callback_start is not None:
callback_start()
if test:
sec = self.status['test-exp-sec']
else:
sec = self.status['exp-sec']
self.status['exp_in_progress'] = True
for i in range(0, int(sec + 0.5)):
time.sleep(1)
self.status['cur_time'] = i
self.status['exp_in_progress'] = False
h, w, c = self.im.shape
im = np.rot90(self.im[:, 0:w/2])
bl = np.abs(self.focuser.pos / 150.0)**2 + 1
ibl = int(bl + 1)
#im = cv2.blur(im, (ibl, ibl))
#im = cv2.blur(im, (ibl, ibl))
im = cv2.GaussianBlur(im, (51, 51), bl)
im = cv2.add(im, np.random.normal(3, 3, im.shape), dtype = cv2.CV_16UC3)
if callback_end is not None:
im = np.array(im, dtype=np.uint8)
ret, file_data = cv2.imencode('.jpg', im)
file_data = file_data.tobytes()
#f = open('IMG_6662.JPG', 'rb')
#file_data = f.read()
#f.close()
callback_end(file_data, "img_{}.jpg".format(time.time()))
def shutdown(self):
pass
class Camera_test_kstars_g:
def __init__(self, status, cam0):
self.status = status
self.status['exp-sec'] = 0.5
self.cam0 = cam0
def cmd(self, cmd):
log.info("camera: %s", cmd)
if cmd.startswith('exp-sec-'):
self.status['exp-sec'] = float(cmd[len('exp-sec-'):])
def capture(self):
time.sleep(self.status['exp-sec'])
self.cam0.capture()
im = self.cam0.im
h, w, c = im.shape
im = im[h/4:h/4*3, w/2:]
#im = cv2.flip(im, 1)
bl = np.abs((self.cam0.focuser.pos + 200) / 150.0)**2 + 1
ibl = int(bl + 1)
#im = cv2.blur(im, (ibl, ibl))
#im = cv2.blur(im, (ibl, ibl))
im = cv2.GaussianBlur(im, (51, 51), bl)
im = cv2.add(im, np.random.normal(3, 3, im.shape), dtype = cv2.CV_16UC3)
return im, time.time()
def shutdown(self):
pass
if __name__ == "__main__":
from guide_out import GuideOut
go_ra = GuideOut("./guide_out_ra")
go_dec = GuideOut("./guide_out_dec")
cam = Camera_test_kstars({}, go_ra, go_dec)
im, t = cam.capture()