安装完OpenCV是不是已经等不急要试试它的强大能力了?这里教大家使用自带的Haar级联分类器来进行人脸检测。
对于没有安装OpenCV的伙伴,可参考:
树莓派3B+ 安装计算机视觉库(OpenCV_2.4.9官方源)
树莓派3B+ 安装计算机视觉库(OpenCV_3+OpenCV_Contrib_3)
对于官方源安装的OpenCV,可以在 “/usr/share/opencv/” 目录下找到,对于自行编译安装的,请找到下载的源文件解压目录。(一般会在 “/home/pi/opencv-3.3.1/data/”)
在这里,默认是使用官方源版路径,其它请行修改。
人脸检测首先是加载图像并检测人脸,这也是最基本的一步。为了使所得到的结果直观显示,这里对原始图像的人脸周围绘制矩形框。
注意:OpenCV3.x版本的,请自行修改(接收返回值)
cv2.rectangle
—>img=cv2.rectangle...
// 以下为python脚本,请保存为detectface.py后缀
#!/usr/bin/env python
#coding=utf-8
import cv2
import sys
# rectangle color and stroke
color = (0,0,255) # reverse of RGB (B,G,R) - weird
strokeWeight = 2 # thickness of outline
def detect(filename):
face_cascade=cv2.CascadeClassifier('/usr/share/opencv/haarcascades/haarcascade_frontalface_default.xml')
# 载入要检测的图片
img = cv2.imread(filename)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces=face_cascade.detectMultiScale(gray,1.3,5)
for x,y,w,h in faces:
#这里要注意,OpenCV3.x版本需要接收返回值
#即img=cv2.rectangle(img, (x,y), (x+w, y+h), color, strokeWeight)
cv2.rectangle(img, (x,y), (x+w, y+h), color, strokeWeight)
cv2.imshow('faces',img)
#cv2.imwrite(filename+"-faces.jpg", img)
if cv2.waitKey(1000 // 12) & 0xff == ord("q"):
break
if __name__ == '__main__':
if len(sys.argv)>1:
i=1;
while iprint 'img:'+sys.argv[i]
detect(sys.argv[i])
i+=1
if len(sys.argv)==1:
print "USAGE: detectface.py <待检测图片存放路径>"
保存后,执行
python detectface.py photo.png
这里photo.png为待检测图片。如果你要保存检测的结果,可去掉注释cv2.imwrite(filename+"-faces.jpg", img)
注意:OpenCV3.x版本的,请自行修改(接收返回值)
cv2.rectangle
—>img=cv2.rectangle...
// 以下为python脚本,请保存为detecteye.py后缀
#!/usr/bin/env python
#coding=utf-8
import cv2
import sys
# rectangle color and stroke
face_color = (192,220,240) # reverse of RGB (B,G,R) - weird
eye_color = (160,160,164)
strokeWeight = 2 # thickness of outline
def detect(filename):
face_cascade=cv2.CascadeClassifier('/usr/share/opencv/haarcascades/haarcascade_frontalface_default.xml')
eye_cascade=cv2.CascadeClassifier('/usr/share/opencv/haarcascades/haarcascade_eye.xml')
# 载入要检测的图片
img = cv2.imread(filename)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces=face_cascade.detectMultiScale(gray,1.3,5)
for x,y,w,h in faces:
cv2.rectangle(img, (x,y), (x+w, y+h), face_color, strokeWeight)
face_gray=gray[y:y+h,x:x+w]
eyes=eye_cascade.detectMultiScale(face_gray,1.03,5,0,(40,40))
for ex,ey,ew,eh in eyes:
cv2.rectangle(img,(x+ex,y+ey),(x+ex+ew,y+ey+eh),eye_color,strokeWeight)
#cv2.namedWindow('detect_face')
cv2.imshow('faces-eyes',img)
#cv2.imwrite(filename+"-eyes.jpg", img)
if cv2.waitKey(1000 // 12) & 0xff == ord("q"):
break
if __name__ == '__main__':
if len(sys.argv)>1:
i=1;
while iprint 'img:'+sys.argv[i]
detect(sys.argv[i])
i+=1
if len(sys.argv)==1:
print "USAGE: detecteye.py <待检测图片存放路径>"
保存后,执行
python detecteye.py photo.png
这里photo.png为待检测图片。如果你要保存检测的结果,可去掉注释cv2.imwrite(filename+"-eyes.jpg", img)
这里直接提供一个多线程的检测方案,保证帧率更高。
注意:OpenCV3.x版本的,请自行修改(接收返回值)
cv2.rectangle
—>img=cv2.rectangle...
// 以下为python脚本,请保存为detectcamera.py后缀
#!/usr/bin/env python
#coding=utf-8
### Imports ###################################################################
import multiprocessing as mp
import cv2
import os
import time
### Setup #####################################################################
resX = 480
resY = 320
# The face cascade file to be used
face_cascade=cv2.CascadeClassifier('/usr/share/opencv/haarcascades/haarcascade_frontalface_default.xml')
eye_cascade=cv2.CascadeClassifier('/usr/share/opencv/haarcascades/haarcascade_eye.xml')
face_color = (192,220,240)
eye_color = (200,160,164)
strokeWeight = 2
t_start = time.time()
fps = 0
### Helper Functions ##########################################################
def get_faces( img ):
gray = cv2.cvtColor( img, cv2.COLOR_BGR2GRAY )
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
return faces, img, gray
def draw_frame( faces, img, gray):
global xdeg
global ydeg
global fps
global time_t
for x, y, w, h in faces:
cv2.rectangle( img, ( x, y ),( x + w, y + h ), face_color, strokeWeight)
cv2.putText(img, "Face No." + str( len( faces ) ), ( x, y ), cv2.FONT_HERSHEY_SIMPLEX, 0.5, ( 0, 0, 255 ), 2 )
face_gray=gray[y:y+h,x:x+w]
eyes=eye_cascade.detectMultiScale(face_gray,1.03,5,0,(40,40))
for ex,ey,ew,eh in eyes:
cv2.rectangle(img,(x+ex,y+ey),(x+ex+ew,y+ey+eh),eye_color,strokeWeight)
# Calculate and show the FPS
fps = fps + 1
sfps = fps / (time.time() - t_start)
cv2.putText(img, "FPS : " + str( int( sfps ) ), ( 10, 10 ), cv2.FONT_HERSHEY_SIMPLEX, 0.5, ( 0, 0, 255 ), 2 )
cv2.imshow( "Frame", img )
### Main ######################################################################
if __name__ == '__main__':
camera = cv2.VideoCapture(0)
camera.set(cv2.cv.CV_CAP_PROP_FRAME_WIDTH,resX)
camera.set(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT,resY)
pool = mp.Pool( processes=4 )
read, img = camera.read()
pr1 = pool.apply_async( get_faces, [ img ] )
read, img = camera.read()
pr2 = pool.apply_async( get_faces, [ img ] )
read, img = camera.read()
pr3 = pool.apply_async( get_faces, [ img ] )
read, img = camera.read()
pr4 = pool.apply_async( get_faces, [ img ] )
fcount = 1
while (True):
read, img = camera.read()
if fcount == 1:
pr1 = pool.apply_async( get_faces, [ img ] )
faces, img, gray=pr2.get()
draw_frame( faces, img, gray )
elif fcount == 2:
pr2 = pool.apply_async( get_faces, [ img ] )
faces, img, gray=pr3.get()
draw_frame( faces, img, gray )
elif fcount == 3:
pr3 = pool.apply_async( get_faces, [ img ] )
faces, img, gray=pr4.get()
draw_frame( faces, img, gray )
elif fcount == 4:
pr4 = pool.apply_async( get_faces, [ img ] )
faces, img, gray=pr1.get()
draw_frame( faces, img, gray )
fcount = 0
fcount += 1
if cv2.waitKey(1000 // 12) & 0xff == ord("q"):
break
cv2.destroyAllWindows()
保存后,执行
python detectcamera.py
演示效果:
人脸识别:
http://blog.csdn.net/kxwinxp/article/details/78522545