-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
65 lines (50 loc) · 1.73 KB
/
app.py
File metadata and controls
65 lines (50 loc) · 1.73 KB
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
from flask import Flask, render_template, Response, request
import threading
import time
import cv2
import yaml
from main import process_frame, capture_frames, input_frame_queue, processed_queue, stop_thread, process_frames, display_frames
app = Flask(__name__)
# Load configuration
with open('config.yaml', 'r') as f:
config = yaml.safe_load(f)
# Global flag to stop threads
stop_thread = False
def start_threads(rtsp_url):
capture_thread = threading.Thread(target=capture_frames, args=(rtsp_url,))
process_thread = threading.Thread(target=process_frames)
display_thread = threading.Thread(target=display_frames)
capture_thread.start()
process_thread.start()
display_thread.start()
return [capture_thread, process_thread, display_thread]
@app.route('/')
def index():
return render_template('index.html')
def generate():
while not stop_thread:
if not processed_queue.empty():
frame = processed_queue.get()
ret, jpeg = cv2.imencode('.jpg', frame)
if not ret:
continue
frame = jpeg.tobytes()
yield (b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n\r\n')
@app.route('/video_feed')
def video_feed():
return Response(generate(), mimetype='multipart/x-mixed-replace; boundary=frame')
@app.route('/start', methods=['POST'])
def start():
global stop_thread
stop_thread = False
rtsp_url = request.form.get('rtsp_url', config['video_path'])
start_threads(rtsp_url)
return 'Started'
@app.route('/stop', methods=['POST'])
def stop():
global stop_thread
stop_thread = True
return 'Stopped'
if __name__ == "__main__":
app.run(host='0.0.0.0', port=8089, debug=True)