srivatsavdamaraju commited on
Commit
348b0d0
·
verified ·
1 Parent(s): 1cdf75c

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -0
app.py ADDED
@@ -0,0 +1,50 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from flask import Flask, render_template, Response, request
2
+ from flask_cors import CORS
3
+ import base64
4
+ from threading import Lock
5
+
6
+ app = Flask(__name__)
7
+ CORS(app)
8
+
9
+ # Store the latest frame with thread safety
10
+ latest_frame = None
11
+ frame_lock = Lock()
12
+
13
+ @app.route('/')
14
+ def index():
15
+ return render_template('index.html')
16
+
17
+ @app.route('/receive_frame', methods=['POST'])
18
+ def receive_frame():
19
+ global latest_frame
20
+ try:
21
+ frame_data = request.get_json()['frame']
22
+ with frame_lock:
23
+ latest_frame = frame_data
24
+ return {'status': 'success'}, 200
25
+ except Exception as e:
26
+ return {'status': 'error', 'message': str(e)}, 400
27
+
28
+ @app.route('/video_feed')
29
+ def video_feed():
30
+ def generate_frames():
31
+ global latest_frame
32
+ while True:
33
+ with frame_lock:
34
+ current_frame = latest_frame
35
+
36
+ if current_frame:
37
+ try:
38
+ # Extract the base64 data
39
+ frame_data = current_frame.split(',')[1].encode()
40
+ yield (b'--frame\r\n'
41
+ b'Content-Type: image/jpeg\r\n\r\n' +
42
+ base64.b64decode(frame_data) + b'\r\n')
43
+ except Exception:
44
+ continue
45
+
46
+ return Response(generate_frames(),
47
+ mimetype='multipart/x-mixed-replace; boundary=frame')
48
+
49
+ if __name__ == '__main__':
50
+ app.run(debug=True, threaded=True)