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
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • 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:6.1 - Detecting Things OpenCv and raspberry pi
  • 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: 6 Nov 2015 2:58 PM Date Created
  • Views 771 views
  • Likes 1 like
  • Comments 0 comments
  • rasp_berry
  • adapted_greenhouse
  • opencv
  • facedetection
Related
Recommended

Automated Green House Blog:6.1 - Detecting Things OpenCv and raspberry pi

m.ratcliffe
m.ratcliffe
6 Nov 2015

We will be using opencv to fully automate the feeding of fish, to do this we need to be able to:

>Identify breed of fish

>identify Size of fish

>Track the food consumption at feed time

 

OpenCv can perform all of these functions using a rapsbery pi and one cheap webcam [like the one in the image below].

 

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

 

The camera that works and is cheap:

 

image

 

OpenCV is an amazing piece of software and the newer Pi's are getting the power to run real time image processing tasks, like face detection.

 

However, OpenCv is not that user friendly, Lets make an attempt to simplify it a little. Most detection scripts need accompanying files that contain the information to identify what we are searching for in the image we just captured. Luckily OpenCv comes pre packed with some of these Haar-Cascaded [The files that tell open cv what to look for], they can be found in:

 

/usr/share/opencv/haarcascades/          [for Pi]

 

 

[for information on what a HaarCascade is or want to build your own to identify a fish or banana? see:

>http://docs.opencv.org/master/d7/d8b/tutorial_py_face_detection.html#gsc.tab=0

>Robotics@Cyborg: How to make your own haar trained ".xml" files

 

OpenCv comes with default Haar-Cascades to find:

>Faces [frontal and side]

>Eyes [Right or left]

>Number Plates

>Upper and lower bodies of people

You can find many more by a quick google search and paste them into the above folder.

 

So how do we use these to detect faces, run in the terminal:

 

$sudo python Scriptname <Path to Haar-Cascade you like to use]

 

To run the one in the video,

>paste the code below into a empty text file saved as facedetect.py, right click and select any one can execute, then:

>Plug in a usb webcam into the pi [the cheap square ones from ebay work well $3]

>Type into terminal:

 

sudo python favedetect.py /usr/share/opencv/haarcascades/haarcascade_frontalface_alt.xml

 

if you want to exit, click on the video window and press esc on keyboard

 

facedetect.py

## ********** Importing Some Libraries **************##

import cv2

import sys

##Dont know what this bit does, but it works

 

 

cascPath = sys.argv[1]

faceCascade = cv2.CascadeClassifier(cascPath)

#Telling script to capture from usb camera

 

 

video_capture = cv2.VideoCapture(0)

 

#************************Main LOOP *****************##

 

 

while True:

 

##********** We burn a few frames to make sure we have the newest one  [stops lag]

    # Capture frame-by-frame

    ret, frame = video_capture.read()

    ret, frame = video_capture.read()

    ret, frame = video_capture.read()

    ret, frame = video_capture.read()

    ret, frame = video_capture.read()

 

##****** Resizing Incoming Image

    res = cv2.resize(frame,None,fx=0.5, fy=0.5, interpolation = cv2.INTER_AREA)

    frame=res

##********Converting to Grey Scale

 

 

    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

##******** Finding Faces, changing the values for size of faces in camera view

 

 

    faces = faceCascade.detectMultiScale(

        gray,

        scaleFactor=1.1,

        minNeighbors=5,

        minSize=(30, 30),

        flags=cv2.cv.CV_HAAR_SCALE_IMAGE

    )

 

 

    # Draw a rectangle around the faces

    for (x, y, w, h) in faces:

        cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)

 

 

    # Display the resulting frame

    cv2.imshow('Video', frame)

 

 

    if cv2.waitKey(33)== 27:

        break

 

 

# When everything is done, release the capture

video_capture.release()

cv2.destroyAllWindows()

 

Checkout the main home page for updates:

 

Automated Green House Blog's Home Page

  • Sign in to reply
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