DepthAI v2 has been superseded by DepthAI v3. You are viewing legacy documentation.
此页面由 AI 自动翻译。查看英文原版
DepthAI 教程
DepthAI API 参考

本页目录

  • 演示
  • 设置
  • 源代码
  • Pipeline

脚本获取本地 IP

此示例演示了如何获取设备的本地 IP(私有网络 中的 IP)。

演示

Command Line
1~/depthai-python/examples/Script$ python3 script_get_ip.py
2    Found device with name: 14442C1031425FD700-ma2480
3    Version: 0.0.15
4
5    Names of layers: ['fp16', 'uint8']
6    NNData size: 13
7    FP16 values: [1.0, 1.2001953125, 3.900390625, 5.5]
8    UINT8 values: [6, 9, 4, 2, 0]

设置

请运行 安装脚本 以下载所有必需的依赖项。请注意,此脚本必须在 git 上下文中运行,因此您必须先下载 depthai-python 存储库,然后运行脚本
Command Line
1git clone https://github.com/luxonis/depthai-python.git
2cd depthai-python/examples
3python3 install_requirements.py
有关更多信息,请遵循 安装指南

源代码

Python

Python
GitHub
1#!/usr/bin/env python3
2import depthai as dai
3
4# Start defining a pipeline
5pipeline = dai.Pipeline()
6
7# Script node
8script = pipeline.create(dai.node.Script)
9script.setProcessor(dai.ProcessorType.LEON_CSS)
10script.setScript("""
11import socket
12import fcntl
13import struct
14
15def get_ip_address(ifname):
16    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
17    return socket.inet_ntoa(fcntl.ioctl(
18        s.fileno(),
19        -1071617759,  # SIOCGIFADDR
20        struct.pack('256s', ifname[:15].encode())
21    )[20:24])
22
23ip = get_ip_address('re0')  # '192.168.0.110'
24node.warn(f'IP of the device: {ip}')
25node.io['end'].send(Buffer(32))
26""")
27
28xout = pipeline.create(dai.node.XLinkOut)
29xout.setStreamName('end')
30script.outputs['end'].link(xout.input)
31
32# Connect to device with pipeline
33with dai.Device(pipeline) as device:
34    device.getOutputQueue("end").get() # Wait for the "end" msg

C++

1#include <iostream>
2
3// Includes common necessary includes for development using depthai library
4#include "depthai/depthai.hpp"
5
6int main() {
7    using namespace std;
8
9    // Start defining a pipeline
10    dai::Pipeline pipeline;
11
12    // Script node
13    auto script = pipeline.create<dai::node::Script>();
14    script->setProcessor(dai::ProcessorType::LEON_CSS);
15    script->setScript(R"(
16    import socket
17    import fcntl
18    import struct
19
20    def get_ip_address(ifname):
21        s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
22        return socket.inet_ntoa(fcntl.ioctl(
23            s.fileno(),
24            -1071617759,  # SIOCGIFADDR
25            struct.pack('256s', ifname[:15].encode())
26        )[20:24])
27
28    ip = get_ip_address('re0')  # '192.168.0.110'
29    node.warn(f'IP of the device: {ip}')
30    node.io['end'].send(Buffer(32))
31    )");
32
33    // XLinkOut
34    auto xout = pipeline.create<dai::node::XLinkOut>();
35    xout->setStreamName("end");
36    script->outputs["end"].link(xout->input);
37
38    // Connect to device with pipeline
39    dai::Device device(pipeline);
40    device.getOutputQueue("end")->get<dai::Buffer>();
41    return 0;
42}

Pipeline

需要帮助?

请前往 Discussion Forum 获取技术支持或提出您可能有的任何其他问题。