用Python中的tornado为MJPEG流提供服务
我试图在这里使用代码:使用我自己的OpenCV例程来显示MJPG流 我顺利地设置了路由用Python中的tornado为MJPEG流提供服务,python,tornado,mjpeg,Python,Tornado,Mjpeg,我试图在这里使用代码:使用我自己的OpenCV例程来显示MJPG流 我顺利地设置了路由 import threading import time import tornado.ioloop import tornado.web from tornado import gen import image_utils class WebServer(threading.Thread): def run(self): paths = [(r"/(.*)\.jpg", WebJPEGHandl
import threading
import time
import tornado.ioloop
import tornado.web
from tornado import gen
import image_utils
class WebServer(threading.Thread):
def run(self):
paths = [(r"/(.*)\.jpg", WebJPEGHandler),
(r"/(.*)\.mjpg", WebMJPEGHandler)]
web = tornado.web.Application(paths,debug=True)
web.listen(listening_port)
tornado.ioloop.IOLoop.instance().start()
JPEG代码运行良好
class WebJPEGHandler(tornado.web.RequestHandler):
@tornado.web.asynchronous
@gen.coroutine
def get(self, quality):
image = image_utils.image_utils.getWebEncodedImage((detector.imageQ[-1]))
self.set_header("Content-type", "image/jpg")
self.write(str(image))
yield gen.Task(self.flush)
但我自己的MJPG代码实现(我也尝试了一些变体)会导致一个网页无法从Chrome获得消息
class WebMJPEGHandler(tornado.web.RequestHandler):
@tornado.web.asynchronous
@gen.coroutine
def get(self, quality):
loop = tornado.ioloop.IOLoop.current()
self.served_image_timestamp = time.time()
self.set_header( 'Content-Type', 'multipart/x-mixed-replace;boundary=--boundarydonotcross')
my_boundary = "--boundarydonotcross\n"
while True:
img = image_utils.image_utils.getWebEncodedImage((detector.imageQ[-1]))
self.write(my_boundary)
self.write("Content-type: image/jpg")
self.write('Content-length: ' + str(len(img)))
self.write(str(img))
self.served_image_timestamp = time.time()
print 'Image sent'
time.sleep(0.5)
yield gen.Task(self.flush)
这可能是我错过的一些有边界和头球的魔术,但请帮我弄清楚它是什么?Tornado本身即使在调试模式下也不会给出错误
通过一些打印语句,我可以看到当我转到127.0.0.1:5000/test.mjpg时,它在循环中出现了2到4次,但随后停止,没有显示任何内容。我对MJPEG一无所知,但您缺少的一点是内容类型和内容长度内部标题后的换行符(可能是内容长度和内容之间的第二个换行符)。我尝试了你的换行符建议,但不幸的是没有改变任何东西。结果证明问题出在chrome上!按照Ben的建议添加新行使代码在Firefox中工作,但chrome需要整个流作为MJPEG流的img标记中的源。