跳转至

开始

引言

在本教程中,我们将视频流连接到"BrainFrame"服务器. 您可以在我们的 GitHub存储库.

在我们开始之前,你应该安装了BrainFrame Server和 Client 如果您还没有,请跟随 设置说明.

这本教程将使用我们的 Python 图书馆 这包裹着 BrainFrame REST API. 库在 Python 中编程了 BrainFrame 如果您使用Python,我们强烈建议您使用我们的Python API. 否则,你总是可以跟随我们的 REST API文件 用 REST API直接使用.

设置环境

首先,让我们安装了BrainFramePython API库, 运行下列命令.你可以在虚拟中执行 环境 (建议) 或根源环境.

pip3 install brainframe-api

Python API现在已经安装,准备好使用.

在本教程中将使用以下API:

  • api.get_stream_configurations()
  • api.set_stream_configuration(...)
  • api.start_analyzing(stream_id=...)

检查现有流量

现在让我们创建一个新的,空白的脚本. 导入了Python API库.

from pathlib import Path
from brainframe.api import BrainFrameAPI, bf_codecs

然后,将一个API实例启动,使用BrainFrame服务器URL. 导读,我们将连接到我们本地运行的 BrainFrame服务器实例 机器.

api = BrainFrameAPI("http://localhost")

现在服务器已经连接了,我们可以开始使用 BrainFrame. 让我们看看是否已经连接到"BrainFrame"的任何流.

stream_configs = api.get_stream_configurations()
print("Existing streams: ", stream_configs)

如果运行脚本,只有一台新安装的 BrainFrame 服务器, 您应该看到一个空白的列表.否则,您的流程列表 已经连接的将出现.

创建一个新的流程编程器

接下来,我们将创建一个新的流设置. api.set_stream_configuration(...)根据功能,只需要一个 作为输入的流配置编程器. 在Python 库中的编码 文件.

目前,我们支持3种类型的视频来源:

  • 专用摄像头
  • 网络摄像头
  • 地方文件

对于不同类型的视频源,你需要设置不同的连接 更多信息请查看此处 文件.

专业IP摄像头

对于IP相机,连接类型将是 IP_CAMERA在" connection_options有效的 url 需要的.

# Create a new IP camera StreamConfiguration codec
new_ip_camera_stream_config = bf_codecs.StreamConfiguration(
    # The display name on the client/in API responses
    name="IP Camera",
    connection_type=bf_codecs.StreamConfiguration.ConnType.IP_CAMERA,
    connection_options={
        # The url of the IP camera
        "url": "your_ip_camera_url",
    },
    runtime_options={},
    premises_id=None,
)

网络摄像头

对于Webcam,连接类型是 WEBCAM注: 这必须连接到 服务器,而不是客户端. connection_options设备的ID 在Linux上,你可以使用:

ls /dev/ | grep video

在你得到设备身份证后,用它在代码中.

# Create a local file StreamConfiguration codec
new_web_camera_stream_config = bf_codecs.StreamConfiguration(
    # The display name on the client/in API responses
    name="Webcam",
    connection_type=bf_codecs.StreamConfiguration.ConnType.WEBCAM,
    connection_options={
        # The device ID of the web camera
        "device_id": 0,
    },
    runtime_options={},
    premises_id=None,
)

地方文件

对于本地文件,你必须先将视频文件上传到BrainFrame 服务器的数据库,并获得存储 ID. FILE在" connection_options文件的存储身份证.

# Upload the local file to the database and create a storage id
storage_id = api.new_storage(
    data=Path("../videos/shopping_cashier_gone.mp4").read_bytes(),
    mime_type="application/octet-stream"
)
# Create a local file stream configuration codec
new_local_file_stream_config = bf_codecs.StreamConfiguration(
    # The display name on the client side
    name="Local File",
    connection_type=bf_codecs.StreamConfiguration.ConnType.FILE,
    # The storage id of the file
    connection_options={
        "storage_id": storage_id,
    },
    runtime_options={},
    premises_id=None,
)

在服务器侧创建流设置

一旦我们有了 StreamConfiguration 编程器,我们可以告诉 BrainFrame Server 在本教程中,我们将使用基于文件的编程. 您可以尝试使用这些作为 现在,我知道.

# Tell the server to connect to the stream configuration
new_local_file_stream_config = api.set_stream_configuration(
    new_local_file_stream_config)

一旦服务器接收了流设置,它将连接到它,分配 通过一个流 ID,然后把它送回. 您使用返回值添加的流.

最后,不要忘记告诉"BrainFrame"开始分析/预制推断 在河流上.

# Start analysis on the stream
api.start_analyzing(new_local_file_stream_config.id)

现在你应该能够在"BrainFrame"客户端看到这个流.