Script
Script
如何放置
Python
Python
1pipeline = dai.Pipeline()
2script = pipeline.create(dai.node.Script)输入和输出
用法
Python
Python
1script = pipeline.create(dai.node.Script)
2script.setScript("""
3 import time
4 import marshal
5 num = 123
6 node.warn(f"Number {num}") # Print to host
7 x = [1, "Hello", {"Foo": "Bar"}]
8 x_serial = marshal.dumps(x)
9 b = Buffer(len(x_serial))
10 while True:
11 time.sleep(1)
12 b.setData(x_serial)
13 node.io['out'].send(b)
14""")
15script.outputs['out'].link(xout.input)
16
17# ...
18# After initializing the device, enable log levels
19device.setLogLevel(dai.LogLevel.WARN)
20device.setLogOutputLevel(dai.LogLevel.WARN)与 GPIO 接口
Python
1# Module
2import GPIO
3
4# General
5GPIO.setup(gpio, dir, pud, exclusive)
6GPIO.release(gpio)
7GPIO.write(gpio, value)
8GPIO.read(gpio)
9
10# Interrupts
11GPIO.waitInterruptEvent(gpio = -1) # blocks until any interrupt or interrupt by specified gpio is fired. Interrupts with callbacks are ignored here
12GPIO.hasInterruptEvent(gpio = -1) # returns whether interrupt happened on any or specified gpio. Interrupts with callbacks are ignored here
13GPIO.setInterrupt(gpio, edge, priority, callback = None) # adds interrupt to specified pin
14GPIO.clearInterrupt(gpio) # clears interrupt of specified pin
15
16# PWM
17GPIO.setPwm(gpio, highCount, lowCount, repeat=0) # repeat == 0 means indefinite
18GPIO.enablePwm(gpio, enable)
19
20# Enumerations
21GPIO.Direction: GPIO.IN, GPIO.OUT
22GPIO.State: GPIO.LOW, GPIO.HIGH
23GPIO.PullDownUp: GPIO.PULL_NONE, GPIO.PULL_DOWN, GPIO.PULL_UP
24GPIO.Edge: GPIO.RISING, GPIO.FALLING, GPIO.LEVEL_HIGH, GPIO.LEVEL_LOWPython
1import GPIO
2MX_PIN = 40
3
4ret = GPIO.setup(MX_PIN, GPIO.OUT, GPIO.PULL_DOWN)
5toggleVal = True
6
7while True:
8 data = node.io['in'].get() # Wait for a message from the host computer
9
10 node.warn('GPIO toggle: ' + str(toggleVal))
11 toggleVal = not toggleVal
12 ret = GPIO.write(MX_PIN, toggleVal) # Toggle the GPIO时间同步
Python
1import time
2interval = 60
3ctrl = CameraControl()
4ctrl.setCaptureStill(True)
5previous = 0
6while True:
7 time.sleep(0.001)
8
9 tnow_full = Clock.nowHost() # Synced clock with host
10 # Clock.now() -> internal/device clock
11 # Clock.offsetToHost() -> Offset between internal/device clock and host clock
12
13 now = tnow_full.seconds
14 if now % interval == 0 and now != previous:
15 previous = now
16 node.warn(f'{tnow_full}')
17 node.io['out'].send(ctrl)使用 DepthAI 消息
depthai 模块已隐式导入到脚本节点。您可以创建新的 depthai 消息并为其赋值,例如:Python
1buf = Buffer(100) # 为 Buffer 消息分配 100 字节
2
3# 创建 CameraControl 消息,设置手动对焦
4control = CameraControl()
5control.setManualFocus(100)
6
7imgFrame = ImgFrame(300*300*3) # 具有 300x300x3 字节的缓冲区可用模块和库
Text
1"posix", "errno", "pwd", "_sre", "_codecs", "_weakref", "_functools", "_operator",
2"_collections", "_abc", "itertools", "atexit", "_stat", "time", "_datetime", "math",
3"_thread", "_io", "_symtable", "marshal", "_ast", "gc", "_warnings", "_string", "_struct"Text
1"binascii", "_random", "_socket", "_md5", "_sha1", "_sha256", "_sha512", "select",
2"array", "unicodedataText
1"__main__", "_collections_abc", "_frozen_importlib", "_frozen_importlib_external",
2"_sitebuiltins", "abc", "codecs", "datetime", "encodings", "encodings.aliases",
3"encodings.ascii", "encodings.latin_1", "encodings.mbcs", "encodings.utf_8", "genericpath",
4"io", "os", "posixpath", "site", "stat", "threading", "types", "struct", "copyreg",
5"reprlib", "operator", "keyword", "heapq", "collections", "functools", "sre_constants",
6"sre_parse", "sre_compile", "enum", "re", "json", "json.decoder", "json.encoder",
7"json.scanner", "textwrap"Text
1"http", "http.client", "http.server", "html", "mimetypes", "copy", "shutil", "fnmatch",
2"socketserver", "contextlib", "email", "email._encoded_words", "email._header_value_parser",
3"email._parseaddr", "email._policybase", "email.base64mime", "email.charset",
4"email.contentmanager", "email.encoders", "email.errors", "email.feedparser",
5"email.generator", "email.header", "email.headerregistry", "email.iterators", "email.message",
6"email.parser", "email.policy", "email.quoprimime", "email.utils", "string", "base64",
7"quopri", "random", "warnings", "bisect", "hashlib", "logging", "traceback", "linecache",
8"socket", "token", "tokenize", "weakref", "_weakrefset", "collections.abc", "selectors",
9"urllib", "urllib.parse", "calendar", "locale", "uu", "encodings.idna", "stringprep"Python
1script = pipeline.create(dai.node.Script)
2script.setProcessor(dai.ProcessorType.LEON_CSS)功能示例
- 脚本相机控制 - 控制相机
- 脚本获取本地 IP - 获取本地 IP
- 脚本 HTTP 客户端 - 发送 HTTP 请求
- 脚本 TCP 流 - 从 Script 节点内部进行 TCP 通信,可以是主机或客户端模式
- 脚本 MQTT 发布 - 从 Script 节点内部进行 MQTT 发布
- 脚本 HTTP 服务器 - 通过 HTTP 发送静态图像
- 脚本 MJPEG 服务器 - 通过 HTTP 进行 MJPEG 视频流
- 脚本 NNData 示例 - 构建 NNData
- 三角测量实验
- Movenet 解码(边缘模式) - geaxgx 的一个更复杂的示例
参考
class
depthai.node.Script(depthai.Node)
method
getProcessor(self) -> depthai.ProcessorType: depthai.ProcessorTypeGet on which processor the script should run Returns: Processor type - Leon CSS or Leon MSS
method
getScriptName(self) -> str: strGet the script name in utf-8. When name set with setScript() or setScriptPath(), returns that name. When script loaded with setScriptPath() with name not provided, returns the utf-8 string of that path. Otherwise, returns "<script>" Returns: std::string of script name in utf-8
method
setProcessor(self, arg0: depthai.ProcessorType)Set on which processor the script should run Parameter ``type``: Processor type - Leon CSS or Leon MSS
method
method
property
property
需要帮助?
请前往 Discussion Forum 获取技术支持或提出您可能有的任何其他问题。