|
| 1 | +# setup start |
| 2 | + |
| 3 | +import sys |
| 4 | +sys.path.append('/usr/local/lib/python2.7/site-packages') |
| 5 | +from picamera import PiCamera |
| 6 | +from time import sleep |
| 7 | +import time, os, fnmatch, shutil, serial, csv |
| 8 | +port = serial.Serial("/dev/ttyUSB0", baudrate=9600, timeout=1) |
| 9 | +camera = PiCamera() |
| 10 | +import RPi.GPIO as GPIO |
| 11 | +GPIO.setmode(GPIO.BOARD) |
| 12 | +TRIG = 38 |
| 13 | +ECHO = 40 |
| 14 | +i=0 |
| 15 | +GPIO.setwarnings(False) |
| 16 | +GPIO.setup(TRIG,GPIO.OUT) |
| 17 | +GPIO.setup(ECHO,GPIO.IN) |
| 18 | + |
| 19 | +# setup over |
| 20 | + |
| 21 | +def ic(): #image capture |
| 22 | + timestamp = time.strftime('%Y-%m-%d-%H%M%S', time.localtime()); |
| 23 | + FILE_NAME = ("/home/pi/data/" + timestamp+'.png') |
| 24 | + camera.start_preview() |
| 25 | + camera.capture(FILE_NAME) |
| 26 | + sleep(1) |
| 27 | + camera.stop_preview() |
| 28 | + |
| 29 | +def vc(): #video capture |
| 30 | + timestamp = time.strftime('%Y-%m-%d-%H%M%S', time.localtime()); |
| 31 | + FILE_NAME = ("/home/pi/data/" + timestamp+'.h264') |
| 32 | + camera.start_preview() |
| 33 | + camera.start_recording(FILE_NAME) |
| 34 | + sleep(20) |
| 35 | + camera.stop_recording() |
| 36 | + camera.stop_preview() |
| 37 | + |
| 38 | +def ld(): # location detection |
| 39 | + while 1: |
| 40 | + data = port.readline() |
| 41 | + if "$GPGGA" in data: |
| 42 | + #print(data) |
| 43 | + mylist=data.split(",") |
| 44 | + print(mylist[2]+' N', mylist[4]+' E') |
| 45 | + break |
| 46 | + |
| 47 | +def dd(): #distance detection |
| 48 | + GPIO.output(TRIG, False) |
| 49 | + time.sleep(2) |
| 50 | + GPIO.output(TRIG, True) |
| 51 | + time.sleep(0.00001) |
| 52 | + GPIO.output(TRIG, False) |
| 53 | + while GPIO.input(ECHO)==0: |
| 54 | + pulse_start = time.time() |
| 55 | + while GPIO.input(ECHO)==1: |
| 56 | + pulse_end = time.time() |
| 57 | + pulse_duration = pulse_end - pulse_start |
| 58 | + distance = pulse_duration * 17150 |
| 59 | + distance = round(distance+1.15, 2) |
| 60 | + print(distance) |
| 61 | + |
| 62 | +def aione(): #image capture, distance, location, all in one |
| 63 | + timestamp = time.strftime('%Y-%m-%d-%H%M%S', time.localtime()); |
| 64 | + FILE_NAME = ("/home/pi/data/" + timestamp+'.png') |
| 65 | + camera.start_preview() |
| 66 | + camera.capture(FILE_NAME) |
| 67 | + sleep(1) |
| 68 | + datarow=[timestamp,FILE_NAME,'lat','long','dis','leafHealth','productCount'] |
| 69 | + camera.stop_preview() |
| 70 | + while 1: |
| 71 | + data = port.readline() |
| 72 | + if "$GPGGA" in data: |
| 73 | + mylist=data.split(",") |
| 74 | + datarow[2]=mylist[2]+' N' |
| 75 | + datarow[3]=mylist[4]+' E' |
| 76 | + sleep(1) |
| 77 | + break |
| 78 | + GPIO.output(TRIG, False) |
| 79 | + time.sleep(2) |
| 80 | + GPIO.output(TRIG, True) |
| 81 | + time.sleep(0.00001) |
| 82 | + GPIO.output(TRIG, False) |
| 83 | + while GPIO.input(ECHO)==0: |
| 84 | + pulse_start = time.time() |
| 85 | + while GPIO.input(ECHO)==1: |
| 86 | + pulse_end = time.time() |
| 87 | + pulse_duration = pulse_end - pulse_start |
| 88 | + distance = pulse_duration * 17150 |
| 89 | + distance = round(distance+1.15, 2) |
| 90 | + datarow[4]=distance |
| 91 | + print(datarow);print('\n') |
| 92 | + with open('/home/pi/data/metadata.csv', 'a') as csvFile: |
| 93 | + writer = csv.writer(csvFile) |
| 94 | + writer.writerow(datarow) |
| 95 | + csvFile.close() |
| 96 | + |
| 97 | + |
| 98 | +if __name__ == '__main__': |
| 99 | +# all in one |
| 100 | + aione() |
| 101 | + |
| 102 | +# 1 image |
| 103 | +# ic() |
| 104 | + |
| 105 | +# gps |
| 106 | +# ld() |
| 107 | + |
| 108 | +# untrasonic |
| 109 | +# dd() |
| 110 | + |
| 111 | +# 6 images |
| 112 | +# ic();ic();ic();ic();ic();ic(); |
| 113 | + |
| 114 | +# 18 images |
| 115 | +# ic();ic();ic();ic();ic();ic();ic();ic();ic();ic();ic();ic();ic();ic();ic();ic();ic();ic(); |
| 116 | + |
| 117 | +# 1 video, 20 secs total |
| 118 | +# vc() |
| 119 | + |
| 120 | +# 3 videos, 60 secs total |
| 121 | +# vc();vc();vc(); |
| 122 | + |
| 123 | + |
| 124 | +# a;echo 1;a;echo 2;a;echo 3;a;echo 4;a;echo 5;a;echo 6;a;echo 7;a;echo 8;a;echo 9;a;echo 10; |
0 commit comments