-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpicamera-4-microscope_vers_3_Touchphat_hispeed.py
240 lines (203 loc) · 8.88 KB
/
picamera-4-microscope_vers_3_Touchphat_hispeed.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#!/usr/bin/env python
'''
A module "picamera"-based script for the Raspberry Pi HQ camera microscope,
controlled by a Pimoroni Touchphat
Install required libraries before use
Runs in Mode 4 for low-res high speed videos
'''
from time import sleep
import picamera
import datetime as dt
import touchphat
import os
#from guizero import App, Text, TextBox, Picture, Slider, PushButton
#import gpiozero # for a physical button control version
#import signal
#import subprocess
#import sys
w_dir = os.getcwd()
print ('working directory: ', w_dir)
camera = picamera.PiCamera() #turns camera on
#General camera settings: loads of parameters to play with
camera.rotation= 180 # 90,180,270
#camera.hflip = True # default: False
#camera.vflip = True # default: False
camera.meter_mode = 'spot' # optimize illumination,
# alternative options: 'average', 'backlit', 'matrix', 'spot'
camera. awb_mode = 'auto' # automatic whithe balance.. You may play with setting fitting best to your setting
# alternatives: off, auto, sunlight, cloudy, shade, tungsten, fluorescent, incandelescent, flash, horizon, greyworld
#camera.iso = 100 # 200,400, 800
#camera.contrast = 0.5 #
camera.brightness = 50 # 0 ...100, default 50
camera.sharpness = 0 #-100 to 100, default 0
# set annotation format in images
camera.annotate_background = picamera.Color ('blue') # sets background color for text
#camera.annotate_foreground = picamera.Color (Y=1, U=0, V=0) # sets brightness for text Y=0..1
#select effects to apply while capturing image
effect1 = 'emboss' #Touchpad "C"
effect2 = 'solarize' #Touchpad "D"
'''
#further options: negative, solarize, sketch, denoise, emboss, oilpaint, hatch, pastel, watercolor, film, blur, saturation, colorswap, washedout, posterize, colorpoint, colorbalance, cartoon, deinterlace1, deinterlace2
'''
#preview settings
preview_time_1 = 8 # duration for displaying preview in sec:
#for total view: optimize object placing & focus
preview_time_2 = 8 #
#for total view emboss: fine tuning
preview_time_3 = 8
#for ROI-view: fine tuning
#image capture settings
delay_time = 2 # delay between images taken, in sec
#video length settings
vga_time = 5 # for 506x380 video
hires_time = 5 # for 1012x760 video
# define position and size of preview window
preview_xy = (800,300) #position of upper left corner of preview window on screen
preview_size = (1012, 760) # size of preview window, here: 1/4 of max resolution HQ camera
#Zeitpunkt = "" # set as general variable for timestamp text
#check for type of camera installed
camera_version = camera.revision
print ('camera type: ', camera_version)
if (camera_version == 'imx477'):
print ('HQ camera detected')
print ('Modus 1: 2028 x 1080 (1-50 fps), Modus 2: 2028x1520 (1-50), Modus 3: 4056x3040 (up to 10), Modus 4: 1012x760 (50 - 120)')
modus = 4
camera.sensor_mode = modus
print ('Activated modus: ', modus)
camera.resolution = (1012,760) # defines image size
print ('Image size: ', camera.resolution)
camera.framerate = 60 # Mode 4: 51 - 120
print ('Framerate set to: ', camera.framerate, 'fps')
elif (camera.version == 'ov5647'):
print ('version 1 camera detected')
print ('modus 1: 1080p (30 fps), modi 2/3: 2592x1944, modus 4: 1296x972, modus 5: 1296x730, modi 6/7: VGA (60/90 fps)')
print ('code may require some adaptation')
camera.resolution = (1014,760) # defines image size
print ('Image size: ', camera.resolution)
camera.framerate = 30
elif (camera.version == 'imx219'):
print ('version 2 camera detected')
print ('Settings may require some adjustments')
print ('modus 1: 1080p (30 fps), modi 2/3: 3280x2464 (15), modus 4: 1640x1232 (40), modus 5: 1640x922, modus 6: 720p (90), modus 7: VGA (90)')
print ('code may require some adaptation')
camera.resolution = (1014,760) # defines image size
print ('Image size: ', camera.resolution)
camera.framerate = 30
else:
print ('No HQ camera! Please modify settings manually') # code to be optimized for version 2 camera
# determine timestamp
def get_zeitpunkt():
Time_info = dt.datetime.now().strftime('%Y-%m-%d %H:%M')
print ('Timestamp: ', Time_info)
return Time_info
#Routines for preview and taking images, evoked by touckphat
@touchphat.on_release('Enter') # button 'Enter' evokes Preview total for previewtime 1 seconds
def preview_ROI_none():
touchphat.led_on(6)
camera.zoom = (0.375, 0.375, 0.25, 0.25) #restrict to ROI: central 25 %, equals maximal resolution
#camera.preview.crop = (380, 285, 253, 190) # starting points for and size of selection: central 25%
camera.annotate_text = 'Preview ROI'
sleep (preview_time_3)
#camera.preview.crop = (0, 0, preview_size[0], preview_size[1]) #back to normal
camera.zoom = (0,0,0,0)
camera.annotate_text =''
touchphat.led_off(6)
''' #not used here
def preview_Total_emboss():
touchphat.led_on(1)
camera.image_effect =('emboss') # 'none, 'cartoon', 'negative','sketch', 'emboss' ...
camera.annotate_text = "Preview - emboss"
sleep (preview_time_2)
camera.image_effect =('none')# back to standard
touchphat.led_off(1)
'''
@touchphat.on_release('A') # button 'A' evokes taing image of total area, no effects
def take_Total_none(): # take image of tolal view without applying any effect
touchphat.led_on(2)
Zeitpunkt = get_zeitpunkt()
Image_text = Zeitpunkt + ' - overview'
camera.annotate_text = Image_text
camera.capture (Image_text + '.jpg') # get total image, store as jpg
print ('Image taken: ',Image_text)
camera.annotate_text ='Preview total'
touchphat.led_off(2)
@touchphat.on_release('B') # button 'B' evokes taing image of ROI, no effects
def take_ROI_none(): #take image of ROI applying no effect
# zoom to ROI
touchphat.led_on(3)
camera.zoom = (0.375, 0.375, 0.25, 0.25) #restrict to ROI: central 25x25 %, equals maximal resolution
sleep (2)
Zeitpunkt = get_zeitpunkt()
Image_text = Zeitpunkt + ' - ROI-none' # for unmodified image
camera.annotate_text = Image_text
camera.capture (Image_text +'.png') # store as png to reduce loss of information
print ('Image taken: ',Image_text)
camera.zoom = (0, 0, 0, 0)
camera.annotate_text ='Preview total'
touchphat.led_off(3)
@touchphat.on_release('C') # button 'C' evokes taking lores high speed video
def take_hispeed_video(): #low res high framerate video
touchphat.led_on(4)
camera.resolution = (253, 190) #(506, 380)
#camera.zoom = (0.375, 0.375, 0.25, 0.25) #restrict to ROI: central 25 %, equals maximal resolution
camera.framerate = 120 # max 120 fps in mode 4
Zeitpunkt = get_zeitpunkt()
video_name = Zeitpunkt + '_hispeed.h264'
camera.annotate_text = 'hispeed'
print ('start recording')
camera.start_recording(video_name)
camera.wait_recording(vga_time)
camera.stop_recording()
print ('recording stopped')
#camera.zoom = (0, 0, 0, 0)
camera.resolution =(1012,760) # back to total view
camera.framerate = 60 # back to normal speed
camera.annotate_text = 'Preview Total'
#camera.start_preview()
touchphat.led_off(4)
@touchphat.on_release('D') # button 'D' evokes taking total area video
def take_video_total():
touchphat.led_on(5)
Zeitpunkt = get_zeitpunkt()
video_name = Zeitpunkt + '_largevid.h264'
camera.annotate_text = 'total area.'
print ('start recording')
camera.start_recording(video_name)
camera.wait_recording(hires_time)
camera.stop_recording()
print ('stop recording')
camera.annotate_text = 'Preview total'
touchphat.led_off(5)
@touchphat.on_release('Back') # button 'Back' evokes end of programm and cleaning of memory
def end_camera(): #end routine
touchphat.led_on(1)
camera.stop_preview()
camera.close () # close camera, free storage
print ("Camera closed")
sleep (1)
touchphat.led_off(1)
print ('Stop script now manually!') # how to stop all processes from tread?
#sys.exit()
#quit()
#exit()
#instructions for use
print ()
print ('On Touch phat: ')
print (' press "Enter" (right side) for a 10 sec preview of ROI (no effect applied)')
print (' press "A" to take an image of total area, no effect applied')
print (' press "B" to take an image of ROI, no effect applied')
print (' press "C" to take a video, 120 fps, ROI resolution')
print (' press "D" to take a video 60 fps, total area')
print (' press "Back" (left side) to terminate program')
print ()
#start with total area preview w/o effects applied
#camera warm-up time 3 sec
touchphat.all_on() # turn all LEDs on
sleep (3)
touchphat.all_off() # turn them off
#start preview
camera.start_preview()
camera.preview.fullscreen = False # enables display of preview in defined window
camera.preview.window = (preview_xy[0], preview_xy[1], preview_size[0], preview_size[1]) # defines position of upper left corner and size of display window
camera.annotate_text = 'Preview total area'
#sleep (preview_time_1)