openmv/usr/examples/face_tracking.py
2015-07-09 04:38:07 +02:00

49 lines
1.2 KiB
Python

import sensor, time
# Reset sensor
sensor.reset()
# Sensor settings
sensor.set_contrast(1)
sensor.set_gainceiling(16)
sensor.set_framesize(sensor.QQVGA)
sensor.set_pixformat(sensor.GRAYSCALE)
def find_face():
global sensor, time
# Load Haar Cascade
face_cascade = HaarCascade("frontalface")
while (True):
image = sensor.snapshot()
objects = image.find_features(face_cascade, threshold=0.65, scale=1.85)
if objects:
print (objects[0])
image.draw_rectangle(objects[0])
try:
kpts1 = image.find_keypoints(threshold=32, normalized=False, roi=objects[0])
except:
continue
if kpts1:
image.draw_keypoints(kpts1)
time.sleep(1000)
return kpts1
kpts1 = find_face()
clock = time.clock()
while (True):
clock.tick()
image = sensor.snapshot()
try:
kpts2 = image.find_keypoints(threshold=32)
except:
continue
if (kpts2==None):
continue
c=image.match_keypoints(kpts1, kpts2, 70)
if (c):
l=10
image.draw_line((c[0]-l, c[1], c[0]+l, c[1]))
image.draw_line((c[0], c[1]-l, c[0], c[1]+l))
time.sleep(10)
print (clock.fps())