Nav apraksta

app.py 2.0KB

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