Golang中Librealsense2的使用与开发指南

Golang中Librealsense2的使用与开发指南 大家好,有没有人尝试过在 Go 中使用 librealsense?我过去两天一直在努力尝试,但没能找到解决方案。

1 回复

更多关于Golang中Librealsense2的使用与开发指南的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html


在Go中使用librealsense2确实需要一些技巧,因为官方只提供了C++和Python的SDK。以下是两种可行的解决方案:

方案一:使用cgo直接调用librealsense2 C API

// rs2.go
package main

/*
#cgo LDFLAGS: -lrealsense2
#include <librealsense2/rs.h>
#include <stdlib.h>
*/
import "C"
import (
    "fmt"
    "unsafe"
)

func main() {
    // 创建上下文
    var ctx *C.rs2_context
    ctx = C.rs2_create_context(C.RS2_API_VERSION, nil)
    defer C.rs2_delete_context(ctx)
    
    // 获取设备列表
    var deviceList *C.rs2_device_list
    deviceList = C.rs2_query_devices(ctx, nil)
    defer C.rs2_delete_device_list(deviceList)
    
    count := C.rs2_get_device_count(deviceList, nil)
    fmt.Printf("找到 %d 个设备\n", count)
    
    if count > 0 {
        // 获取第一个设备
        device := C.rs2_create_device(deviceList, 0, nil)
        defer C.rs2_delete_device(device)
        
        // 获取设备名称
        name := C.rs2_get_device_info(device, C.RS2_CAMERA_INFO_NAME, nil)
        defer C.free(unsafe.Pointer(name))
        fmt.Printf("设备名称: %s\n", C.GoString(name))
    }
}

方案二:使用现有的Go封装库

目前有几个第三方库可以简化集成:

  1. go-realsense (推荐):
import "github.com/IntelRealSense/librealsense2/go"

func main() {
    ctx := realsense2.NewContext()
    defer ctx.Close()
    
    devices := ctx.QueryDevices()
    fmt.Printf("设备数量: %d\n", devices.Count())
    
    for i := 0; i < devices.Count(); i++ {
        device := devices.GetDevice(i)
        name := device.GetInfo(realsense2.CameraInfoName)
        fmt.Printf("设备 %d: %s\n", i, name)
    }
}
  1. 安装依赖:
# 首先安装librealsense2系统库
sudo apt-get install librealsense2-dev

# 然后安装Go绑定
go get github.com/IntelRealSense/librealsense2/go

示例:获取深度帧数据

package main

import (
    "fmt"
    "github.com/IntelRealSense/librealsense2/go"
)

func main() {
    pipe := realsense2.NewPipeline()
    defer pipe.Destroy()
    
    cfg := realsense2.NewConfig()
    cfg.EnableStream(realsense2.StreamDepth, 640, 480, realsense2.FormatZ16, 30)
    
    // 启动管道
    profile := pipe.Start(cfg)
    defer pipe.Stop()
    
    // 获取深度流
    stream := profile.GetStream(realsense2.StreamDepth).AsVideoStreamProfile()
    fmt.Printf("深度流: %dx%d @ %dHz\n", 
        stream.GetWidth(), 
        stream.GetHeight(), 
        stream.GetFramerate())
    
    // 等待并获取帧
    for i := 0; i < 10; i++ {
        frames := pipe.WaitForFrames()
        defer frames.Release()
        
        depthFrame := frames.GetDepthFrame()
        defer depthFrame.Release()
        
        width := depthFrame.GetWidth()
        height := depthFrame.GetHeight()
        fmt.Printf("帧 %d: %dx%d\n", i, width, height)
    }
}

常见问题解决

  1. 编译错误 “realsense2.h: No such file”:
# 设置CGO编译标志
export CGO_CFLAGS="-I/usr/include/librealsense2"
export CGO_LDFLAGS="-lrealsense2"
  1. 运行时错误 “librealsense2.so: cannot open shared object file”:
# 添加库路径
export LD_LIBRARY_PATH=/usr/lib/x86_64-linux-gnu:$LD_LIBRARY_PATH
  1. 权限问题:
# 添加当前用户到video组
sudo usermod -a -G video $USER

性能优化建议

// 使用帧队列进行异步处理
queue := realsense2.NewFrameQueue(10)
go func() {
    for {
        frame := queue.WaitForFrame()
        // 处理帧数据
        frame.Release()
    }
}()

// 在主线程中推送帧
for {
    frames := pipe.WaitForFrames()
    queue.Enqueue(frames)
}

这些方法应该能帮助你开始在Go中使用librealsense2。Intel官方提供的Go绑定是目前最稳定的选择。

回到顶部