Let's activate the camera and apply a mosaic to the human face (OpenCV)

1.First of all

Hello everyone! This time, using python and OpenCV, I created a program that can start the camera and write a mosaic on a human face in real time. Maybe someone else has already written the article ... maybe it's out. Let's do it anyway!

2. Procedure

The flow of the program is here.

  1. Start the camera.
  2. Recognize human faces. (Haar-like feature classifier)
  3. Mosaicize where there is a human face. that's all.

3. Start the camera.

Start the camera using OpenCV.

kido.py


import cv2
cap = cv2.VideoCapture(0)
while True:
    ret, img = cap.read()
    cv2.imshow('video image', img)#'video image'Is the name of the camera window
    key = cv2.waitKey(10)
    if key == 27:  #Exit with ESC key
        break
cap.release()
cv2.destroyAllWindows()

video image.png

Now you can orbit the camera. Press the Esc key and the window will close.

4. Recognize human faces (Haar-like feature classifier)

OpenCV comes with a Haar-like feature classifier in advance. Convenient! However, it cannot be handled unless the file is saved. Since I want to recognize human faces this time, I will handle haarcascade_frontalface_alt.xml of here.

By the way, the Haar-like feature classifier detects the face by extracting the features while changing the size of the Haar-like filter by the Haar-like features focusing on the difference in brightness from the features that can take the features of the image and AdaBoost. I will. Now, let's start the camera and let it recognize the human face.

ninsiki.py


import cv2

#Haar-like Be able to handle feature classifiers.
face_cascade_path="haarcascade_frontalface_alt.xml" #Please specify the path.
face_cascade = cv2.CascadeClassifier(face_cascade_path)

cap = cv2.VideoCapture(0)
while True:
    ret, img = cap.read()
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    faces = face_cascade.detectMultiScale(gray, scaleFactor=1.3, minNeighbors=5)
    #Rectangle around a person's face
    for x, y, w, h in faces:
        cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2)
        face = img[y: y + h, x: x + w]
        face_gray = gray[y: y + h, x: x + w]
    cv2.imshow('video image', img)
    key = cv2.waitKey(10)
    if key == 27:  #Exit with ESC key
        break

cap.release()
cv2.destroyAllWindows()

video image.png Now you can detect the human face.

5. Apply a mosaic

Without thinking that mosaic processing is difficult, the face image is reduced once, then enlarged and pasted on the face image.

mozaic.py


import cv2

#Haar-like Be able to handle feature classifiers.
face_cascade_path="haarcascade_frontalface_alt.xml"
face_cascade = cv2.CascadeClassifier(face_cascade_path)
ratio = 0.07 #Changing the value here changes the roughness of the mosaic.

cap = cv2.VideoCapture(0)
while True:
    ret, img = cap.read()
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    faces = face_cascade.detectMultiScale(gray, scaleFactor=1.3, minNeighbors=5)
    for x, y, w, h in faces:
        #The mosaic processing is done in two individual lines.
        small = cv2.resize(img[y: y+h, x: x+w], None,fy=ratio, fx=ratio,interpolation=cv2.INTER_NEAREST)
        img[y: y + h, x: x + w] = cv2.resize(small, (w, h), interpolation=cv2.INTER_NEAREST)
    cv2.imshow('video image', img)
    key = cv2.waitKey(10)
    if key == 27:  #Exit with ESC key
        break

cap.release()
cv2.destroyAllWindows()

video image.png It was completed.

6. Summary

This time, we recognized the face in real time and applied mosaic processing to the face. Since it is classified by the difference in brightness, even a caricature will react. (There are quite a few false positives), but OpenCV is fun because you can try face detection (face recognition) with a short code. It may be difficult to understand because I have never sent information such as blogs. From now on, I would like to disseminate information and improve my writing skills, and my dream is to become a person who can play with technology, so please watch with warm eyes. Thank you in advance. Well then!

Recommended Posts

Let's activate the camera and apply a mosaic to the human face (OpenCV)
How to make a surveillance camera (Security Camera) with Opencv and Python
Let's apply the brand image color to the matplotlib colormap!
A server that returns the number of people in front of the camera with bottle.py and OpenCV
Create a web surveillance camera with Raspberry Pi and OpenCV
Build a Python environment and transfer data to the server
Shoot time-lapse from a PC camera using Python and OpenCV
I want to record the execution time and keep a log.
Let's write a program to solve the 4x4x4 Rubik's Cube! 2. Algorithm
Let's write a program to solve the 4x4x4 Rubik's Cube! 3. Implementation
How to format a table using Pandas apply, pivot and swaplevel
Try to write a program that abuses the program and sends 100 emails
I want to check the position of my face with OpenCV!
The fastest way to get camera images regularly with python opencv