[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)
Create opencvEnv environment with venv
$ python3 -m venv opencvEnv
#Activate
$ source opencvEnv/bin/activate
(opencvEnv)$ ...
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
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 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/
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].
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
Try face detection in real time using a webcam
I also do a personal blog. Nagano Engineer Life
Recommended Posts