-
Notifications
You must be signed in to change notification settings - Fork 0
/
src.py
320 lines (222 loc) · 8.33 KB
/
src.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
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
#!/usr/bin/env python3
import abc
import picamera
import time
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
# --- Video frame analysis utility functions ----------------------------------
def edgemedian(data, border):
"""Find the median of the pixels in a band at the edge of the data
Args:
data (numpy array): rectangle of pixels
border (int): width of border to find median within
Returns:
float: The median pixel value
Examples:
>>> x = np.array([[1,2,3,1],[3,9,9,2],[2,9,9,3],[1,3,2,1]])
>>> edgemedian(x,1)
2.0
>>> edgemedian(x,2)
2.5
"""
edge = np.ones(shape=data.shape, dtype=bool) # Array of True
edge[border:-border, border:-border] = 0 # Set middle False
return np.median(data[edge]) # Median of edge
def getbox(pos, size, width):
"""Get a slice surrounding a pixel, clipped as necessary
Note:
A coordinate which is an integer refers to the centre of a pixel,
equivalently, coordinates in the range -0.5 to +0.5 will be regarded as
within the pixel at index 0.
Args:
pos (number): centre coordinate
size (int): number of pixels each side of pixel containing the centre
width (int): slice then moved if necessary to fit within [0,width)
Returns:
(tuple): bounds of the slice
Examples:
>>> getbox(4, 5, 100)
(0, 11)
>>> getbox(5.49, 5, 100)
(0, 11)
>>> getbox(5.51, 5, 100)
(1, 12)
>>> getbox(93.49, 5, 100)
(88, 99)
>>> getbox(93.51, 5, 100)
(89, 100)
>>> getbox(95, 5, 100)
(89, 100)
"""
low = int(round(pos)) - size
high = int(round(pos)) + size + 1
if low < 0:
high -= low
low = 0
if high > width:
low -= (high - width)
high = width
return low, high
def locate_spot(image):
"""Find the coordinates of the pixel with the largest value
Args:
image: A 2D numpy array of pixel values
Returns:
A tuple (x, y) of the position of the spot
"""
y, x = np.unravel_index(image.argmax(), image.shape)
return x, y
def refine_spot(image, spot_size, x, y):
"""Refine an estimate of the position of the spot
Args:
image: A 2D numpy array of pixel values
spot_size: The size of the spot to look for
x, y: An initial estimate of the position of the spot
Returns:
A tuple (x, y) of the refined position of the spot
Notes:
This function subtracts an estimate of the background and then computes
the centre of gravity of the pixel values surrounding the crude
estimate of the spot position. This process is iterated a couple of
times.
"""
nit = 2
border = 2
h, w = image.shape
for it in range(nit):
# Get a small box surrounding the spot
lox, hix = getbox(x, spot_size, w)
loy, hiy = getbox(y, spot_size, h)
n = image[loy:hiy, lox:hix]
# Calculate the centre of gravity of the data within that box,
# first subtracting the background level (determined as the median
# value of the pixels in some border at the edge of the box).
m = edgemedian(n, border)
n = n - m
f = np.sum(n)
if f == 0: # Whole box the same intensity...
break # ...give up now
j, i = np.indices(n.shape)
x = np.sum(i*n) / f + lox
y = np.sum(j*n) / f + loy
# Return results
return x, y
# --- Spot finder class -------------------------------------------------------
class SpotFinder(abc.ABC):
"""Class to use a Raspberry Pi camera to find the location of a spot
To use this class, define your own subclass overriding at least the
spot_handler() method, create an instance of that class, and call the
start() method.
You may also wish to override the find_spot() method to change the spot
finding algorithm. Your override function may, but need not, use either
or both of the utility functions locate_spot() and refine_spot().
"""
def start(
self,
*,
spot_size,
sensor_mode=1,
resolution="320x304",
framerate=30,
exposure=5000,
duration=10,
aoi=None
):
"""Start spot location processing
Args:
spot_size: the size of the spot, in pixels
sensor_mode: the sensor mode to use for the camera [*]
resolution: the resolution of the returned video frames [*]
framerate: the framerate, in frames per second, of the video [*]
exposure: the exposure time, in microseconds [*]
duration: the duration, in seconds, to process video
aoi: a tuple (left, right, top, bottom) of a subregion of the frame
within which the spot is known to be located. The default value
of None means the whole frame is searched.
Notes:
Raspberry Pi camera documentation, for arguments marked [*], can be
found at https://picamera.readthedocs.io
"""
self._spot_size = spot_size
self._aoi = aoi
self._x = []
self._y = []
self._t = []
with picamera.PiCamera(
clock_mode='raw',
sensor_mode=sensor_mode,
resolution=resolution,
framerate=framerate
) as self._camera:
self._camera.shutter_speed = exposure
time.sleep(3) # let the camera warm up
self._camera.start_recording(self, format='rgb')
self._camera.wait_recording(duration)
self._camera.stop_recording()
self._camera = None
def write(self, buf):
"""Handle a video frame"""
# Extract timestamp and create np array view onto the passed frame data
ts = self._camera.frame.timestamp
width, height = self._camera.resolution
image = np.frombuffer(buf, dtype=np.uint8, count=width*height*3)
image = image.reshape((height, width,3))
image = image[:,:,0]
#image = image[80:250,50:250]
image = image.copy()
image[160:180,150:170] = 0
#print(image.shape)
# Locate the spot, but if an area of interest is specified, only look
# there
xtemp = [1]
ytemp = [1]
if self._aoi is not None:
image = image[self._aoi[2]:self._aoi[3], self._aoi[0]:self._aoi[1]]
image[:,180:-1] = 0
x, y = self.find_spot(image, self._spot_size)
if self._aoi is not None:
x += self._aoi[0]
y += self._aoi[2]
self._x.append(x)
self._y.append(y)
self._t.append(ts)
#self.spot_handler(ts, x, y)
def find_spot(self, image, spot_size):
"""Find the spot in an image
Args:
image:
spot_size:
Returns:
Notes:
The default implementation calls locate_spot to find an approximate
location of the spot, and then refine_spot to improve that
estimate.
This function may be overridden in a subclass if a different spot
location algorithm is wanted.
"""
#import matplotlib.pyplot as plt
#plt.imshow(image)
#plt.show()
#import numpy
#numpy.savetxt('image.csv',image, delimiter=",")
x, y = locate_spot(image)
x, y = refine_spot(image, spot_size, x, y)
return x, y
#@abc.abstractmethod
#def spot_handler(self, timestamp, x, y):
'''Take any desired action for a located spot in a frame
Args:
timestamp: timestamp in microseconds from an arbitrary epoch
x, y: pixel coordinates (floats) of the spot
Notes:
This function must be overridden in a subclass.
'''
#pass
def flush(self):
"""Called when all video frames have been processed
Notes:
This function may be overridden in a subclass if any action is
needed when all video frames have been processed.
"""
pass