-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathaverage
executable file
·355 lines (285 loc) · 10.4 KB
/
average
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
#!/usr/bin/python3
import os
os.environ['PYGAME_HIDE_SUPPORT_PROMPT'] = "hide"
import pygame
import pygame.camera
from pygame.locals import *
from PIL import Image
#import antigravity
import math
import time
import shlex, subprocess
import numpy
from configparser import ConfigParser
import argparse
# read config file, to override the default (fallback) settings
configFile = os.path.join(os.path.dirname(os.path.realpath(__file__)),"config.ini")
config = ConfigParser()
config.read(configFile)
width = config.getint('Spectrometer','width',fallback=1280)
height = config.getint('Spectrometer','height',fallback=720)
videoDev = config.get('Spectrometer','videoDev',fallback='/dev/video0')
averageItems = config.getint('Spectrometer','averageItems',fallback=20)
pixelClip = config.getint('Spectrometer','pixelClip',fallback=250)
calibDefault = config.get('Spectrometer','calibDefault',fallback=',,410,900')
# read command line, to override the config file settings
parser = argparse.ArgumentParser(description='Spectrometer')
parser.add_argument('-x','--width' ,dest='width',default=width,type=int)
parser.add_argument('-y','--height' ,dest='height',default=height,type=int)
parser.add_argument('-v','--video' ,dest='videoDev',default=videoDev)
parser.add_argument('-a','--average' ,dest='averageItems',default=averageItems,type=int)
parser.add_argument('-c','--clip' ,dest='pixelClip',default=pixelClip,type=int)
args = parser.parse_args()
width = args.width
height = args.height
videoDev = args.videoDev
averageItems = args.averageItems
pixelClip = args.pixelClip
pygame.init()
pygame.camera.init()
fontSize = 24
font = pygame.font.Font(None, fontSize)
WHITE = (255,255,255)
BLACK = (0,0,0)
RED = (255,0,0)
BLUE = (0,0,255)
GREEN = (0,255,0)
CYAN = (0,255,255)
resolution = (width,height)
(x,y) = (0,0)
averageIndex = -1
# array to hold the lines to be averaged
averageArraySurface = pygame.surface.Surface((width,averageItems))
averageArray = pygame.surfarray.array3d(averageArraySurface)
# the average of the lines
lineSurface = pygame.surface.Surface((width,1))
lineSurfArray = pygame.surfarray.array3d(lineSurface)
# output surface, the averaged spectrum
outSurface = pygame.surface.Surface(resolution)
outSurface.fill(BLACK)
outSurface.set_colorkey(BLACK)
# Out Of Range surface
oorSurface = pygame.surface.Surface((width,1))
oorSurface.fill(BLACK)
oorSurface.set_colorkey(BLACK)
# text surface
txtSurface = pygame.surface.Surface(resolution)
txtSurface.fill(BLACK)
txtSurface.set_colorkey(BLACK)
showAverage = False
noAverage = False
yDisplayRow = -1
# read calibration file
try:
calib = open("calibration.csv","r")
calibration = calib.read()
calib.close()
except:
calibration = calibDefault
lcd = pygame.display.set_mode(resolution)
cam = pygame.camera.Camera(videoDev,resolution)
cam.start()
# buttons
bottomRow = height-10
D = {
"SAVE": {"pos":(width/2,bottomRow), "text":"SAVE", "align":"MB", "bg":GREEN, "color":(1,1,1) },
"DESC": {"rightof":"SAVE", "text":"description"},
"TAG": {"leftof":"SAVE", "text":"prefix"},
"BRIGHT": {"pos":(10,bottomRow), "text":"", "align":"BL", "border":False, "bg":BLACK },
"QUIT": {"pos":(width-10,bottomRow),"text":"QUIT", "align":"BR", "bg":RED},
"AVERAGE":{"pos":(width/4,bottomRow), "text":"AVERAGE", "align":"MB", "bg":BLUE}
}
def TXTdisplay(key) :
margin = D[key].get('margin',3)
# position to the right of a previous entry in the dictionary
rightof = D[key].get('rightof',"")
if rightof != "" :
D[key]['pos'] = numpy.add(D[rightof]['rect'].bottomright,(margin,0)) # give it a little space
D[key]['align'] = 'BL'
# position to the left of a previous entry in the dictionary
leftof = D[key].get('leftof',"")
if leftof != "" :
D[key]['pos'] = numpy.subtract(D[leftof]['rect'].bottomleft,(margin,0))
D[key]['align'] = 'BR'
tempSurface = font.render(D[key].get('text',""),True,D[key].get('color',WHITE))
# if the line is shorter, need to clear previous box
pygame.draw.rect(txtSurface, BLACK, D[key].get('rect',(0,0,0,0)),0)
txtRect = tempSurface.get_rect()
boxRect = txtRect.inflate(10,4)
align = D[key].get('align','BR')
if align == 'BR' :
boxRect.bottomright = D[key]['pos']
if align == 'BL':
boxRect.bottomleft = D[key]['pos']
if align == 'MB' :
boxRect.midbottom = D[key]['pos']
txtRect.center = boxRect.center
D[key]['rect'] = boxRect
pygame.draw.rect(txtSurface,D[key].get('bg',(1,1,1)),boxRect,0) # text background default to almost black. can't use black: it's transparent
txtSurface.blit(tempSurface,txtRect)
if D[key].get('border',True) :
pygame.draw.rect(txtSurface,WHITE,boxRect,2)
def TXThighlight(key,highlight) :
if key in D :
if highlight :
pygame.draw.rect(txtSurface,RED,D[key]['rect'],2)
else:
pygame.draw.rect(txtSurface,WHITE,D[key]['rect'],2)
#utility functions
def setV4L2( ctrl, value ) :
return subprocess.run(shlex.split(f"v4l2-ctl -d {videoDev} --set-ctrl {ctrl}={value}"),stderr=subprocess.DEVNULL,stdout=subprocess.DEVNULL).returncode
def getV4L2( ctrl ) :
ret = subprocess.run(shlex.split(f"v4l2-ctl -d {videoDev} --get-ctrl {ctrl}"),capture_output=True, text=True)
if ret.returncode == 0 :
return ret.stdout.split(" ")[1].strip()
else :
return ""
txtActive = "" # a text object is active and wants attention. the value is the dictionary key to the object.
camBrightness = int(getV4L2("brightness"))
D['BRIGHT']['text'] = f"Brightness: {camBrightness}"
# display all of the objects
for key in list(D):
TXTdisplay(key)
image = cam.get_image()
active = True
while active:
events = pygame.event.get()
for e in events:
if e.type == QUIT or (e.type == KEYDOWN and e.key == K_ESCAPE):
active = False
if (e.type == MOUSEBUTTONDOWN):
TXThighlight(txtActive,False) # turn off previous highlight if any
txtActive = "" # a click anywhere ends any active txt inputs
for key in list(D):
# collide with dictionary
if D[key]['rect'].collidepoint(e.pos):
D[key]['value'] = 1
txtActive = key
TXThighlight(key,True)
break
if txtActive == "" :
# so, didn't collide with anything
if showAverage : # if showing average, mouse click is only for box collisions
continue
else: # ... otherwise mouse click is also selects the averaging line
(x,y) = e.pos
if (e.type == KEYDOWN and e.key == K_UP):
camBrightness = int(getV4L2("brightness"))
camBrightness += 2
setV4L2("brightness",camBrightness )
D['BRIGHT']['text'] = f"Brightness: {camBrightness}"
TXTdisplay('BRIGHT')
if (e.type == KEYDOWN and e.key == K_DOWN):
camBrightness = int(getV4L2("brightness"))
camBrightness -= 2
setV4L2("brightness",camBrightness )
D['BRIGHT']['text'] = f"Brightness: {camBrightness}"
TXTdisplay('BRIGHT')
if (e.type == KEYDOWN):
if txtActive != "":
# text input
if e.key == pygame.K_RETURN:
TXThighlight(txtActive,False)
txtActive = ""
else:
if e.key == pygame.K_BACKSPACE:
D[txtActive]['text'] = D[txtActive]['text'][:-1]
elif e.key == pygame.K_DELETE:
D[txtActive]['text'] = ""
else:
D[txtActive]['text'] += e.unicode
TXTdisplay(txtActive)
TXThighlight(txtActive,True)
else:
if (e.key == K_a):
noAverage = not noAverage
# camera stuff
if cam.query_image():
image = cam.get_image()
averageIndex += 1
averageIndex = averageIndex % averageItems
averageArray[0:width,averageIndex] = pygame.surfarray.array3d(image)[0:width,y]
if showAverage :
# average line over time
oorSurface.fill(BLACK) # initialize out of range pixel errors
# average the columns
for xCol in range(width):
for zColor in range(3):
if averageArray[xCol,averageIndex,zColor] > pixelClip: # value (almost) clipped
oorSurface.set_at((xCol,0),RED)
iTotal = 0
for yRow in range(averageItems):
iTotal += averageArray[xCol,yRow,zColor]
lineSurfArray[xCol,0,zColor] = int(iTotal/averageItems)
## what does it look like without averaging?
if noAverage:
lineSurfArray[0:width,0] = averageArray[0:width,averageIndex]
# convert lineSurfArray to lineSurface
lineSurface = pygame.surfarray.make_surface(lineSurfArray)
# fill lcd with lineSurface
# fill it in chunks, so that you can see that something is happening...
for i in range (int(height/8)):
yDisplayRow += 1
yDisplayRow = yDisplayRow % height
outSurface.blit(lineSurface, (0,yDisplayRow))
# the average surface...
lcd.blit(outSurface,(0,0))
# ...and Out Of Range marks
for i in range(10):
lcd.blit(oorSurface,(0,i)) # display OOR at top of window
else:
# camera image with averaging line
lcd.blit(image, (0,0))
pygame.draw.line(lcd, RED, (0,y), (width,y), 1)
# display text layer over everything
lcd.blit(txtSurface,(0,0))
pygame.display.flip()
# after every frame, check actions
if D['QUIT'].get('value',0) > 0:
active = False
if D['BRIGHT'].get('value',0) > 0:
# it's display only, didn't like being able to select and modify it...
D['BRIGHT']['value'] = 0
TXTdisplay('BRIGHT') # turn off highlight (rewrite it)
txtActive = ""
if D['AVERAGE'].get('value',0) > 0:
showAverage = not showAverage
D['AVERAGE']['value'] = 0
TXThighlight(txtActive,False) # turn off highlight
txtActive = ""
if D['SAVE'].get('value',0) > 0:
D['SAVE']['value'] = 0
TXThighlight(txtActive,False) # turn off highlight
txtActive = ""
timestr = time.strftime("%Y%m%d-%H%M%S")
name = D['TAG'].get('text','UNK')
desc = D['DESC'].get('text','unknown')
# write time averaged image (integer average, 8-bits/color)
fileName = "./%s-%s.jpg" % (name,timestr)
pygame.image.save(outSurface, fileName)
# a little bit of feedback for the operation
pygame.display.set_caption(fileName)
# write time averaged CSV, floating-point averaged colors
fileName = "./%s-%s.csv" % (name,timestr)
f = open(fileName, "x")
f.write( "%s,%s,%s\n" % (calibration.strip(),name,desc) )
# each column
for xCol in range (width):
z0 = 0
z1 = 0
z2 = 0
# average over time
for yRow in range(averageItems):
# average the colors
z0 += averageArray[xCol,yRow,0]
z1 += averageArray[xCol,yRow,1]
z2 += averageArray[xCol,yRow,2]
#for zColor in range(3):
# iTotal += averageArray[xCol,yRow,zColor]
iTotal = (z0 + z1 + z2)/3.0/averageItems
z0 = z0/averageItems
z1 = z1/averageItems
z2 = z2/averageItems
f.write("%d,%f,%f,%f,%f\n" % (xCol,iTotal,z0,z1,z2) )
f.close()
cam.stop()