Python+Opencv 摄像头视频流人脸视频检测追踪

1、利用Opencv自带的haarcascade_frontalface_default.xml 人脸分类器做视频流的人脸识别

2、利用cv2.VideoCapture获取视频流

3、导入Opencv分类器 haarcascade_frontalface_default 是好是绝对路径

4、视频流的每一帧做人脸识别

5、识别到的人脸保存到本地

6、附具体代码

# -*- coding: utf-8 -*-
"""
Created on Wed Jun 13 12:24:53 2018

@author: Administrator 在窗口显示摄像头帧
"""

import cv2

clicked = False
# CAMERA_CAPTURE = 'rtsp://admin:[email protected]:554/h264/ch1/main/av_stream'
CAMERA_CAPTURE = 'rtsp://admin:[email protected]/mpeg4/ch1/sub/av_stream'
def onMouse(event, x, y, flags, param):
    global clicked
    if event == cv2.EVENT_LBUTTONUP:
        clicked = True
        
cameraCapture = cv2.VideoCapture(CAMERA_CAPTURE)

cv2.namedWindow('MyWindow')
cv2.setMouseCallback('MyWindow', onMouse)

print('showing camear feed. Click window or press any key to stop')

success, frame = cameraCapture.read()

pathf = 'D:\\ProgramData\\Anaconda3\\Lib\\site-packages\\cv2\\data\\haarcascade_frontalface_default.xml'
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
face_cascade.load(pathf)
count = 0
while success and cv2.waitKey(1) == -1 and not clicked:
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    faces = face_cascade.detectMultiScale(gray, 1.2, 5)
    for (x, y , w, h) in faces:
        frame = cv2.rectangle(frame, (x, y), (x+w, y+h),(255,0,0),2)
        f = cv2.resize(gray[y:y+h, x:x+w], (200,200))
        cv2.imwrite('./jm/%s.jpg' % str(count) , f)
        count +=1 
    cv2.imshow('MyWindow', frame)
    success, frame = cameraCapture.read()
    
cv2.destroyWindow('MyWindow') 
cameraCapture.release()


 
 

你可能感兴趣的:(人脸识别)