Python and OpenCV video real-time face coverage

created at 07-31-2021 views: 5

problem

The video real-time face detection function has been made, but the face coverage is bad. For help, my code is as follows, please directly add the face coverage code to my code, thank you!

import cv2

face_cascade = cv2.CascadeClassifier('E:\openCV\opencv\sources/data/haarcascades/haarcascade_frontalface_alt2.xml')
cap = cv2.VideoCapture(0)
while True:
    ret,img = cap.read()
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    faces = face_cascade.detectMultiScale(gray, 1.3, 5)
    for (x,y,w,h) in faces:
        cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
        cv2.imshow('img',img)

if cv2.waitKey(1) &0xFF == ord('q'):
    break
cap.release()
cv2.destroyAllWindows()

solution

import cv2
img1_path = r'C:\\Users\\Jack\\Desktop\\Head\\head4.jpg'
img1 = cv2.imread(img1_path) # open image1
face_cascade = cv2.CascadeClassifier(r'D:\\Firefox\\1_Download\\opencv-master\\data\\haarcascades\\haarcascade_frontalface_default.xm1' )
cap = cv2. VideoCapture(1)
cv2. imshow( 'img1',img1)
while True :
    ret, img = cap.read(
    gray = cv2. cvtColor(img, cv2. COLOR _BGR2GRAY)
    faces = face_cascade.detectMultiScale(gray, 1.3, 5)
    for (x,Y,w,h) in faces:
        cv2.rectang1e(img, (x,y),(x+w,y+h), (255,0,0),2)
        img1 = cv2.resize(img1, (w,h))
        img[y:y+W,X:x+h] = img1
    cv2 . imshow('img' ,img)
    if cv2.waitKey(1) &8xFF == ord('q'):
        break
cap.release()
cv2.destroyAllWindows()
created at:07-31-2021
edited at: 07-31-2021: