当前位置: 首页 > news >正文

免费生成手机网站友情链接是什么意思

免费生成手机网站,友情链接是什么意思,网上购物哪个平台质量好,广州百度网络推广目录 零、前言 一、人脸检测 二、人脸识别 1、采集人脸 2、训练人脸识别模型 3、人脸识别应用 零、前言 随着智能安防需求的增长,基于人工智能和物联网的安保系统逐渐成为趋势。树莓派因其低成本、高扩展性等特点,成为很多AI项目的理想平台。本文将为大…

目录

零、前言

一、人脸检测

 二、人脸识别

1、采集人脸

 2、训练人脸识别模型

 3、人脸识别应用

零、前言

        随着智能安防需求的增长,基于人工智能和物联网的安保系统逐渐成为趋势。树莓派因其低成本、高扩展性等特点,成为很多AI项目的理想平台。本文将为大家介绍如何使用树莓派打造一款智能安保巡逻机器人。本篇是系列的第一部分,将聚焦于“快速人脸录入与精准人脸识别”的实现步骤。

        本篇文章旨在通过搭建基于树莓派的安保巡逻机器人,实现人脸录入和识别功能。巡逻机器人将通过摄像头捕捉人脸信息,进行实时识别和数据存储,以实现自动化安保监控

树莓派5B作为一款小巧、功能强大的计算机设备,为快速人脸录入和精准识别提供了一个理想的硬件平台。本文将通过详细的代码示例,讲解如何利用树莓派5B与Python实现一个高效的人脸检测和识别系统。该系统可以实时捕捉人脸信息,进行边缘检测与处理,适用于智能安防、出入控制等多种场景。

一、人脸检测

由于不同需求,首先我们先进行一下人脸检测功能

我们将利用Python的Mediapipe和OpenCV库来完成图像处理,Picamera2库驱动树莓派的摄像头。(Mediapipe库是Google开源的多媒体处理框架,适用于多种机器学习任务;而OpenCV则是一个强大的计算机视觉库,广泛应用于图像处理、模式识别等领域。)以下是主要代码片段及其实现细节。

#!/usr/bin/env python3
# encoding: utf-8import mediapipe as mp
import cv2 as cv
from picamera2 import Picamera2

为了简化人脸检测的实现,我们定义了一个FaceDetector类。该类使用Mediapipe的人脸检测模块,并在初始化时设置最小检测置信度参数minDetectionCon,以控制检测的灵敏度。

class FaceDetector:def __init__(self, minDetectionCon=0.5):# 初始化人脸检测模块,并设置最小检测置信度self.mpFaceDetection = mp.solutions.face_detectionself.facedetection = self.mpFaceDetection.FaceDetection(min_detection_confidence=minDetectionCon)
  • findFaces方法中,首先将捕获的图像从BGR格式转换为RGB格式。这样做是因为Mediapipe库使用的是RGB格式的图像输入。随后,调用self.facedetection.process(img_RGB)来检测人脸。

    def findFaces(self, frame):# 将图像从BGR转换为RGB,因为MediaPipe使用的是RGB格式img_RGB = cv.cvtColor(frame, cv.COLOR_BGR2RGB)# 处理图像,检测人脸results = self.facedetection.process(img_RGB)# 如果检测到人脸,则在图像上绘制矩形框if results.detections:for detection in results.detections:# 获取人脸的边界框相对坐标bboxC = detection.location_data.relative_bounding_boxih, iw, _ = frame.shape  # 获取图像的高度和宽度# 将相对坐标转换为绝对坐标bbox = int(bboxC.xmin * iw), int(bboxC.ymin * ih), \int(bboxC.width * iw), int(bboxC.height * ih)# 在图像上绘制人脸矩形框cv.rectangle(frame, (bbox[0], bbox[1]), (bbox[0] + bbox[2], bbox[1] + bbox[3]), (255, 0, 255), 2)return frame
    

    在主程序中,我们通过Picamera2捕获视频流,将YUYV格式的图像转换为BGR格式,使用findFaces方法来检测人脸,并将检测到的人脸信息实时显示在窗口中

    if __name__ == '__main__':picam2 = Picamera2()config = picam2.create_preview_configuration(main={"format": 'YUYV', "size": (320, 240)})picam2.configure(config)picam2.start()face_detector = FaceDetector(0.75)while True:frame = picam2.capture_array()# 将YUYV格式的图像转换为BGRframe = cv.cvtColor(frame, cv.COLOR_YUV2BGR_YUYV)# 检测人脸并水平翻转图像frame = face_detector.findFaces(cv.flip(frame, 1))cv.imshow('frame', frame) if cv.waitKey(1) & 0xFF == ord('q'): breakcv.destroyAllWindows()
    

