-
Notifications
You must be signed in to change notification settings - Fork 0
/
gpio_camera.py
84 lines (69 loc) · 1.8 KB
/
gpio_camera.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
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import picamera
import numpy as np
from gpiozero import Button, LED
from signal import pause
from time import sleep
import pickle
def NDVI_Capture():
with picamera.PiCamera() as camera:
camera.resolution = (320, 240)
#camera.awb_mode = 'off'
#camera.awb_gains = ((1.2),(1))
sleep(2)
output = np.empty((240, 320, 3), dtype=np.uint8)
camera.capture(output, 'rgb')
red = 0
nir = 2
output32 = output.astype(np.float32)
a = (output32[:, :, nir] - output32[:, :, red])
b = (output32[:, :, red] + output32[:, :, nir])
ndvi = np.divide(a, b, out=np.zeros_like(a), where=b!=0)
print(ndvi)
plt.figure(1)
plt.imshow(ndvi, cmap = "nipy_spectral", vmin = -1, vmax = 1)
plt.colorbar()
plt.savefig('/home/pi/Desktop/pics/NDVI.%s.%s.png' % (count, pic))
plt.close()
print("Saved NDVI")
print(output)
plt.figure(2)
plt.imshow(output)
plt.savefig('/home/pi/Desktop/pics/RGB.%s.%s.png' % (count, pic))
plt.close()
print("Saved RGB")
count = 1
save = '/home/pi/camera/cameraCount.pk'
with open(save, 'rb') as fi:
count = pickle.load(fi)
count += 1
with open(save, 'wb') as fi:
pickle.dump(count, fi)
button = Button(2)
led = LED(17)
pic = 1
sleep(2)
while True:
if button.is_pressed:
for i in range(2):
led.off()
sleep(0.5)
led.on()
sleep(0.5)
while button.is_pressed:
NDVI_Capture()
led.off()
sleep(1)
led.on()
pic += 1
sleep(5)
else:
for i in range(2):
led.on()
sleep(0.5)
led.off()
sleep(5)
led.off()