English
联系我们
网站地图
邮箱
旧版回顾



焦作懊咎妥通讯股份正版挂牌_基于树莓派的人脸认证门禁

?

 现在实验室用的门禁都是刷卡进入,经常忘记带卡而进不去,所以就用树莓派自己做了一个刷脸认证的门禁系统,简单易用,准确性还挺高。接下来详细的说下具体的制作过程:

1.准备树莓派

首先你得有一块树莓派,淘宝卖家很多,选择三代性能相对好一点。

然后得需要电源,电源使用的microUSB供电。

树莓派安装系统得需要外接SD卡,一般16G大小的便足够了。其他的配件如显示屏,鼠标,键盘这些就不用细说啦,肯定需要准备的。

2.树莓派安装系统

如果买来的树莓派已经刷好系统了,那么就可以跳过这一步。官方提供的操作系统是 Raspbian,这是 Debian 系统的定制版。

下载完成镜像后,解压,再下载一个工具Win32 Disk Imager,专门用来写“.img"文件到SD和USB卡中的软件Win32 Disk Imager怎么下载?百度一下“Win32 Disk Imager”即可,下载安装之后

写入镜像,ImageFile就是镜像文件,Device就是你的SD卡。到此为止准备工作已经做好了。

3.注册百度云

由于树莓派的性能限制,运行神经网络来进行人脸识别的难度较大,所以我们采用了开源的人脸识别SDK来进行人脸的识别和检测,对比了国内几家的SDK,个人觉得百度人脸识别的效果要好一点,特别是并发数要比其他的好很多。下面介绍下百度云注册和SDK的使用方法:

1) 注册百度云

  首先你要注册一个百度云账号,注册完成之后进入控制台:

创建一个自己的应用,这里我创建好了一个刷脸门禁应用:

这里要记住APPID,API Key,Secret Key。这三个后面的程序需要用到。然后在百度开放平台下载人脸识别SDK:

我是用的python语言,所以下载对应的python的SDK,如果你用的别的语言可以下载对应的SDK来使用,具体的SDK怎么安装使用可以详细的看下SDK文档,按照文档来操作就OK啦~