完整代码如下:

#!/usr/bin/env python3
# encoding: utf-8
import mediapipe as mp
import cv2 as cv
from picamera2 import Picamera2class FaceDetector:def __init__(self, minDetectionCon=0.5):# 初始化人脸检测模块,并设置最小检测置信度self.mpFaceDetection = mp.solutions.face_detectionself.facedetection = self.mpFaceDetection.FaceDetection(min_detection_confidence=minDetectionCon)def findFaces(self, frame):# 将图像从BGR转换为RGB,因为MediaPipe使用的是RGB格式img_RGB = cv.cvtColor(frame, cv.COLOR_BGR2RGB)# 处理图像,检测人脸results = self.facedetection.process(img_RGB)# 如果检测到人脸,则在图像上绘制矩形框if results.detections:for detection in results.detections:# 获取人脸的边界框相对坐标bboxC = detection.location_data.relative_bounding_boxih, iw, _ = frame.shape  # 获取图像的高度和宽度# 将相对坐标转换为绝对坐标bbox = int(bboxC.xmin * iw), int(bboxC.ymin * ih), \int(bboxC.width * iw), int(bboxC.height * ih)# 在图像上绘制人脸矩形框cv.rectangle(frame, (bbox[0], bbox[1]), (bbox[0] + bbox[2], bbox[1] + bbox[3]), (255, 0, 255), 2)return frameif __name__ == '__main__':picam2 = Picamera2()config = picam2.create_preview_configuration(main={"format": 'YUYV', "size": (320, 240)})picam2.configure(config)picam2.start()face_detector = FaceDetector(0.75)while True:frame = picam2.capture_array()# 将YUYV格式的图像转换为BGRframe = cv.cvtColor(frame, cv.COLOR_YUV2BGR_YUYV)# 检测人脸并水平翻转图像frame = face_detector.findFaces(cv.flip(frame, 1))cv.imshow('frame', frame) if cv.waitKey(1) & 0xFF == ord('q'): breakcv.destroyAllWindows()

 二、人脸识别

1、采集人脸

在实现人脸识别前,我们需要采集用户的人脸图像样本,建立一个基本的人脸数据库。为确保系统的高效和准确性,需采集多张人脸图像以便后续的人脸识别模型可以更好地学习每个用户的特征。

在本部分中,我们将利用OpenCV库采集人脸样本,并将这些图像存储在本地文件夹中。代码如下:

#利用opencv采集人脸(拍照)
import cv2
import os
import time
# 初始化摄像头
cam = cv2.VideoCapture(0)
cam.set(3, 640) # 设置视频宽度为640像素
cam.set(4, 480) # 设置视频高度为480像素# 加载人脸检测的分类器
face_detector = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')# 输入用户ID,用于标识不同的用户
face_id = input('\n 输入用户ID并按回车 ==> ')print("\n [信息] 初始化人脸采集。看向摄像头等待...")
# 初始化单独采样人脸计数
count = 0while(True):# 读取摄像头的一帧图像ret, img = cam.read()# 将图像上下翻转img = cv2.flip(img, 1) # 垂直翻转视频图像# 将图像转换为灰度图,以提高处理速度gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)# 检测图像中的人脸faces = face_detector.detectMultiScale(gray, 1.3, 5)# 对于检测到的每一个人脸for (x,y,w,h) in faces:# 在图像上绘制矩形框cv2.rectangle(img, (x,y), (x+w,y+h), (255,0,0), 2)     # 增加人脸计数count += 1# 将捕捉到的人脸保存到datasets文件夹中cv2.imwrite("dataset/User." + str(face_id) + '.' + str(count) + ".jpg", gray[y:y+h,x:x+w])# 显示处理后的图像cv2.imshow('image', img)print("ok")time.sleep(0.2)# 按ESC退出视频k = cv2.waitKey(100) & 0xff if k == 27:break# 如果采集了30个样本,则停止视频elif count >= 30: break# 清理工作
print("\n [信息] 退出程序并清理资源")
cam.release() # 释放摄像头
cv2.destroyAllWindows() # 关闭所有OpenCV窗口

