You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
60 lines
1.4 KiB
60 lines
1.4 KiB
from time import sleep
|
|
|
|
import cv2
|
|
import threading
|
|
import time
|
|
|
|
import numpy as np
|
|
import requests
|
|
from flask import Flask, Response, render_template_string, request
|
|
|
|
import utils
|
|
|
|
app_flask = Flask(__name__)
|
|
|
|
# 全局变量,用于存储最新的帧
|
|
latest_frame:np.ndarray = None
|
|
lock = threading.Lock()
|
|
is_running = True
|
|
|
|
def update_latest_frame(n_bytes:np.ndarray):
|
|
global latest_frame
|
|
latest_frame=n_bytes
|
|
|
|
def generate_mjpeg():
|
|
"""生成MJPEG格式的视频流"""
|
|
while is_running:
|
|
with lock:
|
|
if latest_frame is None:
|
|
continue
|
|
_,latest_frame_bytes = utils.frame_to_img(latest_frame)
|
|
frame = latest_frame_bytes.tobytes()
|
|
pass
|
|
sleep(0.1)
|
|
yield (b'--frame\r\n'
|
|
b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')
|
|
|
|
|
|
@app_flask.route('/video_flow')
|
|
def video_feed():
|
|
"""视频流路由"""
|
|
return Response(
|
|
generate_mjpeg(),
|
|
mimetype='multipart/x-mixed-replace; boundary=frame'
|
|
)
|
|
|
|
def run():
|
|
port=2240
|
|
print(f"推流服务启动,访问端口 127.0.0.1:{port}/video_flow")
|
|
app_flask.run(host='0.0.0.0', port=port, threaded=True)
|
|
|
|
def video_server_run():
|
|
thread = threading.Thread(target=run)
|
|
thread.daemon = True # 设置为守护线程,主线程退出时自动终止
|
|
thread.start()
|
|
def stop():
|
|
global is_running
|
|
is_running=False
|
|
|
|
if __name__ == '__main__':
|
|
run()
|