Well I learned a little bit of python and have cleaned the basic code up ready for evolving it into the ultimate plug and play self feeding system.
This is the basic code, we will be developing it to identify fish breed, size and optimise feeding times and lengths to suit. Also keep a accurate log of fish growth vs time.
[LongVideo, Becuase we are looking at all of the feeders]
The Basic Code:
| Header 1 |
|---|
#!/usr/bin/python
##*********Explain a Little About the Codes Purpose *****************## print("Vision Based Fish Feeder") print("This code is the first of its kind, addresing fish feeding with an active feedback system") print("Proof Of Concept Code, Under GNU etc") print("10th/11/2015") print("By Michael Ratcliffe") print("Mike@MichaelRatcliffe.com")
##**********************Import Some Librays**************************## import cv2 import numpy as np import RPi.GPIO as GPIO import time
print("Librarys Loaded")
##******** Creating Some Feeder Variables**************************## PelletsPerSecond=50.0 PelletSetpoint =25.0
##*** Creating Video Capture and Defining Pins and other variables **## cap = cv2.VideoCapture(0) butPin = 14 # Broadcom pin 21 (P1 pin xx) motorPin = 26 # Broadcom pin 18 (P1 pin 12) #dc = 20 # duty cycle (0-100) for PWM pin##not used unless pwm
GPIO.setmode(GPIO.BCM) # Broadcom pin-numbering scheme GPIO.setup(motorPin, GPIO.OUT) # PWM pin set as output #pwm = GPIO.PWM(pwmPin, 50) # Initialize PWM on pwmPin 100Hz frequency GPIO.setup(butPin, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Button pin set as input w/ pull-up #pwm.start(0)
global Start Start =0.000
print("Definitions Finished and Entering Main Loop")
########################################################################################### # Functions called from main loop # ###########################################################################################
#******************* Snapping an image from Camera *******************## def Capture_Frame(): #Buring any memory buffer global frame _,frame = cap.read() _,frame = cap.read() _,frame = cap.read() _,frame = cap.read() _,frame = cap.read()
#******************** Resizing to speed up processing time ***********## def Resize(): global frame frame = cv2.resize(frame,None,fx=0.5, fy=0.5, interpolation = cv2.INTER_AREA)
##***************** Bluring Image to unify color *********************##
def Blur(): global frame frame = cv2.blur(frame,(3,3))
#************** Converting to Grey Scale ****************************## def Gray(): global gray gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
#************** Convert to black and white *************************## def BlackWhite(): global thresh thresh = cv2.adaptiveThreshold( gray,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,\ cv2.THRESH_BINARY,121,20)
##*********** Identify Number of Pellets **************************## def Pelletes(): global contours contours,hierarchy = cv2.findContours(thresh,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)
##**********Feed The Fish If Needed ******************************## def Feed(): FeedLength=((PelletSetpoint-len(contours))/PelletsPerSecond) print(FeedLength)
#if (len(contours) > PelletSetpoint or GPIO.input(butPin)): if (len(contours) > PelletSetpoint): print("Waiting for Fish to Eat") GPIO.output(motorPin,False);
else: # button is pressed:
print("Adding Food") GPIO.output(motorPin,True) time.sleep(FeedLength) GPIO.output(motorPin,False);
##************* Tell What To Show To Screen *******************##
def Display(): print'Amount Of Food: ',len(contours)-1,' Pellets'
cv2.imshow('Normal',frame) cv2.imshow('Gray',gray) cv2.imshow('Pellets',thresh)
##************** Map The Size of Fish ************************## def Fish_Size(): GPIO.output(motorPin,False)
##************** Calculate fish Breed ***********************## def Fish_Breed(): GPIO.output(motorPin,False)
##************** Used For Testing Loop Time and optmisation ****## def Loop_Info(): global Start print("total time taken this loop: ", time.time() - Start) Start = time.time() ########################################################################################### # End Of Functions # ###########################################################################################
##************************************** Main Loop ***************************************## ## ## ## Runs Continuosly for testing purposes ## ## Eventualy It will Run every Hour 8am-8pm ## ## Feeding the Fish as Much as thet Can Eat in 5-10 minutes ## ##****************************************************************************************##
while(1):
Capture_Frame() Resize() Blur() Gray() BlackWhite() Fish_Size() Fish_Breed() Pelletes() Display() Feed() Loop_Info()
##***Checks If User Has Asked to Leave if cv2.waitKey(33)== 27: break ##************** Clean up everything before leaving *****************##
GPIO.cleanup() # cleanup all GPIO cap.release() cv2.destroyAllWindows() ############################################################################################ ##******************************** END OF LOOP *******************************************## ############################################################################################
|
On Model 2 Pi, Logitec C270 webcam:
Loop Time no reduction of image: 1.5 seconds ish
Loop Time Resized Images : 0.5 seconds ish
How To use
-Install openCV:
Blog:6 - Raspbery Pi and OpenCv the Easy Way
-Wire up a button to earth and a opto isolated relay [see code for used pins]
-Make fnew file in home "ProofofConcept.py
-Locate the File "ProofofConcept.py" Right Click on the File Properties>Permissions and select everyone> OK
-Because we are using the GPIO pins. we must run it as root
-cd home/pi [or wherever your proofofconcept.py is located]
-sudo python ProofofConcept.py
Now if everything is working you will have three screen pop up and when you pres the button it will do its best to keep the set amout of food in the pond. As it stands this will not be enough to optimise your fish feeding, it still needs to be ran at the optimum times during the day and not left to run all day etc. But It should give you a little intro into how things are coming along.
-
DAB
-
Cancel
-
Vote Up
0
Vote Down
-
-
Sign in to reply
-
More
-
Cancel
-
m.ratcliffe
in reply to DAB
-
Cancel
-
Vote Up
0
Vote Down
-
-
Sign in to reply
-
More
-
Cancel
Comment-
m.ratcliffe
in reply to DAB
-
Cancel
-
Vote Up
0
Vote Down
-
-
Sign in to reply
-
More
-
Cancel
Children