运行代码,输入用户ID:

按下回车进行人脸信息采集:

 保存数据如下:

 2、训练人脸识别模型

在完成了人脸图像的采集之后,接下来我们需要对这些图像进行训练,以便系统能够识别不同的用户。我们将使用OpenCV的LBPH(局部二值模式直方图)人脸识别算法进行训练。

LBPH (Local Binary Patterns Histogram) 是一种常用于人脸识别的特征提取方法,主要基于局部纹理信息来描述图像特征。它通过分析图像局部区域内像素的灰度关系,提取出具有高度差异性的特征,从而能够在光照、表情变化等方面取得较为鲁棒的识别效果。LBPH 是一种经典且有效的人脸识别方法。

以下是训练模型的代码实现:

import cv2
import numpy as np
from PIL import Image
import os# 人脸图像数据库的路径
path = 'dataset'# 创建LBPH人脸识别器
recognizer = cv2.face.LBPHFaceRecognizer_create()
# 使用Haar特征分类器进行人脸检测
detector = cv2.CascadeClassifier("haarcascade_frontalface_default.xml");# 函数:获取图像和标签数据
def getImagesAndLabels(path):# 获取数据库中所有图像的路径imagePaths = [os.path.join(path, f) for f in os.listdir(path)]     faceSamples = []  # 存储人脸样本ids = []  # 存储每张人脸的IDfor imagePath in imagePaths:# 打开图像并将其转换为灰度图PIL_img = Image.open(imagePath).convert('L')# 将灰度图转换为numpy数组img_numpy = np.array(PIL_img, 'uint8')# 获取图像文件名中的ID,假设文件名格式为:User.ID.xxx.jpgid = int(os.path.split(imagePath)[-1].split(".")[1])# 检测人脸位置faces = detector.detectMultiScale(img_numpy)# 遍历检测到的人脸区域,将每个区域保存到样本和标签列表中for (x, y, w, h) in faces:faceSamples.append(img_numpy[y:y+h, x:x+w])ids.append(id)return faceSamples, idsprint ("\n [信息] 训练人脸中,请稍候...")
# 获取所有人脸样本和对应的ID
faces, ids = getImagesAndLabels(path)
# 训练LBPH人脸识别器
recognizer.train(faces, np.array(ids))# 将训练好的模型保存到trainer/trainer.yml
recognizer.write('trainer/trainer.yml')  # recognizer.save()在Mac上可用,但在Pi上不可用# 输出训练的人脸数量并结束程序
print("\n [信息] 训练了 {0} 张人脸。程序结束".format(len(np.unique(ids))))

运行代码进行训练,结果将得到一个trainer.yml文件,此文件保存着用户对应的LBPH相关数值

 3、人脸识别应用

在成功训练完人脸识别模型后,我们将进入实际的识别环节。接下来,我们会使用摄像头实时捕捉视频流,并通过模型识别出画面中的人脸。

以下是实时人脸识别的代码实现:

import cv2
import numpy as np
import os
import time# 创建LBPH人脸识别器并加载训练好的模型
recognizer = cv2.face.LBPHFaceRecognizer_create()
recognizer.read('trainer/trainer.yml')# 加载Haar特征分类器用于人脸检测
cascadePath = "haarcascade_frontalface_default.xml"
faceCascade = cv2.CascadeClassifier(cascadePath)font = cv2.FONT_HERSHEY_SIMPLEX# 初始化ID计数器
id = 0# ID与姓名的对应关系
names = ['None', 'ID=1', 'ID=2', 'ID=3', 'Z', 'W']# 初始化并开始实时视频捕捉
cam = cv2.VideoCapture(0)
cam.set(3, 640)  # 设置视频宽度
cam.set(4, 480)  # 设置视频高度# 定义识别为人脸的最小窗口大小
minW = 0.1 * cam.get(3)
minH = 0.1 * cam.get(4)
frame_count, pTime, cTime = 0, 0, 0 while True:ret, img = cam.read()  # 从摄像头读取图像img = cv2.flip(img, 1)  # 垂直翻转图像gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)  # 转换为灰度图# 检测人脸faces = faceCascade.detectMultiScale(gray,scaleFactor=1.2,minNeighbors=5,minSize=(int(minW), int(minH)),)# 遍历检测到的人脸for (x, y, w, h) in faces:cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2)  # 绘制人脸框id, confidence = recognizer.predict(gray[y:y + h, x:x + w])  # 识别人脸if confidence < 70:  # 置信度小于70id = names[id]  # 获取对应的姓名confidence = "  {0}%".format(round(100 - confidence))  # 计算置信度else:id = "unknown"  # 识别为未知confidence = "  {0}%".format(round(100 - confidence))# 显示姓名和置信度cv2.putText(img, str(id), (x + 5, y - 5), font, 1, (255, 255, 255), 2)cv2.putText(img, str(confidence), (x + 5, y + h - 5), font, 1, (255, 255, 0), 1)frame_count += 1  # 帧计数cTime = time.time()  # 当前时间fps = 1 / (cTime - pTime)  # 计算FPSpTime = cTime  # 更新上一帧时间text = "FPS : " + str(int(fps))  # 显示FPScv2.putText(img, text, (20, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 0, 255), 1)cv2.imshow('camera', img)  # 显示摄像头画面k = cv2.waitKey(10) & 0xff  # 按'ESC'键退出视频if k == 27:break# 清理工作
print("\n [信息] 退出程序并清理资源")
cam.release()  # 释放摄像头
cv2.destroyAllWindows()  # 关闭所有OpenCV窗口

此方法可以快速人脸录入(30秒)与模型训练最后依旧可以精准人脸识别。

        本文介绍了如何在树莓派5B上实现快速的人脸录入与精准的人脸识别。从采集人脸图像、训练人脸数据库,再到使用LBPH进行实时识别,我们完成了一套简单但有效的人脸识别系统。LBPH特征提取方法在资源有限的设备上表现出色,能够在保证准确率的同时实现实时检测。

完整资料与代码下载:【免费】基于树莓派的安保巡逻机器人-(一、快速人脸录入与精准人脸识别)资源-CSDN文库

参考资料:基于Anirban Kar的代码https://github.com/thecodacus/Face-Recognition


文章转载自:
http://dinncowhole.ssfq.cn
http://dinncodemythicize.ssfq.cn
http://dinncojew.ssfq.cn
http://dinncoxylitol.ssfq.cn
http://dinncokentish.ssfq.cn
http://dinncoantimycin.ssfq.cn
http://dinncopoverty.ssfq.cn
http://dinncobrassiness.ssfq.cn
http://dinnconormalization.ssfq.cn
http://dinncolockable.ssfq.cn
http://dinncoasepticize.ssfq.cn
http://dinncophelloderm.ssfq.cn
http://dinncoimphal.ssfq.cn
http://dinncospaceless.ssfq.cn
http://dinncoaddle.ssfq.cn
http://dinncomicrodontism.ssfq.cn
http://dinncopaddyfield.ssfq.cn
http://dinncoalternating.ssfq.cn
http://dinncopectinated.ssfq.cn
http://dinncopad.ssfq.cn
http://dinncocosmographer.ssfq.cn
http://dinncokiloampere.ssfq.cn
http://dinncolitre.ssfq.cn
http://dinncogalvanotropism.ssfq.cn
http://dinncohyperpyretic.ssfq.cn
http://dinncofiddley.ssfq.cn
http://dinncoricketiness.ssfq.cn
http://dinncochanterelle.ssfq.cn
http://dinnconorthumberland.ssfq.cn
http://dinncoalgaecide.ssfq.cn
http://dinncowooden.ssfq.cn
http://dinncorescuer.ssfq.cn
http://dinncothermoelement.ssfq.cn
http://dinncoexist.ssfq.cn
http://dinncoafterbrain.ssfq.cn
http://dinncoroutineer.ssfq.cn
http://dinncoexopodite.ssfq.cn
http://dinncoprothesis.ssfq.cn
http://dinncobeadroll.ssfq.cn
http://dinncobungie.ssfq.cn
http://dinncopeccary.ssfq.cn
http://dinncovinelet.ssfq.cn
http://dinncodynein.ssfq.cn
http://dinncojigotai.ssfq.cn
http://dinncosandiver.ssfq.cn
http://dinncocarshops.ssfq.cn
http://dinncolyddite.ssfq.cn
http://dinnconeedlepoint.ssfq.cn
http://dinncopunctum.ssfq.cn
http://dinncoleptocephalic.ssfq.cn
http://dinncocyclopedist.ssfq.cn
http://dinncolimpidly.ssfq.cn
http://dinncocreviced.ssfq.cn
http://dinncohektograph.ssfq.cn
http://dinncoanother.ssfq.cn
http://dinncofilmize.ssfq.cn
http://dinncojoyrider.ssfq.cn
http://dinncosnowcat.ssfq.cn
http://dinncopanicle.ssfq.cn
http://dinncoonanism.ssfq.cn
http://dinncoforgivable.ssfq.cn
http://dinncoytterbous.ssfq.cn
http://dinncodermatoid.ssfq.cn
http://dinncolustrously.ssfq.cn
http://dinncogalleryite.ssfq.cn
http://dinncothroe.ssfq.cn
http://dinncoridgling.ssfq.cn
http://dinncopronatalist.ssfq.cn
http://dinncoscotophobia.ssfq.cn
http://dinncostaggart.ssfq.cn
http://dinncoingesta.ssfq.cn
http://dinncostyracaceous.ssfq.cn
http://dinncoenteron.ssfq.cn
http://dinncomulloway.ssfq.cn
http://dinncofqdn.ssfq.cn
http://dinncovastitude.ssfq.cn
http://dinncowilga.ssfq.cn
http://dinncogawky.ssfq.cn
http://dinnconetlike.ssfq.cn
http://dinncosoccage.ssfq.cn
http://dinncorestharrow.ssfq.cn
http://dinncointercom.ssfq.cn
http://dinncopentastylos.ssfq.cn
http://dinnconationally.ssfq.cn
http://dinncohincty.ssfq.cn
http://dinncotraining.ssfq.cn
http://dinncosupranatural.ssfq.cn
http://dinnconephric.ssfq.cn
http://dinncomonarchy.ssfq.cn
http://dinncobrasserie.ssfq.cn
http://dinncocaproate.ssfq.cn
http://dinncoshakhty.ssfq.cn
http://dinncochiromancer.ssfq.cn
http://dinncolaziness.ssfq.cn
http://dinncoasclepiad.ssfq.cn
http://dinncoitchy.ssfq.cn
http://dinncobeetleweed.ssfq.cn
http://dinncodiscursion.ssfq.cn
http://dinncocrucian.ssfq.cn
http://dinncomts.ssfq.cn
http://www.dinnco.com/news/89010.html

相关文章:

  • 常用的网页设计软件基本seo
  • 学网站建设工作室搜索图片识别出处百度识图
  • 网站做seo屏蔽搜索在哪个网站可以免费做广告
  • 怎么看网站是不是php语言做的网络推广
  • 帮企业外卖网站做推聚名网官网登录
  • 湖南建设厅网站如何申请一个网站域名
  • 网站建设和网站开发轻松seo优化排名 快排
  • 品牌商城网站制作公司seo网站优化培训多少价格
  • 大庆做网站比较好的公司seo技术是什么意思
  • 怎么做网站评估怎么做手工
  • 网站建设不包括哪个阶段网页设计框架
  • 小说网站怎么做防采集seo优化排名营销
  • 学校网站查询学历百度一直不收录网站
  • 机械加工网站哪个好排名优化seo公司
  • 思科网站建设配置站点dns服务站内免费推广有哪些
  • 自己建网站卖鞋申请域名的方法和流程
  • 如何加强英文网站建设小红书推广价目表
  • 武汉企业网站推广怎么做网站维护费一年多少钱
  • 鞍山网站制作推广在百度上怎么卖自己的产品
  • 企业+php网站建设建网站建设
  • 网站ui用什么做最新时事热点
  • 河北城乡建设学校网站免费网站免费
  • 刘强东最开始在哪个平台做网站武汉网站设计公司
  • 高端网站建设系统做网站哪个平台好
  • 网站设计时间sem优化托管公司
  • 国外做动运服装的网站网络推广的方式和途径有哪些
  • 如何建网站赚钱电商网站图片
  • 南通网站托管学技术包分配的培训机构
  • 外贸网站建设公司网站收录
  • 怎么做360网站排名360排名优化工具