This Is the Code for Finding fish food in a pond suing OpenCv and a raspberyPi.
Prerequisites:
>Raspberrypi 2b running OpenCV [Install Instructions Here]
>USB camera
>A Feeder, controlled by opto isolated relay {See instructions here}
This is a basic proof of Concept script, what it does it count the number of food particles in a Fish Pond.
It does this by:
>RaspberryPi request Image from USB Camera
>Turns Image into Gray Scale
>Turns grey scale into black and white
>Uses Blob detection on white image to identify food
The code below has been tested to work on a RaspberryPI 2b , running a Rasbian Image.
#!/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, Submitted to HackaDay under GNU etc")
print("10th/8/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 Video Capture and Defining Pins and other variables **##
cap = cv2.VideoCapture(0)
butPin = 21 # Broadcom pin 21 (P1 pin xx)
motorPin = 18 # 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)
print("Definitions Finished and Entering Main Loop")
##*********************** 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):
##**************** Capturing Image From Webcam ***********************##
_,frame = cap.read()
##***************** Bluring Image to unify color *********************##
frame = cv2.blur(frame,(3,3))
##**************** Converting To Gray Scale **************************##
## ##
##Simple Conversion Using RGB Works Empty Tank ##
##Convert from One Channel to Cancel Noise from Fish Color if Needed ##
##********************************************************************##
gray= cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
thresh = cv2.adaptiveThreshold(
gray,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,\
cv2.THRESH_BINARY,75,20)
thresh2 = thresh.copy()
##***************** Identify Pellets Of Food ************************##
contours,hierarchy = cv2.findContours(thresh,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)
##************ Simple Logic to Feed Fish on Demand ******************##
## ##
## PWM PIN Used Later As Project Evloves ##
## Most Likley Proportional+Intergral Control ##
##*******************************************************************##
if (len(contours) > 7 or GPIO.input(butPin)):
print("Waiting for Fish to Eat")
GPIO.output(motorPin,False);
else: # button is pressed:
print("Adding Food")
GPIO.output(motorPin,True)
time.sleep(0.01)
GPIO.output(motorPin,False);
##************ Print Some Useful Things to the Screen ***************##
print'Amount Of Food: ',len(contours)-1,' Pellets'
cv2.imshow('Normal',frame)
cv2.imshow('Gray',gray)
cv2.imshow('Pellets',thresh2)
time.sleep(0.1)
##**************** 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()
You Can download this file and an explanation on how to use it from:
www.MichaelRatcliffe.com/Projects
-either download the folder from the above link and unzip it into your home/pi folder. or paste the code from the above box into a text editor on the pi and save it as "ProofofConcept.py" in Home/Pi.
-Locate the File "ProofofConcept.py" Right Click on the File >Properties>Permissions and select everyone> OK
- because we are using the GPIO pins. Run it using:
cd home/pi
sudo python ProofofConcept.py
The Script should now run, it will output three windows and data to the command terminal about how much food it can see.
So it Works, what is left to do now:
>Improve immunity to noise in image processing
-Convert just main color into Gray Scale [ie red food, make a grey scale from just red component of image]
-Improve Blob Detection Implementation to stop groups of pellets being classed as a single pellet
>Calculate rate of food consumption or fish in tank
That is it for now, we have proved that it is possible to identify the pelleted food.
Discussions
Become a Hackaday.io Member
Create an account to leave a comment. Already have an account? Log In.