[PYTHON] Try face detection in real time using a webcam

Introduction

I installed software called motion on the Raspberry PI and used it as a surveillance camera, and I am trying to identify who the person in the picture is, but it is a bit unsatisfactory because it only identifies using the saved still image. Around this time.

Since it was a big deal, I was wondering if I could do something by processing the video in real time and collecting information, but since there was an article that seems to be related, I made a script to detect the face in real time while referring to it.

Try converting webcam / camcorder videos in real time with OpenCV

OpenCV installation

This is miso, but please refer to the following article for the installation procedure.

Procedure to quickly create a deep learning environment on Mac with TensorFlow and OpenCV

Also, copy the classifier used for face detection to the working folder with the following command.

$ cp /usr/local/share/OpenCV/haarcascades/haarcascade_frontalface_alt2.xml ./

Script creation

The script published on the reference site has been modified as follows.

sebcam.py


import cv2

if __name__ == '__main__':
    #Constant definition
    ESC_KEY = 27     #Esc key
    INTERVAL= 33     #Waiting time
    FRAME_RATE = 30  # fps

    ORG_WINDOW_NAME = "org"
    GAUSSIAN_WINDOW_NAME = "gaussian"

    DEVICE_ID = 0

    #Designation of classifier
    cascade_file = "haarcascade_frontalface_alt2.xml"
    cascade = cv2.CascadeClassifier(cascade_file)
    
    #Camera image acquisition
    cap = cv2.VideoCapture(DEVICE_ID)

    #Loading the initial frame
    end_flag, c_frame = cap.read()
    height, width, channels = c_frame.shape

    #Window preparation
    cv2.namedWindow(ORG_WINDOW_NAME)
    cv2.namedWindow(GAUSSIAN_WINDOW_NAME)

    #Conversion processing loop
    while end_flag == True:

        #Image acquisition and face detection
        img = c_frame
        img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
        face_list = cascade.detectMultiScale(img_gray, minSize=(100, 100))
        
        #Mark the detected face
        for (x, y, w, h) in face_list:
            color = (0, 0, 225)
            pen_w = 3
            cv2.rectangle(img_gray, (x, y), (x+w, y+h), color, thickness = pen_w)
        
        #Frame display
        cv2.imshow(ORG_WINDOW_NAME, c_frame)
        cv2.imshow(GAUSSIAN_WINDOW_NAME, img_gray)

        #Exit with Esc key
        key = cv2.waitKey(INTERVAL)
        if key == ESC_KEY:
            break

        #Read next frame
        end_flag, c_frame = cap.read()

    #End processing
    cv2.destroyAllWindows()
    cap.release()

Operation check

When you execute the following command, two windows will be displayed, and you should see the original image and grayscale with a square drawn on the face.

python


$ python sebcam.py

It was done (^-^) /

Bonus (optical flow)

There was also a sample that tracks an object detected in real time, so I tried it.

Draw optical flow in real time with OpenCV (Shi-Tomasi method, Lucas-Kanade method)

Since the video reading source of the reference destination was a video file, I changed this to a webcam. And when the detected feature points disappear, it is changed to be detected again.

LucasKande.py


import numpy as np
import cv2

DEVICE_ID = 0
cap = cv2.VideoCapture(DEVICE_ID)

# Shi-Tomasi corner detection parameters
feature_params = dict( maxCorners = 100,
                       qualityLevel = 0.3,
                       minDistance = 7,
                       blockSize = 7 )

# Lucas-Kanade method parameters
lk_params = dict( winSize  = (15,15),
                  maxLevel = 2,
                  criteria = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 0.03))

#Randomly generate 100 colors (generate a random ndarray with 100 rows and 3 columns in the range of 0 to 255)
color = np.random.randint(0, 255, (100, 3))

#Processing of the first frame
end_flag, frame = cap.read()
gray_prev = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
feature_prev = cv2.goodFeaturesToTrack(gray_prev, mask = None, **feature_params)
mask = np.zeros_like(frame)

