Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import cv2
- import pyscreenshot as ImageGrab
- class VideoCamera(object):
- def __init__(self):
- # Using OpenCV to capture from device 0. If you have trouble capturing
- # from a webcam, comment the line below out and use a video file
- # instead.
- self.video = cv2.VideoCapture(0)
- # If you decide to use video.mp4, you must have this file in the folder
- # as the main.py.
- # self.video = cv2.VideoCapture('video.mp4')
- def __del__(self):
- self.video.release()
- def get_frame(self):
- success, image = self.video.read()
- # We are using Motion JPEG, but OpenCV defaults to capture raw images,
- # so we must encode it into JPEG in order to correctly display the
- # video stream.
- ret, jpeg = cv2.imencode('.jpg', image)
- return jpeg.tobytes()
- def get_screen_frame(self):
- img = ImageGrab.grab()
- return img.tobytes()
- from flask import Flask, render_template, Response, send_file
- from camera import VideoCamera
- app = Flask(__name__)
- @app.route('/')
- def index():
- return render_template('index.html')
- def gen(camera):
- while True:
- frame = camera.get_frame()
- yield (b'--framern'
- b'Content-Type: image/jpegrnrn' + frame + b'rnrn')
- @app.route('/video_feed')
- def video_feed():
- return Response(gen(VideoCamera()),
- mimetype='multipart/x-mixed-replace; boundary=frame')
- def gen_screen(camera):
- while True:
- frame = camera.get_screen_frame()
- yield (b'--framern'
- b'Content-Type: image/jpegrnrn' + frame + b'rnrn')
- @app.route('/video_feed_screen')
- def video_feed_screen():
- return Response(gen_screen(VideoCamera()),
- mimetype='multipart/x-mixed-replace; boundary=frame')
- if __name__ == '__main__':
- app.run(host='0.0.0.0', debug=True)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement