树莓派人脸识别

本文介绍了如何使用树莓派搭建一个人脸识别系统,通过详细步骤解析了从安装依赖库到配置模型的过程,让你的树莓派具备智能识别人脸的能力。
摘要由CSDN通过智能技术生成
import cv2
import smbus
import time
import _thread
import face_recognition
from voice import SetReader
from voice import Reader_Type
from voice import SetVolume
from voice import Speech_text
from voice import EncodingFormat_Type
from voice import GetChipStatus
from voice import ChipStatus_Type
'''
程序执行部分,以上为配置相关的函数和字典定义
'''
SetReader(Reader_Type["Reader_XiaoYan"])#选择播音人晓萍
SetVolume(10)


my_image = cv2.imread("/home/pi/Pictures/1.jpg")
my_image = cv2.cvtColor(my_image, cv2.COLOR_BGR2RGB)
my_face_encoding = face_recognition.face_encodings(my_image)
#载入人脸分类器
face_cascade = cv2.CascadeClassifier('/usr/share/opencv/haarcascades/haarcascade_frontalface_default.xml')  
cap = cv2.VideoCapture(0)  #开始读取摄像头信号

def read_cap():
    global flag
    while cap.isOpened():
        flag=0
        (ret, frame) = cap.read()  #读取每一帧视频图像为frame
        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)  #将图像转换为灰度图
        faces = face_cascade.detectMultiScale(gray, minSize=(100, 100), flags=cv2.CASCADE_FIND_BIGGEST_OBJECT)  #检测人脸的位置
        face_locations = []  #定义一个列表存储人脸的位置信息
        
        for (left, top, width, height) in faces:  #便利找到的人脸的位置信息
            #Speech_text("请面向摄像头,正在人脸识别",EncodingFormat_Type["GB2312"])
            #while GetChipStatus() != ChipStatus_Type['ChipStatus_Idle']:#等待当前语句播报结束
                #time.sleep(0.001)
                
            #计算出右下角的坐标
            right = left+width
            bottom = top+height
            #为face_locations添加一个人脸的位置信息,注意需要强制转换为int类型
            face_locations.append((int(top), int(right), int(bottom), int(left)))
            cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)
        
        rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
        face_encodings = face_recognition.face_encodings(rgb_frame, face_locations)
        for face_encoding in face_encodings:  #遍历得到所有人脸特征信息
            matches = face_recognition.compare_faces(my_face_encoding, face_encoding, tolerance=0.3)
            if True in matches:
                flag=1
            else:
                flag=2
                
        cv2.imshow("Recognition", frame)
        cv2.waitKey(5)
    cap.release()
    cv2.destroyAllWindows()

def speech():
    global flag
    time.sleep(0.3)
    while True:
        if flag==1:
            Speech_text("门已开,请通行",EncodingFormat_Type["GB2312"])

            while GetChipStatus() != ChipStatus_Type['ChipStatus_Idle']:#等待当前语句播报结束
                time.sleep(0.001)
        elif flag==2:
            Speech_text("识别失败",EncodingFormat_Type["GB2312"])

            while GetChipStatus() != ChipStatus_Type['ChipStatus_Idle']:#等待当前语句播报结束
                time.sleep(0.001)
        
_thread.start_new_thread(read_cap,())
_thread.start_new_thread(speech,())
'''
cap=cv2.VideoCapture(0)
while cap.isOpened():
    (ret, frame) = cap.read()
    frame = cv2.flip(frame, 1)
    cv2.imshow("Shot", frame)
    k = cv2.waitKey(5)
    if k == ord('s'):
        cv2.imwrite("/home/pi/Pictures/5.jpg", frame)
'''

#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Tue May  4 01:10:21 2021

@author: pi
"""
 
import cv2
import face_recognition
my_image = cv2.imread("/home/pi/Pictures/1.jpg")
my_image = cv2.cvtColor(my_image, cv2.COLOR_BGR2RGB)
my_face_encoding = face_recognition.face_encodings(my_image
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值