graspnet+Astra2相机实现部署

news2024/10/5 2:50:18

graspnet+Astra2相机实现部署

🚀 环境配置 🚀

  • ubuntu 20.04
  • Astra2相机
  • cuda 11.0.1
  • cudnn v8.9.7
  • python 3.8.19
  • pytorch 1.7.0
  • numpy 1.23.5

1. graspnet的复现

具体的复现流程可以参考这篇文章:Ubuntu20.04下GraspNet复现流程

这里就不再详细介绍了

2. Astra2的Python API

以下内容都是参考官方文档:Orbbec SDK for Python 使用手册

我们首先确认输入到网络中的数据为一个点云数据,再一个我们需要一个rgb图像用来给点云上色,graspnetAPI帮我们写好了从深度图转换到点云的函数create_point_cloud_from_depth_image,所以我们只需要写好从相机的视频流获取深度图片和rgb图片的部分就好了,特别注意的是,大多数相机的rgb的fov是要大于深度图的fov所以我们要对两者进行对齐操作,对齐操作的本质就是在深度图中填充0,使得深度图和rgb图的大小一致。大多数的相机厂商已经提供了具体的示例来演示如何进行对齐,这里就不再赘述。

这里我直接给出我写的astra2.py,用于获取相机的深度图和rgb图的代码,大家可以参考一下思路

astra2.py

from pyorbbecsdk import *
import numpy as np
import cv2
import os
import open3d as o3d

