flask读取服务器信息,使用OpenCV(Python)从Flask服务器读取MJPEG流

我正在使用Flask和flask-restful生成一个MJPEG流。出于原因,我想在另一个Python程序中捕获此流,我使用OpenCV(3)。

问题是请求的第一帧进展顺利。另一方面,未正确接收请求的第二帧(延迟之后),并抛出错误:

[mpjpeg @ 0000017a86f524a0] Expected boundary '--' not found, instead found a line of 82 bytes

多次。

我相信这是因为手动设置了帧的边界。我将把违规代码放在下面。

MJPEG流生成:

## Controller for the streaming of content.

class StreamContent(Resource):

@classmethod

def setVis(self, vis):

self.savedVis = vis

def get(self):

return Response(gen(VideoCamera(self.savedVis)),

mimetype='multipart/x-mixed-replace; boundary=frame')

## Generate a new VideoCamera and stream the retrieved frames.

def gen(camera):

frame = camera.getFrame()

while frame != None:

yield (b'--frame\r\n'

b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n\r\n')

time.sleep(0.07)

frame = camera.getFrame()

## Allows for the reading of video frames.

class VideoCamera(object):

def __init__(self, vis):

#object we retrieve the frame from.

self.vis = vis

## Get the current frame.

def getFrame(self):

image = self.vis.mat_frame_with_overlay

# We are using Motion JPEG, but OpenCV defaults to capture raw images,

# so we must encode it into JPEG in order to correctly display the

# video/image stream.

ret, jpeg = cv2.imencode('.jpg', image)

return jpeg.tobytes()

MJPEG流检索:

"""

Get a single frame from the camera.

"""

class Image(Resource):

def get(self):

camera = VideoCamera()

return Response(camera.getSingleFrame(), mimetype='image/jpeg')

"""

Contains methods for retrieving video information from a source.

"""

class VideoCamera(object):

def __del__(self):

self.video.release()

@classmethod

def setVideo(self, video):

self.video = video

## Get the current frame.

def getSingleFrame(self):

self.startVideoFromSource(self.video)

ret, image = self.video.read()

time.sleep(0.5)

ret, image = self.video.read()

# We are using Motion JPEG, but OpenCV defaults to capture raw images,

# so we must encode it into JPEG in order to correctly display the

# video/image stream.

ret, jpeg = cv2.imencode('.jpg', image)

self.stopVideo()

return jpeg.tobytes()

def stopVideo(self):

self.video.release()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值