歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Python人臉檢測的簡單實現

Python人臉檢測的簡單實現

日期:2017/3/1 9:05:21   编辑:Linux編程

參考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

Copyright © Linux教程網 All Rights Reserved