class Camera:
    def __init__(self, width=1280, height=720,fps=15):
        self.im_width = width
        self.im_height = height
        self.fps = fps
        self.intrinsic = None
        self.scale = None

        # 连接相机
        # self.connect()
    
    def connect(self):
        """用于连接相机"""
        self.pipeline = Pipeline()
        config = Config()

        # color config
        profile_list = self.pipeline.get_stream_profile_list(OBSensorType.COLOR_SENSOR)
        color_profile = profile_list.get_default_video_stream_profile()
        config.enable_stream(color_profile)
        # depth config
        profile_list = self.pipeline.get_stream_profile_list(OBSensorType.DEPTH_SENSOR)
        assert profile_list is not None
        depth_profile = profile_list.get_default_video_stream_profile()
        assert depth_profile is not None
        print("color profile : {}x{}@{}_{}".format(color_profile.get_width(),
                                                   color_profile.get_height(),
                                                   color_profile.get_fps(),
                                                   color_profile.get_format()))
        print("depth profile : {}x{}@{}_{}".format(depth_profile.get_width(),
                                                   depth_profile.get_height(),
                                                   depth_profile.get_fps(),
                                                   depth_profile.get_format()))
        config.enable_stream(depth_profile)
        # set synchronize for depth img and color img
        config.set_align_mode(OBAlignMode.SW_MODE)
        self.pipeline.enable_frame_sync()
        # start config
        self.pipeline.start(config)

        # get intrinsic
        self.intrinsic = self.get_intrinsic()
    
    def disconnect(self):
        """用于断开相机"""
        self.pipeline.stop()
    

    def get_frame(self):
        """通过流来获取color frame和depth frame"""
        while True:
            frames: FrameSet = self.pipeline.wait_for_frames(200)
            if frames is None:
                continue
            color_frame = frames.get_color_frame()
            if color_frame is None:
                continue
            depth_frame = frames.get_depth_frame()
            if depth_frame is None:
                continue
            if color_frame != None and depth_frame != None:
                break
        
        return color_frame, depth_frame

    def frame2data(self, color_frame, depth_frame):
        """暂时没用"""
        width = depth_frame.get_width()
        height = depth_frame.get_height()
        scale = depth_frame.get_depth_scale()
        depth_data = np.frombuffer(depth_frame.get_data(), dtype=np.uint16)
        depth_data = depth_data.reshape((height, width))

        width = color_frame.get_width()
        height = color_frame.get_height()
        color_data = np.asanyarray(color_frame.get_data(), dtype=np.uint16)
        # color_data = color_data.reshape((height, width, 3))
        return color_data.astype(np.float32), depth_data.astype(np.float32)
    
    def get_data(self):
        """通过流来获取color data和depth data"""

        # 连接相机
        self.connect()
        color_frame, depth_frame = self.get_frame()

        width = color_frame.get_width()
        height = color_frame.get_height()
        color_format = color_frame.get_format()
        data = np.asanyarray(color_frame.get_data())
        color_data = cv2.imdecode(data, cv2.IMREAD_COLOR)
        color_data.astype(np.float32)

        print('color_image.shape: ', color_data.shape)
        # print("===width: {}===".format(width))
        # print("===height: {}===".format(height))
        width = depth_frame.get_width()
        height = depth_frame.get_height()
        scale = depth_frame.get_depth_scale()
        print("===width: {}===".format(width))
        print("===height: {}===".format(height))
        print("===scale: {}===".format(scale))
        save_dir = os.path.join(os.getcwd(), "real/intrinsic")
        if not os.path.exists(save_dir):
            os.mkdir(save_dir)
        filename = save_dir + "/camera_depth_scale.txt"
        save = np.array([scale])
        np.savetxt(filename, save, delimiter=' ')

        depth_data = np.frombuffer(depth_frame.get_data(), dtype=np.uint16)
        depth_data = depth_data.reshape((height, width))
        depth_data = depth_data.astype(np.float32) * scale
        print('depth_image.shape: ', depth_data.shape)

        depth_data = cv2.normalize(depth_data, None, 0, 255, cv2.NORM_MINMAX, dtype=cv2.CV_16U)
        
        # 断开相机
        self.disconnect()

        return color_data, depth_data

    def get_data_saved(self):
        color_data, depth_data = self.get_data()
        
        # depth_image = depth_data_normalized.astype(np.uint8)

        save_image_dir = os.path.join(os.getcwd(), "real/images")
        if not os.path.exists(save_image_dir):
            os.mkdir(save_image_dir)
        depth_filename = save_image_dir + "/depth_{}x{}.png".format(depth_data.shape[0], depth_data.shape[1])
        color_filename = save_image_dir + "/color_{}x{}.png".format(color_data.shape[0], color_data.shape[1])
        cv2.imwrite(color_filename, color_data)
        # depth_data.tofile(depth_filename)
        cv2.imwrite(depth_filename, depth_data)

        return color_data, depth_data
        

    def get_intrinsic(self):
        """获取内参"""
        # get intrinsic
        itsc = self.pipeline.get_camera_param()
        raw_intrinsic = itsc.depth_intrinsic

        intrinsic = np.array([raw_intrinsic.fx, 0, raw_intrinsic.cx, 
                                0, raw_intrinsic.fy, raw_intrinsic.cy,
                                0, 0, 1]).reshape(3,3)
        print("intrinsic: ", itsc)
        print('depth intrinsic: ', raw_intrinsic)
        print("intrinsic matrix", intrinsic)
        save_dir = os.path.join(os.getcwd(), "real/intrinsic")
        if not os.path.exists(save_dir):
            os.mkdir(save_dir)
        filename = save_dir + "/camera_itcs.txt"

        np.savetxt(filename, intrinsic, delimiter=' ')

        return intrinsic
    
    # for test
    def visualize(self):
        """显示rgbd图像"""
        color_data, depth_data = self.get_data()
        depth_image = cv2.normalize(depth_data, None, 0, 255, cv2.NORM_MINMAX, dtype=cv2.CV_8U)
        depth_image = cv2.applyColorMap(depth_image, cv2.COLORMAP_JET)
        # overlay color image on depth image
        depth_image = cv2.addWeighted(color_data, 0.5, depth_image, 0.5, 0)
        cv2.imshow("Depth with Color", depth_image)
        cv2.waitKey(500)
    
    

if __name__ == '__main__':
    camera = Camera()
    camera.visualize()
    color, depth = camera.get_data()
    print("depth.shape: ", depth.shape)

测试相机是否实现对齐,结果如下

Image

表明了相机确实实现了对齐操作

3. 修改demo.py

我们使用get_data()函数就能够让相机进行一次拍摄,然后得到color_datadepth_data供我们进行后续的处理。然后可以修改一下demo.py,将从文件读取数据改为直接从相机进行读取

demo.py

...
from astra2 import Camera()
astra2 = Camera()

...

def get_and_process_data():

    # 使用相机获取一次数据
    color, depth = astra2.get_data()
    color = color / 255.0
    ...

然后别的部分可以保持不变或者以后再修改。这里一定要使用官方提供的checkpoint-rs.tar,如果使用checkpoint-kn.tar,会出现异常,暂时我也没有找到原因。

最后处理的效果如下

Image

可以看到出现了许多我们不希望存在的抓取框,这个可以通过调整workspace_mask来进行过滤

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1698471.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

贪心-leetcode402.移掉 K 位数字-XMUOJ符文序列

题目 思路 话不多说&#xff0c;直接上代码 代码 /*leetcode402.移掉 K 位数字-XMUOJ符文序列--JinlongW-2024/05/26单调栈贪心*/ #include<bits/stdc.h> const int N1010; char num[N],result[N],numStack[N]; int k; using namespace std;void removeKdigits( int k…

Excel 多行表头的列转行

Excel中A3:F6是带表头的典型表格&#xff0c;但上面多了额外的两行表头&#xff1a; ABCDEF1ActualsActualsPlanPlan2FY20FY21FY20FY213CountryOwner1/1/20201/1/20201/1/20201/1/20204FranceRichard100150801605FranceMartin1201401301406FrancePierre501005080 现在要将典型…

美业美容院会员服务预约店铺管理小程序的效果是什么

美容业各个服务都有不少人需要&#xff0c;美容项目通常价格高&#xff0c;本地客户触达的同时&#xff0c;品牌形象触达外地客户也可获取&#xff0c;女性消费群体在“美”的各方面多数情况下是不惜资金投入。 客户需要找到靠谱商家&#xff0c;而项目消费/同行竞争/升级发展…

matplotlib latex表格

使用python3环境 import matplotlib.gridspec as gridspec import matplotlib.pyplot as pltimport numpy as np import matplotlib as mpl #mpl.use(pgf)def figsize(scale, nplots 1):fig_width_pt 390.0 # Get this from LaTeX using \the\text…

如何解决mfc110udll丢失的问题,7个方法可解决mfc110udll丢失

mfc110u.dll是一个动态链接库文件&#xff0c;属于Microsoft Visual C 2012 Redistributable Package的一部分。它是Microsoft Foundation Classes (MFC) 库的一个版本&#xff0c;专门用于支持基于MFC开发的应用程序运行。MFC是一个用于Windows操作系统上使用C进行本机应用程序…

【启程Golang之旅】深入解析函数的奥秘与技巧

欢迎来到Golang的世界&#xff01;在当今快节奏的软件开发领域&#xff0c;选择一种高效、简洁的编程语言至关重要。而在这方面&#xff0c;Golang&#xff08;又称Go&#xff09;无疑是一个备受瞩目的选择。在本文中&#xff0c;带领您探索Golang的世界&#xff0c;一步步地了…

command not found: wire 解决方案【学习笔记,不作教程】

command not found: wire command not found: wire command not found: wire go get github.com/google/wire/cmd/wirego install github.com/google/wire/cmd/wirelatest再次在 /bubble/cmd/bubble目录下执行wire wire wire: bubble/cmd/bubble: wrote /Users/zhengshijie/go…

JavaEE之线程(7)_单例模式(设计模式概念、单例模式优点、懒汉、饿汉模式)

一、什么是设计模式&#xff1f; 单例模式是设计模式中较为常见的一种。那么&#xff0c;什么是单例模式&#xff1f; 设计模式&#xff08;Design Pattern&#xff09;都是一些相对优秀的解决方案&#xff0c;很多问题都是典型的、有代表性的问题&#xff0c;学习设计模式&am…

外企也半夜发布上线吗?

0 别把问题想得太复杂 如果有灰度发布的能力&#xff0c;最好白天发布&#xff1b;如果没有灰度发布&#xff0c;只能在半夜发布。 即使有灰度发布能力&#xff0c;也不要沾沾自喜&#xff0c;好好反思一下你们的灰度发布是否真的经得起考验&#xff0c;还是仅仅是装装样子。…

Python语言绘制好看的小提琴图、箱形图、散点图、山脊图和柱状图等等

废话不多说&#xff0c;今天给大家分享一个&#xff0c;使用python绘制小提琴图、箱形图、散点图、山脊图和柱状图等等 图中的数据是随机生成的&#xff0c;图例&#xff0c;图注以及坐标题目各种信息&#xff0c;具体内容大家可以自己修改~ 效果图如下所示 &#x1f447;&a…

