https://github.com/opencv/opencv/tree/master/data
cv.CascadeClassifier():
detectMultiScale函数:检测人脸算法,其参数如下:
LBP:
本代码中不涉及具体的LBP算法的实现,而是直接调用了现成的LBP分类器,直接实现了效果。
可以看到,近处的人脸识别效果还可以,但是由于摄像头清晰度等原因,远处的人脸识别并不是很准确。
但是,抗遮挡效果貌似还可以:
动态效果如下:
import cv2
##################################################
############使用LBP识别人脸#######################
##################################################
def use_LBP_to_distinguish_face():
# 识别打开摄像头
cap = cv2.VideoCapture(0, cv2.CAP_DSHOW)
# 创建LBP特征的分类器
face_detect = cv2.CascadeClassifier(r'tool/lbpcascades/lbpcascade_frontalface_improved.xml')
while True:
# 读取视频的每一帧
flag, frame = cap.read()
frame = cv2.flip(frame, 1)
if not flag:
break
# 进行灰度处理
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# 多个尺度空间进行人脸检测 返回检测到的人脸区域坐标信息
face_zone = face_detect.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5)
# 绘制矩形和圆形检测人脸
for x, y, w, h in face_zone:
cv2.rectangle(frame, pt1=(x, y), pt2=(x + w, y + h), color=[0, 0, 255], thickness=2)
#
cv2.imshow('video', frame)
#
if ord('q') == cv2.waitKey(30):
break
cv2.destroyAllWindows()
cap.release()
说实话,感觉HOG的效果,精度好像不是很高…仅仅是个人感觉。
def use_HOG_to_distinguish_people():
# 识别打开摄像头
cap = cv2.VideoCapture(0, cv2.CAP_DSHOW)
while True:
# 读取视频的每一帧
flag, frame = cap.read()
frame = cv2.flip(frame, 1)
if not flag:
break
#设置hog和分类svm
hog = cv2.HOGDescriptor()
hog.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector())
(rects,weight) = hog.detectMultiScale(frame,
winStride=(2,4),
padding=(8,8),
scale=1.2,
useMeanshiftGrouping=False)
# 检测人身
for (x,y,w,h) in rects:
cv2.rectangle(frame,(x,y),(x+w,y+h),(0,255,0),2)
#
cv2.imshow('video', frame)
#
if ord('q') == cv2.waitKey(30):
break
cv2.destroyAllWindows()
cap.release()
import cv2
##################################################
############使用LBP识别人脸#######################
##################################################
def use_LBP_to_distinguish_face():
# 识别打开摄像头
cap = cv2.VideoCapture(0, cv2.CAP_DSHOW)
# 加载一个LBP特征的分类器
face_detect = cv2.CascadeClassifier(r'tool/lbpcascades/lbpcascade_frontalface_improved.xml')
while True:
# 读取视频的每一帧
flag, frame = cap.read()
frame = cv2.flip(frame, 1)
if not flag:
break
# 进行灰度处理
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# 多个尺度空间进行人脸检测 返回检测到的人脸区域坐标信息
face_zone = face_detect.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5)
# 检测人脸
for x, y, w, h in face_zone:
cv2.rectangle(frame, pt1=(x, y), pt2=(x + w, y + h), color=[0, 0, 255], thickness=2)
#
cv2.imshow('video', frame)
#
if ord('q') == cv2.waitKey(30):
break
cv2.destroyAllWindows()
cap.release()
################################################
###########HOG################################
###############################################
def use_HOG_to_distinguish_people():
# 识别打开摄像头
cap = cv2.VideoCapture(0, cv2.CAP_DSHOW)
while True:
# 读取视频的每一帧
flag, frame = cap.read()
frame = cv2.flip(frame, 1)
if not flag:
break
#设置hog和分类svm
hog = cv2.HOGDescriptor()
hog.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector())
(rects,weight) = hog.detectMultiScale(frame,
winStride=(2,4),
padding=(8,8),
scale=1.2,
useMeanshiftGrouping=False)
# 检测人身
for (x,y,w,h) in rects:
cv2.rectangle(frame,(x,y),(x+w,y+h),(0,255,0),2)
#
cv2.imshow('video', frame)
#
if ord('q') == cv2.waitKey(30):
break
cv2.destroyAllWindows()
cap.release()
################################################
##################main####################
################################################
use_LBP_to_distinguish_face()
#use_HOG_to_distinguish_people()
HOG:
https://blog.csdn.net/qq_39071739/article/details/103306454
LBP:
https://www.jb51.net/article/165120.htm
https://cloud.tencent.com/developer/article/1699695