MJRoBot (Marcelo Rovai)
Published © GPL3+

Video Streaming Web Server

We will learn how to stream a video and integrate it with a Python/Flask Web Server.

IntermediateFull instructions provided8 hours20,228
Video Streaming Web Server

Things used in this project

Hardware components

Raspberry Pi 3 Model B
Raspberry Pi 3 Model B
×1
DHT22 Temperature Sensor
DHT22 Temperature Sensor
×1
5MP IR-sensitive Autofocus Camera Module
UDOO 5MP IR-sensitive Autofocus Camera Module
×1
Resistor 4.75k ohm
Resistor 4.75k ohm
×1

Software apps and online services

Flask

Story

Read more

Schematics

Electrical diagram

Code

Code snippet #9

Plain text
from flask import Flask, render_template, Response

# Raspberry Pi camera module (requires picamera package, developed by Miguel Grinberg)
from camera_pi import Camera

app = Flask(__name__)

@app.route('/')
def index():
    """Video streaming home page."""
    return render_template('index.html')

def gen(camera):
    """Video streaming generator function."""
    while True:
        frame = camera.get_frame()
        yield (b'--frame\r\n'
               b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')

@app.route('/video_feed')
def video_feed():
    """Video streaming route. Put this in the src attribute of an img tag."""
    return Response(gen(Camera()),
                    mimetype='multipart/x-mixed-replace; boundary=frame')

if __name__ == '__main__':
    app.run(host='0.0.0.0', port =80, debug=True, threaded=True)

Code snippet #10

Plain text
<html>
  <head>
    <title>MJRoBot Lab Live Streaming</title>
    <link rel="stylesheet" href='../static/style.css'/>
  </head>
  <body>
    <h1>MJRoBot Lab Live Streaming</h1>
    <h3><img src="{{ url_for('video_feed') }}" width="90%"></h3>
    <hr>
    <p> @2018 Developed by MJRoBot.org</p>
  </body>
</html>

Code snippet #12

Plain text
β”œβ”€β”€ Documents
       β”œβ”€β”€ camWebServer
               β”œβ”€β”€ camera_pi.py
               β”œβ”€β”€ appCam.py
               β”œβ”€β”€ templates
               |     β”œβ”€β”€ index.html
               └── static
                     β”œβ”€β”€ style.css

Code snippet #18

Plain text
import Adafruit_DHT
DHT22Sensor = Adafruit_DHT.DHT22
DHTpin = 16
humidity, temperature = Adafruit_DHT.read_retry(DHT22Sensor, DHTpin)

if humidity is not None and temperature is not None:
    print('Temp={0:0.1f}*C  Humidity={1:0.1f}%'.format(temperature, humidity))
else:
    print('Failed to get reading. Try again!')

Code snippet #20

Plain text
from flask import Flask, render_template, Response
app = Flask(__name__)

# Raspberry Pi camera module (requires picamera package)
from camera_pi import Camera

import Adafruit_DHT
import time

# get data from DHT sensor
def getDHTdata():		
	DHT22Sensor = Adafruit_DHT.DHT22
	DHTpin = 16
	hum, temp = Adafruit_DHT.read_retry(DHT22Sensor, DHTpin)
	
	if hum is not None and temp is not None:
		hum = round(hum)
		temp = round(temp, 1)
	return temp, hum


@app.route("/")
def index():
	timeNow = time.asctime( time.localtime(time.time()) )
	temp, hum = getDHTdata()
	
	templateData = {
      'time': timeNow,
      'temp': temp,
      'hum'	: hum
	}
	return render_template('index.html', **templateData)

@app.route('/camera')
def cam():
	"""Video streaming home page."""
	timeNow = time.asctime( time.localtime(time.time()) )
	templateData = {
      'time': timeNow
	}
	return render_template('camera.html', **templateData)


def gen(camera):
    """Video streaming generator function."""
    while True:
        frame = camera.get_frame()
        yield (b'--frame\r\n'
               b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')


@app.route('/video_feed')
def video_feed():
    """Video streaming route. Put this in the src attribute of an img tag."""
    return Response(gen(Camera()),
                    mimetype='multipart/x-mixed-replace; boundary=frame')


if __name__ == '__main__':
    app.run(host='0.0.0.0', port =80, debug=True, threaded=True)

Code snippet #21

Plain text
<!doctype html>
<html>
<head>
    <title>MJRoBot Lab Sensor Data</title>
    <link rel="stylesheet" href='../static/style.css'/>
</head>
<body>
	<h1>MJRoBot Lab Sensor Data</h1>
	<h3> TEMPERATURE   ==>  {{ temp }} oC</h3>
	<h3> HUMIDITY (Rel.) ==>  {{ hum }} %</h3>
	<hr>
	<h3> Last Sensors Reading: {{ time }} ==> <a href="/"class="button">REFRESH</a></h3>	
	<hr>
	<h3> MJRoBot Lab Live Streaming ==> <a href="/camera" class="button">LIVE</a></h3>
	<hr>	
	<p> @2018 Developed by MJRoBot.org</p>
</body>
</html>

Code snippet #22

Plain text
<html>
  <head>
    <title>MJRoBot Lab Live Streaming</title>
    <link rel="stylesheet" href='../static/style.css'/>
    <style>
	body {
		text-align: center;
	}
    </style>
  </head>
  <body>
    <h1>MJRoBot Lab Live Streaming</h1>
    <h3><img src="{{ url_for('video_feed') }}" width="80%"></h3>
    <h3>{{ time }}</h3>
    <hr>
	<h3> Return to main page ==> <a href="/"class="button">RETURN</a></h3>	
	<hr>
	<p> @2018 Developed by MJRoBot.org</p>
  </body>
</html>

Code snippet #23

Plain text
body{
	background: blue;
	color: yellow;
	padding:1%
}

.button {
	font: bold 15px Arial;
	text-decoration: none;
	background-color: #EEEEEE;
	color: #333333;
	padding: 2px 6px 2px 6px;
	border-top: 1px solid #CCCCCC;
	border-right: 1px solid #333333;
	border-bottom: 1px solid #333333;
	border-left: 1px solid #CCCCCC;
}

Code snippet #24

Plain text
β”œβ”€β”€ camWebServer2
         β”œβ”€β”€ camera_pi.py
         └─── appCam2.py                
                   β”œβ”€β”€ templates
                   β”‚       β”œβ”€β”€ index.html
                   β”‚       β”œβ”€β”€ camera.html
                   └── static
                           └──── style.css

Code snippet #26

Plain text
β”œβ”€β”€ Sensors_Database
       β”œβ”€β”€ sensorsData.db
       β”œβ”€β”€ logDHT.py
       └── dhtWebHistCam
               β”œβ”€β”€ appDhtWebHistCam.py                 
               β”œβ”€β”€ camera_pi.py
               └─── appCam2.py                
                       β”œβ”€β”€ templates
                       β”‚       β”œβ”€β”€ index.html
                       β”‚       β”œβ”€β”€ camera.html
                       └── static
                               β”œβ”€β”€ justgage.js
		               β”œβ”€β”€ raphael-2.1.4.min.js
                               └──── style.css

Github file

https://github.com/Mjrovai/Video-Streaming-with-Flask/blob/master/camWebServer/camera_pi.py

Github

https://github.com/adafruit/Adafruit_Python_DHT

Github file

https://github.com/Mjrovai/RPI-Flask-SQLite/blob/master/suport_files/DHT22_test.py

Credits

MJRoBot (Marcelo Rovai)

MJRoBot (Marcelo Rovai)

60 projects β€’ 912 followers
Professor, Engineer, MBA, Master in Data Science. Writes about Electronics with a focus on Physical Computing, IoT, ML, TinyML and Robotics.
Thanks to Miguel Grinberg.

Comments