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

本页目录

  • 设置
  • 先决条件
  • 源代码
  • 管道

Script EMMC 访问

此示例演示了如何使用 Script 节点访问设备的 EMMC 内存。EMMC 内存的默认位置是 /media/mmcsd-0-0/。管道中的第一个脚本通过将图像写入 EMMC 内存来工作。 第二个脚本在 :/media/mmcsd-0-0/ 目录上启动一个 Web 服务器,并提供来自 EMMC 内存的图像。

设置

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

先决条件

我们首先需要将 EMMC 内存作为设备上的存储启用。为此,我们需要使用启用了 EMMC 的应用程序刷新设备。示例应用程序:
Command Line
1import depthai as dai
2
3    # 创建管道
4    pipeline = dai.Pipeline()
5
6    # 设置板载配置
7    board = dai.BoardConfig()
8    board.emmc = True
9    config = dai.Device.Config()
10    config.board = board
11    pipeline.setBoardConfig(board)
12
13    (f, bl) = dai.DeviceBootloader.getFirstAvailableDevice()
14    bootloader = dai.DeviceBootloader(bl)
15    progress = lambda p : print(f'闪烁进度: {p*100:.1f}%')
16    (r, errmsg) = bootloader.flash(progress, pipeline, memory=dai.DeviceBootloader.Memory.EMMC)
17    if r: print("闪烁成功")
上面的代码将使用启用脚本节点访问 EMMC 内存的应用程序刷新设备。现在,即使设备处于标准模式(连接到主机 PC),我们也应该能够访问 EMMC 内存。

源代码

Python

Python
GitHub
1#!/usr/bin/env python3
2
3import depthai as dai
4import cv2
5
6# Start defining a pipeline
7pipeline = dai.Pipeline()
8
9board = dai.BoardConfig()
10board.emmc = True
11pipeline.setBoardConfig(board)
12
13# Define source and output
14camRgb = pipeline.create(dai.node.ColorCamera)
15jpegEncoder = pipeline.create(dai.node.VideoEncoder)
16
17# Properties
18camRgb.setResolution(dai.ColorCameraProperties.SensorResolution.THE_4_K)
19jpegEncoder.setDefaultProfilePreset(1, dai.VideoEncoderProperties.Profile.MJPEG)
20
21#Set a write script
22script_write = pipeline.createScript()
23script_write.setProcessor(dai.ProcessorType.LEON_CSS)
24script_write.setScript("""
25
26    import os
27    index = 1000
28    import time
29    while True:
30        # Find an unused file name first
31        while True:
32            path = '/media/mmcsd-0-0/' + str(index) + '.jpg'
33            if not os.path.exists(path):
34                break
35            index += 1
36        frame = node.io['jpeg'].get()
37        node.warn(f'Saving to EMMC: {path}')
38        with open(path, 'wb') as f:
39            f.write(frame.getData())
40        index += 1
41        time.sleep(3)
42
43""")
44                      
45#Set a read script
46script_read = pipeline.createScript()
47script_read.setProcessor(dai.ProcessorType.LEON_CSS)
48script_read.setScript("""
49
50    import http.server
51    import socketserver
52    import socket
53    import fcntl
54    import struct
55    import os
56
57    def get_ip_address(ifname):
58        s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
59        return socket.inet_ntoa(fcntl.ioctl(
60            s.fileno(),
61            -1071617759,  # SIOCGIFADDR
62            struct.pack('256s', ifname[:15].encode())
63        )[20:24])
64
65    # Note: `chdir` here will prevent unmount, this should be improved!
66    os.chdir('/media/mmcsd-0-0')
67
68    PORT = 80
69    Handler = http.server.SimpleHTTPRequestHandler
70
71    with socketserver.TCPServer(("", PORT), Handler) as httpd:
72        ip = get_ip_address('re0')
73        node.warn(f'===== HTTP file server accessible at: http://{ip}')
74        httpd.serve_forever()
75
76""")
77                      
78# Linking
79
80camRgb.video.link(jpegEncoder.input)
81jpegEncoder.bitstream.link(script_write.inputs['jpeg'])
82script_write.inputs['jpeg'].setBlocking(False)
83xout = pipeline.create(dai.node.XLinkOut)
84xout.setStreamName("rgb")
85script_read.outputs['jpeg'].link(xout.input)
86
87
88# Pipeline defined, now the device is connected to
89with dai.Device(pipeline) as device:
90    # Output queue will be used to get the rgb frames from the output defined above
91    qRgb = device.getOutputQueue(name="rgb", maxSize=100, blocking=False)
92
93    while True:
94        inRgb = qRgb.tryGet() 
95        
96        if inRgb is not None:
97            cv2.imshow("rgb", inRgb.getCvFrame())
98            
99        if cv2.waitKey(1) == ord('q'):
100            break

管道

需要帮助?

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