sipeed wiki logo

wiki

  • 产品
  • 软件文档
    • MaixPy
    • MaixPy3
    • Lichee
    • AI 指南
  • 在线服务
    • MaixHub: AI 训练和分享平台
    • 下载中心
    • 社区: 沟通交流
  • 博文精选
  • 线上商城
  • 贡献文档
  • Github
  • Language: 中文 简体
    • English
    • 中文 简体
  • Translate
  • 搜索
    输入关键词,多关键词空格隔开 正在加载,请稍候。。。 下载文件失败,请刷新重试或检查网络 来自其它文档的结果 当前文档搜索结果
  • 产品总览
  • Maix Zero
    • Maix M0
      • M0sense 开发板
      • M0sense 基础使用
    • Maix M0S
  • Maix-I
    • Maix M1
      • M1 模块
        • M1/M1w
        • M1n
      • M1 开发板
        • Maix Bit
        • Maix Dock(M1/M1W)
        • Maix Duino
        • Maix Nano
        • Maix Cube
        • Maix Amigo
        • Maix HAT
        • Maix Go(售罄)
    • Maix M1s
      • M1s 模块
      • M1s 开发板
        • 简介
        • 上手使用
        • 其他事项
      • M1s 获取机器码
      • M1s 常见问题
    • Maix M1a
  • Maix-II
    • MaixII-Dock
      • 烧录系统
      • 基础使用
      • 其他事项
      • 连接MaixPy3
    • MaixII-S
      • 板级资源介绍
    • MaixII-Sense
      • 烧录系统
      • 配置系统
      • 基本使用
      • 连接MaixPy3
  • Maix-III
    • Maix-III AXera-Pi
      • 产品上手指南
      • 系统使用手册
      • 试试 Python 编程
      • 开始 C/C++ 编程
      • AI 开发指南
      • 常见问题(FAQ)
  • LicheePi
    • LicheePI Zero
    • LicheePI Nano
    • LicheePI ZeroPlus
    • LicheePI RV
      • 版型介绍
        • LicheePI RV 86 Panel
        • LicheePI RV Dock
      • 烧录系统
      • 基础上手
      • 常见问题
      • 使用 Ubuntu 镜像
  • Tang FPGA
    • Tang Nano 系列
      • Tang Nano 1K
      • Tang Nano 4k
      • Tang Nano 9K
      • Tang Nano (售罄)
      • 安装 IDE
      • 相关例程
      • 常见问题
    • Tang Primer
      • Tang Primer 20K
        • 开箱指南
        • 安装 IDE
        • 例程指南
        • 常见问题
      • Tang Primer (售罄)
        • 前言
        • 安装TD
          • Windows安装TD
          • Linux安装TD
        • 使用FPGA点灯
        • 搭建蜂鸟开发环境
        • FPGA码流烧录
  • MaixSense
    • MaixSense-a010
    • MaixSense-a075v
  • Longon
    • Longon Nano
      • PIO配置
      • 点灯测试
      • DEBUG 调试
        • 使用 Sipeed 调试器
        • 使用 RV-LINK
      • 示例
        • 串口输出
        • 播放 Badapple
  • MaixFace
    • MF-ST40 人脸识别模块
    • MF 资料
      • MF 人脸识别模块 PCB 设计注意事项
      • 点我前往 github 查看全部资料
  • 外设模组
    • SP-MOD外设模组
      • 转接板类
        • SP-Extender
        • SP-Grove
        • SP-FPC
        • SP-MicArray
        • SP-JotStick
        • SP-Servo
        • SP-TypeC
      • 传感器类
        • SP-Weather
        • SP-TOF-1P
      • 通信类
        • SP-BT
        • SP-LoRa
        • SP-PSRAM
        • SP-RFID
        • SP-Ethernet
      • 显示类
        • SP-LCD 1.14
        • SP-Eink
    • 模块 & 调试器
      • 调试器 & 串口模块
      • 模块
        • 摄像头
        • 麦克风模块
          • 麦克风阵列
        • Sipeed Gamepad

