Face Tracking with Pan/Tilt Servos

Hi!

I am currently working on a way to have OpenMV track a face, and follow it using a pan/tilt servo kit. So far, I have tried to have it follow the crosses drawn by the script onto the face,
but the servo just moves back and forth, and cannot actually follow it.

import sensor, time, image
from pyb import Servo

# Reset sensor
sensor.reset()

# Sensor settings
sensor.set_contrast(1)
sensor.set_gainceiling(16)
sensor.set_framesize(sensor.QQVGA)
sensor.set_pixformat(sensor.GRAYSCALE)
sensor.__write_reg(0x0C, sensor.__read_reg(0x0C) | (1 << 7)) #Flips Camera
s1 = Servo(1) # servo on position 1 (PB15, VIN, GND)


# Load Haar Cascade
# By default this will use all stages, lower satges is faster but less accurate.
face_cascade = image.HaarCascade("frontalface", stages=16)
print(face_cascade)

def find_face():
    for i in range(0, 30):
        img = sensor.snapshot()
    while (True):
        img = sensor.snapshot()
        objects = img.find_features(face_cascade, threshold=0.65, scale=1.65)
        for r in objects:
            img.draw_rectangle(r)
            try:
                kpts1 = img.find_keypoints(threshold=32, normalized=False, roi=objects[0])
            except:
                continue
            if kpts1:
                img.draw_keypoints(kpts1)
                time.sleep(100)
                return kpts1

kpts1 = find_face()

clock = time.clock()
while (True):
    clock.tick()
    img = sensor.snapshot()
    try:
        kpts2 = img.find_keypoints(threshold=32, normalized=False)
    except:
        continue

    if (kpts2==None):
        continue

    c=img.match_keypoints(kpts1, kpts2, 70)
    if (c):
        l=10
        img.draw_line((c[0]-l,  c[1],  c[0]+l, c[1]))
        img.draw_line((c[0],  c[1]-l,  c[0], c[1]+l))
        s1.angle(c[0], 500)
        time.sleep(20)
    print (clock.fps())

Does anyone have any suggestions as to how to have OpenMV relay face marker position to servo position? Thanks in advance! :slight_smile:

I would test servos separately, in a small script, if they work as expected then move to face tracking…

I noticed you have an old script, you should use the new face tracking example from the IDE’s menu: Examples->07-Face-Detection->face_tracking.py

Or copy it from the repo:
https://github.com/openmv/openmv/blob/master/usr/examples/07-Face-Detection/face_tracking.py

If you have a problem running this script you should update your IDE/FW.

Thanks for the new example link. After updating my firmware and the new IDE, I got the following error when running the example:

>>> width:24 height:24 n_stages:25 n_features:2913 n_rectangles:6383



size:22 threshold:32 normalized:0



Traceback (most recent call last):

  File "<stdin>", line 56, in <module>

TypeError: function does not take keyword arguments

Micro Python v1.4.4-128-g0941f86-dirty on 2016-01-28; OPENMV2 with STM32F427

Type "help()" for more information.

>>>

It’s working on my cam, I can only assume you’re still on an older FW, anyway you can just comment this line:

img.draw_keypoints(kpts1, size=12)