live camera web app flsak
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Camera Stream App</title>
</head>
<body>
<h1>Camera Stream App</h1>
<form action="{{ url_for('add_camera') }}" method="post">
<label for="rtsp_link">RTSP Link:</label>
<input type="text" id="rtsp_link" name="rtsp_link" required>
<button type="submit">Add Camera</button>
</form>
<div>
{% for camera_id, camera_info in camera_streams.items() %}
<div>
<h3>{{ camera_info.rtsp_link }}</h3>
<img src="{{ url_for('stream', camera_id=camera_id) }}" alt="Camera Stream" width="320" height="240">
<a href="{{ url_for('delete_camera', camera_id=camera_id) }}">Delete</a>
</div>
{% endfor %}
</div>
</body>
</html>
app.py
=================================================================
from flask import Flask, render_template, request, redirect, url_for, Response
import cv2
import uuid
app = Flask(__name__)
camera_streams = {}
@app.route('/')
def index():
return render_template('index.html', camera_streams=camera_streams)
@app.route('/add_camera', methods=['POST'])
def add_camera():
rtsp_link = request.form['rtsp_link']
camera_id = str(uuid.uuid4())
camera_streams[camera_id] = {'rtsp_link': rtsp_link, 'camera': cv2.VideoCapture(rtsp_link)}
return redirect(url_for('index'))
@app.route('/stream/<camera_id>')
def stream(camera_id):
camera = camera_streams[camera_id]['camera']
def generate():
while True:
ret, frame = camera.read()
if not ret:
break
_, jpeg = cv2.imencode('.jpg', frame)
yield (b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + jpeg.tobytes() + b'\r\n')
return Response(generate(), content_type='multipart/x-mixed-replace; boundary=frame')
@app.route('/delete_camera/<camera_id>')
def delete_camera(camera_id):
camera_info = camera_streams.pop(camera_id, None)
if camera_info:
camera_info['camera'].release()
return redirect(url_for('index'))
if __name__ == '__main__':
app.run(debug=True)
Comments
Post a Comment