# Help with Pi Pan/Tilt Face tracker

**URL:** <https://forums.pimoroni.com/t/help-with-pi-pan-tilt-face-tracker/2333>\
**Category:** Support\
**Created:** [May 5, 2016, 6:51pm UTC](https://forums.pimoroni.com/t/help-with-pi-pan-tilt-face-tracker/2333 "2016-05-05T18:51:55Z")\
**Posts on this page:** 9\
**Page:** 2

<div class="post-metadata">

**Author:** ![RogueM](https://sea2.discourse-cdn.com/flex016/user_avatar/forums.pimoroni.com/roguem/32/311_2.png) [@RogueM](https://forums.pimoroni.com/u/RogueM)\
**Post date:** [May 10, 2016, 9:19am UTC](https://forums.pimoroni.com/t/help-with-pi-pan-tilt-face-tracker/2333/21 "2016-05-10T09:19:51Z")

</div>

I can imagine it’s becoming quite frustrating for you. I do have to repeat my advice to try to get something more basic working first though, such as the [simpletest.py](http://simpletest.py).

Once you’ve troubleshooted your servos then hopefully facetracker will also work, but until then it’s adding a layer of complexity that makes it hard to tell where the problem lies.

---

<div class="post-metadata">

**Author:** ![SPlatten](https://sea2.discourse-cdn.com/flex016/user_avatar/forums.pimoroni.com/splatten/32/1031_2.png) [@SPlatten](https://forums.pimoroni.com/u/SPlatten)\
**Post date:** [May 10, 2016, 1:05pm UTC](https://forums.pimoroni.com/t/help-with-pi-pan-tilt-face-tracker/2333/22 "2016-05-10T13:05:46Z")

</div>

Thank you, I’ll give it a go tonight.

---

<div class="post-metadata">

**Author:** ![SPlatten](https://sea2.discourse-cdn.com/flex016/user_avatar/forums.pimoroni.com/splatten/32/1031_2.png) [@SPlatten](https://forums.pimoroni.com/u/SPlatten)\
**Post date:** [May 10, 2016, 5:46pm UTC](https://forums.pimoroni.com/t/help-with-pi-pan-tilt-face-tracker/2333/23 "2016-05-10T17:46:40Z")

</div>

I’ve installed the Adafruit\_Python\_PCA9685 package and run:

```
    sudo python simpletest.py

```

The camera mount server rotates left and right continuously until I press Ctrl-C to quit.

Now what?

---

<div class="post-metadata">

**Author:** ![SPlatten](https://sea2.discourse-cdn.com/flex016/user_avatar/forums.pimoroni.com/splatten/32/1031_2.png) [@SPlatten](https://forums.pimoroni.com/u/SPlatten)\
**Post date:** [May 11, 2016, 7:11am UTC](https://forums.pimoroni.com/t/help-with-pi-pan-tilt-face-tracker/2333/24 "2016-05-11T07:11:03Z")

</div>

Could the camera be the issue? I have one of these:

[Waveshare Raspberry Pi Camera](https://www.amazon.co.uk/Waveshare-Raspberry-Camera-Version-Raspberry-pi/dp/B00N9YWI1I/ref=sr_1_2?ie=UTF8&qid=1462950607&sr=8-2&keywords=pi+camera)

---

<div class="post-metadata">

**Author:** ![RogueM](https://sea2.discourse-cdn.com/flex016/user_avatar/forums.pimoroni.com/roguem/32/311_2.png) [@RogueM](https://forums.pimoroni.com/u/RogueM)\
**Post date:** [May 11, 2016, 1:13pm UTC](https://forums.pimoroni.com/t/help-with-pi-pan-tilt-face-tracker/2333/25 "2016-05-11T13:13:53Z")

</div>

It’s a possibility, though nothing in the Waveshare doc seems to indicate it wouldn’t work.

You should set some debug functions in facetracker and see if the various tracking variables change. Depending on the answer it may give you some leads as to whether hardware or software is the culprit.

---

<div class="post-metadata">

**Author:** ![SPlatten](https://sea2.discourse-cdn.com/flex016/user_avatar/forums.pimoroni.com/splatten/32/1031_2.png) [@SPlatten](https://forums.pimoroni.com/u/SPlatten)\
**Post date:** [May 16, 2016, 8:36am UTC](https://forums.pimoroni.com/t/help-with-pi-pan-tilt-face-tracker/2333/26 "2016-05-16T08:36:28Z")

</div>

I haven’t done anything with Python before, I’ve recently read a book on it, so will have a play with this and see what I can find out, I’ve checked the wiring and everything looks correct as per the information published online and photos. When I run a script to test the servo’s the arm rotates left to right, but nothing happens when I run the facetracker script, except a window showing the camera output but nothing else…

---

<div class="post-metadata">

**Author:** ![RogueM](https://sea2.discourse-cdn.com/flex016/user_avatar/forums.pimoroni.com/roguem/32/311_2.png) [@RogueM](https://forums.pimoroni.com/u/RogueM)\
**Post date:** [May 26, 2016, 6:42pm UTC](https://forums.pimoroni.com/t/help-with-pi-pan-tilt-face-tracker/2333/27 "2016-05-26T18:42:06Z")

</div>

Haha! I have some news for you. Looks like there was a dependency required that was missed out in the tutorial… when not present it annoyingly did not lead to any error and pretty much the situation you described!

So try the following:

```
sudo apt-get install python-smbus python-opencv opencv-data

```

… hopefully it will now start tracking your face. Let us know if that did the trick!

---

<div class="post-metadata">

**Author:** ![SPlatten](https://sea2.discourse-cdn.com/flex016/user_avatar/forums.pimoroni.com/splatten/32/1031_2.png) [@SPlatten](https://forums.pimoroni.com/u/SPlatten)\
**Post date:** [May 26, 2016, 6:58pm UTC](https://forums.pimoroni.com/t/help-with-pi-pan-tilt-face-tracker/2333/28 "2016-05-26T18:58:32Z")

</div>

> [@RogueM](#):
>
> l

Thank you, works great now.

---

<div class="post-metadata">

**Author:** ![stitch](https://avatars.discourse-cdn.com/v4/letter/s/c2a13f/32.png) [@stitch](https://forums.pimoroni.com/u/stitch)\
**Post date:** [September 16, 2018, 1:50pm UTC](https://forums.pimoroni.com/t/help-with-pi-pan-tilt-face-tracker/2333/29 "2018-09-16T13:50:44Z")

</div>

Does this program work with Pi Zero W? I have been able to make the Picamera work and the Pan tilt hat separately but not together yet. Also none of the face tracker programs work no matter how many updates, downloads of facetracker I download and run.  
The latest I tried was this one:  
#!/usr/bin/env python

import numpy as np  
import cv2.cv as cv  
import os  
from pantilthat import \*

os.system(‘sudo modprobe bcm2835-v4l2’)

cascade = cv.Load(’/usr/share/opencv/haarcascades/haarcascade\_frontalface\_default.xml’)  
#cascade = cv.Load(’/usr/share/opencv/lbpcascades/lbpcascade\_frontalface.xml’)

cam\_pan = 90  
cam\_tilt = 45

# Turn the camera to the default position

pan(cam\_pan-90)  
tilt(cam\_tilt-90)  
light\_mode(WS2812)

def lights(r,g,b,w):  
for x in range(18):  
set\_pixel\_rgbw(x,r if x in [3,4] else 0,g if x in [3,4] else 0,b,w if x in [0,1,6,7] else 0)  
show()

lights(0,0,0,50)

min\_size = (15, 15)  
image\_scale = 5  
haar\_scale = 1.2  
min\_neighbors = 2  
haar\_flags = cv.CV\_HAAR\_DO\_CANNY\_PRUNING

cap = cv.CreateCameraCapture(0)  
cv.NamedWindow(“Tracker”, 1)

if cap:  
frame\_copy = None

while(True):  
# Capture frame-by-frame  
frame = cv.QueryFrame(cap)  
if not frame:  
cv.WaitKey(0)  
break  
if not frame\_copy:  
frame\_copy = cv.CreateImage((frame.width,frame.height),  
cv.IPL\_DEPTH\_8U, frame.nChannels)  
if frame.origin == cv.IPL\_ORIGIN\_TL:  
cv.Flip(frame, frame, -1)

```
# Our operations on the frame come here
gray = cv.CreateImage((frame.width,frame.height), 8, 1)
small_img = cv.CreateImage((cv.Round(frame.width / image_scale),
               cv.Round (frame.height / image_scale)), 8, 1)

# convert color input image to grayscale
cv.CvtColor(frame, gray, cv.CV_BGR2GRAY)

# scale input image for faster processing
cv.Resize(gray, small_img, cv.CV_INTER_LINEAR)

cv.EqualizeHist(small_img, small_img)

midFace = None

if(cascade):
    t = cv.GetTickCount()
    # HaarDetectObjects takes 0.02s
    faces = cv.HaarDetectObjects(small_img, cascade, cv.CreateMemStorage(0),
                                 haar_scale, min_neighbors, haar_flags, min_size)
    t = cv.GetTickCount() - t
    if faces:
        lights(50 if len(faces) == 0 else 0, 50 if len(faces) > 0 else 0,0,50)

        for ((x, y, w, h), n) in faces:
            # the input to cv.HaarDetectObjects was resized, so scale the
            # bounding box of each face and convert it to two CvPoints
            pt1 = (int(x * image_scale), int(y * image_scale))
            pt2 = (int((x + w) * image_scale), int((y + h) * image_scale))
            cv.Rectangle(frame, pt1, pt2, cv.RGB(100, 220, 255), 1, 8, 0)
            # get the xy corner co-ords, calc the midFace location
            x1 = pt1[0]
            x2 = pt2[0]
            y1 = pt1[1]
            y2 = pt2[1]

            midFaceX = x1+((x2-x1)/2)
            midFaceY = y1+((y2-y1)/2)
            midFace = (midFaceX, midFaceY)

            offsetX = midFaceX / float(frame.width/2)
            offsetY = midFaceY / float(frame.height/2)
            offsetX -= 1
            offsetY -= 1

            cam_pan -= (offsetX * 5)
            cam_tilt += (offsetY * 5)
            cam_pan = max(0,min(180,cam_pan))
            cam_tilt = max(0,min(180,cam_tilt))

            print(offsetX, offsetY, midFace, cam_pan, cam_tilt, frame.width, frame.height)

            pan(int(cam_pan-90))
            tilt(int(cam_tilt-90))
            break
            
# Display the resulting frame
cv.ShowImage('Tracker',frame)
if cv.WaitKey(1) & 0xFF == ord('q'):
    break

```

# When everything done, release the capture

cv.DestroyWindow(“Tracker”)

[Previous page](https://forums.pimoroni.com/t/help-with-pi-pan-tilt-face-tracker/2333.md?page=1)
