forked from geezacoleman/OpenWeedLocator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgreenonbrown.py
419 lines (351 loc) · 16.8 KB
/
greenonbrown.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
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
#!/home/pi/.virtualenvs/owl/bin/python3
from algorithms import exg, exg_standardised, exg_standardised_hue, hsv, exgr, gndvi, maxg
from button_inputs import Selector, Recorder
from image_sampler import image_sample
from imutils.video import VideoStream, FileVideoStream, FPS
from relay_control import Controller
from queue import Queue
from time import strftime
import subprocess
import imutils
import shutil
import numpy as np
import time
import sys
import cv2
import os
def nothing(x):
pass
def green_on_brown(image, exgMin=30, exgMax=250, hueMin=30, hueMax=90, brightnessMin=5, brightnessMax=200, saturationMin=30,
saturationMax=255, minArea=1, headless=True, algorithm='exg'):
'''
Uses a provided algorithm and contour detection to determine green objects in the image. Min and Max
thresholds are provided.
:param image: input image to be analysed
:param exgMin:
:param exgMax:
:param hueMin:
:param hueMax:
:param brightnessMin:
:param brightnessMax:
:param saturationMin:
:param saturationMax:
:param minArea: minimum area for the detection - used to filter out small detections
:param headless: True: no windows display; False: watch what the algorithm does
:param algorithm: the algorithm to use. Defaults to ExG if not correct
:return: returns the contours, bounding boxes, centroids and the image on which the boxes have been drawn
'''
# different algorithm options, add in your algorithm here if you make a new one!
threshedAlready = False
if algorithm == 'exg':
output = exg(image)
elif algorithm == 'exgr':
output = exgr(image)
elif algorithm == 'maxg':
output = maxg(image)
elif algorithm == 'nexg':
output = exg_standardised(image)
elif algorithm == 'exhsv':
output = exg_standardised_hue(image, hueMin=hueMin, hueMax=hueMax,
brightnessMin=brightnessMin, brightnessMax=brightnessMax,
saturationMin=saturationMin, saturationMax=saturationMax)
elif algorithm == 'hsv':
output, threshedAlready = hsv(image, hueMin=hueMin, hueMax=hueMax,
brightnessMin=brightnessMin, brightnessMax=brightnessMax,
saturationMin=saturationMin, saturationMax=saturationMax)
elif algorithm == 'gndvi':
output = gndvi(image)
else:
output = exg(image)
print('[WARNING] DEFAULTED TO EXG')
if not headless:
cv2.imshow("Threshold", output)
# run the thresholds provided
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3))
# if not a binary image, run an adaptive threshold on the area that fits within the thresholded bounds.
if not threshedAlready:
output = np.where(output > exgMin, output, 0)
output = np.where(output > exgMax, 0, output)
output = np.uint8(np.abs(output))
if not headless:
cv2.imshow("post", output)
thresholdOut = cv2.adaptiveThreshold(output, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV, 31, 2)
thresholdOut = cv2.morphologyEx(thresholdOut, cv2.MORPH_CLOSE, kernel, iterations=1)
# if already binary, run morphological operations to remove any noise
if threshedAlready:
thresholdOut = cv2.morphologyEx(output, cv2.MORPH_CLOSE, kernel, iterations=5)
if not headless:
cv2.imshow("Threshold", thresholdOut)
# find all the contours on the binary images
cnts = cv2.findContours(thresholdOut.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)
weedCenters = []
boxes = []
# loop over all the detected contours and calculate the centres and bounding boxes
for c in cnts:
# filter based on total area of contour
if cv2.contourArea(c) > minArea:
# calculate the min bounding box
startX, startY, boxW, boxH = cv2.boundingRect(c)
endX = startX + boxW
endY = startY + boxH
cv2.rectangle(image, (int(startX), int(startY)), (endX, endY), (0, 0, 255), 2)
# save the bounding box
boxes.append([startX, startY, boxW, boxH])
# compute box center
centerX = int(startX + (boxW / 2))
centerY = int(startY + (boxH / 2))
weedCenters.append([centerX, centerY])
# returns the contours, bounding boxes, centroids and the image on which the boxes have been drawn
return cnts, boxes, weedCenters, image
# the
class Owl:
def __init__(self, video=False, videoFile=None, recording=False, nozzleNum=4, headless=True,
exgMin=30, exgMax=180, hueMin=30,hueMax=92, brightnessMin=5, brightnessMax=200,
saturationMin=30, saturationMax=255, resolution=(832, 624), framerate=32):
# different detection parameters
self.headless = headless
self.recording = recording
self.resolution = resolution
self.framerate = framerate
# threshold parameters for different algorithms
self.exgMin = exgMin
self.exgMax = exgMax
self.hueMin = hueMin
self.hueMax = hueMax
self.saturationMin = saturationMin
self.saturationMax = saturationMax
self.brightnessMin = brightnessMin
self.brightnessMax = brightnessMax
# setup the track bars if headless is False
if not self.headless:
# create trackbars for the threshold calculation
cv2.namedWindow("Params")
cv2.createTrackbar("thresholdMin", "Params", self.exgMin, 255, nothing)
cv2.createTrackbar("thresholdMax", "Params", self.exgMax, 255, nothing)
# instantiate the recorder if recording is True
if self.recording:
self.fourcc = cv2.VideoWriter_fourcc(*'MJPG')
self.writer = None
else:
self.record = False
self.saveRecording = False
# check if test video or videostream from camera
if video:
self.cam = FileVideoStream(videoFile).start()
# if no video, start the camera with the provided parameters
else:
try:
self.cam = VideoStream(usePiCamera=True, resolution=self.resolution, framerate=self.framerate).start()
except ModuleNotFoundError:
self.cam = VideoStream(src=0).start()
time.sleep(1.0)
# set the sprayqueue size
self.sprayQueue = Queue(maxsize=10)
# nozzleDict maps the reference nozzle number to a boardpin on the embedded device
self.nozzleDict = {
0: 13,
1: 15,
2: 16,
3: 18
}
### Data collection only ###
# algorithmDict maps pins to algorithms for data collection
self.algorithmDict = {
"exg": 29,
"nexg": 31,
"hsv": 33,
"exhsv": 35,
}
# this is where the recording button can be added. Currently set to pin 37
if self.recording:
self.recorderButton = Recorder(recordGPIO=37)
############################
# instantiate the nozzle controller - successful start should beep the buzzer
self.controller = Controller(nozzleDict=self.nozzleDict)
# instantiate the logger
self.logger = self.controller.logger
# sensitivity and weed size to be added
self.sensitivity = None
self.laneCoords = {}
# add the total number of nozzles. This can be changed easily, but the nozzleDict and physical relays would need
# to be updated too. Fairly straightforward, so an opportunity for more precise application
self.nozzleNum = nozzleNum
def hoot(self, sprayDur, delay, sample=False, sampleDim=400, saveDir='output', camera_name='cam1', algorithm='exg',
selectorEnabled=False, minArea=10):
# track FPS and framecount
fps = FPS().start()
if selectorEnabled:
self.selector = Selector(switchDict=self.algorithmDict)
try:
while True:
delay = self.update_delay(delay)
frame = self.cam.read()
if selectorEnabled:
algorithm, newAlgorithm = self.selector.algorithm_selector(algorithm)
if newAlgorithm:
self.logger.log_line('[NEW ALGO] {}'.format(algorithm))
if self.recording:
self.record = self.recorderButton.record
self.saveRecording = self.recorderButton.saveRecording
if frame is None:
fps.stop()
print("[INFO] Stopped. Approximate FPS: {:.2f}".format(fps.fps()))
self.stop()
break
if self.record and self.writer is None:
saveDir = os.path.join(saveDir, strftime("%Y%m%d-{}-{}".format(camera_name, algorithm)))
if not os.path.exists(saveDir):
os.makedirs(saveDir)
self.baseName = os.path.join(saveDir, strftime("%Y%m%d-%H%M%S-{}-{}".format(camera_name, algorithm)))
videoName = self.baseName + '.avi'
self.logger.new_video_logfile(name=self.baseName + '.txt')
self.writer = cv2.VideoWriter(videoName, self.fourcc, 30, (frame.shape[1], frame.shape[0]), True)
# retrieve the trackbar positions for thresholds
if not self.headless:
self.exgMin = cv2.getTrackbarPos("thresholdMin", "Params")
self.exgMax = cv2.getTrackbarPos("thresholdMax", "Params")
else:
# this leaves it open to adding dials for sensitivity. Static at the moment, but could be dynamic
self.update(exgMin=self.exgMin, exgMax=self.exgMax) # add in update values here
# pass image, thresholds to green_on_brown function
cnts, boxes, weedCentres, imageOut = green_on_brown(frame.copy(), exgMin=self.exgMin,
exgMax=self.exgMax,
hueMin=self.hueMin,
hueMax=self.hueMax,
saturationMin=self.saturationMin,
saturationMax=self.saturationMax,
brightnessMin=self.brightnessMin,
brightnessMax=self.brightnessMax,
headless=self.headless,
algorithm=algorithm, minArea=minArea)
##### IMAGE SAMPLER #####
# record sample images if required of weeds detected
# uncomment if needed
# if frameCount % 60 == 0 and sample is True:
# saveFrame = frame.copy()
# sampleThread = Thread(target=image_sample, args=[saveFrame, weedCentres, saveDir, sampleDim])
# sampleThread.start()
#########################
# activation region limit - once weed crosses this line, nozzle is activated
self.yAct = int((0.2) * frame.shape[0])
laneWidth = imageOut.shape[1] / self.nozzleNum
# calculate lane coords and draw on frame
for i in range(self.nozzleNum):
laneX = int(i * laneWidth)
# cv2.line(displayFrame, (laneX, 0), (laneX, imageOut.shape[0]), (0, 255, 255), 2)
self.laneCoords[i] = laneX
# loop over the ID/weed centres from contours
for ID, centre in enumerate(weedCentres):
# if they are in activation region the spray them
if centre[1] > self.yAct:
sprayTime = time.time()
for i in range(self.nozzleNum):
# determine which lane needs to be activated
if int(self.laneCoords[i]) <= centre[0] < int(self.laneCoords[i] + laneWidth):
# log a spray job with the controller using the nozzle, delay, timestamp and spray duration
# if GPS is used/speed control, delay can be updated automatically based on forward speed
self.controller.receive(nozzle=i, delay=delay, timeStamp=sprayTime, duration=sprayDur)
# update the framerate counter
fps.update()
if not self.headless:
cv2.imshow("Output", imutils.resize(imageOut, width=600))
if self.record and not self.saveRecording:
self.writer.write(frame)
if self.saveRecording and not self.record:
self.writer.release()
self.controller.solenoid.beep(duration=0.1)
self.recorderButton.saveRecording = False
fps.stop()
self.writer = None
self.logger.log_line_video("[INFO] {}. Approximate FPS: {:.2f}".format(self.baseName, fps.fps()), verbose=True)
fps = FPS().start()
k = cv2.waitKey(1) & 0xFF
if k == 27:
fps.stop()
self.logger.log_line_video("[INFO] Stopped. Approximate FPS: {:.2f}".format(fps.fps()), verbose=True)
self.stop()
break
except KeyboardInterrupt:
fps.stop()
self.logger.log_line_video("[INFO] Stopped. Approximate FPS: {:.2f}".format(fps.fps()), verbose=True)
self.stop()
except Exception as e:
self.controller.solenoid.beep(duration=0.5, repeats=5)
self.logger.log_line("[CRITICAL ERROR] STOPPED: {}".format(e))
# still in development
def update_software(self):
USBDir, USBConnected = check_for_usb()
if USBConnected:
files = os.listdir(USBDir)
workingDir = '/home/pi'
# move old version to version control directory first
oldVersionDir = strftime(workingDir + "/%Y%m%d-%H%M%S_update")
os.mkdir(oldVersionDir)
currentDir = '/home/pi/owl'
shutil.move(currentDir, oldVersionDir)
# move new directory to working directory
for item in files:
if 'owl' in item:
shutil.move()
def stop(self):
self.controller.running = False
self.controller.solenoid.all_off()
self.controller.solenoid.beep(duration=0.1)
self.controller.solenoid.beep(duration=0.1)
self.cam.stop()
if self.record:
self.writer.release()
self.recorderButton.running = False
if not self.headless:
cv2.destroyAllWindows()
sys.exit()
def update(self, exgMin=30, exgMax=180):
self.exgMin = exgMin
self.exgMax = exgMax
def update_delay(self, delay=0):
# if GPS added, could use it here to return a delay variable based on speed.
return delay
def check_for_usb():
try:
nanoMediaFolder = 'ls /media/pi'
proc = subprocess.Popen(nanoMediaFolder, shell=True, preexec_fn=os.setsid, stdout=subprocess.PIPE)
usbName = proc.stdout.readline().rstrip().decode('utf-8')
if len(usbName) > 0:
print('[INFO] Saving to {} usb'.format(usbName))
saveDir = '/media/pi/{}/'.format(usbName)
return saveDir, True
else:
print('[INFO] No USB connected. Saving to videos')
saveDir = '/home/pi/owl/videos'
return saveDir, False
except AttributeError:
print('[INFO] Windows computer detected...')
saveDir = '/videos/'
return saveDir, False
# business end of things
if __name__ == "__main__":
owl = Owl(video=False,
videoFile=r'',
headless=True,
recording=False,
exgMin=25,
exgMax=200,
hueMin=39,
hueMax=83,
saturationMin=50,
saturationMax=220,
brightnessMin=60,
brightnessMax=190,
framerate=32,
resolution=(416, 320))
# start the targeting!
owl.hoot(sprayDur=0.15,
delay=0,
sample=False,
sampleDim=1000,
saveDir='/home/pi',
algorithm='exhsv',
selectorEnabled=False,
camera_name='hsv',
minArea=10)