Commit 4e8c83fa authored by Samaranayake S.L's avatar Samaranayake S.L

Merge branch 'IT19107356_Shevon_Krishmal' into 'master'

Live monitoring and image capturing system

See merge request !4
parents 41bf2710 651ca451
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
import cv2
import matplotlib.pyplot
import numpy as np
# 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)"""
sql_insert_blob_query_procssed = """ INSERT INTO processed_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 processImage(image):
#Read the path of the image
#------------------------------------------------------
# read image, support bmp,jpg,png,tiff format
#Read the path of the image in the computer
img = cv2.imread(image)#Change the image path to your own
#Set the RGB value in the image to a 64-bit floating point
img=np.array(img,dtype='float64')
#Declare the three 0 matrices, and put the R,G, and B values of the picture into the three matrices.
b = np.zeros((img.shape[0],img.shape[1]), dtype=img.dtype)
g = np.zeros((img.shape[0],img.shape[1]), dtype=img.dtype)
r = np.zeros((img.shape[0],img.shape[1]), dtype=img.dtype)
b[:,:] = img[:,:,0]
g[:,:] = img[:,:,1]
r[:,:] = img[:,:,2]
#A formula for generating grayscale images
new=2*g-r-b
w=new.min()
e=new.max()
new=new-w
new=new/e*255
new=np.array(new,dtype='uint8')
#-----------------------------------------------------------------------
# Otsu filtering method and fill the hole
#-----------------------------------------------------------------------
# A Otsu filtering method is used for filtering
ret2, th2 = cv2.threshold(new, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
#Check the threshold of Otsu filtering method
print("threshold of Otsu filtering:",ret2)
# Otsu filtering's results are replicated to hole
hole=th2.copy()
#Find the hole and fill it
cv2.floodFill(hole,None,(0,0),255)
hole=cv2.bitwise_not(hole)
filledEdgesOut=cv2.bitwise_or(th2,hole)
#---------------------------------------------------------------------
#The image of corrosion
#-------------------------------------------------------------------------------
#A circle of diameter 5 is used as the corrosion structure
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(5,5))
#image of corrosion
eroded = cv2.erode(filledEdgesOut,kernel)
#-------------------------------------------------------------------------------
#Eliminate connected region
#-------------------------------------------------------------------------------
def baweraopen(image,size):
output=image.copy()
nlabels, labels, stats, centroids = cv2.connectedComponentsWithStats(image)
for i in range(1,nlabels-1):
regions_size=stats[i,4]
if regions_size<size:
x0=stats[i,0]
y0=stats[i,1]
x1=stats[i,0]+stats[i,2]
y1=stats[i,1]+stats[i,3]
for row in range(y0,y1):
for col in range(x0,x1):
if labels[row,col]==i:
output[row,col]=0
return output
im2=baweraopen(eroded,180)#200 is the size of the connected region to be eliminated.
#---------------------------------------------------------------------------------
# Count the number of pixels in the plant
#---------------------------------------------------------------------------------
print("number of pixels in the plant:",len(im2.nonzero()[0]))
#distance is 50
distance_top=50
Area=(pow((0.000122*(distance_top-0.304)/0.304),2)*len(im2.nonzero()[0]))
print("leaf area:",round(Area, 2))
#---------------------------------------------------------------------------------
#show the new image.
#---------------------------------------------------------------------------------
img[:,:,2]=im2*r
img[:,:,1]=im2*g
img[:,:,0]=im2*b
matplotlib.pyplot.imshow((img * 255).astype(np.uint8))
#---------------------------------------------------------------------------------
#save the new image
filename = "p_"+image
cv2.imwrite(filename, img)
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("Raw Image Uploaded Successfully")
time.sleep(2)
processImage(pic_name)
processed_img = "p_"+pic_name
blob = convertToBinaryData(processed_img)
# Convert data into tuple format
insert_blob_tuple = ("p_"+name, str(index), date, blob)
result = cursor.execute(sql_insert_blob_query_procssed, insert_blob_tuple)
conn.commit()
print("Processed Image 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