OpenCV+Dlib+FR人脸识别和对齐

 

目录

1.装包

2.实例分析

1.人脸定位(opencv)

 2.人脸定位(face_recognition)

3.特征点检测

4.人脸对齐 


1.装包

本人是在anaconda下进行python包的安装

pip install opencv-python

接着安装Dlib,发现仅仅是pip的话无法下载。然后采用下载whl文件的办法,pip install whl文件下载成功;

最后安装face_recognition

pip install face_recognition

环境配置完成

2.实例分析

1.人脸定位(opencv)

import cv2

def detect(filename):
    face_cascade = cv2.CascadeClassifier('D:\Anaconda3\Lib\site-packages\cv2\data\haarcascade_frontalface_default.xml')

    img = cv2.imread(filename)
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

    faces = face_cascade.detectMultiScale(gray, 1.3, 5)

    for (x, y, w, h) in faces:
        img = cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2)


    cv2.imshow('Person Detected!', img)
    cv2.waitKey(0)
    cv2.destroyAllWindows()


if __name__ == '__main__':
    detect('test7.jpg')

 

 2.人脸定位(face_recognition)

import face_recognition
import cv2

image = face_recognition.load_image_file("test7.jpg")
face_locations_noCNN=face_recognition.face_locations(image)
print("face_location_noCNN:")
print(face_locations_noCNN)
face_num2=len(face_locations_noCNN)
print(face_num2)
org = cv2.imread("test7.jpg")
img = cv2.imread("test7.jpg")
cv2.imshow("test7.jpg",img)
for i in range(0,face_num2):
    top = face_locations_noCNN[i][0]
    right = face_locations_noCNN[i][1]
    bottom = face_locations_noCNN[i][2]
    left = face_locations_noCNN[i][3]

    start = (left, top)
    end = (right, bottom)

    color = (0,255,255)
    thickness = 2
    cv2.rectangle(org, start, end, color, thickness)

cv2.imshow("no cnn ",org)

cv2.waitKey(0)
cv2.destroyAllWindows()

我们可以发现,使用face_recognition准确度明显提升

3.特征点检测

#coding=utf-8

import cv2
import dlib

path = "11.jpg"
img = cv2.imread(path)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

#人脸分类器
detector = dlib.get_frontal_face_detector()
# 获取人脸检测器
predictor = dlib.shape_predictor(r"D:\Anaconda3\Lib\site-packages\shape_predictor_68_face_landmarks.dat")
dets = detector(gray, 1)
for face in dets:
    shape = predictor(img, face)  # 寻找人脸的68个标定点
    # 遍历所有点,打印出其坐标,并圈出来
    for pt in shape.parts():
        pt_pos = (pt.x, pt.y)
        cv2.circle(img, pt_pos, 2, (0, 255, 0), 1)
    cv2.imshow("image", img)

cv2.waitKey(0)
cv2.destroyAllWindows()

4.人脸对齐 

import cv2
import dlib
import sys
import numpy as np
import os

predicter_path =  'D:\Anaconda3\Lib\site-packages\shape_predictor_68_face_landmarks.dat'
face_file_path =  '3.jpg'
detector = dlib.get_frontal_face_detector()
sp = dlib.shape_predictor(predicter_path)
bgr_img = cv2.imread(face_file_path)
if bgr_img is None:
   print("Sorry, we could not load '{}' as an image".format(face_file_path))
   exit()
rgb_img = cv2.cvtColor(bgr_img, cv2.COLOR_BGR2RGB)
dets = detector(rgb_img, 1)
num_faces = len(dets)
if num_faces == 0:
   print("Sorry, there were no faces found in '{}'".format(face_file_path))
   exit()
faces = dlib.full_object_detections()
for det in dets:
   faces.append(sp(rgb_img, det))
images = dlib.get_face_chips(rgb_img, faces, size=320)
image_cnt = 0
for image in images:
    image_cnt += 1
    cv_rgb_image = np.array(image).astype(np.uint8)
    cv_bgr_image = cv2.cvtColor(cv_rgb_image, cv2.COLOR_RGB2BGR)
    cv2.imshow('%s'%(image_cnt), cv_bgr_image)

cv2.waitKey(0)
cv2.destroyAllWindows()

 

人脸识别门禁系统是一种基于人脸识别技术的智能门禁系统,其可通过对人脸进行采集、识别和比对,实现对门禁的控制和管理。本文将详细阐述基于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作为数据库系统。通过对这些技术的应用,实现了人脸采集、检测、识别和门禁控制等核心功能。该系统可以应用于各类场景的门禁控制和身份验证,具有较高的实用价值。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值