前言
为了不让树莓派吃灰较劲了脑汁,其实这个功能很早之前就折腾过了,但是当时鼓捣的的外观并不好看,所以也没有打算分享的计划.最近一直在折腾树莓派ZERO WH,后面又买了UPS和墨水屏,个人认为这就是树莓派ZERO WH的最终归宿,能断电展示信息(墨水屏性质),UPS又能保证不间断电源.
在此基础上能衍生出很多玩法,因为ZERO轻巧并搭配了UPS,因此就可以随身放置,衍生一个好玩的想法,比如接上摄像头搞一些监控或者实时图像识别,同时最近也在开发微信机器人,接入机器人也是不错的玩法.
树莓派折腾开始.
旧新成品
3B+ 不方便携带

ZERO WH小巧方便



硬件
- 树莓派ZERO WH
- 树莓派红外摄像头
- 可选(树莓派UPS)
- 可选(树莓派2.13墨水屏)
安装
通过树莓派专用摄像头连接线连接树莓派和摄像头.
这里要注意,树莓派ZERO和其他树莓派型号插线不通用.购买的时候要注意选择ZERO版本,插口会比较小一点.
教程
保证树莓派安装了Raspberry系统,我这里为了节省性能安装的lite版本.不需要可视化界面.
下载地址:https://www.raspberrypi.org/software/operating-systems/#raspberry-pi-os-32-bit
安装python3或者python2.7(自带)
我这里用的python3,2.7没尝试.懒得装3的可以试试自带的是否可以.
墨水屏驱动
如果不需要回显墨水屏可以跳过此步骤.
首先到github下载对应墨水屏的驱动文件
https://github.com/waveshare/e-Paper,下载即可,后面会用到.
百度图像识别SDK注册并安装
注册图像识别API
https://ai.baidu.com/tech/imagerecognition/general
注册免费每天100次接口调用,学习足够了.
下载SDK:https://ai.baidu.com/sdk#bfr
选择 图像识别 python sdk
安装SDK
将下载好的SDK上传至树莓派并解压,进入目录执行安装
sudo python3 setup.py install
代码
代码流程
- 执行shell拍照脚本,并返回照片名字
- 读取上述返回照片
- 将照片内容上传至百度图像识别API获取识别结果
- 将结果回显墨水屏或者打印控制台
python代码
camera2AI.py (如果有墨水屏,该文件放置墨水屏驱动同级目录)
#!/usr/bin/python
# -*- coding:utf-8 -*-
import os
import sys
picdir = os.path.join(os.path.dirname(os.path.dirname(os.path.realpath(__file__))), 'pic')
libdir = os.path.join(os.path.dirname(os.path.dirname(os.path.realpath(__file__))), 'lib')
if os.path.exists(libdir):
sys.path.append(libdir)
import logging
import time
from waveshare_epd import epd2in13_V2
from PIL import Image, ImageDraw, ImageFont
import traceback
import socket
import requests
import json
import re
from aip import AipImageClassify
import math
logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s')
# 读取图片
def get_file_content(filePath):
with open(filePath, 'rb') as fp:
return fp.read()
def getClient():
# 这里输入你创建应用获得的三个参数 百度图像识别注册获取
APP_ID = ''
API_KEY = ''
SECRET_KEY = ''
return AipImageClassify(APP_ID, API_KEY, SECRET_KEY)
try:
client = getClient()
img = os.popen('sh /home/pi/shell/camera.sh').read() #拍照脚本代码路径
logging.info(img)
image = get_file_content('/media/local/camera/' + img)#拍照后保持的照片地址
# 调用通用物体识别
result = client.advancedGeneral(image).get('result')
logging.info(result)
epd = epd2in13_V2.EPD()
epd.init(epd.FULL_UPDATE)
epd.Clear(0xFF)
# Drawing on the image
font15 = ImageFont.truetype(os.path.join(picdir, 'Font.ttc'), 15)
time_image = Image.new('1', (epd.height, epd.width), 255)
time_draw = ImageDraw.Draw(time_image)
step = 1
for r in result:
text = '物品:{} 可信度:{}%'.format(r.get('keyword'), round(r.get('score') * 100))
time_draw.text((50, step), text, font=font15, fill=0)
step = step + 20
epd.display(epd.getbuffer(time_image))
logging.info("Goto Sleep...")
# epd.sleep()
except IOError as e:
logging.info(e)
except KeyboardInterrupt:
logging.info("ctrl + c:")
epd2in13_V2.epdconfig.module_exit()
exit()
上面需要调用shell脚本进行实时拍照
camera.sh
time=$(date "+%Y%m%d%H%M%S")
sudo raspistill -o /media/local/camera/$time.jpg #拍照后保持的照片地址
echo -n "$time.jpg"
运行
python3 camera2AI.py
扩展
思路扩展
根据以上配置好后,可以放置摄像头对准一处,通过crontab定时执行图像识别
玩法
识别到指定任务或者物品可以上传云端,再通过微信机器人转发至微信等处
微信机器人下篇教你如何玩转
本文由 SAn 创作,采用 知识共享署名4.0 国际许可协议进行许可
本站文章除注明转载/出处外,均为本站原创或翻译,转载前请务必署名
最后编辑时间为:
2021/02/28 14:13