“This is the 13th day of my participation in the First Challenge 2022. For details: First Challenge 2022”

preface

I didn’t want to do more today, but I thought I’d finish what I started. If you have opencV installed and pycharm used to open the Python project, you will be able to use it easily. Now, after the 20th anniversary of xiaoye today, the 10-year journey of xiaoye has begun.

process

To prepare

This little demo is pretty simple. We use the training files that come with OpencV directlyThen we are preparing a picture I think you’ve figured out what I’m going to do, which is replace your face with this cute picture.The code is as follows:

# Import CV module
import cv2.cv2 as cv
import cv2.data as data
import time
# detection function

Head = cv.imread("head.jpg")


def face_detect_demo(img) :
    global Head
    gary = cv.cvtColor(img,cv.COLOR_BGR2GRAY)
    face_detect = cv.CascadeClassifier(data.haarcascades+"haarcascade_frontalface_default.xml")
    face = face_detect.detectMultiScale(gary)

    for x,y,w,h in face:

        cv.rectangle(img,(x,y),(x+w,y+h),color=(0.0.255),thickness=2)
        Head = cv.resize(Head,(w,h))

        if((x+w,y+h,3)<img.shape):
            img[x: w+x, y: h+y] = Head
    cv.imshow('result',img)



cap = cv.VideoCapture(0)
while True:
    flag, img = cap.read()
    if not flag:
        break
    face_detect_demo(img)
    if ord('q') == cv.waitKey(1) :break

cap.release()

# free memory
cv.destroyAllWindows()




Copy the code

And then it works perfectly

As for the effect, I will not demonstrate, super simple there is no ~. (OK, that’s the last post of the year.)