一、OpenCV、dlib安装
环境:win10
1.使用命令安装OpenCV
pip install opencv_python
2.在终端查看Python的版本,根据对应版本下载dlib文件
输入命令
python
下载地址:http://dlib.net/files/
注意:还要下载页面最下方的Dlib预测器
输入命令安装dlib
pip install D:\aq\dlib-19.19.0-cp38-cp38-win_amd64.whl
二、打开摄像头,实时采集人脸并保存、绘制68个特征点
"""
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/86199/tvcamera'
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]
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
detector = dlib.get_frontal_face_detector()
camera = cv2.VideoCapture(0)
ok = True
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor('shape_predictor_68_face_landmarks.dat')
while 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):
pos = (point[0, 0], point[0, 1])
print(idx,pos)
cv2.circle(img, pos, 2, color=(0, 255, 0))
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:
break
camera.release()
cv2.destroyAllWindows()
三、人脸虚拟P上一付墨镜
首先导入代码需要用到的包
import numpy as np
import cv2
import dlib
import os
import sys
import random
添加函数,获得默认的人脸检测器和训练好的68特征点检测器
def get_detector_and_predicyor():
detector = dlib.get_frontal_face_detector()
"""
功能:人脸检测画框
参数:PythonFunction和in Classes
in classes表示采样次数,次数越多获取的人脸的次数越多,但更容易框错
返回值是矩形的坐标,每个矩形为一个人脸(默认的人脸检测器)
"""
predictor = dlib.shape_predictor('..\\source\\shape_predictor_68_face_landmarks.dat')
return detector,predictor
detector,predictor=get_detector_and_predicyor()
添加给眼睛画圆的函数,这个就是找到眼睛周围的特征点,然后确认中心点,后面用cirecle函数画出来
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)
painting_sunglasses(img,detector,predictor)
cv2.imshow('video', img)
k = cv2.waitKey(1)
if k == 27:
break
camera.release()
cv2.destroyAllWindows()
四、参考文献
https://blog.csdn.net/weixin_56102526/article/details/121119472 https://blog.csdn.net/qq_46689721/article/details/121273562
|