Advertisement
Guest User

Untitled

a guest
Dec 4th, 2016
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.71 KB | None | 0 0
  1. import cv2
  2. import pyscreenshot as ImageGrab
  3.  
  4. class VideoCamera(object):
  5. def __init__(self):
  6. # Using OpenCV to capture from device 0. If you have trouble capturing
  7. # from a webcam, comment the line below out and use a video file
  8. # instead.
  9. self.video = cv2.VideoCapture(0)
  10. # If you decide to use video.mp4, you must have this file in the folder
  11. # as the main.py.
  12. # self.video = cv2.VideoCapture('video.mp4')
  13.  
  14. def __del__(self):
  15. self.video.release()
  16.  
  17. def get_frame(self):
  18. success, image = self.video.read()
  19. # We are using Motion JPEG, but OpenCV defaults to capture raw images,
  20. # so we must encode it into JPEG in order to correctly display the
  21. # video stream.
  22. ret, jpeg = cv2.imencode('.jpg', image)
  23. return jpeg.tobytes()
  24.  
  25. def get_screen_frame(self):
  26. img = ImageGrab.grab()
  27.  
  28. return img.tobytes()
  29.  
  30. from flask import Flask, render_template, Response, send_file
  31. from camera import VideoCamera
  32.  
  33. app = Flask(__name__)
  34.  
  35. @app.route('/')
  36. def index():
  37. return render_template('index.html')
  38.  
  39. def gen(camera):
  40. while True:
  41. frame = camera.get_frame()
  42. yield (b'--framern'
  43. b'Content-Type: image/jpegrnrn' + frame + b'rnrn')
  44.  
  45. @app.route('/video_feed')
  46. def video_feed():
  47. return Response(gen(VideoCamera()),
  48. mimetype='multipart/x-mixed-replace; boundary=frame')
  49.  
  50. def gen_screen(camera):
  51. while True:
  52. frame = camera.get_screen_frame()
  53. yield (b'--framern'
  54. b'Content-Type: image/jpegrnrn' + frame + b'rnrn')
  55.  
  56. @app.route('/video_feed_screen')
  57. def video_feed_screen():
  58. return Response(gen_screen(VideoCamera()),
  59. mimetype='multipart/x-mixed-replace; boundary=frame')
  60.  
  61. if __name__ == '__main__':
  62. app.run(host='0.0.0.0', debug=True)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement