参考OpenCV自带的例子,30行python代码实现人脸检测,不得不说,Python这个语言的优势太明显了,几乎把所有复杂的细节都屏蔽了,虽然效率较差,不过在调用opencv的模块时,因为模块都是C语言编写,所以在效率上并不会比用C或者C++编写慢太多。本例子使用自带的级联分类器。
#!/usr/bin/env python
import cv2
def faceDetect(img, face_cascade):
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),(255,0,0),2)
return img
def main():
cap = cv2.VideoCapture(1)
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
while(True):
# Capture frame-by-frame
ret, frame = cap.read()
#frame = imread("2017-02-26-200818.jpg")
# Our operations on the frame come here
if ret == True:
frame = faceDetect(frame, face_cascade)
# Display the resulting frame
cv2.imshow('frame',frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# When everything done, release the capture
cap.release()
cv2.destroyAllwindows()
main()
下面关于 Python 的文章您也可能喜欢,不妨参考下:
《Python核心编程 第二版》.(Wesley J. Chun ).[高清PDF中文版] 下载见 http://www.linuxidc.com/Linux/2013-06/85425.htm零基础如何入门Python http://www.linuxidc.com/Linux/2016-10/136485.htm
Ubuntu 14.04安装Python 3.3.5 http://www.linuxidc.com/Linux/2014-05/101481.htm
CentOS 6.5 脚本自动化装 Python2.6升级2.7 http://www.linuxidc.com/Linux/2017-02/140406.htm
CentOS上源码安装Python3.4 http://www.linuxidc.com/Linux/2015-01/111870.htm
Ubuntu 14.04下Python数据处理环境搭建 http://www.linuxidc.com/Linux/2017-01/139568.htm
Python Paramiko模块安装和使用 http://www.linuxidc.com/Linux/2017-01/139973.htm
《Python开发技术详解》.( 周伟,宗杰).[高清PDF扫描版+随书视频+代码] http://www.linuxidc.com/Linux/2013-11/92693.htm在CentOS 6.5上安装Python2.7 http://www.linuxidc.com/Linux/2016-10/136206.htm
Ubuntu 14.04 LTS下编译安装Open Babel和Python语言绑定 http://www.linuxidc.com/Linux/2017-01/139569.htm
Python常见数据结构整理 http://www.linuxidc.com/Linux/2017-02/140613.htm
本文永久更新链接地址 : http://www.linuxidc.com/Linux/2017-02/141151.htm