python人臉學習

2021-10-01 11:18:19 字數 4744 閱讀 2528

一、對進行人臉檢測

import numpy as np

import cv2

from pylab import *

img=cv2.imread("f:/rl/pichost/5.jpg",1)# 讀取

color = (0, 255, 0)

grey = cv2.cvtcolor(img,cv2.color_bgr2gray) #變為灰度

facerects = face_patterns.detectmultiscale(grey, scalefactor=1.2, minneighbors=3, minsize=(32, 32))

if len(facerects) > 0: # large to 0,then it is a face

for facerect in facerects:

x, y, w, h = facerect

cv2.rectangle(img, (x - 10, y - 10), (x + w + 10, y + h + 10), color, 3)

#writen to image

font=cv2.font_hershey_******x

img=cv2.puttext(img,'bset regards! from argmin2018.',(1,40),font,1.2,(14,26,25),2)

cv2.imwrite('output.jpg',img) # 儲存

cv2.imshow("find faces!",img)# 顯示

cv2.waitkey(0)

numpy:用來儲存和處理大型多維矩陣

cv2:處理

pylab:畫圖

cascadeclassifier:級聯分類器

二、使用筆記本的攝像頭進行人臉檢測

啟動不成功:可能裝置管理器裡面的攝像頭未啟動

import cv2

cap = cv2.videocapture(0)

while(true):

# capture frame-by-frame

ret, frame = cap.read()

# our operations on the frame come here

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

face_cascade = cv2.cascadeclassifier(xmlfile)

faces = face_cascade.detectmultiscale(

gray,

scalefactor=1.15,

minneighbors=5,

minsize=(5, 5),

)print("發現個目標!".format(len(faces)))

for (x, y, w, h) in faces:

cv2.rectangle(frame, (x, y), (x + w, y + w), (0, 255, 0), 2)

cv2.imshow("frame", frame)

# display the resulting frame

if cv2.waitkey(1) & 0xff == ord('q'):

break

# when everything done, release the capture

cap.release()

cv2.destroyallwindows()

三、人臉收集

import cv2

import os

# 呼叫筆記本內建攝像頭,所以引數為0,如果有其他的攝像頭可以調整引數為1,2

cap = cv2.videocapture(0)

face_id = input('\n enter user id:')

print('\n initializing face capture. look at the camera and wait ...')

count = 0

while true:

# 從攝像頭讀取

sucess, img = cap.read()

# 轉為灰度

gray = cv2.cvtcolor(img, cv2.color_bgr2gray)

# 檢測人臉

faces = face_detector.detectmultiscale(gray, 1.3, 5)

for (x, y, w, h) in faces:

cv2.rectangle(img, (x, y), (x+w, y+w), (255, 0, 0))

count += 1

# 儲存影象

# 保持畫面的持續。

k = cv2.waitkey(1)

if k == 27: # 通過esc鍵退出攝像

break

elif count >= 1: # 得到1000個樣本後退出攝像

break

# 關閉攝像頭

cap.release()

cv2.destroyallwindows()

四、人臉訓練

import numpy as np

from pil import image

import os

import cv2

# 人臉資料路徑

path = 'facedata'

recognizer = cv2.face.lbphfacerecognizer_create()

def getimagesandlabels(path):

imagepaths = [os.path.join(path, f) for f in os.listdir(path)] # join函式的作用?

facesamples =

ids =

for imagepath in imagepaths:

pil_img = image.open(imagepath).convert('l') # convert it to grayscale

img_numpy = np.array(pil_img, 'uint8')

id = int(os.path.split(imagepath)[-1].split(".")[1])

faces = detector.detectmultiscale(img_numpy)

for (x, y, w, h) in faces:

return facesamples, ids

print('training faces. it will take a few seconds. wait ...')

faces, ids = getimagesandlabels(path)

recognizer.train(faces, np.array(ids))

recognizer.write(r'face_trainer\trainer.yml')

print(" faces trained. exiting program".format(len(np.unique(ids))))

五、人臉識別

import numpy as np

from pil import image

import os

import cv2

# 人臉資料路徑

path = 'facedata'

recognizer = cv2.face.lbphfacerecognizer_create()

def getimagesandlabels(path):

imagepaths = [os.path.join(path, f) for f in os.listdir(path)] # join函式的作用?

facesamples =

ids =

for imagepath in imagepaths:

pil_img = image.open(imagepath).convert('l') # convert it to grayscale

img_numpy = np.array(pil_img, 'uint8')

id = int(os.path.split(imagepath)[-1].split(".")[1])

faces = detector.detectmultiscale(img_numpy)

for (x, y, w, h) in faces:

return facesamples, ids

print('training faces. it will take a few seconds. wait ...')

faces, ids = getimagesandlabels(path)

recognizer.train(faces, np.array(ids))

recognizer.write(r'face_trainer\trainer.yml')

print(" faces trained. exiting program".format(len(np.unique(ids))))

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 讀取...

人臉檢測python

我用的是python27。opencv340.匯入opencv庫 import cv2 載入opencv自帶的分類器 face patterns cv2.cascadeclassifier e opencv build etc haarcascades haarcascade frontalface...