简介

上了一门计算机视觉的课程,初步了解, dlib python库

参考链接

http://dlib.net/face_detector.py.html 人脸检测的链接

原理

This face detector is made using the now classic Histogram of Oriented
Gradients (HOG) feature combined with a linear classifier, an image
pyramid, and sliding window detection scheme. This type of object detector
is fairly general and capable of detecting many types of semi-rigid objects
in addition to human faces. Therefore, if you are interested in making
your own object detectors then read the train_object_detector.py example program.

谷歌翻译

该面部检测器是使用现在经典的定向直方图(HOG)功能与线性分类器,图像金字塔和滑动窗口检测方案组合而成的。
这种类型的物体检测器相当通用,并且能够检测除人脸之外的许多类型的半刚性物体。
因此,如果您有兴趣制作自己的对象检测器,请阅读train_object_detector.py示例程序。
如果没有猜错应该使用了传统的方法??

想得到分数

# Finally, if you really want to you can ask the detector to tell you the score
# for each detection.  The score is bigger for more confident detections.
# The third argument to run is an optional adjustment to the detection threshold,
# where a negative value will return more detections and a positive value fewer.
# Also, the idx tells you which of the face sub-detectors matched.  This can be
# used to broadly identify faces in different orientations.

code

# coding=utf-8
import dlib # 人脸算法库
from imageio import imread
import glob # 操作文件的相关模块

# 准备好人脸检测器和显示窗口,获取图片路径
detector = dlib.get_frontal_face_detector()
win = dlib.image_window()
paths=glob.glob('faces/*.jpg')
# 对每一张图片进行检测,并显示检测结果对应的矩形框
for path in paths:
    img = imread(path)
    # 1 表示将图片放大一杯,便于检测更过人脸
    dets = detector(img, 1)
    print('检测到了 %d 个人脸' % len(dets))
    for i,d in enumerate(dets): 
        # 函数用于将一个可遍历的数据对象(如列表、元组或字符串)
        # 组合为一个索引序列,同时列出数据和数据下标,一般用在 for 循环当中。
        print("Detection {}: Left: {} Top: {} Right: {} Bottom: {}".format(i, d.left(), d.top(), d.right(), d.bottom())) # dets 是已经检测到的人脸
    win.clear_overlay() # 清除覆盖
    win.set_image(img)
    win.add_overlay(dets)
    dlib.hit_enter_to_continue()
    dets, scores, idx = detector.run(img, 1, -1) # 其中1 表示放大 -1 表示 阈值 如果为正数那么相关的低于阈值的信息就会被筛选掉
    for i, d in enumerate(dets):
        print("Detection {}, score: {}, face_type:{}".format(d, scores[i], idx[i]))

结果

检测到了 1 个人脸
Detection 0: Left: 206 Top: 428 Right: 872 Bottom: 1094
Hit enter to continue
Detection [(206, 428) (872, 1094)], score: 2.9385107197297926, face_type:0
Detection [(114, 744) (176, 806)], score: -0.7111557926037881, face_type:2
Detection [(170, 847) (213, 890)], score: -0.8928528437771357, face_type:3
Detection [(121, 793) (196, 868)], score: -0.9795470083884772, face_type:1

posted on 2020-06-18 10:26  HDU李少帅  阅读(237)  评论(0编辑  收藏  举报