Tracking using "Face detect" filter in OpenCV

UPDATED (PID added and WORKS :D)

from java.lang import String
from java.lang import Class
from java.awt import Rectangle
from org.myrobotlab.service import Runtime
from org.myrobotlab.service import OpenCV
from org.myrobotlab.opencv import OpenCVData
from com.googlecode.javacv.cpp.opencv_core import CvPoint;
from org.myrobotlab.service import OpenCV
xpid = Runtime.createAndStart("xpid","PID")
ypid = Runtime.createAndStart("ypid","PID")
xpid.setOutputRange(-10, 10)
xpid.setPID(10.0, 5.0, 1.0)
xpid.setSetpoint(160) # we want the target in the middle of the x
ypid.setOutputRange(-10, 10)
ypid.setPID(10.0, 5.0, 1.0)
# create or get a handle to an OpenCV service
opencv = Runtime.create("opencv","OpenCV")
# reduce the size - face tracking doesn't need much detail
# the smaller the faster
opencv.addFilter("PyramidDown1", "PyramidDown")
# add the face detect filter
opencv.addFilter("FaceDetect1", "FaceDetect")
def input():
    #print 'found face at (x,y) ',[0].x(),[0].y()
    opencvData =[0]
    global x
    global y
    global sposx
    global sposy
    global posx
    global posy
    global servox
    global servoy
    if (opencvData.getBoundingBoxArray().size() > 0) :
     rect = opencvData.getBoundingBoxArray().get(0)
     posx = rect.x
     posy = rect.y
     w = rect.width
     h = rect.height
     sposx = (w/2)
     sposy = (h/2)
     x = (posx + sposx)
     y = (posy + sposy)
     print x
     print y
     servox = xpid.getOutput()
     print 'x servo' , servox
     servoy = ypid.getOutput()
     print 'y servo' , servoy
     return object
# create a message route from opencv to python so we can see the coordinate locations
opencv.addListener("publishOpenCVData",, "input", OpenCVData().getClass()); 
# set the input source to the first camera


Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
GroG's picture

Hi Alessandruino - This

Hi Alessandruino -

This line 

h = rect.hight 

needs to change to 

h = rect.height

Alessandruino's picture it it works... thanks :D