python+opencv+dlib实现人脸识别

一、win10安装opencv和dlib

1.使用命令查看当前python版本为3.8

python

在这里插入图片描述

2.使用命令安装opencv

 pip3 install opencv_python

3.搜索对应版本的dlib文件下载好后用命令在适合的位置进行安装

python3.8的链接:https://pan.baidu.com/s/1kLn0uEqO5xinuTMZzk3fFA
提取码:kh99
python3.7的链接:https://pan.baidu.com/s/14cxfDkC2dODyncLAZ3bwaQ
提取码:w8hp

 pip install dlib-19.21.99-cp38-cp38-win_amd64.whl

二、打开摄像头,实时采集人脸并保存、绘制68个特征点

# -*- 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/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]
    #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()

在这里插入图片描述

三、人脸虚拟P上一付墨镜

#具体啥玩意的你就点击运行就行了。d键是开始,c键是替换照片,q键是结束。这代码一看就会。
import dlib
from PIL import Image, ImageDraw, ImageFont
import random

import cv2

from imutils.video import VideoStream
from imutils import face_utils, translate, rotate, resize

import numpy as np

vs = VideoStream().start()

detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor('shape_predictor_68_face_landmarks.dat')

max_width = 500
frame = vs.read()
frame = resize(frame, width=max_width)

fps = vs.stream.get(cv2.CAP_PROP_FPS) # need this for animating proper duration

animation_length = fps * 5
current_animation = 0
glasses_on = fps * 3

# uncomment for fullscreen, remember 'q' to quit
# cv2.namedWindow('deal generator', cv2.WND_PROP_FULLSCREEN)
#cv2.setWindowProperty('deal generator', cv2.WND_PROP_FULLSCREEN,
#                          cv2.WINDOW_FULLSCREEN)

deal = Image.open("C:/Users/86199/tvcamera/glasses.png")
text = Image.open('C:/Users/86199/tvcamera/xz.jpg')

dealing = False
number =0
while True:
    frame = vs.read()
    frame = resize(frame, width=max_width)
    img_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    faces = []

    rects = detector(img_gray, 0)
    img = Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
    # print(rects)
    for rect in rects:
        face = {}
        shades_width = rect.right() - rect.left()

        # predictor used to detect orientation in place where current face is
        shape = predictor(img_gray, rect)
        shape = face_utils.shape_to_np(shape)

        # grab the outlines of each eye from the input image
        leftEye = shape[36:42]
        rightEye = shape[42:48]

        # compute the center of mass for each eye
        leftEyeCenter = leftEye.mean(axis=0).astype("int")
        rightEyeCenter = rightEye.mean(axis=0).astype("int")

	    # compute the angle between the eye centroids
        dY = leftEyeCenter[1] - rightEyeCenter[1]
        dX = leftEyeCenter[0] - rightEyeCenter[0]
        angle = np.rad2deg(np.arctan2(dY, dX)) 
        # print((shades_width, int(shades_width * deal.size[1] / deal.size[0])))
        # 图片重写
        current_deal = deal.resize((shades_width, int(shades_width * deal.size[1] / deal.size[0])),
                               resample=Image.LANCZOS)
        current_deal = current_deal.rotate(angle, expand=True)
        current_deal = current_deal.transpose(Image.FLIP_TOP_BOTTOM)

        face['glasses_image'] = current_deal
        left_eye_x = leftEye[0,0] - shades_width // 4
        left_eye_y = leftEye[0,1] - shades_width // 6
        face['final_pos'] = (left_eye_x, left_eye_y)

        # I got lazy, didn't want to bother with transparent pngs in opencv
        # this is probably slower than it should be
        # 图片动画以及配置
        if dealing:
            # print("current_y",int(current_animation / glasses_on * left_eye_y))
            if current_animation < glasses_on:
                current_y = int(current_animation / glasses_on * left_eye_y)
                img.paste(current_deal, (left_eye_x, current_y-20), current_deal)
            else:
                img.paste(current_deal, (left_eye_x, left_eye_y-20), current_deal)
                # img.paste(text, (75, img.height // 2 - 52), text)

    # 起初动画配置
    if dealing:
        current_animation += 1
        frame = cv2.cvtColor(np.asarray(img), cv2.COLOR_RGB2BGR)
    # 按键选择
    cv2.imshow("deal generator", frame)
    key = cv2.waitKey(1) & 0xFF
    #退出程序
    if key == ord("q"):
        break
    # 开始程序
    if key == ord("d"):
        dealing = not dealing
    # 图片切换
    if key == ord("c"):
        # 让图片从上面重新开始
        # current_animation = 0

        number = str(random.randint(0, 8))
        print(number)
        deal = Image.open("'C:/Users/86199/tvcamera/'"+number+".png")
cv2.destroyAllWindows()
vs.stop()

在这里插入图片描述

四、总结

人体面貌的识别过程三步,首先建立人体面貌的面像档案。即用摄像机采集单位人员的人体面貌的面像文件或取他们的照片形成面像文件,并将这些面像文件生成面纹(Faceprint)编码贮存起来。然后获取当前的人体面像 ,即用摄像机捕捉的当前出入人员的面像,或取照片输入,并将当前的面像文件生成面纹编码。醉后用当前的面纹编码与档案库存的比对, 即将当前的面像的面纹编码与档案库存中的面纹编码进行检索比对。上述的“面纹编码”方式是根据人体面貌脸部的本质特征和开头来工作的。这种面纹编码可以抵抗光线、皮肤色调、面部毛发、发型、眼镜、表情和姿态的变化,具有强大的可靠性,从而使它可以从百万人中精确地辩认出某个人。

