编写人脸检测程序

新建一个py文件,命名为facedetectionwithdlib.py。添加如下代码:

【代码 facedetectionwithdlib.py】

# -*- coding: utf-8 -*-
'''
使用dlib实现人脸检测
'''

import face_recognition
import cv2
import time

# 超参数
detection_method = 'hog' # 参数值为hog/cnn。表示人脸检测使用hog提取特征还是使用cnn提取特征。

# video_path = 'test.mp4'
video_path = ''

# 初始化摄像头
if video_path:
    cap = cv2.VideoCapture(video_path)
else:
    cap = cv2.VideoCapture(0)

cap.set(0,640) # 视频宽度
cap.set(1,480) # 视频高度
time.sleep(2)


while True:# 拍100张图片就结束
    ret, img = cap.read()
    # 人脸检测不依赖色彩,所以先把人脸图像转成灰度图像
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 
    
    face_locations = face_recognition.face_locations(
                     gray, number_of_times_to_upsample=1, 
                     model = detection_method)
    # 用矩形框框出人脸位置
    for (top, right, bottom, left) in face_locations:
        cv2.rectangle(img, (left, top), (right, bottom), 
                      (0, 0, 255), 2)
        cv2.rectangle(gray, (left, top), (right, bottom), 
                      (0, 0, 255), 2)
    
    cv2.imshow('original image', img)
    cv2.imshow('gray image', gray)
    
    # 按 'ESC' 键终止
    k = cv2.waitKey(100) & 0xff 
    if k == 27:
        break
 
cap.release()
cv2.destroyAllWindows()

 

相关推荐

  1. 编写人脸检测程序

    2024-03-23 22:30:03       17 阅读
  2. 程序人脸识别—检测人脸图片获取图片

    2024-03-23 22:30:03       26 阅读
  3. 人脸检测算法

    2024-03-23 22:30:03       31 阅读
  4. python 人脸检测人脸识别

    2024-03-23 22:30:03       27 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-03-23 22:30:03       14 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-03-23 22:30:03       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-03-23 22:30:03       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-03-23 22:30:03       18 阅读

热门阅读

  1. Vue3:编程式路由导航

    2024-03-23 22:30:03       21 阅读
  2. MySQL系统参数配置实战:生产环境优化

    2024-03-23 22:30:03       19 阅读
  3. Android设计规范及分辨率简介

    2024-03-23 22:30:03       20 阅读
  4. c++学习笔记(9)

    2024-03-23 22:30:03       14 阅读
  5. Cisco Catalyst3850交换机RTU license使用方法

    2024-03-23 22:30:03       19 阅读