簡單的Python人臉識(shí)別系統(tǒng)
案例一 導(dǎo)入圖片
思路: 1.導(dǎo)入庫 2.加載圖片 3.創(chuàng)建窗口 4.顯示圖片 5.暫停窗口 6.關(guān)閉窗口
# 1.導(dǎo)入庫import cv2# 2.加載圖片img = cv2.imread(’a.png’)# 3.創(chuàng)建窗口cv2.namedWindow(’window 1 haha’)# 4.顯示圖片cv2.imshow(’window 1’,img)# 5.暫停窗口cv2.waitKey(0)# 6.關(guān)閉窗口cv2.destroyAllWindows()
案例二 在圖片上添加人臉識(shí)別
思路: 1.導(dǎo)入庫 2.加載圖片 3.加載人臉模型 4.調(diào)整圖片灰度 5.檢查人臉 6.標(biāo)記人臉 7.創(chuàng)建窗口 8.顯示圖片 9.暫停窗口 10.關(guān)閉窗口
# 1.導(dǎo)入庫import cv2# 2.加載圖片img = cv2.imread(’a.png’)# 3.加載人臉模型,opencv官網(wǎng)下載face = cv2.CascadeClassifier(’haarcascade_frontalface_default.xml’)# 4.調(diào)整圖片灰度:沒必要識(shí)別顏色,灰度可以提高性能gray = cv2.cvtColor(img,cv2.COLOR_RGB2GRAY)# 5.檢查人臉faces = face.detectMultiScale(gray)# 6.標(biāo)記人臉for (x,y,w,h) in faces: # 里面有4個(gè)參數(shù) 1.寫圖片 2.坐標(biāo)原點(diǎn) 3.識(shí)別大小 4.顏色 5.線寬 cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0),4)# 7.創(chuàng)建窗口 cv2.namedWindow(’window 1 haha’)# 8.顯示圖片cv2.imshow(’window 1’, img)# 9.暫停窗口cv2.waitKey(0)# 10.關(guān)閉窗口cv2.destroyAllWindows()
案例三 調(diào)用攝像頭
思路: 1.導(dǎo)入庫 2.打開攝像頭 3.獲取攝像頭實(shí)時(shí)畫面 4.釋放資源 5.關(guān)閉窗口
# 1.導(dǎo)入庫import cv2# 2.打開攝像頭capture = cv2.VideoCapture(0)# 3.獲取攝像頭實(shí)時(shí)畫面cv2.namedWindow(’camera’)while True: #3.1 獲取攝像頭的幀畫面 ret,frame = capture.read() #3.2 顯示圖片(渲染畫面) cv2.imshow(’window 1’,frame) #3.3 暫停窗口 if cv2.waitKey(5) & 0xFF == ord(’q’): break # 4.釋放資源capture.release()# 5.關(guān)閉窗口cv2.destroyAllWindows()
案例四 攝像頭識(shí)別人臉
思路: 1.導(dǎo)入庫 2.加載人臉模型 3.打開攝像頭 4.創(chuàng)建窗口 5.獲取攝像頭實(shí)時(shí)畫面 6.釋放資源 7.關(guān)閉窗口
# 1.導(dǎo)入庫import cv2# 2.加載人臉模型face = cv2.CascadeClassifier(’haarcascade_frontalface_default.xml’)# 3.打開攝像頭capture = cv2.VideoCapture(0)# 4.創(chuàng)建窗口cv2.namedWindow(’window 1’)# 5.獲取攝像頭實(shí)時(shí)畫面while True: # 5.1 獲取攝像頭的幀畫面 ret,frame = capture.read() # 5.2 圖片灰度調(diào)整 gray = cv2.cvtColor(frame,cv2.COLOR_RGB2GRAY) # 5.3 檢查人臉 faces = face.detectMultiScale(gray) # 5.4 標(biāo)記人臉 for (x, y, w, h) in faces:# 里面有4個(gè)參數(shù) 1.寫圖片 2.坐標(biāo)原點(diǎn) 3.識(shí)別大小 4.顏色 5.線寬cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 4) # 5.5 顯示圖片cv2.imshow(’camera’,frame) # 5.6 暫停窗口if cv2.waitKey(5) & 0xFF == ord(’q’): break# 6.釋放資源capture.release()# 7.關(guān)閉窗口cv2.destroyAllWindows()
以上就是簡單的Python人臉識(shí)別系統(tǒng)的詳細(xì)內(nèi)容,更多關(guān)于Python人臉識(shí)別的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章: