>>> 星瞳科技|OpenMV中国官方网站 <<<

>>> 星瞳科技店铺地址|OpenMV中国官方代理,全球九大代理商之一 <<<

>>> OpenMV3 Cam M7 官方高配版上市啦~戳我戳我戳我~ <<<

>>> 星瞳科技-OpenMV中文教程网 <<<

本例程为 08-Eye_Tracking-iris_detection.py
本例程先通过人眼识别找到人眼,然后利用find_eye函数找到瞳孔。find_eye的原理是寻找人眼中颜色最深的地方,即瞳孔。

# Iris Detection 2 Example
#
# This example shows how to find the eye gaze (pupil detection) after finding
# the eyes in an image. This script uses the find_eyes function which determines
# the center point of roi that should contain a pupil. It does this by basically
# finding the center of the darkest area in the eye roi which is the pupil center.
#
# Note: This script does not detect a face first, use it with the telephoto lens.

import sensor, time, image

# Reset sensor
sensor.reset()

# Sensor settings
sensor.set_contrast(3)
sensor.set_gainceiling(16)

# Set resolution to VGA.
sensor.set_framesize(sensor.VGA)

#拉近镜头,使眼睛的更多细节展现在摄像头中。
# Bin/Crop image to 200x100, which gives more details with less data to process
sensor.set_windowing((220, 190, 200, 100))

sensor.set_pixformat(sensor.GRAYSCALE)

# Load Haar Cascade
# By default this will use all stages, lower satges is faster but less accurate.
#加载眼睛的haar算子
eyes_cascade = image.HaarCascade("eye", stages=24)
print(eyes_cascade)

# FPS clock
clock = time.clock()

while (True):
    clock.tick()
    # Capture snapshot
    img = sensor.snapshot()
    # Find eyes !
    # Note: Lower scale factor scales-down the image more and detects smaller objects.
    # Higher threshold results in a higher detection rate, with more false positives.
    eyes = img.find_features(eyes_cascade, threshold=0.5, scale=1.5)
    #先利用find_features函数识别人眼。image.find_features(cascade, threshold=0.5, scale=1.5),thresholds越大,匹配速度越快,错误率也会上升。scale可以缩放被匹配特征的大小。

    # Find iris
    #在识别到的人眼中寻找瞳孔。
    for e in eyes:
        iris = img.find_eye(e)
        #image.find_eye((x, y, w, h)),find_eye的参数是一个矩形区域,左上顶点为
        #(x,y),宽w,高h,注意(x,y,w,h)是一个元组,不要漏掉括号()。上行代码中
        #的e即代表识别到的眼睛的矩形区域。
        #find_eye的原理是找到区域中颜色最深处的中心。
        img.draw_rectangle(e)
        img.draw_cross(iris[0], iris[1])
        #用矩形标记人眼,用十字形标记瞳孔。

    # Print FPS.
    # Note: Actual FPS is higher, streaming the FB makes it slower.
    print(clock.fps())

运行程序效果如图: