
安装需要的程序集
pip install Flask mss pyinstaller opencv-python
Python文件内容
from flask import Flask, Response
import cv2
import numpy as np
import mss
app = Flask(__name__)
def generate_frames():
with mss.mss() as sct:
# 获取屏幕的监视器信息
monitor = sct.monitors[1] # 选择第一个监视器
while True:
# 捕获屏幕
img = sct.grab(monitor)
# 将捕获的图像转换为 NumPy 数组
frame = np.array(img)
# 转换颜色通道,从 BGRA 到 BGR
frame = cv2.cvtColor(frame, cv2.COLOR_BGRA2BGR)
# 编码为 JPEG 格式
_, buffer = cv2.imencode('.jpg', frame)
frame = buffer.tobytes()
# 生成视频流
yield (b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')
@app.route('/video_feed')
def video_feed():
return Response(generate_frames(), mimetype='multipart/x-mixed-replace; boundary=frame')
@app.route('/')
def index():
return '''
<html>
<head>
<title>屏幕共享</title>
</head>
<body>
<img src="/video_feed" width="100%">
</body>
</html>
'''
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000)
python -m PyInstaller --onefile --icon=app_icon.ico screen_share.py
下载链接: https://pan.baidu.com/s/1xnfNl8AfJOKzggl_ptDIHg?pwd=uvrg 提取码: uvrg
发表评论