我为我的计算机视觉项目编写了一个简单的flask应用程序。我使用流响应来流处理的帧。
这是主烧瓶应用程序:
from flask import Flask, render_template, Response
from vid_reader_streamer import Stream
import video_capture_Q_buf as vid_cap_q # import as alias
from video_capture_Q_buf import VideoCaptureQ # class import
import cv2
import numpy as np
import gc
app = Flask(__name__)
stream = Stream()
@app.route('/')
def index():
return render_template('index.html')
def gen(stream):
while True:
frame, frames_left = stream.get_frame() # get processed frame
try:
if frame.shape != (1080, 1920, 3):
# not proper dimension
frame = cv2.resize(frame, (1920, 1080))
except:
print('failed')
frame = np.random.randint(0, 1, (1080,1920,3))
(flag, encodedImage) = cv2.imencode(".jpg", frame)
yield (b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + bytearray(encodedImage) + b'\r\n')
@app.route('/video_feed')
def video_feed():
return Response(gen(stream),
mimetype='multipart/x-mixed-replace; boundary=frame')
if __name__ == '__main__':
app.run(host='0.0.0.0', port=6001, debug=False)
Stream
是将两个深度学习模型加载到GPU(__init__
),从cv2.VideoCapture
读取帧,使用深度学习模型处理该帧并返回处理后的帧的类框架(get_frame()
功能)。
如果我从单个浏览器窗口访问Web应用程序,一切都将正常运行。如果我使用多个Windows /多个浏览器打开应用程序,则该应用程序将崩溃并显示错误“ CUDA错误:遇到了非法的内存访问。”
我跟踪了这个问题,似乎对于每个不同的窗口/浏览器,请求和响应都没有同步,因此对于一个新窗口,一些新请求在运行另一个计算的同时被发送到GPU(或者正在重新加载模型) ),这会破坏程序。
如何使我的应用程序完全同步(所有客户端的输出都相同),以便所有独立于客户端访问该应用程序的用户的流都相同?
我有一些虚拟的修复程序:1.在保存帧的同时单独运行GPU进程,flask应用程序读取最新处理的帧并将其流化。但这是一个不好的补丁,我需要一些建议来改进当前的设计。如果我打错了任何名词,请原谅我,我对Web开发的了解很少。