streamVideo.py (1305B)
1from flask import Flask, Response
2import cv2
3import numpy as np
4import pyautogui
5import time
6import utility
78
FRAME_RATE = 15
9IP = utility.get_ip_address()
1011
app = Flask(__name__)
1213
SCREEN_SIZE = (1280,720)
14# SCREEN_SIZE = (1920,1080)
1516
def gen_frames():
17while True:
18start_time = time.time() # Record the start time
1920
# Capture the screen
21img = cv2.cvtColor(np.array(pyautogui.screenshot()), cv2.COLOR_RGB2BGR)
2223
# Resize the screenshot to the desired resolution
24img = cv2.resize(img, SCREEN_SIZE)
2526
# Convert the frame to a JPEG image
27ret, buffer = cv2.imencode('.jpg', img)
2829
# Yield the image data as bytes
30yield (b'--frame\r\n'
31b'Content-Type: image/jpeg\r\n\r\n' + buffer.tobytes() + b'\r\n')
3233
# Calculate the time elapsed since the start of the loop
34elapsed_time = time.time() - start_time
3536
# If the elapsed time is less than the desired time per frame, delay the loop
37if elapsed_time < 1 / FRAME_RATE:
38time.sleep(1 / FRAME_RATE - elapsed_time)
3940
@app.route('/')
41def video():
42return Response(gen_frames(),
43mimetype='multipart/x-mixed-replace; boundary=frame')
4445
if __name__ == '__main__':
46print(f'Path: http://{IP}:5000')
47app.run(host=IP)
4849