element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet & Tria Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      • Japan
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Vietnam
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Vertical Farming
  • Challenges & Projects
  • Design Challenges
  • Vertical Farming
  • More
  • Cancel
Vertical Farming
Blog Automated Green House Blog:5.1 - Updated Basic Vision Code
  • Blog
  • Forum
  • Documents
  • Polls
  • Files
  • Events
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: m.ratcliffe
  • Date Created: 8 Nov 2015 4:58 PM Date Created
  • Views 716 views
  • Likes 2 likes
  • Comments 2 comments
  • adapted_greenhouse
Related
Recommended

Automated Green House Blog:5.1 - Updated Basic Vision Code

m.ratcliffe
m.ratcliffe
8 Nov 2015

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]

You don't have permission to edit metadata of this video.
Edit media
x
image
Upload Preview
image

 

 

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.

  • Sign in to reply
Parents
  • DAB
    DAB over 10 years ago

    Nice update and example on using OpenCv.

     

    Are you planning to do some follow up posts as you get data related to feeding and fish size?

     

    It would be very useful to the rest of us to see how well your experiment turned out over time.

     

    DAB

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • m.ratcliffe
    m.ratcliffe over 10 years ago in reply to DAB

    Hey DAB,

    I'm just building a Haar Cascade, well attempting to build my first one.

     

    The test bed is up and running hydroponic mode until around june next year as it is a bit cold for the fish to grow much. I will be using video feed from a friends system in the mean time to try and create a robust system for some experiments early next year. Being able to log fish growth by camera will save me a lot of time and give better experiment data for future tests. Next years tests will be comparing feed amounts to productivity.

     

    The Blogs will continue after the competition on this and a few other topics and open sourced as always. Hopefully by June a new Pi will be out that can reduce the loop speed for food consumption, 2Hz is good but 10Hz would allow some averaging.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
Comment
  • m.ratcliffe
    m.ratcliffe over 10 years ago in reply to DAB

    Hey DAB,

    I'm just building a Haar Cascade, well attempting to build my first one.

     

    The test bed is up and running hydroponic mode until around june next year as it is a bit cold for the fish to grow much. I will be using video feed from a friends system in the mean time to try and create a robust system for some experiments early next year. Being able to log fish growth by camera will save me a lot of time and give better experiment data for future tests. Next years tests will be comparing feed amounts to productivity.

     

    The Blogs will continue after the competition on this and a few other topics and open sourced as always. Hopefully by June a new Pi will be out that can reduce the loop speed for food consumption, 2Hz is good but 10Hz would allow some averaging.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
Children
No Data
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube