>>> 星瞳科技|OpenMV中国官方网站 <<<
>>> 星瞳科技店铺地址|OpenMV中国官方代理,全球九大代理商之一 <<<
>>> OpenMV3 Cam M7 官方高配版上市啦~戳我戳我戳我~ <<<
>>> 星瞳科技-OpenMV中文教程网 <<<
本例程为 08-Eye_Tracking-face_eye_detection
本例程先用haar算子进行人脸识别,然后利用haar算子找到人脸中的眼睛,实现人眼追踪。
# Face Eye Detection Example # # This script uses the built-in frontalface detector to find a face and then # the eyes within the face. If you want to determine the eye gaze please see the # iris_detection script for an example on how to do that. import sensor, time, image # Reset sensor sensor.reset() # Sensor settings sensor.set_contrast(1) sensor.set_gainceiling(16) sensor.set_framesize(sensor.HQVGA) sensor.set_pixformat(sensor.GRAYSCALE) # Load Haar Cascade # By default this will use all stages, lower satges is faster but less accurate. face_cascade = image.HaarCascade("frontalface", stages=25)#人脸识别的haar算子 eyes_cascade = image.HaarCascade("eye", stages=24)#眼睛的haar算子 #image.HaarCascade(path, stages=Auto)加载一个haar模型。haar模型是二进制文件, #这个模型如果是自定义的,则引号内为模型文件的路径;也可以使用内置的haar模型, #比如“frontalface” 人脸模型或者“eye”人眼模型。 #stages值未传入时使用默认的stages。stages值设置的小一些可以加速匹配,但会降低准确率。 print(face_cascade, eyes_cascade) # FPS clock clock = time.clock() while (True): clock.tick() # Capture snapshot img = sensor.snapshot() # Find a face ! # 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. objects = img.find_features(face_cascade, threshold=0.5, scale=1.5) #先利用haar算子找到视野中的人脸。image.find_features(cascade, threshold=0.5, scale=1.5),thresholds越大,匹配速度越快,错误率也会上升。scale可以缩放被匹配特征的大小。 # Draw faces #将找到的人脸用矩形标记出来 for face in objects: img.draw_rectangle(face) # Now find eyes within each face. # Note: Use a higher threshold here (more detections) and lower scale (to find small objects) eyes = img.find_features(eyes_cascade, threshold=0.5, scale=1.2, roi=face) #在人脸中识别眼睛。roi参数设置特征寻找的范围,roi=face即在找到的人脸中识别眼睛。 #将找到的眼睛标记出来 for e in eyes: img.draw_rectangle(e) # Print FPS. # Note: Actual FPS is higher, streaming the FB makes it slower. print(clock.fps())
运行程序效果如图: