不知道你们是否有关门后又回到门口检查门是否关了的经历,反正我有,做为一个懒人应该采起一些措施,在不往回跑的前提下检查门是否关了。javascript
几乎没有忘记关过门,可是强迫症逼死人:(html
解决方案前端
远程控制联网手机在房内对门口拍照,而后查看图片检查是否关门. 远程方面使用web方式.java
这里放一张截图:python
如图所示,若是关门了,经过照片能看出是否关门了,这里拍摄的是打了小锁的状况。git
实施方案github
硬件:web
这里使用的红米note7 pro作的测试, 旧手机有点问题.shell
软件:编程
主要是手机APP安装以及APP里面Linux环境软件的安装
手机安装termux, termux-api, juiceSSH
经过你中意的应用市场找到这两个app而后安装
建议经过Google商店安装, 不过可能有难度,须要自行研究
安装好以后能够经过自带的窗口或者经过JuiceSSH链接,由于JuiceSSH对于命令输入更友好
关于JuiceSSH或者电脑链接手机的Termux能够参考: 一个旧手机的威力
Linux包以及Python包
pkg install termux-api
必定要既安装手机APP的Termux-API, 也要经过上面命令安装termux-api
经过这个包咱们能够得到一堆访问手机意见或者软件的访问API, 好比相机,短信,电话,震动之类的API.
可经过如下命令调用手机相机拍照
# 查看帮助 ➜ ~ termux-camera-photo -h Usage: termux-camera-photo [-c camera-id] output-file Take a photo and save it to a file in JPEG format. -c camera-id ID of the camera to use (see termux-camera-info), default: 0 # 经过相机id为1的相机拍照,并保存相片到本地1.jpeg termux-camera-photo -c 1 1.jpeg
至于你的哪一个相机是前置哪一个是后置,或者某个后置(后置可能有多个), 须要本身测试。
可是上面的照片是放在APK的存储空间里面,因此手机相册看不到,因此须要经过其余API将图片放到手机的本地存储。
执行如下命令后,会在加目录生成一个storage目录,经过这个目录咱们能够在termux内访问手机的本地存储
# 手机应该会询问该APP能否访问手机存储 termux-setup-storage
而后咱们就能够将将拍摄的图片移到外面,而后就能够经过相册或者文件管理器打开
cp 1.jpeg storage/downloads
注: 在执行相关命令的时候会弹出权限是否容许,须要手动点击容许, 不容许的话天然这些API或者说命令都无法用了.
Termux-API参考连接: Termux-API
选择你本身喜欢的编程语言或者web框架均可以了,这里选择Python, flask实现web远程控制。
# 安装Python pkg install python # 安装flask pillow pip install flask pillow
安装pillow是为了压缩图片。
若是安装sanic, pillow失败,能够考虑安装如下依赖(我安装了不少的其余库,因此肯定哪一个是必定必要的)
apt install python python-dev clang fftw libzmq libzmq-dev freetype freetype-dev libpng libpng-dev pkg-config zlib zlib-dev libiconv libiconv-dev curl
内网穿透也参考这篇文章: 一个旧手机的威力
至此咱们完成了全部的准备工做。
效果以下
因为我暂时没想好一个好的放置位置(跟房间布局有关),因此暂时尚未实际应用,因此照片拍摄的照片是手持手机,而后远程控制拍摄。
纯演示项目,若是以为有意思本身完善吧。
为了尽量的少代码,前端方面没使用任何第三方库。
目录结构
➜ ~ tree watchdog watchdog ├── app.py ├── static │ ├── 2019-05-18-18-54-00.jpeg │ └── latest.jpeg └── templates └── index.html 2 directories, 4 files
代码app.py
from flask import Flask import subprocess as sp from datetime import datetime from PIL import Image from os import path from flask import render_template app = Flask(__name__) @app.route("/") def index(): # 为了避免缓存图片 now = datetime.now() timestamp = now.timestamp() return render_template('index.html', timestamp=timestamp) @app.route("/watch") def watch(): cwd = path.dirname(path.abspath("__file__")) static_dir = path.join(cwd, "static") now = datetime.now() date_str = now.strftime("%Y-%m-%d-%H-%M-%S") # 拍摄的照片保存位置 img_path = path.join(static_dir, "{}.jpeg".format(date_str)) print(img_path) # 拼凑出latest.png文件路径 latest_img_path = path.join(static_dir, "latest.jpeg") print(latest_img_path) cmd = "termux-camera-photo -c 0 {}".format(img_path) retcode = sp.call(cmd, shell=True) if retcode != 0: return "failed" img = Image.open(img_path) # 根据实际图片压缩,本身测试 new_img = img.resize((int(img.size[0] * 0.4), int(img.size[1] * 0.4))) try: new_img.save(latest_img_path) except Exception as e: print(e) return "failed" return "ok" if __name__ == '__main__': app.run(host="0.0.0.0", debug=True)
index.html
<!DOCTYPE html> <html> <head> <title>Watch Dog</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> <meta http-equiv="Pragma" content="no-cache"> <meta http-equiv="Cache-Control" content="no-cache"> <meta http-equiv="Expires" content="0"> <style> .container { text-align: center; } img { width: 80%; } button{ padding: 10px 40px; background: #09F; color: #FFF; text-decoration: none; } </style> <script type="text/javascript"> function watch() { var xhttp = new XMLHttpRequest(); xhttp.open("GET", "/watch", true) xhttp.onloadend = e => { var resp = xhttp.responseText alert(resp) if (resp == "ok") { // 刷新页面 window.location.reload() } }; xhttp.send() } </script> </head> <body> <div class="container"> <!-- 加个时间戳为了强制更新图片 --> <img src="/static/latest.jpeg?{{timestamp}}" alt=""> <br> <button onclick="watch()">关门了吗?</button> </div> </body> </html>
在windows上安装一些须要编译的python库实在是件痛苦的事情,由于须要Microsoft Visual C++ 14.0, 而为了装这个又得须要装visual studio.
总的来讲流程以下
若是你喜欢的话也能够经过写一个APP的方式实现。
代码连接: watchdog