Spaces:
Sleeping
Sleeping
Update test_streaming.py
Browse files- test_streaming.py +63 -12
test_streaming.py
CHANGED
|
@@ -1,11 +1,61 @@
|
|
| 1 |
-
from flask import Flask, render_template, request, Response
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import base64
|
| 3 |
import cv2
|
| 4 |
import numpy as np
|
| 5 |
from datetime import datetime
|
|
|
|
| 6 |
|
| 7 |
app = Flask(__name__)
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
@app.route('/')
|
| 11 |
def index():
|
|
@@ -13,29 +63,30 @@ def index():
|
|
| 13 |
|
| 14 |
@app.route('/upload_frame', methods=['POST'])
|
| 15 |
def upload_frame():
|
| 16 |
-
|
| 17 |
data_url = request.json['image']
|
| 18 |
header, encoded = data_url.split(",", 1)
|
| 19 |
img_bytes = base64.b64decode(encoded)
|
| 20 |
nparr = np.frombuffer(img_bytes, np.uint8)
|
| 21 |
frame = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
|
| 22 |
-
|
| 23 |
return '', 204
|
| 24 |
|
| 25 |
-
def stream():
|
| 26 |
-
global latest_frame
|
| 27 |
while True:
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
|
|
|
| 31 |
yield (b'--frame\r\n'
|
| 32 |
-
b'Content-Type: image/jpeg\r\n\r\n' +
|
| 33 |
else:
|
| 34 |
yield b''
|
| 35 |
-
|
| 36 |
@app.route('/video_feed')
|
| 37 |
def video_feed():
|
| 38 |
-
|
|
|
|
| 39 |
|
| 40 |
if __name__ == '__main__':
|
| 41 |
app.run(host='0.0.0.0', port=7860)
|
|
|
|
| 1 |
+
# from flask import Flask, render_template, request, Response
|
| 2 |
+
# import base64
|
| 3 |
+
# import cv2
|
| 4 |
+
# import numpy as np
|
| 5 |
+
# from datetime import datetime
|
| 6 |
+
|
| 7 |
+
# app = Flask(__name__)
|
| 8 |
+
# latest_frame = None # Store the latest frame
|
| 9 |
+
|
| 10 |
+
# @app.route('/')
|
| 11 |
+
# def index():
|
| 12 |
+
# return render_template('test_streaming_index.html')
|
| 13 |
+
|
| 14 |
+
# @app.route('/upload_frame', methods=['POST'])
|
| 15 |
+
# def upload_frame():
|
| 16 |
+
# global latest_frame
|
| 17 |
+
# data_url = request.json['image']
|
| 18 |
+
# header, encoded = data_url.split(",", 1)
|
| 19 |
+
# img_bytes = base64.b64decode(encoded)
|
| 20 |
+
# nparr = np.frombuffer(img_bytes, np.uint8)
|
| 21 |
+
# frame = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
|
| 22 |
+
# latest_frame = frame
|
| 23 |
+
# return '', 204
|
| 24 |
+
|
| 25 |
+
# def stream():
|
| 26 |
+
# global latest_frame
|
| 27 |
+
# while True:
|
| 28 |
+
# if latest_frame is not None:
|
| 29 |
+
# _, buffer = cv2.imencode('.jpg', latest_frame)
|
| 30 |
+
# frame = buffer.tobytes()
|
| 31 |
+
# yield (b'--frame\r\n'
|
| 32 |
+
# b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')
|
| 33 |
+
# else:
|
| 34 |
+
# yield b''
|
| 35 |
+
|
| 36 |
+
# @app.route('/video_feed')
|
| 37 |
+
# def video_feed():
|
| 38 |
+
# return Response(stream(), mimetype='multipart/x-mixed-replace; boundary=frame')
|
| 39 |
+
|
| 40 |
+
# if __name__ == '__main__':
|
| 41 |
+
# app.run(host='0.0.0.0', port=7860)
|
| 42 |
+
|
| 43 |
+
from flask import Flask, render_template, request, Response, session
|
| 44 |
import base64
|
| 45 |
import cv2
|
| 46 |
import numpy as np
|
| 47 |
from datetime import datetime
|
| 48 |
+
from uuid import uuid4
|
| 49 |
|
| 50 |
app = Flask(__name__)
|
| 51 |
+
app.secret_key = 'your-secret-key' # Needed for session management
|
| 52 |
+
|
| 53 |
+
user_frames = {} # Dictionary to store frames per user session
|
| 54 |
+
|
| 55 |
+
@app.before_request
|
| 56 |
+
def ensure_session():
|
| 57 |
+
if 'uid' not in session:
|
| 58 |
+
session['uid'] = str(uuid4()) # Unique ID per user
|
| 59 |
|
| 60 |
@app.route('/')
|
| 61 |
def index():
|
|
|
|
| 63 |
|
| 64 |
@app.route('/upload_frame', methods=['POST'])
|
| 65 |
def upload_frame():
|
| 66 |
+
uid = session['uid']
|
| 67 |
data_url = request.json['image']
|
| 68 |
header, encoded = data_url.split(",", 1)
|
| 69 |
img_bytes = base64.b64decode(encoded)
|
| 70 |
nparr = np.frombuffer(img_bytes, np.uint8)
|
| 71 |
frame = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
|
| 72 |
+
user_frames[uid] = frame # Store frame per user
|
| 73 |
return '', 204
|
| 74 |
|
| 75 |
+
def stream(uid):
|
|
|
|
| 76 |
while True:
|
| 77 |
+
frame = user_frames.get(uid)
|
| 78 |
+
if frame is not None:
|
| 79 |
+
_, buffer = cv2.imencode('.jpg', frame)
|
| 80 |
+
frame_bytes = buffer.tobytes()
|
| 81 |
yield (b'--frame\r\n'
|
| 82 |
+
b'Content-Type: image/jpeg\r\n\r\n' + frame_bytes + b'\r\n')
|
| 83 |
else:
|
| 84 |
yield b''
|
| 85 |
+
|
| 86 |
@app.route('/video_feed')
|
| 87 |
def video_feed():
|
| 88 |
+
uid = session['uid']
|
| 89 |
+
return Response(stream(uid), mimetype='multipart/x-mixed-replace; boundary=frame')
|
| 90 |
|
| 91 |
if __name__ == '__main__':
|
| 92 |
app.run(host='0.0.0.0', port=7860)
|