#!/usr/bin/env python
# opencv-picam-face.py - Opencv using picamera for face tracking using pan/tilt search and lock
# written by Claude Pageau -
# This is a little laggy but does work OK.
# Uses pipan.py module from openelectron.com RPI camera pan/tilt to control
# camera tracking or use your own pan/tilt module and modify code accordingly.
# if you are not using openelectrons.com pan/tilt hardware.
# Also picamera python module must be installed as well as opencv
# To install opencv and python for opencv
# sudo apt-get install libopencv-dev python-opencv
# To install picamera python module
# sudo apt-get install python-picamera
# You will also need to install python picamera.array tha includes numpy
# sudo pip install "picamera[array]"
# Note
# v4l2 driver is not used since stream is created using picamera module
# using picamera.array
# If you have any questions email pageauc@gmail.com
import io
import time
import picamera
import picamera.array
import cv2
# openelectron.com python module and files from the OpenElectron RPI camera pan/tilt
# Copy pipan.py to same folder as this script.
import pipan
p = pipan.PiPan()
# Approx Center of Pan/Tilt motion
pan_x_c = 150
pan_y_c = 140
# bounds checking for pan/tilt search.
limit_y_bottom = 80
limit_y_top = 180
limit_y_level = 140
limit_x_left = 60
limit_x_right = 240
# To speed things up, lower the resolution of the camera
CAMERA_WIDTH = 320
CAMERA_HEIGHT = 240
# Camera center of image
cam_cx = CAMERA_WIDTH / 2
cam_cy = CAMERA_HEIGHT / 2
# Face detection opencv center of face box
face_cx = cam_cx
face_cy = cam_cy
# Pan/Tilt motion center point
pan_cx = pan_x_c
pan_cy = pan_y_c
# Amount pan/tilt moves when searching
pan_move_x = 30
pan_move_y = 20
# Timer seconds to wait before starting pan/tilt search for face.
wait_time = 60
# load a cascade file for detecting faces. This file must be in
# same folder as this script. Can usually be found as part of opencv
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
# Saving the picture to an in-program stream rather than a file
stream = io.BytesIO()
# Move the pan/tilt to a specific location. has built in limit checks.
def pan_goto(x,y):
p.do_pan (int(x))
p.do_tilt (int(y))
# Start Main Program
with picamera.PiCamera() as camera:
camera.resolution = (CAMERA_WIDTH, CAMERA_HEIGHT)
camera.vflip = True
time.sleep(2)
# Put camera in a known good position.
pan_goto(pan_cx, pan_cy)
face_found = False
start_time = time.time()
while(True):
with picamera.array.PiRGBArray(camera) as stream:
camera.capture(stream, format='bgr')
# At this point the image is available as stream.array
image = stream.array
# Convert to grayscale, which is easier
gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
# Look for faces over the given image using the loaded cascade file
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
for (x,y,w,h) in faces:
if w > 0 :
face_found = True
start_time = time.time()
# Opencv has built in image manipulation functions
cv2.rectangle(image,(x,y),(x+w,y+h),(255,0,0),2)
face_cx = x + w/2
Nav_LR = cam_cx - face_cx
pan_cx = pan_cx - Nav_LR /5
face_cy = y + h/2
Nav_UD = cam_cy - face_cy
pan_cy = pan_cy - Nav_UD /4
pan_goto(pan_cx, pan_cy)
# Print Navigation required to center face in image
print " Nav LR=%s UD=%s " % (Nav_LR, Nav_UD)
elapsed_time = time.time() - start_time
# start pan/tilt search for face if timer runs out
if elapsed_time > wait_time:
face_found = False
print "Timer=%d > %s seconds" % (elapsed_time, wait_time)
pan_cx = pan_cx + pan_move_x
if pan_cx > limit_x_right:
pan_cx = limit_x_left
pan_cy = pan_cy + pan_move_y
if pan_cy > limit_y_top:
pan_cy = limit_y_bottom
pan_goto (pan_cx, pan_cy)
# Use opencv built in window to show the image
# Leave out if your Raspberry Pi isn't set up to display windows
cv2.imshow('Test Image',image)
if cv2.waitKey(1) & 0xFF == ord('q'):
# Close Window
cv2.destroyAllWindows()
exit
Statistics: Posted by pageauc — Fri Aug 22, 2014 1:53 am
Statistics: Posted by pageauc — Thu Aug 21, 2014 1:17 pm
Statistics: Posted by Alan — Thu Aug 21, 2014 7:58 am
Statistics: Posted by pageauc — Thu Aug 21, 2014 6:16 am
#!/usr/bin/env python
# opencv-test7 - Opencv face tracking face tracking with pan/tilt
# written by Claude Pageau - Still a work in progress so excuse the mess
# and unused variables. Just trying to get something to work.
# This is a little laggy but does work OK.
# Uses pipan.py module for openelectron.com RPI camera pan/tilt to control
# camera tracking so you will need to use your own pan/tilt module if
# you are not using openelectrons.com hardware.
# Also picamera python module must be installed as well as opencv
# and v4l2 driver (execute sudo modprobe bcm2834-v4l2 command to
# install /dev/video0 device.
# numby not being used so can be removed since changed code to
# use picamera.array instead.
# Still need to write a pan/tilt search for face routine to try to
# find a face if none found in current frame.
import io
import time
import picamera
import picamera.array
import cv2
import numpy as np
import pipan
p = pipan.PiPan()
sleep_time = 0.5
pan_x_c = 150
pan_y_c = 130
# these variables are not currently used. pipan module does
# bounds checking already so pan/tilt will not exceed limits.
limit_y_bottom = 80
limit_y_top = 180
limit_y_level = 140
limit_x_left = 60
limit_x_right = 240
#load a cascade file for detecting faces
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
#saving the picture to an in-program stream rather than a file
stream = io.BytesIO()
#to speed things up, lower the resolution of the camera
CAMERA_WIDTH = 320
CAMERA_HEIGHT = 240
cam_cx = 160
cam_cy = 120
pan_cx = pan_x_c
pan_cy = pan_y_c
face_cx = cam_cx
face_cy = cam_cy
cx_ratio = limit_x_right/cam_cx
cy_ratio = limit_y_top/cam_cy
def pan_goto(x,y):
p.do_pan (int(x))
p.do_tilt (int(y))
with picamera.PiCamera() as camera:
camera.resolution = (CAMERA_WIDTH, CAMERA_HEIGHT)
camera.vflip = True
time.sleep(2)
# put camera in a known good position.
pan_goto(pan_cx, pan_cy)
while(True):
with picamera.array.PiRGBArray(camera) as stream:
camera.capture(stream, format='bgr')
# At this point the image is available as stream.array
image = stream.array
# convert to grayscale, which is easier
gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
# look for faces over the given image using the loaded cascade file
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
for (x,y,w,h) in faces:
#opencv has built in image manipulation functions
cv2.rectangle(image,(x,y),(x+w,y+h),(255,0,0),2)
face_cx = x + w/2
Nav_LR = cam_cx - face_cx
pan_cx = pan_cx - Nav_LR /5
face_cy = y + h/2
Nav_UD = cam_cy - face_cy
pan_cy = pan_cy - Nav_UD /4
pan_goto(pan_cx, pan_cy)
print " Nav LR=%s UD=%s " % (Nav_LR, Nav_UD)
# use opencv built in window to show the image
# leave out if your Raspberry Pi isn't set up to display windows
cv2.imshow('Test Image',image)
if cv2.waitKey(1) & 0xFF == ord('q'):
# Close Window
cv2.destroyAllWindows()
break
Statistics: Posted by pageauc — Thu Aug 21, 2014 5:06 am
Statistics: Posted by Alan — Wed Aug 20, 2014 6:21 pm
Statistics: Posted by pageauc — Tue Aug 19, 2014 2:18 pm