使用Python计算人脸数量 – OpenCV
先决条件:使用dlib和openCV进行人脸检测
在本文中,我们将使用图像处理来检测和计算人脸数量。我们不应该获得面部的所有特征。取而代之的是,目标是通过一些方法即图像中人脸的坐标来获得边界框,这取决于坐标数量所覆盖的不同区域,将计算出的人脸数量。
需要的库:
- OpenCV Python中的库是一个计算机视觉库,主要用于图像处理、视频处理和分析、人脸识别和检测等。
- Python中的Dlib库包含预训练的面部标志检测器,用于检测映射到面部面部结构的 (x, y) 坐标。
- 麻木 是一个通用的数组处理包。它提供了一个高性能的多维数组对象和用于处理这些数组的工具。
以下是计算面数的分步方法:
第 1 步:导入所需的库。
Python3
# Import libraries
import cv2
import numpy as np
import dlib
Python3
# (0) in VideoCapture is used to
# connect to your computer's default camera
cap = cv2.VideoCapture(0)
# Get the coordinates
detector = dlib.get_frontal_face_detector()
Python3
while True:
# Capture frame-by-frame
ret, frame = cap.read()
frame = cv2.flip(frame, 1)
# Our operations on the frame come here
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = detector(gray)
# Counter to count number of faces
i = 0
for face in faces:
x, y = face.left(), face.top()
x1, y1 = face.right(), face.bottom()
cv2.rectangle(frame, (x, y), (x1, y1), (0, 255, 0), 2)
# Increment the iterartor each time you get the coordinates
i = i+1
# Adding face number to the box detecting faces
cv2.putText(frame, 'face num'+str(i), (x-10, y-10),
cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2)
print(face, i)
# Display the resulting frame
cv2.imshow('frame', frame)
Python3
# Enter key 'q' to break the loop
if cv2.waitKey(1) & 0xFF == ord('q'):
break
Python3
# When everything done, release
# the capture and destroy the windows
cap.release()
cv2.destroyAllWindows()
Python3
# Import required libraries
import cv2
import numpy as np
import dlib
# Connects to your computer's default camera
cap = cv2.VideoCapture(0)
# Detect the coordinates
detector = dlib.get_frontal_face_detector()
# Capture frames continuously
while True:
# Capture frame-by-frame
ret, frame = cap.read()
frame = cv2.flip(frame, 1)
# RGB to grayscale
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = detector(gray)
# Iterator to count faces
i = 0
for face in faces:
# Get the coordinates of faces
x, y = face.left(), face.top()
x1, y1 = face.right(), face.bottom()
cv2.rectangle(frame, (x, y), (x1, y1), (0, 255, 0), 2)
# Increment iterator for each face in faces
i = i+1
# Display the box and faces
cv2.putText(frame, 'face num'+str(i), (x-10, y-10),
cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2)
print(face, i)
# Display the resulting frame
cv2.imshow('frame', frame)
# This command let's us quit with the "q" button on a keyboard.
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# Release the capture and destroy the windows
cap.release()
cv2.destroyAllWindows()
第二步:打开默认摄像头抓脸,使用dlib库获取坐标。
蟒蛇3
# (0) in VideoCapture is used to
# connect to your computer's default camera
cap = cv2.VideoCapture(0)
# Get the coordinates
detector = dlib.get_frontal_face_detector()
第 3 步:计算人脸的数量。
- 连续捕获帧。
- 将帧转换为灰度(不是必需的)。
- 取一个迭代器i并将其初始化为零。
- 每次获得帧中人脸结构的坐标时,将迭代器增加 1。
- 在每个检测到的人脸周围绘制方框及其人脸数。
蟒蛇3
while True:
# Capture frame-by-frame
ret, frame = cap.read()
frame = cv2.flip(frame, 1)
# Our operations on the frame come here
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = detector(gray)
# Counter to count number of faces
i = 0
for face in faces:
x, y = face.left(), face.top()
x1, y1 = face.right(), face.bottom()
cv2.rectangle(frame, (x, y), (x1, y1), (0, 255, 0), 2)
# Increment the iterartor each time you get the coordinates
i = i+1
# Adding face number to the box detecting faces
cv2.putText(frame, 'face num'+str(i), (x-10, y-10),
cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2)
print(face, i)
# Display the resulting frame
cv2.imshow('frame', frame)
第 4 步:终止循环。
蟒蛇3
# Enter key 'q' to break the loop
if cv2.waitKey(1) & 0xFF == ord('q'):
break
第五步:清空窗户。
蟒蛇3
# When everything done, release
# the capture and destroy the windows
cap.release()
cv2.destroyAllWindows()
以下是上述方法的完整程序:
蟒蛇3
# Import required libraries
import cv2
import numpy as np
import dlib
# Connects to your computer's default camera
cap = cv2.VideoCapture(0)
# Detect the coordinates
detector = dlib.get_frontal_face_detector()
# Capture frames continuously
while True:
# Capture frame-by-frame
ret, frame = cap.read()
frame = cv2.flip(frame, 1)
# RGB to grayscale
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = detector(gray)
# Iterator to count faces
i = 0
for face in faces:
# Get the coordinates of faces
x, y = face.left(), face.top()
x1, y1 = face.right(), face.bottom()
cv2.rectangle(frame, (x, y), (x1, y1), (0, 255, 0), 2)
# Increment iterator for each face in faces
i = i+1
# Display the box and faces
cv2.putText(frame, 'face num'+str(i), (x-10, y-10),
cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2)
print(face, i)
# Display the resulting frame
cv2.imshow('frame', frame)
# This command let's us quit with the "q" button on a keyboard.
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# Release the capture and destroy the windows
cap.release()
cv2.destroyAllWindows()
输出: