|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+from flask import render_template, Flask, send_from_directory, Response
|
|
|
2
|
+import cv2
|
|
|
3
|
+
|
|
|
4
|
+app = Flask(__name__)
|
|
|
5
|
+
|
|
|
6
|
+
|
|
|
7
|
+@app.after_request
|
|
|
8
|
+def add_header(response):
|
|
|
9
|
+ response.headers['X-UA-Compatible'] = 'IE=Edge,chrome=1'
|
|
|
10
|
+ response.headers['Cache-Control'] = 'no-cache, no-store, must-revalidate'
|
|
|
11
|
+ response.headers['Pragma'] = 'no-cache'
|
|
|
12
|
+ response.headers['Expires'] = '0'
|
|
|
13
|
+ return response
|
|
|
14
|
+
|
|
|
15
|
+
|
|
|
16
|
+def find_camera(id):
|
|
|
17
|
+ '''
|
|
|
18
|
+ cameras = ['rtsp://username:password@ip_address:554/user=username_password='password'_channel=channel_number_stream=0.sdp',
|
|
|
19
|
+ 'rtsp://username:password@ip_address:554/user=username_password='password'_channel=channel_number_stream=0.sdp']
|
|
|
20
|
+ '''
|
|
|
21
|
+ cameras = ['rtsp://admin:@Unv123456@192.168.10.252:554/unicast/c1/s1/live']
|
|
|
22
|
+ return cameras[int(id)]
|
|
|
23
|
+# for cctv camera use rtsp://username:password@ip_address:554/user=username_password='password'_channel=channel_number_stream=0.sdp' instead of camera
|
|
|
24
|
+# for webcam use zero(0)
|
|
|
25
|
+
|
|
|
26
|
+
|
|
|
27
|
+def gen_frames(camera_id):
|
|
|
28
|
+
|
|
|
29
|
+ cam = find_camera(camera_id)
|
|
|
30
|
+ cap= cv2.VideoCapture(cam)
|
|
|
31
|
+
|
|
|
32
|
+ while True:
|
|
|
33
|
+ # for cap in caps:
|
|
|
34
|
+ # # Capture frame-by-frame
|
|
|
35
|
+ success, frame = cap.read() # read the camera frame
|
|
|
36
|
+ if not success:
|
|
|
37
|
+ break
|
|
|
38
|
+ else:
|
|
|
39
|
+ ret, buffer = cv2.imencode('.jpg', frame)
|
|
|
40
|
+ frame = buffer.tobytes()
|
|
|
41
|
+ yield (b'--frame\r\n'
|
|
|
42
|
+ b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n') # concat frame one by one and show result
|
|
|
43
|
+
|
|
|
44
|
+
|
|
|
45
|
+
|
|
|
46
|
+@app.route('/video_feed/<string:id>/', methods=["GET"])
|
|
|
47
|
+def video_feed(id):
|
|
|
48
|
+
|
|
|
49
|
+ """Video streaming route. Put this in the src attribute of an img tag."""
|
|
|
50
|
+ return Response(gen_frames(id),
|
|
|
51
|
+ mimetype='multipart/x-mixed-replace; boundary=frame')
|
|
|
52
|
+
|
|
|
53
|
+
|
|
|
54
|
+@app.route('/')
|
|
|
55
|
+def index():
|
|
|
56
|
+ return render_template('index.html')
|
|
|
57
|
+
|
|
|
58
|
+
|
|
|
59
|
+@app.route('/video/<string:file_name>')
|
|
|
60
|
+def stream(file_name):
|
|
|
61
|
+ video_dir = './video'
|
|
|
62
|
+ return send_from_directory(video_dir, file_name)
|
|
|
63
|
+
|
|
|
64
|
+
|
|
|
65
|
+if __name__ == '__main__':
|
|
|
66
|
+ app.run(debug=True)
|