web控制树莓派摄像头

首先测试摄像头保证能顺利拍照 raspistill -o a.jpg

安装flask sudo pip install flask

确认无误之后向下进行。

文件夹结构:

CapPic
----static
         >a.jpg
----templates
         >index.html
----main.py

原理:利用flask搭建一个网页,利用python的os库函数执行raspistill命令进行拍照,并显示出来。其中点击"get a picture"会跳转到on页面进行自动拍照。

代码:

//写的巨丑

<!DOCTYPE html>
<!-- saved from url=(0021)http://192.168.2.202/ -->
<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Show Picture</title>
    <style type="text/css">
    body{
        background: #ccc;
    }
    div {
        width: 150px;
        border: 2px solid black;
            padding: 5px;
            margin: 5px;

            font-size: 20px;
            font-weight: bold;
            background-color: #ccc;
    }
    div a{
        text-decoration: none;
        color: #000;
    }
    </style>
</head>
<body>
    <div >
        <a href="/on">get a picture</a>
    </div>
    <img src="static/a.jpg" width="50%" height="auto">
</body>
</html>
index.html
from flask import Flask,render_template
import os
app = Flask(__name__)

@app.route('/')
def hello_world():
    #return 'Hello World!'
    return render_template('index.html')


@app.route('/on')
def get_pic():
    os.system("raspistill -o a.jpg")
    os.system("mv a.jpg static")
    return render_template('index.html')

if __name__ == '__main__':
    app.run(host='0.0.0.0',port=80)
main.py

运行:

执行 sudo python main.py

浏览器访问 本机IP:80,局域网内的设备均可。

 

备注:

1. flask 的app.run填0.0.0.0,不然只有树莓派本身能访问,其他设备不同访问网页

2. 静态文件必须放在 static 文件夹中,例如图片、CSS

 

 

参考链接:

1. https://www.cnblogs.com/ttssrs/p/4890635.html

2. https://blog.csdn.net/weixin_40929147/article/details/83795154

 

 

posted @ 2019-11-13 21:14  Rogn  阅读(1157)  评论(0编辑  收藏  举报