A memo when face is detected with Python + OpenCV quickly

table of contents

[1. Preparing the virtual environment](# 1-Preparing the virtual environment) [2. Install openCV](# 2-Install openCV) [3. Face detection program preparation](# 3-Face detection program preparation) [4. Get the classifier file](# 4-Get the classifier file) [5. Execute](# 5-Execute) [6. Source Code](# 6-Source Code)

Preparing the virtual environment

Create opencvEnv environment with venv

$ python3 -m venv opencvEnv

#Activate
$ source opencvEnv/bin/activate

(opencvEnv)$  ...

install openCV

Install openCV with pip

(opencvEnv)$ pip install opencv-python

Collecting opencv-python
  Downloading https://files.pythonhosted.org/packages/e2/a9/cd3912ca0576ea6588095dce55e54c5f0efeb3d63fb88f16f4c06c0fac8d/opencv_python-4.1.2.30-cp36-cp36m-macosx_10_9_x86_64.whl (45.2MB)
    100% |████████████████████████████████| 45.2MB 721kB/s
Collecting numpy>=1.11.3 (from opencv-python)
  Using cached https://files.pythonhosted.org/packages/22/99/36e3408ae2cb8b72260de4e538196d17736d7fb82a1086cb2c21ee156ddc/numpy-1.17.4-cp36-cp36m-macosx_10_9_x86_64.whl
Installing collected packages: numpy, opencv-python
Successfully installed numpy-1.17.4 opencv-python-4.1.2.30
import cv2

Face detection program preparation

Create face_detect.py

face_detect.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 = "../xml/haarcascade_frontalface_alt2.xml"
    cascade = cv2.CascadeClassifier(cascade_file)

    #Camera image acquisition
    cap = cv2.VideoCapture(DEVICE_ID)

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

Get the classifier file

Get the haarcascade_frontalface_alt2.xml used for face detection from the following site and download the file to any location </ b>.

https://ja.osdn.net/projects/sfnet_magicvisionport/downloads/mvp/cascades/haarcascade_frontalface_alt2.xml/

Run

Execute face_ detect.py. The terminal will ask for permission to access the camera, so allow it.

(opencvEnv)$ python face_ detect.py

Faces can be detected from images that can be acquired by the camera. The camera can be killed with [Esc]. 20191219213757.gif

Source code

This source code is reprinted from face_detect.py in the following repository.

https://github.com/kawakeee/openCV_practice/blob/master/detection/face_detect.py

Reference article

Try face detection in real time using a webcam

I also do a personal blog. Nagano Engineer Life

Recommended Posts

A memo when face is detected with Python + OpenCV quickly
A memo for when pip3 is installed with python2.7 for some reason
A memo when creating a python environment with miniconda
Face detection with Python + OpenCV
[Python] What is a with statement?
Try face recognition with python + OpenCV
Cut out face with Python + OpenCV
Face recognition with camera with opencv3 + python2.7
Face detection with Python + OpenCV (rotation invariant)
A memo with Python2.7 and Python3 on CentOS
[python, openCV] base64 Face recognition with images
Macbook Air with M1 is here! Quickly create a Python computing environment
Hello World and face detection with OpenCV 4.3 + Python
Error when installing a module with Python pip
Quickly build a Python Django environment with IntelliJ
python openCV installation (memo)
Python OpenCV tutorial memo
A memo connected to HiveServer2 of EMR with python
Problems when creating a csv-json conversion tool with python
Fill the background with a single color with OpenCV2 + Python
[Python] A memo to write CSV vertically with Pandas
A memo that I touched the Datastore with python
A memo about building a Django (Python) application with Docker
What is God? Make a simple chatbot with python
A memo when checking whether the specified key exists in the defined dictionary with python
A memo of misunderstanding when trying to load the entire self-made module with Python3
A memo that reads data from dashDB with Python & Spark
Improve detection accuracy quickly by specifying parameters with openCV face detection
Face recognition with Python's OpenCV
Determine if a string is a time with a python regular expression
"Apple processing" with OpenCV3 + Python3
Quickly take a query string with API Gateway-> Lambda (Python)
Twitter graphing memo with Python
Image editing with python OpenCV
Camera capture with Python + OpenCV
[Python] Using OpenCV with Python (Basic)
[python] Reverse with slices! !! (There is also a commentary on slices!)
[python] A note when trying to use numpy with Cython
Face detection with Python + dlib
Use a macro that runs when saving python with vscode
A memo when creating a directed graph using Graphviz in Python
Python list is not a list
Grayscale image is displayed as a color image in OpenCV / Python
Make a fortune with Python
Create a simple video analysis tool with python wxpython + openCV
Error when playing with python
When "No changes detected" is displayed in python3 manage.py makemigrations
Face recognition / cutting with OpenCV
Try face recognition with Python
Create a directory with python
Using OpenCV with Python @Mac
What is a python map?
Anime face detection with OpenCV
[Python, Selenium, PhantomJS] A story when scraping a website with lazy load
Create a striped illusion with gamma correction for Python3 and openCV3
Make a cat detector with Google Colabratory (Part 2) [Python] ~ Use OpenCV ~
[Python] A memo that I tried to get started with asyncio
[Python] Execution time when a function is entered in a dictionary value
How to make a surveillance camera (Security Camera) with Opencv and Python
Make a simple OMR (mark sheet reader) with Python and OpenCV
[Python] A memo to operate ROM created by GBDK with PyBoy