-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
61 lines (50 loc) · 1.52 KB
/
app.py
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
import cv2
import numpy
import time
import threading
from flask import Flask, render_template, Response, stream_with_context, request
app = Flask(__name__)
video = None
frames = None
thread_lock = threading.Lock()
stop_flag = threading.Event()
def read_frames():
global video, frames, stop_flag
while not stop_flag.is_set():
if not video.isOpened():
continue
ret, frame = video.read()
if not ret:
print('Failed to read stream')
continue
with thread_lock:
frames = frame
def release_video():
global video
if video is not None:
print("Release video")
video.release()
def video_stream():
while True:
with thread_lock:
frame = frames
if frame is None:
continue
ret, buffer = cv2.imencode('.jpeg',frame)
if not ret:
print('Failed to encode frame')
continue
frame = buffer.tobytes()
yield (b' --frame\r\n' b'Content-type: image/jpeg\r\n\r\n' + frame +b'\r\n')
@app.route('/video_feed')
def video_feed():
return Response(video_stream(), mimetype='multipart/x-mixed-replace; boundary=frame')
if __name__ == '__main__':
video = cv2.VideoCapture('udp://127.0.0.1:5800?fifo_size=5000000')
frame_thread = threading.Thread(target=read_frames, daemon=True)
frame_thread.start()
app.run(host='0.0.0.0', port='5700', debug=False)
print("Stopping frame_thread")
stop_flag.set()
frame_thread.join()
release_video()