-
Notifications
You must be signed in to change notification settings - Fork 2
/
microscope_control.py
184 lines (160 loc) · 7.04 KB
/
microscope_control.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
# -*- coding: utf-8 -*-
#!/usr/bin/env python
"""
Simple stage control for Fergus Riche's "fergboard" controller.
Written 2016 by Richard Bowman, Abhishek Ambekar, James Sharkey and Darryl Foo
Released under GNU GPL v3 or later.
Usage:
microscope move <x> <y> [<z>]
microscope focus <z>
microscope [options] control [<step_size>]
microscope [options]
Options:
--output=<filepath> Set output directory/filename [default: ~/Desktop/images]
-h --help Show this screen.
"""
import io
import sys
import os
import time
import numpy as np
import docopt
import curses
import curses.ascii
import picamera
from openflexure_stage import OpenFlexureStage
def validate_filepath(filepath):
"""Check the filepath is valid, creating dirs if needed
The final format is ~/Desktop/images/image_%d.img
%d is formatted with number by (filepath %n)
https://pyformat.info/"""
filepath = os.path.expanduser(filepath)
if "%d" not in filepath and ".jp" not in filepath:
if not os.path.isdir(filepath):
os.mkdir(filepath)
return os.path.join(filepath, "image_%03d.jpg")
elif "%d" not in filepath and ".jp" in filepath:
'add automatic numbering to filename'
filepath = filepath.split('.')
filepath = filepath[0] + '_%03d.' + filepath[1]
return filepath
elif "%d" in filepath and ".jp" in filepath:
return filepath
else:
raise ValueError("Error setting output filepath. Valid filepaths should"
" either be [creatable] directories, or end with a "
"filename that contains '%d' and ends in '.jpg' or '.jpeg'")
#run microscope_control.py directly
if __name__ == '__main__':
pass
argv = docopt.docopt(__doc__, options_first=True)
stage = OpenFlexureStage('/dev/ttyUSB0')
if argv['move']:
x, y, z = [int(argv.get(d, 0)) for d in ['<x>', '<y>', '<z>']]
print ("moving", x, y, z)
stage.move_rel([x, y, z])
elif argv['focus']:
stage.focus_rel(int(argv['<z>']))
else: #if argv['control']:
def move_stage_with_keyboard(stdscr):
stdscr.addstr(0,0,"wasd to move in X/Y, qe for Z\n"
"r/f to decrease/increase step.\n"
"v/b to start/stop video preview.\n"
"i/o to zoom in/out.\n"
"t/g to adjust contrast, y/h to adjust brightness.\n"
"j to save jpeg file, k to change output path.\n"
"x to quit\n")
step = int(argv.get('<step>',100))
filepath = validate_filepath(argv['--output'])
fov = 1
#res = (320/2, 2464/2)
res = (640, 480)
with picamera.PiCamera(resolution=res) as camera:
#time.sleep(3)
#camera.start_preview()
#time.sleep(3)
#camera.stop_preview()
while True:
c = stdscr.getch()
if curses.ascii.isascii(c):
c = chr(c)
if c == 'x':
break
elif c == 'w' or c == curses.KEY_UP:
stage.move_rel([0,step,0])
elif c == 'a' or c == curses.KEY_LEFT:
stage.move_rel([step,0,0])
elif c == 's' or c == curses.KEY_DOWN:
stage.move_rel([0,-step,0])
elif c == 'd' or c == curses.KEY_RIGHT:
stage.move_rel([-step,0,0])
elif c == 'q' or c == curses.KEY_PPAGE:
stage.move_rel([0,0,-step])
elif c == 'e' or c == curses.KEY_NPAGE:
stage.move_rel([0,0,step])
elif c == "r" or c == '-' or c == '_':
step /= 2
elif c == "f" or c == '+' or c == '=':
step *= 2
elif c == 'i':
fov *= 0.75
camera.zoom = (0.5-fov/2, 0.5-fov/2, fov, fov)
elif c == 'o':
if fov < 1.0:
fov *= 4.0/3.0
camera.zoom = (0.5-fov/2, 0.5-fov/2, fov, fov)
elif c == 't':
if camera.contrast <= 90:
camera.contrast += 10
elif c == 'g':
if camera.contrast >= -90:
camera.contrast -= 10
elif c == 'y':
if camera.brightness <= 90:
camera.brightness += 10
elif c == 'h':
if camera.brightness >= -90:
camera.brightness -= 10
elif c == 'n':
if camera.shutter_speed <= 1000000:
camera.shutter_speed += 1000
elif c == 'm':
if camera.shutter_speed >= 1000:
camera.shutter_speed -= 1000
elif c == "v":
camera.start_preview()
elif c == "b":
camera.stop_preview()
elif c == "j":
n = 0
while os.path.isfile(os.path.join(filepath % n)):
n += 1
camera.capture(filepath % n, format="png", use_video_port=True)
camera.annotate_text="Saved '%s'" % (filepath % n)
try:
stdscr.addstr("Saved '%s'\n" % (filepath % n))
except:
pass
time.sleep(0.5)
camera.annotate_text=""
elif c == "p":
camera.annotate_text="Position '%s'" % str(stage.position)
try:
stdscr.addstr("Position '%s'\n" % str(stage.position))
except:
pass
time.sleep(0.5)
camera.annotate_text=""
elif c == "k":
camera.stop_preview()
stdscr.addstr("The new output location can be a directory or \n"
"a filepath. Directories will be created if they \n"
"don't exist, filenames must contain '%d' and '.jp'.\n"
"New filepath: ")
curses.echo()
new_filepath = stdscr.getstr()
curses.noecho()
if len(new_filepath) > 3:
filepath = validate_filepath(new_filepath)
stdscr.addstr("New output filepath: %s\n" % filepath)
curses.wrapper(move_stage_with_keyboard)