while(end_flag):
    #Convert to grayscale
    gray_next = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    #Optical flow detection
    feature_next, status, err = cv2.calcOpticalFlowPyrLK(gray_prev, gray_next, feature_prev, None, **lk_params)

    if len(feature_next[status == 1]) == 0:
        feature_prev = cv2.goodFeaturesToTrack(gray_prev, mask = None, **feature_params)
        mask = np.zeros_like(frame)
        feature_next, status, err = cv2.calcOpticalFlowPyrLK(gray_prev, gray_next, feature_prev, None, **lk_params)

    #Select feature points for which optical flow was detected (0: not detected, 1: detected)
    good_prev = feature_prev[status == 1]
    good_next = feature_next[status == 1]
    
    #Draw optical flow
    for i, (next_point, prev_point) in enumerate(zip(good_next, good_prev)):
        prev_x, prev_y = prev_point.ravel()
        next_x, next_y = next_point.ravel()
        mask = cv2.line(mask, (next_x, next_y), (prev_x, prev_y), color[i].tolist(), 2)
        frame = cv2.circle(frame, (next_x, next_y), 25, color[i].tolist(), -1)
    img = cv2.add(frame, mask)

    #Show in window
    cv2.imshow('window', img)

    #Finish by pressing the ESC key
    if cv2.waitKey(30) & 0xff == 27:
        break

    #Preparation of next frame and point
    gray_prev = gray_next.copy()
    feature_prev = good_next.reshape(-1, 1, 2)
    end_flag, frame = cap.read()

#End processing
cv2.destroyAllWindows()
cap.release()

did it!

Recommended Posts

Try face detection in real time using a webcam
Face detection using a cascade classifier
Try converting videos in real time with OpenCV
Face detection summary in Python
Try building a neural network in Python without using a library
Try running a function written in Python using Fn Project
Try using Spyder included in Anaconda
Make a face recognizer using TensorFlow
Try using LevelDB in Python (plyvel)
Try using Leap Motion in Python
Using a webcam with Raspberry Pi
Try using FireBase Cloud Firestore in Python for the time being
How to make a model for object detection using YOLO in 3 hours
I tried to classify guitar chords in real time using machine learning
Try using a Linux server as a backup destination for Time Machine (Ver. 2020)
[Python] [Word] [python-docx] Try to create a template of a word sentence in Python using python-docx
A useful note when using Python for the first time in a while
Try using the Kraken API in Python
Try using the HL band in order
Try sending a SYN packet in Python
Try drawing a simple animation in Python
Quickly try Microsoft's Face API in Python
Try using a stochastic programming language (Pyro)
Draw a tree in Python 3 using graphviz
Try a functional programming pipe in Python
Try using platypus, a multipurpose optimization library
Build a LAMP environment in a very short time
Create a GIF file using Pillow in Python
Try to calculate a statistical problem in Python
Reinforcement learning 10 Try using a trained neural network.
Try to separate Controllers using Blueprint in Flask
Try using a QR code on a Raspberry Pi
Try using the BitFlyer Ligntning API in Python
Try using Sourcetrail, a source code visualization tool
A clever way to time processing in Python
Read the output of subprocess.Popen in real time
View drug reviews using a list in Python
Try using LINE Notify for the time being
Try drawing contour plots using matplotlib in OpenFOAM
Let's try real-time object detection using Faster R-CNN
Implement similar face search in half a day
Tips for using ElasticSearch in a good way
Create a MIDI file in Python using pretty_midi
I wrote a Japanese parser in Japanese using pyparsing.
Record YouTube views in a spreadsheet using Lambda
Try using an object-oriented class in R (R6 method)
Try using ChatWork API and Qiita API in Python
Try using the DropBox Core API in Python
[Super easy] Simultaneous face recognition and facial expression recognition in real time with Python and OpenCV!
Every time I try to read a csv file using pandas, I get a numpy error.
How to unit test a function containing the current time using freezegun in python
Save your heart rate to SpreadSheets in real time (?) Using Python x fitbit API!
A script that transfers tweets containing specific Twitter keywords to Slack in real time
Predict from various data in Python using Facebook Prophet, a time series prediction tool