|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
from flask import Flask, render_template, request, Response, session |
|
|
import base64 |
|
|
import cv2 |
|
|
import numpy as np |
|
|
from datetime import datetime |
|
|
from uuid import uuid4 |
|
|
|
|
|
app = Flask(__name__) |
|
|
app.secret_key = 'your-secret-key' |
|
|
|
|
|
user_frames = {} |
|
|
|
|
|
@app.before_request |
|
|
def ensure_session(): |
|
|
if 'uid' not in session: |
|
|
session['uid'] = str(uuid4()) |
|
|
|
|
|
@app.route('/') |
|
|
def index(): |
|
|
return render_template('test_streaming_index.html') |
|
|
|
|
|
@app.route('/upload_frame', methods=['POST']) |
|
|
def upload_frame(): |
|
|
uid = session['uid'] |
|
|
data_url = request.json['image'] |
|
|
header, encoded = data_url.split(",", 1) |
|
|
img_bytes = base64.b64decode(encoded) |
|
|
nparr = np.frombuffer(img_bytes, np.uint8) |
|
|
frame = cv2.imdecode(nparr, cv2.IMREAD_COLOR) |
|
|
user_frames[uid] = frame |
|
|
return '', 204 |
|
|
|
|
|
def stream(uid): |
|
|
while True: |
|
|
frame = user_frames.get(uid) |
|
|
if frame is not None: |
|
|
_, buffer = cv2.imencode('.jpg', frame) |
|
|
frame_bytes = buffer.tobytes() |
|
|
yield (b'--frame\r\n' |
|
|
b'Content-Type: image/jpeg\r\n\r\n' + frame_bytes + b'\r\n') |
|
|
else: |
|
|
yield b'' |
|
|
|
|
|
@app.route('/video_feed') |
|
|
def video_feed(): |
|
|
uid = session['uid'] |
|
|
return Response(stream(uid), mimetype='multipart/x-mixed-replace; boundary=frame') |
|
|
|
|
|
if __name__ == '__main__': |
|
|
app.run(host='0.0.0.0', port=7860) |
|
|
|