Python基于OpenCV实现视频的人脸检测

本文实例为大家分享了基于OpenCV实现视频的人脸检测具体代码,供大家参考,具体内容如下

前提条件

1.摄像头

2.已安装Python和OpenCV3

代码

import cv2

import sys

import logging as log

import datetime as dt

from time import sleep

cascPath = "haarcascade_frontalface_default.xml"

faceCascade = cv2.CascadeClassifier(cascPath)

# 打开视频捕获设备

video_capture = cv2.VideoCapture(0)

while True:

if not video_capture.isOpened():

print('Unable to load camera.')

sleep(5)

pass

# 读视频帧

ret, frame = video_capture.read()

# 转为灰度图像

gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

# 调用分类器进行检测

faces = faceCascade.detectMultiScale(

gray,

scaleFactor=1.1,

minNeighbors=5,

minSize=(30, 30),

#flags=cv2.cv.CV_HAAR_SCALE_IMAGE

)

# 画矩形框

for (x, y, w, h) in faces:

cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)

# 显示视频

cv2.imshow('Video', frame)

if cv2.waitKey(1) & 0xFF == ord('q'):

break

# 关闭摄像头设备

video_capture.release()

# 关闭所有窗口

cv2.destroyAllWindows()

以上是 Python基于OpenCV实现视频的人脸检测 的全部内容, 来源链接: utcz.com/z/313450.html

回到顶部