raspberrypi树莓派+百度人脸识别api
文章目录
官方教学视频:
准备工作:
注册的百度云账号,申请人脸接口使用
将页面下拉
相关文本文档:
安装api:
安装过程(采用第二中方式):
人脸识别管理:
创建应用:
创建用户组
新建用户
这里开始上传照片
test code(in the raspberryPi):
# accroding to the specified situation(your environment),do some modify to fit you. from aip import AipFace from picamera import PiCamera import urllib.request import RPi.GPIO as GPIO import base64 import time # 百度人脸识别API账号信息 APP_ID = '24400102' API_KEY = 'obDY8f3e59LB3G0qH68T2x9F' SECRET_KEY = 'bMOPk5TgTXkOealxksgX6H9ZYrFXqP63' client = AipFace(APP_ID, API_KEY, SECRET_KEY) # 创建一个客户端用以访问百度云 # 图像编码方式 IMAGE_TYPE = 'BASE64' camera: PiCamera = PiCamera() # 定义一个摄像头对象 # 用户组(改为你自己的用户组(在百度云中一致) print("接入用户组:cxxu...") GROUP: str = 'cxxu' # 照相函数 def getimage() -> None: camera.resolution = (1024, 768) # 摄像界面为1024*768 print("#开始摄像") camera.start_preview() # 开始摄像 time.sleep(2) camera.capture('faceimage.jpg') print("拍照并保存") time.sleep(2) # 对图片的格式进行转换 def transimage() -> bytes: print("转换图片格式...") f = open('faceimage.jpg', 'rb') img = base64.b64encode(f.read()) return img # 上传到百度api进行人脸检测 def upload_api(image:bytes) -> int: print("在百度云人脸库对应的分组中寻找有没有匹配的人脸") result = client.search(str(image, 'utf-8'), IMAGE_TYPE, GROUP) # 在百度云人脸库中寻找有没有匹配的人脸 if result['error_msg'] == 'SUCCESS': # 如果成功了 name: str = result['result']['user_list'][0]['user_id'] # 获取名字 score: float = result['result']['user_list'][0]['score'] # 获取相似度 print("相似度:", score) if score > 80: # 如果相似度大于80 print("welcome!:"+name) else: print("Stranger") name = 'Unknow' return 0 curren_time = time.asctime(time.localtime(time.time())) # 获取当前时间 # 将人员出入的记录保存到Log.txt中 f = open('Log.txt', 'a') f.write("Person: " + name + " " + "Time:" + str(curren_time)+'\n') f.close() return 1 if result['error_msg'] == 'pic not has face': print("no faces found! ") time.sleep(2) return 0 else: print(str(result['error_code'])+' ' + str(result['error_code'])) return 0 # 主函数 if __name__ == '__main__': while True: print('system starting...') if True: getimage() # take photo by the camera img = transimage() # 转换照片格式 res = upload_api(img) # 将转换了格式的图片上传到百度云 if(res == 1): # 是人脸库中的人 print("open the door(analog)") else: print("close the door (analog)") print('模拟门禁系统:稍等1+秒进入下一个') time.sleep(1)
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
2023-08-01 数学归纳法基本原理及其变体和证明
2023-08-01 基本不等式@平均值不等式@双勾函数不等式
2022-08-01 计组/数字电路_cpu引脚缩写参考