dlib.get_frontal_face_detector(), Python format 格式化函数 predictor(img, dets[0])

detector = dlib.get_frontal_face_detector()
# 人脸检测
    dets = detector(img, 1)

    # len(dets) 即为检测到的人脸个数
    print("Number of faces detected: {}".format(len(dets)))

 

Python2.6 开始,新增了一种格式化字符串的函数 str.format(),它增强了字符串格式化的功能。

基本语法是通过 {} 和 : 来代替以前的 % 。

format 函数可以接受不限个参数,位置可以不按顺序。

实例

>>>"{} {}".format("hello", "world") # 不设置指定位置,按默认顺序 'hello world' >>> "{0} {1}".format("hello", "world") # 设置指定位置 'hello world' >>> "{1} {0} {1}".format("hello", "world") # 设置指定位置 'world hello world'

参考:https://www.runoob.com/python/att-string-format.html

 

face_landmark_detection.py, 利用训练好的 shape_predictor("shape_predictor_68_face_landmarks.dat") / 人脸 68 点特征检测器,进行人脸面部轮廓特征提取: 

predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")
shape = predictor(img, dets[0])

dets[0]是图像中识别的第一个人脸在图像中所在的位置,得到shape和,在和img可以得到人脸的128D特征值

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
抱歉,这段代码并没有加入目标跟踪算法。如果要加入目标跟踪算法,可以使用dlib.correlation_tracker()函数进行跟踪。这个函数可以帮助我们跟踪在第一帧中检测到的人脸特征点。 具体步骤如下: 在每秒中的第一帧图像中,使用dlib.get_frontal_face_detector()函数进行人脸检测,然后使用dlib.shape_predictor()函数检测人脸特征点。将这些特征点存储在一个列表中,并将列表传递给dlib.correlation_tracker()函数,以便跟踪这些特征点。 对于后续的每一帧图像,我们可以使用cv2.calcOpticalFlowFarneback()函数计算特征点的运动,然后使用dlib.correlation_tracker()函数进行跟踪。 具体实现可以参考以下代码: ``` import sys import dlib import cv2 predictor_path = "shape_predictor_194_face_landmarks.dat" detector = dlib.get_frontal_face_detector() predictor = dlib.shape_predictor(predictor_path) cam = cv2.VideoCapture(0) cam.set(3, 1280) cam.set(4, 720) color_white = (255, 255, 255) line_width = 3 # 初始化跟踪器 tracker = None tracking_pts = [] while True: ret_val, img = cam.read() rgb_image = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) dets = detector(rgb_image) if len(dets) > 0: # 检测到人脸,获取特征点并初始化跟踪器 det = dets[0] shape = predictor(rgb_image, det) tracking_pts = [(p.x, p.y) for p in shape.parts()] tracker = dlib.correlation_tracker() rect = dlib.rectangle(det.left(), det.top(), det.right(), det.bottom()) tracker.start_track(rgb_image, rect) elif tracker is not None: # 没有检测到人脸,继续跟踪特征点 tracker.update(rgb_image) pos = tracker.get_position() rect = dlib.rectangle(int(pos.left()), int(pos.top()), int(pos.right()), int(pos.bottom())) shape = predictor(rgb_image, rect) tracking_pts = [(p.x, p.y) for p in shape.parts()] for pt in tracking_pts: cv2.circle(img, pt, 2, (0, 255, 0), -1) cv2.imshow('my webcam', img) if cv2.waitKey(1) == 27: break cv2.destroyAllWindows() ``` 这段代码使用dlib.correlation_tracker()函数对人脸特征点进行跟踪,如果没有检测到人脸,则继续跟踪前一帧中检测到的特征点。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值