【人脸识别】基于dlib库实现人脸特征值提取
生活随笔
收集整理的這篇文章主要介紹了
【人脸识别】基于dlib库实现人脸特征值提取
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
一、Dlib庫介紹與安裝
1. Dlib庫簡介
?Dlib庫是一個機器學(xué)習(xí)的開源庫,包含了機器學(xué)習(xí)的很多算法,使用起來很方便,直接包含頭文件即可,并且不依賴于其他庫(自帶圖像編解碼庫源碼)。Dlib可以幫助創(chuàng)建很多復(fù)雜的機器學(xué)習(xí)方面的軟件來幫助解決實際問題。目前Dlib已經(jīng)被廣泛的用在行業(yè)和學(xué)術(shù)領(lǐng)域,包括機器人,嵌入式設(shè)備,移動電話和大型高性能計算環(huán)境。
?
2. 安裝Dlib庫
pip install dlib二、OpenCV介紹即安裝
1. OpenCV簡介
OpenCV是一個基于BSD許可(開源)發(fā)行的跨平臺計算機視覺和機器學(xué)習(xí)軟件庫,可以運行在Linux、Windows、Android和Mac OS操作系統(tǒng)上。它輕量級而且高效——由一系列 C 函數(shù)和少量 C++ 類構(gòu)成,同時提供了Python、Ruby、MATLAB等語言的接口,實現(xiàn)了圖像處理和計算機視覺方面的很多通用算法。
?
2. OpenCV安裝
pip3 install opencv_python三、提取人臉特征點
此處使用視頻測試
# -*- coding: utf-8 -*- """ Created on Wed Oct 27 03:15:10 2021@author: GT72VR """ import numpy as np import cv2 import dlib import os import sys import random # 存儲位置 output_dir = 'C:/Users/王青松/' size = 64if not os.path.exists(output_dir):os.makedirs(output_dir) # 改變圖片的亮度與對比度def relight(img, light=1, bias=0):w = img.shape[1]h = img.shape[0]#image = []for i in range(0,w):for j in range(0,h):for c in range(3):tmp = int(img[j,i,c]*light + bias)if tmp > 255:tmp = 255elif tmp < 0:tmp = 0img[j,i,c] = tmpreturn img#使用dlib自帶的frontal_face_detector作為我們的特征提取器 detector = dlib.get_frontal_face_detector() # 打開攝像頭 參數(shù)為輸入流,可以為攝像頭或視頻文件 camera = cv2.VideoCapture('D:/wudipuls.mp4') #camera = cv2.VideoCapture(?0) ok = Truedetector = dlib.get_frontal_face_detector() predictor = dlib.shape_predictor('shape_predictor_68_face_landmarks.dat')while ok:# 讀取攝像頭中的圖像,ok為是否讀取成功的判斷參數(shù)ok, img = camera.read()# 轉(zhuǎn)換成灰度圖像img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)rects = detector(img_gray, 0)for i in range(len(rects)):landmarks = np.matrix([[p.x, p.y] for p in predictor(img,rects[i]).parts()])for idx, point in enumerate(landmarks):# 68點的坐標pos = (point[0, 0], point[0, 1])print(idx,pos)# 利用cv2.circle給每個特征點畫一個圈,共68個cv2.circle(img, pos, 2, color=(0, 255, 0))# 利用cv2.putText輸出1-68font = cv2.FONT_HERSHEY_SIMPLEXcv2.putText(img, str(idx+1), pos, font, 0.2, (0, 0, 255), 1,cv2.LINE_AA)cv2.imshow('video', img)k = cv2.waitKey(1)if k == 27: # press 'ESC' to quitbreakcamera.release() cv2.destroyAllWindows()?
四、在眼睛處繪制黑色的實心圓
def painting_sunglasses(img,detector,predictor): #給人臉帶上墨鏡rects = detector(img_gray, 0) for i in range(len(rects)):landmarks = np.matrix([[p.x, p.y] for p in predictor(img,rects[i]).parts()])right_eye_x=0right_eye_y=0left_eye_x=0left_eye_y=0for i in range(36,42):#右眼范圍#將坐標相加right_eye_x+=landmarks[i][0,0]right_eye_y+=landmarks[i][0,1]#取眼睛的中點坐標pos_right=(int(right_eye_x/6),int(right_eye_y/6))cv2.circle(img=img, center=pos_right, radius=30, color=(0,0,0),thickness=-1)for i in range(42,48):#左眼范圍#將坐標相加left_eye_x+=landmarks[i][0,0]left_eye_y+=landmarks[i][0,1]#取眼睛的中點坐標pos_left=(int(left_eye_x/6),int(left_eye_y/6))cv2.circle(img=img, center=pos_left, radius=30, color=(0,0,0),thickness=-1)#camera = cv2.VideoCapture(0)#打開攝像頭 camera = cv2.VideoCapture('D:/wudipuls.mp4') ok=True # 打開攝像頭 參數(shù)為輸入流,可以為攝像頭或視頻文件 while ok:ok,img = camera.read()# 轉(zhuǎn)換成灰度圖像img_gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)#display_feature_point(img,detector,predictor)painting_sunglasses(img,detector,predictor)#調(diào)用畫墨鏡函數(shù)cv2.imshow('video', img)k = cv2.waitKey(1)if k == 27: # press 'ESC' to quitbreak camera.release() cv2.destroyAllWindows()五、總結(jié)?
本文簡略地介紹了dlib庫與OpenCV,并初步嘗試使用dlib庫對人臉的特征值進行提取。
六、參考
【人臉識別】基于dlib庫實現(xiàn)人臉特征值提取_YangMax1的博客-CSDN博客
總結(jié)
以上是生活随笔為你收集整理的【人脸识别】基于dlib库实现人脸特征值提取的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: .NET 4.0安装提示一般信任关系失败
- 下一篇: [案例4-2]饲养员喂养动物