From dfec7e20d53ab80e863640f26684c61abd701d8f Mon Sep 17 00:00:00 2001 From: Jay Date: Mon, 27 May 2024 10:14:31 +0800 Subject: [PATCH 1/7] =?UTF-8?q?=E2=9C=A8=20feat(gsdevice.py):=20add=20driv?= =?UTF-8?q?e=20for=20mac?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Just open the camera directly on the Mac and display the image. --- examples/gsdevice.py | 7 +++++++ gelsight/gsdevice.py | 7 +++++++ 2 files changed, 14 insertions(+) diff --git a/examples/gsdevice.py b/examples/gsdevice.py index 7bfee66..1127ab1 100644 --- a/examples/gsdevice.py +++ b/examples/gsdevice.py @@ -17,6 +17,13 @@ def get_camera_id(camera_name): cam_num = None if os.name == 'nt': cam_num = find_cameras_windows(camera_name) + elif os.name == "posix": + import usb.core + devices = usb.core.find(find_all=True) + for idx, device in enumerate(devices): + if camera_name in device.product: + cam_num = idx + break else: for file in os.listdir("/sys/class/video4linux"): real_file = os.path.realpath("/sys/class/video4linux/" + file + "/name") diff --git a/gelsight/gsdevice.py b/gelsight/gsdevice.py index 7bfee66..1127ab1 100644 --- a/gelsight/gsdevice.py +++ b/gelsight/gsdevice.py @@ -17,6 +17,13 @@ def get_camera_id(camera_name): cam_num = None if os.name == 'nt': cam_num = find_cameras_windows(camera_name) + elif os.name == "posix": + import usb.core + devices = usb.core.find(find_all=True) + for idx, device in enumerate(devices): + if camera_name in device.product: + cam_num = idx + break else: for file in os.listdir("/sys/class/video4linux"): real_file = os.path.realpath("/sys/class/video4linux/" + file + "/name") From 80f283133f129c3d394d3574a7261c2fa7ce122c Mon Sep 17 00:00:00 2001 From: Jay Date: Tue, 28 May 2024 17:25:06 +0800 Subject: [PATCH 2/7] =?UTF-8?q?=F0=9F=8E=88=20perf(gsdevice.py):=20add=20p?= =?UTF-8?q?rompt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Prompt when the camera is not found --- examples/gsdevice.py | 4 +++- gelsight/gsdevice.py | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/examples/gsdevice.py b/examples/gsdevice.py index 1127ab1..d6e78ce 100644 --- a/examples/gsdevice.py +++ b/examples/gsdevice.py @@ -35,7 +35,9 @@ def get_camera_id(camera_name): else: found = " " print("{} {} -> {}".format(found, file, name)) - + if cam_num is None: + print("ERROR! Can't Found Camera Device") + exit() return cam_num if os.name == 'nt': diff --git a/gelsight/gsdevice.py b/gelsight/gsdevice.py index 1127ab1..d6e78ce 100644 --- a/gelsight/gsdevice.py +++ b/gelsight/gsdevice.py @@ -35,7 +35,9 @@ def get_camera_id(camera_name): else: found = " " print("{} {} -> {}".format(found, file, name)) - + if cam_num is None: + print("ERROR! Can't Found Camera Device") + exit() return cam_num if os.name == 'nt': From 990908352862c86d751de049210bdaf1a8070f3c Mon Sep 17 00:00:00 2001 From: Jay Date: Tue, 28 May 2024 17:32:02 +0800 Subject: [PATCH 3/7] =?UTF-8?q?=F0=9F=8E=88=20perf(gs3drecon.py):=20Adjust?= =?UTF-8?q?=20the=20order=20of=20modules?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Import open3d first, otherwise it will get stuck on the mac m series chip computer. --- gelsight/gs3drecon.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gelsight/gs3drecon.py b/gelsight/gs3drecon.py index a0e2993..1f20f6a 100644 --- a/gelsight/gs3drecon.py +++ b/gelsight/gs3drecon.py @@ -1,7 +1,7 @@ +import open3d import torch import torch.nn as nn import torch.nn.functional as F -import open3d import numpy as np import math import os From 356b2c840623dd256f771193e29e776924879c1d Mon Sep 17 00:00:00 2001 From: Jay Date: Tue, 28 May 2024 17:33:23 +0800 Subject: [PATCH 4/7] =?UTF-8?q?=E2=9C=A8=20feat(gs3drecon.py):=20Add=20mac?= =?UTF-8?q?=20drive?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add mac m series chip driver --- gelsight/gs3drecon.py | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/gelsight/gs3drecon.py b/gelsight/gs3drecon.py index 1f20f6a..2cf3114 100644 --- a/gelsight/gs3drecon.py +++ b/gelsight/gs3drecon.py @@ -206,15 +206,24 @@ def forward(self, x): class Reconstruction3D: def __init__(self, dev): - self.cpuorgpu = "cpu" + self.device_type = "cpu" self.dm_zero_counter = 0 self.dm_zero = np.zeros((dev.imgw, dev.imgh)) pass - def load_nn(self, net_path, cpuorgpu): + def load_nn(self, net_path, device_type=None): + + # Automatically select device if not provided + if device_type is None: + if torch.cuda.is_available(): + device_type = 'cuda' + elif torch.backends.mps.is_available(): + device_type = 'mps' + else: + device_type = 'cpu' - self.cpuorgpu = cpuorgpu - device = torch.device(cpuorgpu) + self.device_type = device_type + device = torch.device(device_type) if not os.path.isfile(net_path): print('Error opening ', net_path, ' does not exist') @@ -223,11 +232,15 @@ def load_nn(self, net_path, cpuorgpu): net = RGB2NormNet().float().to(device) - if cpuorgpu=="cuda": + if device_type=="cuda": ### load weights on gpu # net.load_state_dict(torch.load(net_path)) checkpoint = torch.load(net_path, map_location=lambda storage, loc: storage.cuda(0)) net.load_state_dict(checkpoint['state_dict']) + elif device_type == "mps" : + ### load weights on mac m seriel + checkpoint = torch.load(net_path, map_location=lambda storage, loc: storage.mps()) + net.load_state_dict(checkpoint['state_dict']) else: ### load weights on cpu which were actually trained on gpu checkpoint = torch.load(net_path, map_location=lambda storage, loc: storage) @@ -281,7 +294,7 @@ def get_depthmap(self, frame, mask_markers, cm=None): # pxpos[:, 1] = pxpos[:, 1] / ((240 / imgw) * imgw) features = np.column_stack((rgb, pxpos)) - features = torch.from_numpy(features).float().to(self.cpuorgpu) + features = torch.from_numpy(features).float().to(self.device_type) with torch.no_grad(): self.net.eval() out = self.net(features) From da035ed131f29b82c4d51d6cfe911def9e834ca1 Mon Sep 17 00:00:00 2001 From: Jay Date: Tue, 28 May 2024 17:34:23 +0800 Subject: [PATCH 5/7] =?UTF-8?q?=E2=9C=A8=20feat(gs3drecon.py):=20copy=20fr?= =?UTF-8?q?om=20gelsight?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- examples/gs3drecon.py | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/examples/gs3drecon.py b/examples/gs3drecon.py index a0e2993..2cf3114 100644 --- a/examples/gs3drecon.py +++ b/examples/gs3drecon.py @@ -1,7 +1,7 @@ +import open3d import torch import torch.nn as nn import torch.nn.functional as F -import open3d import numpy as np import math import os @@ -206,15 +206,24 @@ def forward(self, x): class Reconstruction3D: def __init__(self, dev): - self.cpuorgpu = "cpu" + self.device_type = "cpu" self.dm_zero_counter = 0 self.dm_zero = np.zeros((dev.imgw, dev.imgh)) pass - def load_nn(self, net_path, cpuorgpu): + def load_nn(self, net_path, device_type=None): + + # Automatically select device if not provided + if device_type is None: + if torch.cuda.is_available(): + device_type = 'cuda' + elif torch.backends.mps.is_available(): + device_type = 'mps' + else: + device_type = 'cpu' - self.cpuorgpu = cpuorgpu - device = torch.device(cpuorgpu) + self.device_type = device_type + device = torch.device(device_type) if not os.path.isfile(net_path): print('Error opening ', net_path, ' does not exist') @@ -223,11 +232,15 @@ def load_nn(self, net_path, cpuorgpu): net = RGB2NormNet().float().to(device) - if cpuorgpu=="cuda": + if device_type=="cuda": ### load weights on gpu # net.load_state_dict(torch.load(net_path)) checkpoint = torch.load(net_path, map_location=lambda storage, loc: storage.cuda(0)) net.load_state_dict(checkpoint['state_dict']) + elif device_type == "mps" : + ### load weights on mac m seriel + checkpoint = torch.load(net_path, map_location=lambda storage, loc: storage.mps()) + net.load_state_dict(checkpoint['state_dict']) else: ### load weights on cpu which were actually trained on gpu checkpoint = torch.load(net_path, map_location=lambda storage, loc: storage) @@ -281,7 +294,7 @@ def get_depthmap(self, frame, mask_markers, cm=None): # pxpos[:, 1] = pxpos[:, 1] / ((240 / imgw) * imgw) features = np.column_stack((rgb, pxpos)) - features = torch.from_numpy(features).float().to(self.cpuorgpu) + features = torch.from_numpy(features).float().to(self.device_type) with torch.no_grad(): self.net.eval() out = self.net(features) From cdccea52e170cf8fadd1678e1166c3005a022386 Mon Sep 17 00:00:00 2001 From: Jay Date: Thu, 13 Jun 2024 10:50:09 +0800 Subject: [PATCH 6/7] =?UTF-8?q?=F0=9F=90=9E=20fix(gsdevice):=20fix=20bugs?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit os.name 无法判断mac or linux --- examples/gsdevice.py | 3 ++- gelsight/gsdevice.py | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/examples/gsdevice.py b/examples/gsdevice.py index d6e78ce..e0f869b 100644 --- a/examples/gsdevice.py +++ b/examples/gsdevice.py @@ -1,5 +1,6 @@ import cv2 import numpy as np +import platform import os import re @@ -17,7 +18,7 @@ def get_camera_id(camera_name): cam_num = None if os.name == 'nt': cam_num = find_cameras_windows(camera_name) - elif os.name == "posix": + elif platform.system() == "Darwin": import usb.core devices = usb.core.find(find_all=True) for idx, device in enumerate(devices): diff --git a/gelsight/gsdevice.py b/gelsight/gsdevice.py index d6e78ce..e0f869b 100644 --- a/gelsight/gsdevice.py +++ b/gelsight/gsdevice.py @@ -1,5 +1,6 @@ import cv2 import numpy as np +import platform import os import re @@ -17,7 +18,7 @@ def get_camera_id(camera_name): cam_num = None if os.name == 'nt': cam_num = find_cameras_windows(camera_name) - elif os.name == "posix": + elif platform.system() == "Darwin": import usb.core devices = usb.core.find(find_all=True) for idx, device in enumerate(devices): From 10033903cf13b46935e4914cade11b5f4afa9aba Mon Sep 17 00:00:00 2001 From: Jay Date: Thu, 13 Jun 2024 14:11:56 +0800 Subject: [PATCH 7/7] update gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index c678a5e..2b71dbb 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ # Byte-compiled / optimized / DLL files __pycache__/ *.py[cod] +.DS_Store \ No newline at end of file