2)识别程序

  百度云注册完成、下载完成SDK后我们就开始写程序,程序的思路很简单,通过树莓派拍一张照片,然后通过SDK来检测照片里面的人是谁,如果这个人在人脸库里面,而且有权限的话,那么就开门,如果不在人脸库就关门。同时我还添加了语音播报的功能,而且还能将开门信息推送到微信上,这样就可以试试的看到有谁来开门了。先上代码:

  1 """
  2 通过摄像头拍一张照片,然后识别出人是谁,进而控制门禁系统
  3 @author: zutterhao
  4 @date:2018年5月19日
  5 """
  6 
  7 from aip import AipFace
  8 from picamera import PiCamera
  9 import urllib.request
 10 import RPi.GPIO as GPIO
 11 import base64
 12 import time
 13 import pyttsx3
 14 import pygame
 15 
 16 #百度人脸识别API账号信息
 17 APP_ID = "********"
 18 API_KEY = "*******************"
 19 SECRET_KEY = "*********************‘
 20 client = AipFace(APP_ID, API_KEY, SECRET_KEY)
 21 #图像编码方式
 22 IMAGE_TYPE="BASE64"
 23 #用户组信息
 24 GROUP = "fanghao"
 25 camera = PiCamera()
 26 engine = pyttsx3.init()
 27 pygame.mixer.init()
 28 GPIO_IN_PIN = 15
 29 GPIO_OUT_PIN1 = 11
 30 GPIO_OUT_PIN2 = 13
 31 #定义一个摄像头对象
 32 def getimage():
 33     camera.resolution = (1024,768)
 34     camera.start_preview()
 35     time.sleep(2)
 36     camera.capture("faceimage.jpg")
 37     pygame.mixer.music.load("./voice/start.mp3")
 38     pygame.mixer.music.play()
 39     time.sleep(2)
 40 
 41 #对图片的格式进行转换
 42 def transimage():
 43     f = open("faceimage.jpg","rb")
 44     img = base64.b64encode(f.read())
 45     return img
 46 #播放声音
 47 def playvioce(name):
 48     pygame.mixer.music.load("./voice/" +name)
 49     pygame.mixer.music.play()
 50 #发送信息到微信上  
 51 def sendmsg(name,main):
 52     url = "https://sc.ftqq.com/***********************************.send?"
 53     urllib.request.urlopen(url + "text="+name+"&desp="+main)
 54     
 55 #上传到百度api进行人脸检测
 56 def go_api(image):
 57     result = client.search(str(image, "utf-8"), IMAGE_TYPE, GROUP);
 58     if result["error_msg"] == "SUCCESS":
 59         name = result["result"]["user_list"][0]["user_id"]
 60         score = result["result"]["user_list"][0]["score"]
 61         if score > 80:
 62             print("Welcome %s !" % name)
 63             if name == "fanghao":
 64                 sendmsg("DoorOpen",name)
 65                 playvioce("fanghao.mp3")
 66                 time.sleep(3)
 67             if name == "chengjiayu":
 68                 sendmsg("DoorOpen",name)
 69                 playvioce("chengjiayu.mp3")
 70                 time.sleep(3)
 71                 playvioce("despacito.mp3")
 72                 time.sleep(5)
 73             if name == "zhangzhijian":
 74                 sendmsg("DoorOpen",name)
 75                 playvioce("zhangzhijian.mp3")
 76                 time.sleep(3)
 77         else:
 78             print("Sorry...I don"t know you !")
 79             playvioce("noroot.mp3")
 80             name = "Unknow"
 81             return 0
 82         curren_time = time.asctime(time.localtime(time.time()))
 83         f = open("Log.txt","a")
 84         f.write("Person: " + name + "     " + "Time:" + str(curren_time)+"
")
 85         f.close()
 86         return 1
 87     if result["error_msg"] == "pic not has face":
 88         print("There is no face in image!")
 89         playvioce("face.mp3")
 90         time.sleep(2)
 91         return 0
 92     else:
 93         print(result["error_code"]+" " + result["error_code"])
 94         return 0
 95 #主函数
 96 if __name__ == "__main__":
 97     GPIO.setmode(GPIO.BOARD)
 98     GPIO.setup(GPIO_IN_PIN,GPIO.IN)
 99     GPIO.setup(GPIO_OUT_PIN1,GPIO.OUT)
100     GPIO.setup(GPIO_OUT_PIN2,GPIO.OUT)
101     while True:
102         print("OK")
103         if True:
104         #if GPIO.input(GPIO_IN_PIN) == GPIO.LOW:
105             getimage()
106             img = transimage()
107             res = go_api(img)
108             if(res == 1):
109                 GPIO.output(GPIO_OUT_PIN2,GPIO.HIGH)
110                 time.sleep(1)
111                 GPIO.output(GPIO_OUT_PIN2,GPIO.LOW)
112             else:
113                 GPIO.output(GPIO_OUT_PIN1,GPIO.HIGH)
114                 time.sleep(1)
115                 GPIO.output(GPIO_OUT_PIN1,GPIO.LOW)
116             print("waite 3 seconds to do next")
117             playvioce("waite.mp3")
118             time.sleep(3)
119 
120                 
121             

这里拍照用的picamera,是专门为树莓派定制的摄像头,淘宝可以买到。picamera的使用方法可以在这里看到。播放音乐我用的pygame的播放音乐模块,我请小姐姐给我录的音,哈哈炒鸡好听~,推送微信我用的一个很好的工具server酱,真的非常好用,只需一行代码就OK~

1 """ 2 tong guo she xiang tou pai yi zhang zhao pian, ran hou shi bie chu ren shi shui, jin er kong zhi men jin xi tong 3 author: zutterhao 4 date: 2018 nian 5 yue 19 ri 5 """ 6 7 from aip import AipFace 8 from picamera import PiCamera 9 import urllib. request 10 import RPi. GPIO as GPIO 11 import base64 12 import time 13 import pyttsx3 14 import pygame 15 16 bai du ren lian shi bie API zhang hao xin xi 17 APP_ID "" 18 API_KEY "" 19 SECRET_KEY "' 20 client AipFace APP_ID, API_KEY, SECRET_KEY 21 tu xiang bian ma fang shi 22 IMAGE_TYPE" BASE64" 23 yong hu zu xin xi 24 GROUP " fanghao" 25 camera PiCamera 26 engine pyttsx3. init 27 pygame. mixer. init 28 GPIO_IN_PIN 15 29 GPIO_OUT_PIN1 11 30 GPIO_OUT_PIN2 13 31 ding yi yi ge she xiang tou dui xiang 32 def getimage: 33 camera. resolution 1024, 768 34 camera. start_preview 35 time. sleep 2 36 camera. capture" faceimage. jpg" 37 pygame. mixer. music. load". voice start. mp3" 38 pygame. mixer. music. play 39 time. sleep 2 40 41 dui tu pian de ge shi jin xing zhuan huan 42 def transimage: 43 f open" faceimage. jpg"," rb" 44 img base64. b64encode f. read 45 return img 46 bo fang sheng yin 47 def playvioce name: 48 pygame. mixer. music. load". voice" name 49 pygame. mixer. music. play 50 fa song xin xi dao wei xin shang 51 def sendmsg name, main: 52 url " https: sc. ftqq. com. send?" 53 urllib. request. urlopen url " text" name" amp desp" main 54 55 shang chuan dao bai du api jin xing ren lian jian ce 56 def go_api image: 57 result client. search str image, " utf8", IMAGE_TYPE, GROUP 58 if result" error_msg" " SUCCESS": 59 name result" result"" user_list" 0" user_id" 60 score result" result"" user_list" 0" score" 61 if score gt 80: 62 print" Welcome s !" name 63 if name " fanghao": 64 sendmsg" DoorOpen", name 65 playvioce" fanghao. mp3" 66 time. sleep 3 67 if name " chengjiayu": 68 sendmsg" DoorOpen", name 69 playvioce" chengjiayu. mp3" 70 time. sleep 3 71 playvioce" despacito. mp3" 72 time. sleep 5 73 if name " zhangzhijian": 74 sendmsg" DoorOpen", name 75 playvioce" zhangzhijian. mp3" 76 time. sleep 3 77 else: 78 print" Sorry... I don" t know you !" 79 playvioce" noroot. mp3" 80 name " Unknow" 81 return 82 curren_time time. asctime time. localtime time. time 83 f open" Log. txt"," a" 84 f. write" Person: " name " " " Time:" str curren_time"" 85 f. close 86 return 1 87 if result" error_msg" " pic not has face": 88 print" There is no face in image!" 89 playvioce" face. mp3" 90 time. sleep 2 91 return 92 else: 93 print result" error_code"" " result" error_code" 94 return 95 zhu han shu 96 if __name__ " __main__": 97 GPIO. setmode GPIO. BOARD 98 GPIO. setup GPIO_IN_PIN, GPIO. IN 99 GPIO. setup GPIO_OUT_PIN1, GPIO. OUT 100 GPIO. setup GPIO_OUT_PIN2, GPIO. OUT 101 while True: 102 print" OK" 103 if True: 104 if GPIO. input GPIO_IN_PIN GPIO. LOW: 105 getimage 106 img transimage 107 res go_api img 108 if res 1: 109 GPIO. output GPIO_OUT_PIN2, GPIO. HIGH 110 time. sleep 1 111 GPIO. output GPIO_OUT_PIN2, GPIO. LOW 112 else: 113 GPIO. output GPIO_OUT_PIN1, GPIO. HIGH 114 time. sleep 1 115 GPIO. output GPIO_OUT_PIN1, GPIO. LOW 116 print" waite 3 seconds to do next" 117 playvioce" waite. mp3" 118 time. sleep 3 119 120 121 zhe li pai zhao yong de picamera, shi zhuan men wei shu mei pai ding zhi de she xiang tou, tao bao ke yi mai dao. picamera de shi yong fang fa ke yi zai zhe li kan dao. bo fang yin yue wo yong de pygame de bo fang yin yue mo kuai, wo qing xiao jie jie gei wo lu de yin, ha ha chao ji hao ting, tui song wei xin wo yong de yi ge hen hao de gong ju server jiang, zhen de fei chang hao yong, zhi xu yi xing dai ma jiu OK

下面贴一张丑照:

  

?

外壳还在打印,所以暂时就用红绿灯表示门的开关了,等外壳打印好了再更新一波实测视频~

?

?

?

?

?

?

当前文章:http://www.xjj2197.com/0uvt/130349-1015512-10763.html

发布时间:01:54:47

中特网开奖结果??白小姐彩图??新版东方心经四柱预测??663737.com??118kj开奖现场一线图库??香港挂牌玄机??天下彩高手论坛??11822品特轩高手之家??94123.com??大富翁论坛www.hk4849.com??

专题推荐

相关新闻


? 1996 - 蜘蛛资讯网 版权所有?? 联系我们

地址:北京市三里河路52号 邮编:100864