opencv實(shí)現(xiàn)簡單人臉識(shí)別
對(duì)于opencv 它提供了許多已經(jīng)練習(xí)好的模型可供使用,我們需要通過他們來進(jìn)行人臉識(shí)別
參考了網(wǎng)上許多資料
假設(shè)你已經(jīng)配好了開發(fā)環(huán)境 ,在我之前的博客中由開發(fā)環(huán)境的配置。
項(xiàng)目代碼結(jié)構(gòu):
dataSet : 存儲(chǔ)訓(xùn)練用的圖片,他由data_gen生成,當(dāng)然也可以修改代碼由其他方式生成
haarcascade_frontalface_alt.xml 、 haarcascade_frontalface_default.xml: 用于人臉檢測的haar分類器,網(wǎng)上普遍說第一個(gè)效果更好,第二個(gè)運(yùn)行速度更快
data_gen.py:生成我們所需的數(shù)據(jù)
trainer.py: 訓(xùn)練數(shù)據(jù)集
train.yml: 由train.py生成的人臉識(shí)別模型,供后面的人臉識(shí)別使用
recognize.py:視頻中的人臉識(shí)別
data_gen.py
連續(xù)拍20張照片當(dāng)作訓(xùn)練數(shù)據(jù),每個(gè)人建立一組數(shù)據(jù)
import cv2 detector = cv2.CascadeClassifier('haarcascade_frontalface_alt.xml') cap = cv2.VideoCapture(0) sampleNum = 0 Id = input('enter your id: ') while True: ret, img = cap.read() gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) faces = detector.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) # incrementing sample number sampleNum = sampleNum + 1 # saving the captured face in the dataset folder cv2.imwrite("dataSet/User." + str(Id) + '.' + str(sampleNum) + ".jpg", gray[y:y + h, x:x + w]) # cv2.imshow('frame', img) # wait for 100 miliseconds if cv2.waitKey(100) & 0xFF == ord('q'): break # break if the sample number is morethan 20 elif sampleNum > 20: break cap.release() cv2.destroyAllWindows()
train.py
訓(xùn)練數(shù)據(jù)
import cv2 import os import numpy as np from PIL import Image # recognizer = cv2.createLBPHFaceRecognizer() detector = cv2.CascadeClassifier("/Users/qiuchenglin/PycharmProjects/face_recognize/haarcascade_frontalface_alt.xml") recognizer = cv2.face.LBPHFaceRecognizer_create() def get_images_and_labels(path): image_paths = [os.path.join(path, f) for f in os.listdir(path)] face_samples = [] ids = [] for image_path in image_paths: image = Image.open(image_path).convert('L') image_np = np.array(image, 'uint8') if os.path.split(image_path)[-1].split(".")[-1] != 'jpg': continue image_id = int(os.path.split(image_path)[-1].split(".")[1]) faces = detector.detectMultiScale(image_np) for (x, y, w, h) in faces: face_samples.append(image_np[y:y + h, x:x + w]) ids.append(image_id) return face_samples, ids Faces, Ids = get_images_and_labels('dataSet') recognizer.train(Faces, np.array(Ids)) recognizer.save('trainner.yml')
recognize.py
下面就是根據(jù)訓(xùn)練好的模型進(jìn)行人臉識(shí)別,根據(jù)之前生成數(shù)據(jù)的編號(hào),可以填入相對(duì)應(yīng)的人名,例如以下示例我訓(xùn)練了三組人的數(shù)據(jù)
import cv2 import numpy as np recognizer = cv2.face.LBPHFaceRecognizer_create() # recognizer = cv2.createLBPHFaceRecognizer() # in OpenCV 2 recognizer.read('/Users/qiuchenglin/PycharmProjects/face_recognize/trainner.yml') # recognizer.load('trainner/trainner.yml') # in OpenCV 2 cascade_path = "/Users/qiuchenglin/PycharmProjects/face_recognize/haarcascade_frontalface_alt.xml" face_cascade = cv2.CascadeClassifier(cascade_path) cam = cv2.VideoCapture(0) # font = cv2.cv.InitFont(cv2.cv.CV_FONT_HERSHEY_SIMPLEX, 1, 1, 0, 1, 1) # in OpenCV 2 font = cv2.FONT_HERSHEY_SIMPLEX while True: ret, im = cam.read() gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY) faces = face_cascade.detectMultiScale(gray, 1.2, 5) for (x, y, w, h) in faces: cv2.rectangle(im, (x - 50, y - 50), (x + w + 50, y + h + 50), (225, 0, 0), 2) img_id, conf = recognizer.predict(gray[y:y + h, x:x + w]) if conf > 50: if img_id == 1: img_id = 'liuzb' elif img_id == 2: img_id = 'linqc' elif img_id == 3: img_id = 'keaibao' else: img_id = "Unknown" # cv2.cv.PutText(cv2.cv.fromarray(im), str(Id), (x, y + h), font, 255) cv2.putText(im, str(img_id), (x, y), font, 1, (0, 255, 0), 1) cv2.imshow('im', im) if cv2.waitKey(10) & 0xFF == ord('q'): break cam.release() cv2.destroyAllWindows()
簡單的一個(gè)人臉識(shí)別就完成了,只能說準(zhǔn)確率沒有非常高。
之后想辦法進(jìn)行提高。
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 手把手教你利用opencv實(shí)現(xiàn)人臉識(shí)別功能(附源碼+文檔)
- springboot集成opencv實(shí)現(xiàn)人臉識(shí)別功能的詳細(xì)步驟
- java+opencv實(shí)現(xiàn)人臉識(shí)別功能
- python opencv人臉識(shí)別考勤系統(tǒng)的完整源碼
- python基于opencv實(shí)現(xiàn)人臉識(shí)別
- python實(shí)現(xiàn)圖片,視頻人臉識(shí)別(opencv版)
- PyQt5+Caffe+Opencv搭建人臉識(shí)別登錄界面
- OpenCV + MFC實(shí)現(xiàn)簡單人臉識(shí)別
- Java OpenCV4.0.0實(shí)現(xiàn)實(shí)時(shí)人臉識(shí)別
- 基于opencv和pillow實(shí)現(xiàn)人臉識(shí)別系統(tǒng)(附demo)
相關(guān)文章
Python入門教程(四十)Python的NumPy數(shù)組創(chuàng)建
這篇文章主要介紹了Python入門教程(四十)Python的NumPy數(shù)組創(chuàng)建,NumPy 用于處理數(shù)組,NumPy 中的數(shù)組對(duì)象稱為 ndarray,我們可以使用 array() 函數(shù)創(chuàng)建一個(gè) NumPy ndarray 對(duì)象,需要的朋友可以參考下2023-05-05python神經(jīng)網(wǎng)絡(luò)Keras搭建RFBnet目標(biāo)檢測平臺(tái)
這篇文章主要為大家介紹了python神經(jīng)網(wǎng)絡(luò)Keras搭建RFBnet目標(biāo)檢測平臺(tái),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05python使用DebugInfo模塊打印一個(gè)條形堆積圖
今天介紹一個(gè)不使用 matplot,通過 DebugInfo模塊打印條形堆積圖的方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2023-08-08Pytorch實(shí)現(xiàn)將label變成one hot編碼的兩種方式
這篇文章主要介紹了Pytorch實(shí)現(xiàn)將label變成one hot編碼的兩種方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-02-02pandas 按照特定順序輸出的實(shí)現(xiàn)代碼
這篇文章主要介紹了pandas 按照特定順序輸出的實(shí)現(xiàn)代碼,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-07-07Python修改文件往指定行插入內(nèi)容的實(shí)例
今天小編就為大家分享一篇Python修改文件往指定行插入內(nèi)容的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-01-01pandas學(xué)習(xí)之df.fillna的具體使用
本文主要介紹了pandas學(xué)習(xí)之df.fillna的具體使用,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08python調(diào)用subprocess模塊實(shí)現(xiàn)命令行操作控制SVN的方法
這篇文章主要介紹了使用python的subprocess模塊實(shí)現(xiàn)對(duì)SVN的相關(guān)操作,通過設(shè)置GitSvn類,在該類下自定義執(zhí)行SVN常規(guī)操作的方法,需要的朋友跟隨小編一起看看吧2022-09-09