# 人脸识别例程
#
# 这个例子展示了OpenMV Cam的内置人脸检测功能。
#
# 人脸检测通过在图像上使用Haar Cascade特征检测器来工作。 haar级联是
# 一系列简单的区域对比检查。 对于内置的前表面探测器,有25个阶段的检查,
# 每个阶段有数百个检查一块。 Haar Cascades运行速度很快,因为只有在
# 以前的阶段过去后才会评估后期阶段。 此外,您的OpenMV使用称为
# 整体图像的数据结构来在恒定时间内快速执行每个区域对比度检查
#(特征检测仅为灰度的原因是因为整体图像的空间需求)。
import sensor, time, image
# 重置感光元件
sensor.reset()
# 感光元件设置
sensor.set_contrast(3)
sensor.set_gainceiling(16)
# HQVGA and GRAYSCALE are the best for face tracking.
# HQVGA和灰度对于人脸识别效果最好
sensor.set_framesize(sensor.HQVGA)
sensor.set_pixformat(sensor.GRAYSCALE)
#注意人脸识别只能用灰度图哦
# 加载Haar算子
# 默认情况下,这将使用所有阶段,更低的satges更快,但不太准确。
face_cascade = image.HaarCascade("frontalface", stages=25)
#image.HaarCascade(path, stages=Auto)加载一个haar模型。haar模型是二进制文件,
#这个模型如果是自定义的,则引号内为模型文件的路径;也可以使用内置的haar模型,
#比如“frontalface” 人脸模型或者“eye”人眼模型。
#stages值未传入时使用默认的stages。stages值设置的小一些可以加速匹配,但会降低准确率。
print(face_cascade)
# FPS clock
clock = time.clock()
while (True):
clock.tick()
# 拍摄一张照片
img = sensor.snapshot()
# Find objects.
# 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.75, scale=1.35)
#image.find_features(cascade, threshold=0.5, scale=1.5),thresholds越大,
#匹配速度越快,错误率也会上升。scale可以缩放被匹配特征的大小。
#在找到的目标上画框,标记出来
for r in objects:
img.draw_rectangle(r)
# 打印FPS。
# 注:实际FPS更高,流FB使它更慢。
print(clock.fps())
利用内置的haar模型来识别人脸,调用find.feature返回找到的人脸的roi,下面直接框出来就识别到了人脸了。
分别不同的人脸
# 用LBP特征进行人脸识别。
# 见 Timo Ahonen's "Face Recognition with Local Binary Patterns".
#
# 运行示例之前:
# 1) 下载AT&T faces数据库http://www.cl.cam.ac.uk/Research/DTG/attarchive/pub/data/att_faces.zip
# 2) 提取并复制orl_faces目录到SD卡根目录。
#
# NOTE: This is just a PoC implementation of the paper mentioned above, it does Not work well in real life conditions.
# 注意:这只是上面提到的论文的一个PoC实现,在现实生活中并不适用。
import sensor, time, image
SUB = "s2"
NUM_SUBJECTS = 5
NUM_SUBJECTS_IMGS = 10
img = image.Image("orl_faces/%s/1.pgm"%(SUB))
d0 = img.find_lbp((0, 0, img.width(), img.height()))
img = None
print("")
for s in range(1, NUM_SUBJECTS+1):
dist = 0
for i in range(2, NUM_SUBJECTS_IMGS+1):
img = image.Image("orl_faces/s%d/%d.pgm"%(s, i))
d1 = img.find_lbp((0, 0, img.width(), img.height()))
dist += image.match_descriptor(d0, d1)
print("Average dist for subject %d: %d"%(s, dist/NUM_SUBJECTS_IMGS))
先存不同人脸的照片信息,然后用用find.lbp这函数寻找图片的特征点,返回会特征点。
然后用match.descriptor函数来比较两个特征点的不同,返回会差异度,然后找出差异度最小的那一个就是当前拍摄的人脸,传入的是lbp特征点。如果传入的
find_keypoints
的特征点话,会传入一个kptmatch,和blob差不多的一个东西。