【问题标题】:How to stream video (motion jpeg) using falcon server?如何使用 falcon 服务器流式传输视频(运动 jpeg)?
【发布时间】:2018-12-03 23:23:13
【问题描述】:

期望的输出

输入:使用 OpenCV 或来自 REST 摄像头 URL 的摄像头馈送。 (不关心这个问题)

输出:在进行一些 OpenCV 处理后流式传输 jpeg 图像


到目前为止,我已经根据Falcontutorial做了以下工作

输入:图像文件作为 POST 请求

输出:带有图像路径的 GET 请求端点

import mimetypes
import os
import re
import uuid
import cv2
import io
import falcon
from falcon import media
import json
import msgpack

class Collection(object):

    def __init__(self, image_store):
        self._image_store = image_store

    def on_get(self, req, resp):
        # TODO: Modify this to return a list of href's based on
        # what images are actually available.
        doc = {
            'images': [
                {
                    'href': '/images/1eaf6ef1-7f2d-4ecc-a8d5-6e8adba7cc0e.png'
                }
            ]
        }

        resp.data = msgpack.packb(doc, use_bin_type=True)
        resp.content_type = falcon.MEDIA_MSGPACK
        resp.status = falcon.HTTP_200

    def on_post(self, req, resp):
        name = self._image_store.save(req.stream, req.content_type)
        # Unnecessary Hack to read the saved file in OpenCV
        image = cv2.imread("images/" + name)
        new_image = do_something_with_image(image)
        _ = cv2.imwrite("images/" + name, new_image)
        resp.status = falcon.HTTP_201
        resp.location = '/images/' + name


class Item(object):

    def __init__(self, image_store):
        self._image_store = image_store

    def on_get(self, req, resp, name):
        resp.content_type = mimetypes.guess_type(name)[0]
        resp.stream, resp.stream_len = self._image_store.open(name)


class ImageStore(object):

    _CHUNK_SIZE_BYTES = 4096
    _IMAGE_NAME_PATTERN = re.compile(
        '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\.[a-z]{2,4}$'
    )

    def __init__(self, storage_path, uuidgen=uuid.uuid4, fopen=io.open):
        self._storage_path = storage_path
        self._uuidgen = uuidgen
        self._fopen = fopen

    def save(self, image_stream, image_content_type):
        ext = mimetypes.guess_extension(image_content_type) # Issue with this code, Not returning the extension so hard coding it in next line
        ext = ".jpg"
        name = '{uuid}{ext}'.format(uuid=self._uuidgen(), ext=ext)
        image_path = os.path.join(self._storage_path, name)

        with self._fopen(image_path, 'wb') as image_file:
            while True:
                chunk = image_stream.read(self._CHUNK_SIZE_BYTES)
                if not chunk:
                    break

                image_file.write(chunk)

        return name

    def open(self, name):
        # Always validate untrusted input!
        if not self._IMAGE_NAME_PATTERN.match(name):
            raise IOError('File not found')

        image_path = os.path.join(self._storage_path, name)
        stream = self._fopen(image_path, 'rb')
        stream_len = os.path.getsize(image_path)
        return stream, stream_len


def create_app(image_store):
    api = falcon.API()
    api.add_route('/images', Collection(image_store))
    api.add_route('/images/{name}', Item(image_store))
    api.add_sink(handle_404, '')
    return api


def get_app():
    storage_path = os.environ.get('LOOK_STORAGE_PATH', './images')
    image_store = ImageStore(storage_path)
    return create_app(image_store)

响应是这样的:

HTTP/1.1 201 创建

连接:关闭

日期:格林威治标准时间 2018 年 12 月 3 日星期一 13:08:14

服务器:gunicorn/19.7.1

内容长度:0

内容类型:应用程序/json; charset=UTF-8

位置:/images/e69a83ee-b369-47c3-8b1c-60ab7bf875ec.jpg

上面的代码有2个问题:

  1. 我首先获取数据流并将其保存在一个文件中,然后在 OpenCV 中读取它以执行一些其他操作,这些操作相当矫枉过正,应该很容易修复,但我不知道如何
  2. 此服务不流式传输 JPG。我只有一个 GET 请求 url,我可以在浏览器中打开它来查看不理想的图像

那么,如何将 req.stream 数据读取为 numpy 数组?更重要的是,我需要进行哪些更改才能从该服务流式传输图像?

附:抱歉发了这么长的帖子

【问题讨论】:

    标签: python-3.x opencv video-streaming falconframework


    【解决方案1】:

    我找到了一个完美的解决方案。欲了解更多信息,请查看this beautiful code

    def gen(camera):
        while True:
            image = camera.get_frame()
            new_image = do_something_with_image(image)
            ret, jpeg = cv2.imencode('.jpg', new_image)
            yield (b'--frame\r\n'
                   b'Content-Type: image/jpeg\r\n\r\n' + jpeg.tobytes() + b'\r\n\r\n')
    
    
    class StreamResource(object):
        def on_get(self, req, resp):
            labeled_frame = gen(VideoCamera())
            resp.content_type = 'multipart/x-mixed-replace; boundary=frame'
            resp.stream = labeled_frame
    
    def get_app():
        api = falcon.API()
        api.add_route('/feed', StreamResource())
        return api
    

    【讨论】:

    • 它工作了 30 秒,然后我必须重新加载客户端页面以发送新请求,似乎每个请求只能工作 30 秒,知道如何解决这个问题吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-12-26
    • 2019-02-25
    • 2011-01-14
    • 2011-07-01
    • 1970-01-01
    • 2015-10-28
    • 1970-01-01
    相关资源
    最近更新 更多