Commit 43501cfd authored by shevon's avatar shevon

Live monitoring and image capturing system

parent 31628f88
import RPi.GPIO as GPIO
from time import sleep
import subprocess
# Direction pin from controller
DIR = 5
CDIR = 10
# Step pin from controller
STEP = 3
CSTEP = 8
# 0/1 used to signify clockwise or counterclockwise.
CW = 1
CCW = 0
#motor steps
tower_steps = 0
found_row = 0
# Setup pin layout on PI
GPIO.setmode(GPIO.BOARD)
# Establish Pins in software
GPIO.setup(DIR, GPIO.OUT)
GPIO.setup(STEP, GPIO.OUT)
GPIO.setup(CDIR, GPIO.OUT)
GPIO.setup(CSTEP, GPIO.OUT)
# Set the first direction you want it to spin
#GPIO.output(DIR, CW)
def tower_rotate(x):
print("Tower Rotate",x)
GPIO.output(DIR,CW)
for x in range(x):
GPIO.output(STEP,GPIO.HIGH)
sleep(.00005)
GPIO.output(STEP,GPIO.LOW)
sleep(.00005)
def camera_movement(y):
distance_palnts = 64000 #plant distance
degrees = (y-1)* distance_palnts
print("Camera Location",degrees)
GPIO.output(CDIR,CW)
for degrees in range(degrees):
GPIO.output(CSTEP,GPIO.HIGH)
sleep(.00005)
GPIO.output(CSTEP,GPIO.LOW)
sleep(.00005)
def tower_default(x):
print("Tower Rerotate",x)
GPIO.output(DIR,CCW)
for x in range(x):
GPIO.output(STEP,GPIO.HIGH)
sleep(.00005)
GPIO.output(STEP,GPIO.LOW)
sleep(.00005)
def camera_default(y):
distance_palnts = 64000 #plant distance
degrees = (y-1)* distance_palnts
print("Camera_defualt",degrees)
GPIO.output(CDIR,CCW)
for degrees in range(degrees):
GPIO.output(CSTEP,GPIO.HIGH)
sleep(.00005)
GPIO.output(CSTEP,GPIO.LOW)
sleep(.00005)
def navigation(input_index):
global tower_steps
global found_row
#search and identify respective row and column
flag = False
for i in range(rows):
for j in range(columns):
if plant_index[i][j]==input_index:
print("Plant Found!")
print('row',5-i)
print("column",j+1)
found_column = j+1
found_row = 5-i
#print(found_row)
#switch case for motor commands
if found_column == 1:
tower_steps = 0
tower_rotate(tower_steps)
sleep(1.0)
camera_movement(found_row)
elif found_column == 2:
tower_steps = 12200
tower_rotate(tower_steps)
sleep(1.0)
camera_movement(found_row)
elif found_column == 3:
tower_steps = 24400
tower_rotate(tower_steps)
sleep(1.0)
camera_movement(found_row)
flag = True
break
if flag == False:
print ("Plant Not found!")
try:
plant_index = [[4,5,14],[3,6,13],[2,7,12],[1,8,11],[0,9,10]]
rows=len(plant_index)
columns=len(plant_index[0])
#display plant 2D array
for x in plant_index: # outer loop
for i in x: # inner loop
print(i, end = " ") # print the elements
print()
#get plant index
input_index = int(input("Enter plant index: "))
print("plant_index",input_index)
#navigate plant location
navigation(input_index)
#live camera on
#execfile("livestreamng.py")
#exec(open("livestreamng.py").read())
#navigatige defualt location
print("tower_default",tower_steps)
print("camera_default",found_row)
input_defualt = bool(input("Do you want to stop inspection: "))
if input_defualt:
camera_default(found_row)
tower_default(tower_steps)
else:
camera_default(found_row)
tower_default(tower_steps)
# Once finished clean everything up
except KeyboardInterrupt:
print("cleanup")
GPIO.cleanup()
import RPi.GPIO as GPIO
from time import sleep
import datetime as dt
import pyglet
from picamera import PiCamera
import time
import mysql.connector
from mysql.connector import errorcode
# Direction pin from controller
DIR = 10
CDIR = 5
# Step pin from controller
STEP = 8
CSTEP = 3
# 0/1 used to signify clockwise or counterclockwise.
CW = 1
CCW = 0
# Setup pin layout on PI
GPIO.setmode(GPIO.BOARD)
# Establish Pins in software
GPIO.setup(DIR, GPIO.OUT)
GPIO.setup(STEP, GPIO.OUT)
GPIO.setup(CDIR, GPIO.OUT)
GPIO.setup(CSTEP, GPIO.OUT)
# Set the first direction you want it to spin
#GPIO.output(DIR, CW)
camera = PiCamera()
# Obtain connection string information from the portal
config = {
'host':'azdbmysqltest.mysql.database.azure.com',
'user':'ThyagaSenatilaka@azdbmysqltest',
'password':'Thagi.12345',
'database':'agroengine',
'client_flags': [mysql.connector.ClientFlag.SSL],
'ssl_ca': 'DigiCertGlobalRootG2.crt.pem'
}
conn = mysql.connector.connect(**config)
if conn.is_connected():
print("Connection established")
cursor = conn.cursor()
sql_insert_blob_query = """ INSERT INTO plant_images
(img_ID, plant_ID, date, image) VALUES (%s,%s,%s,%s)"""
#cnx = mysql.connector.connect(user="ThyagaSenatilaka@azdbmysqltest", password="Thagi.12345", host="azdbmysqltest.mysql.database.azure.com", port=3306, database="agroengine", ssl_verify_cert=False)
#if cnx.is_connected():
# print("Connection established")
def tower_rotate_cw(x):
print("tower cw rotate ",x)
GPIO.output(DIR,CW)
for x in range(x):
GPIO.output(STEP,GPIO.HIGH)
sleep(.0005)
GPIO.output(STEP,GPIO.LOW)
sleep(.0005)
def tower_rotate_ccw(x):
print("tower ccw rotate ",x)
GPIO.output(DIR,CCW)
for x in range(x):
GPIO.output(STEP,GPIO.HIGH)
sleep(.0005)
GPIO.output(STEP,GPIO.LOW)
sleep(.0005)
def camera_movement_cw(y):
distance_palnts = 64000 #plant distance
print("camera location up",y)
GPIO.output(CDIR,CW)
for degrees in range(distance_palnts):
GPIO.output(CSTEP,GPIO.HIGH)
sleep(.0005)
GPIO.output(CSTEP,GPIO.LOW)
sleep(.0005)
def camera_movement_ccw(y):
distance_palnts = 64000 #plant distance
print("camera location down",y)
GPIO.output(CDIR,CCW)
for degrees in range(distance_palnts):
GPIO.output(CSTEP,GPIO.HIGH)
sleep(.0005)
GPIO.output(CSTEP,GPIO.LOW)
sleep(.0005)
def convertToBinaryData(filename):
# Convert digital data to binary format
with open(filename, 'rb') as file:
binaryData = file.read()
return binaryData
def capture(index):
x = str(dt.datetime.now())
date=x.replace('/','_')
name=date+"_"+str(index)
print(name)
pic_name=name+".jpg"
#sound = pyglet.media.load("gohomegota.mp3")
#sound.play()
#sleep(.00005)
camera.start_preview()
time.sleep(2)
camera.capture(pic_name)
blob = convertToBinaryData(pic_name)
# Convert data into tuple format
insert_blob_tuple = (name, str(index), date, blob)
result = cursor.execute(sql_insert_blob_query, insert_blob_tuple)
conn.commit()
print("Uploaded Successfully")
time.sleep(2)
#camera.stop_preview()
try:
"""for col in [0, 1066, 1066]:
#tower_rotate_cw(col)
print(col)
capture1()
for row in range(5):
print(row+1)
capture()
#reverse()
for down in range(5):
print()"""
#tower_rotate_cw(0)
capture(0)
#camera_movement_cw(1)
capture(1)
#camera_movement_cw(2)
capture(2)
#camera_movement_cw(3)
capture(3)
#camera_movement_cw(4)
capture(4)
#tower_rotate_cw(12200)
capture(5)
#camera_movement_ccw(1)
capture(6)
#camera_movement_ccw(2)
capture(7)
#camera_movement_ccw(3)
capture(8)
#camera_movement_ccw(4)
capture(9)
#tower_rotate_cw(12200)
capture(10)
#camera_movement_cw(1)
capture(11)
#camera_movement_cw(2)
capture(12)
#camera_movement_cw(3)
capture(13)
#camera_movement_cw(4)
capture(14)
#camera_movement_ccw(1)
#camera_movement_ccw(2)
#camera_movement_ccw(3)
#camera_movement_ccw(4)
#tower_rotate_ccw(24400)
if conn.is_connected():
cursor.close()
conn.close()
print("MySQL connection is closed")
# Once finished clean everything up
except KeyboardInterrupt:
print("cleanup")
GPIO.cleanup()
import io
import picamera
import logging
import socketserver
from threading import Condition
from http import server
PAGE="""\
<html>
<head>
<title>Raspberry Pi - Surveillance Camera</title>
</head>
<body>
<center><h1>Raspberry Pi - Surveillance Camera</h1></center>
<center><img src="stream.mjpg" width="640" height="480"></center>
</body>
</html>
"""
class StreamingOutput(object):
def __init__(self):
self.frame = None
self.buffer = io.BytesIO()
self.condition = Condition()
def write(self, buf):
if buf.startswith(b'\xff\xd8'):
# New frame, copy the existing buffer's content and notify all
# clients it's available
self.buffer.truncate()
with self.condition:
self.frame = self.buffer.getvalue()
self.condition.notify_all()
self.buffer.seek(0)
return self.buffer.write(buf)
class StreamingHandler(server.BaseHTTPRequestHandler):
def do_GET(self):
if self.path == '/':
self.send_response(301)
self.send_header('Location', '/index.html')
self.end_headers()
elif self.path == '/index.html':
content = PAGE.encode('utf-8')
self.send_response(200)
self.send_header('Content-Type', 'text/html')
self.send_header('Content-Length', len(content))
self.end_headers()
self.wfile.write(content)
elif self.path == '/stream.mjpg':
self.send_response(200)
self.send_header('Age', 0)
self.send_header('Cache-Control', 'no-cache, private')
self.send_header('Pragma', 'no-cache')
self.send_header('Content-Type', 'multipart/x-mixed-replace; boundary=FRAME')
self.end_headers()
try:
while True:
with output.condition:
output.condition.wait()
frame = output.frame
self.wfile.write(b'--FRAME\r\n')
self.send_header('Content-Type', 'image/jpeg')
self.send_header('Content-Length', len(frame))
self.end_headers()
self.wfile.write(frame)
self.wfile.write(b'\r\n')
except Exception as e:
logging.warning(
'Removed streaming client %s: %s',
self.client_address, str(e))
else:
self.send_error(404)
self.end_headers()
class StreamingServer(socketserver.ThreadingMixIn, server.HTTPServer):
allow_reuse_address = True
daemon_threads = True
with picamera.PiCamera(resolution='640x480', framerate=24) as camera:
output = StreamingOutput()
#Uncomment the next line to change your Pi's Camera rotation (in degrees)
#camera.rotation = 90
camera.start_recording(output, format='mjpeg')
try:
address = ('', 8000)
server = StreamingServer(address, StreamingHandler)
server.serve_forever()
finally:
camera.stop_recording()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment