我在FLASK中找到了一个例子,在Web浏览器中通过RTSP协议传输摄像机。
我试着在fastapi中使用同样的例子,但是我没有得到它,图像被冻结了。
flask 中的示例(正常工作):
def generate():
# grab global references to the output frame and lock variables
global outputFrame, lock
# loop over frames from the output stream
while True:
# wait until the lock is acquired
with lock:
# check if the output frame is available, otherwise skip
# the iteration of the loop
if outputFrame is None:
continue
# encode the frame in JPEG format
(flag, encodedImage) = cv2.imencode(".jpg", outputFrame)
# ensure the frame was successfully encoded
if not flag:
continue
# yield the output frame in the byte format
yield (b'--frame\r\n' b'Content-Type: image/jpeg\r\n\r\n' +
bytearray(encodedImage) + b'\r\n')
@app.route("/")
def video_feed():
# return the response generated along with the specific media
# type (mime type)
return Response(generate(),
mimetype="multipart/x-mixed-replace; boundary=frame")
我试着在FastAPI中这样做,但是只有第一帧出现,被冻结了。
def generate():
# grab global references to the output frame and lock variables
global outputFrame, lock
# loop over frames from the output stream
while True:
# wait until the lock is acquired
with lock:
# check if the output frame is available, otherwise skip
# the iteration of the loop
if outputFrame is None:
continue
# encode the frame in JPEG format
(flag, encodedImage) = cv2.imencode(".jpg", outputFrame)
# ensure the frame was successfully encoded
if not flag:
continue
# yield the output frame in the byte format
yield b''+bytearray(encodedImage)
@app.get("/")
def video_feed():
# return the response generated along with the specific media
# type (mime type)
# return StreamingResponse(generate())
return StreamingResponse(generate(), media_type="image/jpeg")
有人知道如何在fastapi中使用相同的函数吗?
如果有人对完整的代码感兴趣,我举了下面的例子:https://www.pyimagesearch.com/2019/09/02/opencv-stream-video-to-web-browser-html-page/
3条答案
按热度按时间7kqas0il1#
在这里张贴后,我想出了如何修复它。
在video_feed函数中,在media_type参数中,只是按照与 flask 中相同的方式放置:
在函数中生成:
我的完整代码:
http://github.com/mpimentel04/rtsp_fastapi
bzzcjhmw2#
范围请求(适用于视频/PDF等...)
用法
yebdmbv43#
答案很简单:
用途
超文本标记语言