Python如何實現人臉識別

2021-10-01 17:45:58 字數 1443 閱讀 4773

#!/usr/bin/python3

# -*- coding: utf-8 -*-

import cv2

import sys

class opencvs():

def main(self):

int_ = 0

cap = cv2.videocapture(0)

#告訴opencv使用人臉識別分類器

classfier = cv2.cascadeclassifier("haarcascade_frontalface_default.xml")

while cap.isopened():

#讀取一幀資料

ok, frame = cap.read()

#顯示方向

frame = cv2.flip(frame,1)

#將當前幀轉換成灰度影象

grey = cv2.cvtcolor(frame, cv2.color_bgr2gray)

#人臉檢測

facerects = classfier.detectmultiscale(grey, scalefactor = 1.8, minneighbors = 4, minsize = (64, 64))

#第乙個引數是灰度影象

#第三個引數是人臉檢測次數,設定越高,誤檢率越低,但是對於迷糊,我們設定越高,越不易檢測出來

if len(facerects) > 0:

int_ +=1

for facerect in facerects:

x, y, w, h = facerect

cv2.rectangle(frame, (x - 10, y - 10), (x + w + 10, y + h + 10), (0, 255, 0) , 1)

#確認3次拍攝後

if int_ > 3 :

cv2.imwrite(".png", grey )

break

#顯示影象

cv2.imshow(' ', frame)

c = cv2.waitkey(10)

if c & 0xff == ord('q'):

break

#釋放攝像頭並銷毀所有視窗

cap.release()

cv2.destroyallwindows()

if __name__ == '__main__':

opencvs().main()

直接可以用 不過記得安裝好python3的opencv庫

我們公司是國內做樹莓派人工智慧系統的

python實現人臉識別

安裝opencv pip install opencv python 基礎知識例項 載入 匯入模組 import cv2 as cv 讀取 img cv.imread tiaotiao.jpg 路徑中不能有中文,否則載入失敗 顯示cv.imshow read img img 等待鍵盤輸入 單位毫秒 ...

python人臉識別

talk is cheap,show you the code 參考自 這裡 無注釋版 注釋版 coding utf 8 import cv2 這裡的 cascadeclassifier 是 層級分類器 的意思。為什麼要 分層 呢?剛才提到在進行機器分析 時,其實是對整個從上到下,從左到右,乙個畫素...

python人臉識別

from numpy import 引入科學計算庫 import cv2 引入opencv庫 face cascade cv2.cascadeclassifier haarcascade frontalface alt.xml 載入haar特徵級聯表 img cv2.imread my.jpg 讀取...