File size: 2,751 Bytes
0b4cb38
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2ea140a
 
 
 
0b4cb38
2ea140a
 
0b4cb38
 
 
 
 
 
 
 
2ea140a
 
 
acaee24
2ea140a
 
 
0b4cb38
2ea140a
 
 
 
 
0b4cb38
2ea140a
 
0b4cb38
2ea140a
0b4cb38
 
 
 
2ea140a
0b4cb38
2ea140a
 
0b4cb38
2ea140a
 
0b4cb38
 
2ea140a
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# from flask import Flask, render_template, request, Response
# import base64
# import cv2
# import numpy as np
# from datetime import datetime

# app = Flask(__name__)
# latest_frame = None  # Store the latest frame

# @app.route('/')
# def index():
#     return render_template('test_streaming_index.html')

# @app.route('/upload_frame', methods=['POST'])
# def upload_frame():
#     global latest_frame
#     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)
#     latest_frame = frame
#     return '', 204

# def stream():
#     global latest_frame
#     while True:
#         if latest_frame is not None:
#             _, buffer = cv2.imencode('.jpg', latest_frame)
#             frame = buffer.tobytes()
#             yield (b'--frame\r\n'
#                    b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')
#         else:
#             yield b''
            
# @app.route('/video_feed')
# def video_feed():
#     return Response(stream(), mimetype='multipart/x-mixed-replace; boundary=frame')

# if __name__ == '__main__':
#     app.run(host='0.0.0.0', port=7860)

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'  # Needed for session management

user_frames = {}  # Dictionary to store frames per user session

@app.before_request
def ensure_session():
    if 'uid' not in session:
        session['uid'] = str(uuid4())  # Unique ID per user

@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  # Store frame per user
    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)