![[机器学习]初识人脸识别,第1张 [机器学习]初识人脸识别,第1张](/aiimages/%5B%E6%9C%BA%E5%99%A8%E5%AD%A6%E4%B9%A0%5D%E5%88%9D%E8%AF%86%E4%BA%BA%E8%84%B8%E8%AF%86%E5%88%AB.png)
- 一、安装Dlib库最简单的方法
- 二、安装opencv
- 三、实践
- 1、实时采集人脸、保存并绘制68个特征点
- 2、给人脸虚拟P上墨镜
- 参考
- 第八次作业所有作业ipynb代码及报告
下载文件,提取码:3690,将文件存放在任意你找得到的位置(我放在了D盘)
设置系统环境变量,在path中新建环境变量即你存放资源文件的位置
打开命令行窗口输入
pip install dlib-19.19.0-cp38-cp38-win_amd64.whl pip install dlib-19.19.0-cp38-cp38-win_amd64.whl
完成后,看见dlib则成功安装
pip list
值得一提的是在实验中有可报这样一个错误:
Unable to open dlib/shape_predictor_68_face_landmarks.dat --dlib.shape_predictor()
原因是缺少人脸识别检测器数据库,只需要下载相关文件,提取码:lzjy,同样存放在一个自己找得到的位置,使用时只需要引用即可,例如(我还是存放在D盘)
predictor = dlib.shape_predictor("D:\FaceRecognitionbasedOnHogSVM\shape_predictor_68_face_landmarks.dat")
二、安装opencv
opencv的安装并不负责这里不多做赘述,网上随便找个教程就行。
三、实践 1、实时采集人脸、保存并绘制68个特征点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)
ok = True
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor('D:shape_predictor_68_face_landmarksshape_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))
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()
效果不太美丽,我一定好好学,争取下次做个美丽点的
- 人脸识别68个特征点检测数据库shape_predictor_68_face_landmarks.dat
- python+OpenCv+dlib实现人脸68个关键点检测并标注
链接:https://pan.baidu.com/s/1G5P4zC0MtC7GNFhC0O0qag
提取码:3690
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)