用Python做一个人脸识别系统,简单操作又实用~

导语

å¾ç

今天给大家介绍一个非常简洁的人脸识别系统:

 人脸识别,是基于人的脸部特征信息进行身份识别的一种生物识别技术。而通过我们Python编程,几行代码就可以实现人脸识别,这主要得益于face_recognition库。

想领取完整源码跟Python学习资料私信我或点击这行字体即可

 一、安装过程

face_recognition 库主要封装了dlib这一 C++ 图形库,通过 Python 语言将它封装为一个非常简单就可以实现人脸识别的 API 库,屏蔽了人脸识别的算法细节,大大降低了人脸识别功能的开发难度。

图片

小编是基于Windows环境下的Python3.6.4进行安装,在查阅相当多贴文后发现这样的成功率更高哦!

这篇文章详细介绍了face_recognition库的安装,感兴趣的同学们可以跟着这篇文章安装face_recognition库哦!过程需要一点耐心,大家加油!https://blog.csdn.net/advancezhang/article/details/100018893

这里有截图哦!

图片

图片

二、基本代码    

1、识别人脸并将其框出

# 检测人脸

import face_recognition

imp
  • 42
    点赞
  • 366
    收藏
    觉得还不错? 一键收藏
  • 9
    评论
好的,以下是一个简单人脸识别系统操作界面示例,使用Python和Tkinter GUI库: ```python import tkinter as tk from tkinter import filedialog from tkinter import messagebox import cv2 import numpy as np class FaceRecognitionApp: def __init__(self, master): self.master = master self.master.title("人脸识别系统") self.master.geometry("400x300") self.master.resizable(False, False) # 创建控件 self.btn_open_image = tk.Button(self.master, text="打开图像", command=self.open_image) self.btn_detect_face = tk.Button(self.master, text="检测人", command=self.detect_face) self.btn_train_model = tk.Button(self.master, text="训练模型", command=self.train_model) self.btn_recognize_face = tk.Button(self.master, text="识别", command=self.recognize_face) self.lbl_image = tk.Label(self.master) self.lbl_status = tk.Label(self.master, text="等待操作...") # 布局控件 self.btn_open_image.pack(pady=10) self.btn_detect_face.pack(pady=10) self.btn_train_model.pack(pady=10) self.btn_recognize_face.pack(pady=10) self.lbl_image.pack(pady=10) self.lbl_status.pack(pady=10) # 初始化变量 self.image_file = None self.image = None self.detector = cv2.CascadeClassifier("haarcascade_frontalface_default.xml") self.recognizer = cv2.face.LBPHFaceRecognizer_create() def open_image(self): # 打开图像文件 self.image_file = filedialog.askopenfilename(filetypes=[("图像文件", "*.jpg;*.jpeg;*.png")]) if self.image_file: # 显示图像 self.image = cv2.imread(self.image_file) self.show_image(self.image) def detect_face(self): if self.image is None: messagebox.showerror("错误", "请先打开图像!") return # 检测人 gray = cv2.cvtColor(self.image, cv2.COLOR_BGR2GRAY) faces = self.detector.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30)) if len(faces) == 0: messagebox.showwarning("警告", "未检测到人!") return # 画出人框 for (x, y, w, h) in faces: cv2.rectangle(self.image, (x, y), (x + w, y + h), (0, 255, 0), 2) # 显示检测结果 self.show_image(self.image) self.lbl_status.config(text="检测到 %d 个人" % len(faces)) def train_model(self): # TODO: 实现训练模型的功能 pass def recognize_face(self): if self.image is None: messagebox.showerror("错误", "请先打开图像!") return if not self.recognizer: messagebox.showerror("错误", "请先训练模型!") return # TODO: 实现识别的功能 pass def show_image(self, image): # 将OpenCV图像格式转换为Tkinter图像格式 image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) image = np.swapaxes(image, 0, 1) image = tk.PhotoImage(master=self.master, data=image.tostring(), width=image.shape[1], height=image.shape[0]) # 显示图像 self.lbl_image.config(image=image) self.lbl_image.image = image def run(self): self.master.mainloop() if __name__ == "__main__": root = tk.Tk() app = FaceRecognitionApp(root) app.run() ``` 这个界面包含四个按钮和两个标签: - 打开图像按钮:用于打开本地的图像文件。 - 检测人按钮:用于在打开的图像中检测人。 - 训练模型按钮:用于训练人脸识别模型。 - 识别按钮:用于在打开的图像中识别。 - 图像标签:用于显示打开的图像和检测或识别的结果。 - 状态标签:用于显示当前操作的状态信息。 你需要根据自己的需求进一步修改和完善这个界面代码,比如实现训练和识别功能、添加菜单栏和工具栏、美化界面等。
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值