Python--基于OpenCV数据集的人脸定位和识别

就是调个库,没什么好说的。上代码:

事前准备:

  • python安装两个库。
pip install opencv-python
pip install opencv-contrib-python

到cv2文件夹下取出三个文件,复制到工作区:

  • haarcascade_frontalcatface.xml
  • haarcascade_frontalcatface_extended.xml
  • haarcascade_frontalface_default.xml

人脸定位:

import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt

def face_detect_demo():
    gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
    face_detector = cv.CascadeClassifier('D:/anaconda3/Lib/site-packages/cv2/data/haarcascade_frontalface_alt_tree.xml')
    faces = face_detector.detectMultiScale(gray, scaleFactor=1.008, minNeighbors=4, maxSize=(47, 47), minSize=(28 ,28))
    for x, y, w, h in faces:
        cv.rectangle(img, (x, y), (x+w, y+h), color=(0, 0, 255), thickness=2)
        cv.circle(img, center=(x+w//2, y+h//2), radius=w//2, color=(0, 255, 0), thickness=2)
    
    cv.imshow('result', img)

img = cv.imread('test05.jpg')

face_detect_demo()
cv.waitKey(4000)
cv.destroyAllWindows()

视频人脸定位:

import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt

def face_detect_demo(img):
    gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
    face_detector = cv.CascadeClassifier('../trainer/haarcascade_frontalface_default.xml')
    faces = face_detector.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=4, minSize=(30 ,30))
    for x, y, w, h in faces:
        cv.rectangle(img, (x, y), (x+w, y+h), color=(0, 0, 255), thickness=2)
        cv.circle(img, center=(x+w//2, y+h//2), radius=w//2, color=(0, 255, 0), thickness=2)
    
    cv.imshow('result', img)

    
cap = cv.VideoCapture('../src/test06_Trim2.mp4')

#save files
fps = 5
size = (int (cap.get(cv.CAP_PROP_FRAME_WIDTH)), int (cap.get(cv.CAP_PROP_FRAME_HEIGHT)))
videowrite = cv.VideoWriter('../result/result_1.mp4', cv.VideoWriter_fourcc('I', '4', '2', '0'), fps, size)

while True:
    flag, frame = cap.read()

    if not flag:
        break

    face_detect_demo(frame)
    videowrite.write(frame)

    if ord('q') == cv.waitKey(10):
        break

cv.destroyAllWindows()
cv.release()

效果:

 

Python--基于OpenCV数据集的人脸定位和识别_第1张图片

打开摄像头,摄像头定位:

import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt

def face_detect_demo(img):
    gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
    face_detector = cv.CascadeClassifier('./data/haarcascade_frontalface_default.xml')
    faces = face_detector.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=4, minSize=(30 ,30))
    for x, y, w, h in faces:
        cv.rectangle(img, (x, y), (x+w, y+h), color=(0, 0, 255), thickness=2)
        cv.circle(img, center=(x+w//2, y+h//2), radius=w//2, color=(0, 255, 0), thickness=2)
    
    cv.imshow('result', img)

    
#cap = cv.VideoCapture('../src/test06_Trim2.mp4')
cap = cv.VideoCapture(0)
#save files
fps = 5
size = (int (cap.get(cv.CAP_PROP_FRAME_WIDTH)), int (cap.get(cv.CAP_PROP_FRAME_HEIGHT)))
videowrite = cv.VideoWriter('../result/result_1.mp4', cv.VideoWriter_fourcc('I', '4', '2', '0'), fps, size)

while True:
    flag, frame = cap.read()

    if not flag:
        break

    face_detect_demo(frame)
    videowrite.write(frame)

    if ord('q') == cv.waitKey(10):
        break

cv.destroyAllWindows()
cv.release()

 

你可能感兴趣的:(Python)