五、参考资料

python3.8安装dlib dlib-19.19.0-cp38-cp38-win_amd64.whl.whl
基于Python+OpenCV的人脸识别实现带墨镜效果

  • 4
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
人脸识别门禁系统是一种基于人脸识别技术的智能门禁系统,其可通过对人脸进行采集、识别和比对,实现对门禁的控制和管理。本文将详细阐述基于python+openCV+dlib+mysql的人脸识别门禁系统的设计与实现。 一、技术选型 本系统主要采用以下技术: 1. Python:作为主要编程语言,用于实现整个系统的逻辑控制和算法设计。 2. OpenCV:作为图像处理库,用于实现人脸检测、特征提取和人脸识别等核心功能。 3. Dlib:作为人脸识别库,用于实现人脸特征点检测和人脸识别等功能。 4. MySQL:作为数据库系统,用于存储人脸特征和相关信息。 二、系统设计 本系统主要包括以下功能模块: 1. 人脸采集模块:用于采集用户的人脸图像,并将其存储到本地或远程数据库中。 2. 人脸检测模块:用于检测人脸区域,提取人脸特征,并将其存储到数据库中。 3. 人脸识别模块:用于识别用户的人脸特征,并与数据库中的人脸特征进行比对,以确定用户身份。 4. 门禁控制模块:根据用户身份结果,控制门禁的开关。 5. 数据库管理模块:用于管理数据库中的人脸特征和相关信息。 三、系统实现 1. 人脸采集模块 人脸采集模块主要是通过摄像头对用户的人脸进行拍摄和保存。代码如下: ```python import cv2 cap = cv2.VideoCapture(0) while True: ret, frame = cap.read() cv2.imshow("capture", frame) if cv2.waitKey(1) & 0xFF == ord('q'): #按q键退出 cv2.imwrite("face.jpg", frame) #保存人脸图像 break cap.release() cv2.destroyAllWindows() ``` 2. 人脸检测模块 人脸检测模块主要是通过OpenCV中的CascadeClassifier类进行人脸检测,再通过Dlib中的shape_predictor类进行人脸特征点检测和特征提取。代码如下: ```python import cv2 import dlib detector = dlib.get_frontal_face_detector() #人脸检测器 predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat") #特征点检测器 img = cv2.imread("face.jpg") #读取人脸图像 gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) #转换为灰度图像 faces = detector(gray, 0) #检测人脸 for face in faces: landmarks = predictor(gray, face) #检测特征点 for n in range(68): x = landmarks.part(n).x y = landmarks.part(n).y cv2.circle(img, (x, y), 2, (0, 255, 0), -1) #绘制特征点 cv2.imshow("face", img) cv2.waitKey(0) cv2.destroyAllWindows() ``` 3. 人脸识别模块 人脸识别模块主要是通过Dlib中的face_recognition类进行人脸特征提取和比对。代码如下: ```python import face_recognition known_image = face_recognition.load_image_file("known_face.jpg") #读取已知的人脸图像 unknown_image = face_recognition.load_image_file("unknown_face.jpg") #读取待识别的人脸图像 known_encoding = face_recognition.face_encodings(known_image)[0] #提取已知人脸的特征 unknown_encoding = face_recognition.face_encodings(unknown_image)[0] #提取待识别人脸的特征 results = face_recognition.compare_faces([known_encoding], unknown_encoding) #比对人脸特征 if results[0]: print("Match") else: print("No match") ``` 4. 门禁控制模块 门禁控制模块主要是通过GPIO控制门禁的开关。代码如下: ```python import RPi.GPIO as GPIO import time GPIO.setmode(GPIO.BOARD) GPIO.setup(11, GPIO.OUT) GPIO.output(11, GPIO.HIGH) #开门 time.sleep(5) #等待5秒 GPIO.output(11, GPIO.LOW) #关门 GPIO.cleanup() #清理GPIO资源 ``` 5. 数据库管理模块 数据库管理模块主要是通过MySQLdb模块实现对MySQL数据库的连接和操作,包括新建数据库、新建表、插入数据、查询数据等。代码如下: ```python import MySQLdb #连接数据库 conn = MySQLdb.connect(host="localhost", user="root", passwd="123456", db="test", charset="utf8") #新建表 cursor = conn.cursor() sql = "CREATE TABLE `face` (`id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(50) NOT NULL, `encoding` text NOT NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8;" cursor.execute(sql) #插入数据 name = "张三" encoding = "0.1,0.2,0.3,0.4" sql = "INSERT INTO `face` (`name`, `encoding`) VALUES (%s, %s)" cursor.execute(sql, (name, encoding)) conn.commit() #查询数据 sql = "SELECT * FROM `face` WHERE `name`=%s" cursor.execute(sql, (name,)) result = cursor.fetchone() print(result) cursor.close() conn.close() ``` 四、总结 本文主要介绍了基于python+openCV+dlib+mysql的人脸识别门禁系统的设计与实现。该系统主要采用了Python作为主要编程语言,OpenCVDlib作为图像处理和人脸识别库,MySQL作为数据库系统。通过对这些技术的应用,实现人脸采集、检测、识别和门禁控制等核心功能。该系统可以应用于各类场景的门禁控制和身份验证,具有较高的实用价值。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值