Python之人臉特征提取
- 一、安裝dlib及OPENCV
- 1. dlib安裝
- 2. 安裝opencv
- 二、繪制人臉的68個特征點
- 三、繪制黑色實心圈
- 三、總結
- 參考
一、安裝dlib及OPENCV
1. dlib安裝
由于本文使用的python版本為3.8,因此直接使用cmd安裝:
pip install dlib-19.19.0-cp38-cp38-win_amd64.whl

2. 安裝opencv
pip3 install opencv_python

二、繪制人臉的68個特征點
所需.dat檔案可在文末鏈接中找到
import numpy as np
import cv2
import dlib
import os
import sys
import random
# 存盤位置
output_dir = './faces'
size = 64
if 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 = 255
elif tmp < 0:
tmp = 0
img[j,i,c] = tmp
return img
#使用dlib自帶的frontal_face_detector作為我們的特征提取器
detector = dlib.get_frontal_face_detector()
# 打開攝像頭 引數為輸入流,可以為攝像頭或視頻檔案
camera = cv2.VideoCapture(0)
#camera = cv2.VideoCapture('C:/Users/CUNGU/Videos/Captures/wang.mp4')
ok = True
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor('shape_predictor_68_face_landmarks.dat')
while ok:
# 讀取攝像頭中的影像,ok為是否讀取成功的判斷引數
ok, img = camera.read()
# 轉換成灰度影像
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-68
font = cv2.FONT_HERSHEY_SIMPLEX
cv2.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 quit
break
camera.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=0
right_eye_y=0
left_eye_x=0
left_eye_y=0
for 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))
"""
利用circle函式畫圓
函式原型
cv2.circle(img, center, radius, color[, thickness[, lineType[, shift]]])
img:輸入的圖片data
center:圓心位置
radius:圓的半徑
color:圓的顏色
thickness:圓形輪廓的粗細(如果為正),負厚度表示要繪制實心圓,
lineType: 圓邊界的型別,
shift:中心坐標和半徑值中的小數位數,
"""
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)#打開攝像頭
ok=True
# 打開攝像頭 引數為輸入流,可以為攝像頭或視頻檔案
while ok:
ok,img = camera.read()
# 轉換成灰度影像
img_gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
#display_feature_point(img,detector,predictor)
painting_sunglasses(img,detector,predictor)#呼叫畫墨鏡函式
cv2.imshow('video', img)
k = cv2.waitKey(1)
if k == 27: # press 'ESC' to quit
break
camera.release()
cv2.destroyAllWindows()
三、總結
dlib是人臉識別常用的一個庫,使用起來十分方便,
參考
python3+opencv3.4+dlib庫編程實作人臉特征點標定
人臉特征提取(dlib+opencv3.4+python3.8)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/347235.html
標籤:其他