web如何做接口层面自动化测试?

接口层面约等于集成化测试&#xff0c;且需要启动web容器 一般web项目的&#xff0c;代码都是按照分层开发的&#xff0c;业务主要是集中在service和dao层&#xff0c;而我们如果仅仅是利用之前的单元测试,然后把依赖的代码直接mock掉&#xff0c;仅仅测试controller这一块是没…

数据结构(四)

数据结构&#xff08;四&#xff09; 算法算法的特征算法和程序的区别怎么样评判一个算法的好坏 常见的查找算法线性树状哈希查找构建哈希函数的方法质数求余法解决冲突 算法 一堆指令的有序集合 算法的特征 唯一性&#xff1a;每一句话只有一种解释 有穷性&#xff1a;算法能…

第十课,while循环

一&#xff0c;认识循环是什么 循环普遍存在于日常生活中&#xff0c;同样&#xff0c;在程序中&#xff0c;循环功能也是至关重要的基础功能。 当程序需要重复执行某一段代码&#xff0c;利用循环可以轻松完成工作 例如我要你打印100次上课&#xff0c;直接写100次print&…

SpringBoo+vue3整合讯飞星火3.5通过webscoket实现聊天功能(全网首发)附带展示效果

API版本&#xff1a;Spring Boot 整合讯飞星火3.5通过接口Api接口实现聊天功能&#xff08;首发&#xff09;复制粘贴即可使用&#xff0c;后续更新WebSocket实现聊天功能_讯飞星火web聊天-CSDN博客https://blog.csdn.net/qq_53722480/article/details/138865508?csdn_share_t…

基于xilinx FPGA的 FFT IP使用例程说明文档(可动态配置FFT点数,可计算信号频率与幅度)

目录 1 概述2 IP examples功能3 IP 使用例程3.1 IP设置3.2 fft_demo端口3.3 例程框图3.4 仿真结果3.5 仿真验证得出的结论4 注意事项5例程位置 1 概述 本文用于讲解xilinx IP 的FFT ip examples的功能说明&#xff0c;方便使用者快速上手。 参考文档&#xff1a;《PG109》 2 …

推荐13款常用的Vscode插件,提高前端日常开发效率

1. Live Server Live Server 插件是一个用于前端开发的扩展&#xff0c;它的主要作用是提供一个本地开发服务器&#xff0c;以便实时预览和调试网页应用程序。其最大特点在于热重载&#xff0c;即开发者可实时预览代码效果。 因为Live Server 允许开发者在浏览器中实时预览您正…

Android Webview加载pdf文件无法缩放问题

WebView设置开启页面缩放&#xff1a; settings webView.getSettings(); settings.setSupportZoom(true); settings.setJavaScriptEnabled(true); settings.setUseWideViewPort(true); settings.setLoadWithOverviewMode(true); settings.setBuiltInZoomControls(true); sett…

集成开发环境GoLand安装配置结合内网穿透实现ssh远程访问服务器

&#x1f49d;&#x1f49d;&#x1f49d;欢迎来到我的博客&#xff0c;很高兴能够在这里和您见面&#xff01;希望您在这里可以感受到一份轻松愉快的氛围&#xff0c;不仅可以获得有趣的内容和知识&#xff0c;也可以畅所欲言、分享您的想法和见解。 推荐:kwan 的首页,持续学…

MOS选型及其参数解析

背景&#xff1a; 整理现有常用元器件选型&#xff0c;日后使用时针对性观看&#xff0c;生成列表链接如下&#xff1a; https://blog.csdn.net/caozhaokun/article/details/126069701 作者&#xff1a;Cayden 时间&#xff1a;2024/05/26 一、MOS选用现状 MOS是电路设计…

【Text2SQL】Spider 数据集

论文&#xff1a;Spider: A Large-Scale Human-Labeled Dataset for Complex and Cross-Domain Semantic Parsing and Text-to-SQL Task ⭐⭐⭐⭐⭐ EMNLP 2018, arXiv:1809.08887 Dataset: spider GitHub: github.com/taoyds/spider 一、论文速读 本文提出了 Text2SQL 方向的…