Python--Face location and recognition based on OpenCV dataset

Just adjust a library, there is nothing to say. Above code:

a:

  • Python installs two libraries.
pip install opencv-python
pip install opencv-contrib-python

Go to the cv2 folder and take out three files and copy them to the workspace:

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

Face positioning:

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()

Video face positioning:

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()

effect:

 

Turn on the camera, camera positioning:

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()

 

Guess you like

Origin blog.csdn.net/u013362192/article/details/107128880