30行Python代碼實現臉部偵測

來源:互聯網
上載者:User

30行Python代碼實現臉部偵測

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

本文永久更新連結地址:https://www.bkjia.com/Linux/2018-03/151331.htm

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.