2022-09-26
编辑本页

Frame encode and decode impl

structure of one frame

packet field size type description
frame frame_id 8B uint64 indicate which frame is it
frame_stamp 8B uint64 xxx ms passed
config 12B config info needed by decode
deep_size 4B int32 size of deepth data
rgb_size 4B int32 size of rgb data
payload adaptive bytes payload data, size is not fixed
config trigger_mode 1B uint8 0:STOP 1:AUTO 2:SINGLE
deep_mode 1B uint8 0:16bit 1:8bit
deep_shift 1B uint8 shift xxx from 16bit, just working for 8bit mode
ir_mode 1B uint8 0:16bit 1:8bit
status_mode 1B uint8 0:16bit 1:2bit 2:8bit 3:1bit
status_mask 1B uint8 just working for 1bit mode 1:1 2:2 4:3
rgb_mode 1B uint8 0:YUV 1:JPG 2:NULL
rgb_res 1B uint8 0:800*600 1:1600*1200
expose_time 4B int32 expose time of this tof, 0 means AE(auto expose)
In [ ]:
import struct
import numpy as np
import cv2


def frame_config_decode(frame_config):
    '''
        @frame_config bytes

        @return fields, tuple (trigger_mode, deep_mode, deep_shift, ir_mode, status_mode, status_mask, rgb_mode, rgb_res, expose_time)
    '''
    return struct.unpack("> with_config[1]
    deepth_img = struct.unpack("<%us" % deepth_size, frame_payload[:deepth_size])[
        0] if 0 != deepth_size else None
    frame_payload = frame_payload[deepth_size:]

    # 0:16bit 1:8bit, resolution: 320*240
    ir_size = (320*240*2) >> with_config[3]
    ir_img = struct.unpack("<%us" % ir_size, frame_payload[:ir_size])[
        0] if 0 != ir_size else None
    frame_payload = frame_payload[ir_size:]

    status_size = (320*240//8) * (16 if 0 == with_config[4] else
                                  2 if 1 == with_config[4] else 8 if 2 == with_config[4] else 1)
    status_img = struct.unpack("<%us" % status_size, frame_payload[:status_size])[
        0] if 0 != status_size else None
    frame_payload = frame_payload[status_size:]

    assert(deep_data_size == deepth_size+ir_size+status_size)

    rgb_size = len(frame_payload)
    assert(rgb_data_size == rgb_size)
    rgb_img = struct.unpack("<%us" % rgb_size, frame_payload[:rgb_size])[
        0] if 0 != rgb_size else None

    if (not rgb_img is None):
        if (1 == with_config[6]):
            jpeg = cv2.imdecode(np.frombuffer(
                rgb_img, 'uint8', rgb_size), cv2.IMREAD_COLOR)
            if not jpeg is None:
                rgb = cv2.cvtColor(jpeg, cv2.COLOR_BGR2RGB)
                rgb_img = rgb.tobytes()
            else:
                rgb_img = None
        # elif 0 == with_config[6]:
        #     yuv = np.frombuffer(rgb_img, 'uint8', rgb_size)
        #     print(len(yuv))
        #     if not yuv is None:
        #         rgb = cv2.cvtColor(yuv, cv2.COLOR_YUV420P2RGB)
        #         rgb_img = rgb.tobytes()
        #     else:
        #         rgb_img = None

    return (deepth_img, ir_img, status_img, rgb_img)

Frame capture from network impl

In [15]:
import numpy as np
import matplotlib.pyplot as plt
import cv2
import requests

HOST = '192.168.233.1'
PORT = 80

def post_encode_config(config=frame_config_encode(),host=HOST, port=PORT):
    r = requests.post('http://{}:{}/set_cfg'.format(host, port), config)
    if(r.status_code == requests.codes.ok):
        return True
    return False

def post_CameraParmsBytes(cameraParms:bytes,host=HOST, port=PORT):
    r = requests.post('http://{}:{}/calibration'.format(host, port), cameraParms)
    if(r.status_code == requests.codes.ok):
        print("ok")

def get_frame_from_http(host=HOST, port=PORT):
    r = requests.get('http://{}:{}/getdeep'.format(host, port))
    if(r.status_code == requests.codes.ok):
        print('Get deep image')
        deepimg = r.content
        print('Length={}'.format(len(deepimg)))
        (frameid, stamp_msec) = struct.unpack('

Example of how to show one frame

  • network example(2D)
  • local file example(2D, 3D)

show frame impl

In [ ]:
import numpy as np
import matplotlib.pyplot as plt
import cv2

def show_frame(frame_data: bytes):
    config = frame_config_decode(frame_data[16:16+12])
    frame_bytes = frame_payload_decode(frame_data[16+12:], config)

    depth = np.frombuffer(frame_bytes[0], 'uint16' if 0 == config[1] else 'uint8').reshape(
        240, 320) if frame_bytes[0] else None

    ir = np.frombuffer(frame_bytes[1], 'uint16' if 0 == config[3] else 'uint8').reshape(
        240, 320) if frame_bytes[1] else None

    status = np.frombuffer(frame_bytes[2], 'uint16' if 0 == config[4] else 'uint8').reshape(
        240, 320) if frame_bytes[2] else None

    rgb = np.frombuffer(frame_bytes[3], 'uint8').reshape(
        (480, 640, 3) if config[6] == 1 else (600, 800, 3)) if frame_bytes[3] else None

    figsize = (12, 12)
    fig = plt.figure(figsize=figsize)

    ax1 = fig.add_subplot(221)
    if not depth is None:
        ax1.imshow(depth)
        # np.save("fg1.npy", depth)
        # np.savetxt("depth.csv", (depth/4).astype('uint16'), delimiter="," )
    ax2 = fig.add_subplot(222)
    if not ir is None:
        ax2.imshow(ir)
    ax3 = fig.add_subplot(223)
    if not status is None:
        ax3.imshow(status)
    ax4 = fig.add_subplot(224)
    if not rgb is None:
        ax4.imshow(rgb)

network example

In [16]:
if post_encode_config(frame_config_encode(1,0,255,0,2,7,1,0,0)):
    p = get_frame_from_http()
    show_frame(p)
    # with open("rgbd.raw", 'wb') as f:
    #     f.write(p)
    #     f.flush()
Get deep image
Length=409712
(0, 118.585)

local file example

In [ ]:
with open("rgbd.raw", 'rb') as fp:
    file_data = fp.read()
    fp.close()
    show_frame(file_data)

local file example (3D)

need library open3d, your python version should be less than 3.9(included)

website: https://pypi.org/project/open3d/

install: pip install open3d

In [ ]:
import numpy as np
import open3d as o3d

points = o3d.io.read_point_cloud("rgbd.pcd")
o3d.visualization.draw_geometries([points])

utils

In [17]:
with open("./sipeed/CameraParms.json", "rb") as f:
    post_CameraParmsBytes(f.read())
ok
  • 其他链接
    • Sipeed 官网
    • MaixHub
    • BBS 论坛
    • Sipeed 淘宝
    • 网站使用 teedoc 生成
  • 网站统计
    • 百度统计
    • 网站地图
  • 源码
    • 网站源码
    • 开源项目源码
  • 关注我们
    • github
    • twitter
    • 淘宝
    • 更多
  • 联系我们
    • 电话: +86 0755-27808509
    • 商业支持: support@sipeed.com
    • 地址: 深圳市宝安区新湖路4008号蘅芳科技办公大厦A座-2101C
    • 加入我们
  • ©2018-2023 深圳矽速科技有限公司
  • 粤ICP备19015433号