-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdemo.py
More file actions
434 lines (322 loc) · 13 KB
/
demo.py
File metadata and controls
434 lines (322 loc) · 13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
from __future__ import annotations
import cv2
import matplotlib.pyplot as plt
import numpy as np
import open3d as o3d
import supervision as sv
import torch
from PIL import Image
import api.utils.o3d_extension as o3de
import api.utils.sv_extension as sve
from api import THREAD_POOL
from api.utils import Path, Timer
from api.utils.camera import Frame, Camera, Pinhole, ObservationSequence
D435I = Camera.from_yaml("config/camera/RS-D435i.yaml")
PCD_PROCESSOR = o3de.PointCloudProcessor(voxel_size=2e-3, nn_radius=1e-2, nn_k_thresh=10)
def test_ai_client():
from api.utils import chat_tool
from langchain_openai import ChatOpenAI
from langchain_core.messages import HumanMessage
content = chat_tool.make_content([
"这副图描绘了什么颜色的内容?",
("image_url", cv2.imread("assets/fruits-c.jpg"))
])
llm = ChatOpenAI(
model="qwen3.6-plus-2026-04-02",
**chat_tool.CLIENT_KWARGS["bailian"]
)
print(llm.invoke([HumanMessage(content)]))
def test_clip():
from api.encoders import OpenCLIP
# print(load_dinov2())
clip = OpenCLIP()
# Image-text similarity
img = Image.open("assets/cat.jpg").convert("RGB")
feat_img = clip.encode_images([img])
feat_text = clip.encode_texts(["a photo of 5 cats", "a dog", "desktop"])
print(feat_img.shape)
score = (feat_img @ feat_text.T).softmax(dim=-1)
print(score)
# Multi-view similarity
for obj in ("green-pot", "mustard"):
root = Path(f"assets/{obj}/color")
img = [Image.open(file).convert("RGB") for file in root.glob("*.png")]
feat_img = clip.encode_images(img)
print(feat_img @ feat_img.T)
def test_codet():
from api.codet import CoDet
from api.segment_anything import SegmentAnythingV2
codet = CoDet()
sam2 = SegmentAnythingV2("tiny")
img = cv2.imread("/media/tongzj/Data/Workbench/Lab/assets/takeout.jpg")
img = sv.resize_image(img, [640] * 2, keep_aspect_ratio=True)
with Timer("Main"):
dets = sam2.inference_with_box(img, THREAD_POOL.submit(codet, img))
cv2.imwrite("runs/codet-sam2.jpg", sve.annotate(img, dets))
# Infer a video stream
from api.utils.realsense import RGBDCamera
for color, depth in RGBDCamera():
with Timer("Main"):
dets = sam2.inference_with_box(color, THREAD_POOL.submit(codet, color))
plt.imshow(sve.annotate(color, dets)[..., ::-1])
plt.pause(1e-3)
def test_cgn():
from api.contact_graspnet import ContactGraspNet
# Load RGB-D data
struct = np.load("assets/rgb-depth-K.npy", allow_pickle=True).item()
camera = Pinhole(img_size=struct["depth"].shape[::-1],
intrinsics=struct["K"][[0, 1, 0, 1], [0, 1, 2, 2]])
pointmap = camera.pointmap(struct["depth"]).astype(np.float32)
mask = (pointmap[..., 2] > 0.2) & (pointmap[..., 2] < 1.8)
# Infer grasp poses
cgn = ContactGraspNet()
grasps = cgn(pointmap[mask])
# contact point
cpt = o3d.geometry.PointCloud(o3d.utility.Vector3dVector(grasps.left_finger_tip))
# Visualize point cloud
if grasps:
vis = o3d.visualization.Visualizer()
vis.create_window()
vis.add_geometry(o3de.rgbd2pcd(pointmap, struct["rgb"], mask))
vis.add_geometry(cpt)
for mesh in grasps.to_meshes(): vis.add_geometry(mesh)
vis.run()
vis.destroy_window()
def test_curobo():
from api.utils.curobo_planning import CuRoboPlanner
from api.utils import pose_tf
Tce = pose_tf.SE3.from_config([0.048098, -0.985089, -0.165184, 0.06595,
0.998825, 0.048422, 0.002063, -0.000357,
0.005966, -0.165089, 0.986261, 0.048961])
CuRoboPlanner.generate_ws_for_lookat(
"runs/franka_ws.npy", focus=np.array([.5, 0, .1]), Tec=np.linalg.inv(Tce))
def test_da():
from api.depth_anything import DepthAnythingV3
camera = D435I
camera.depth_range[1] = .5
obs_seq = ObservationSequence(root="assets/green-pot", img_ext="png")
fabs = [Frame(camera, **data) for data in obs_seq.load()]
if 0:
model = DepthAnythingV3()
with Timer("Reconstruct"):
scene = model.mapping(fabs)
pcds = [PCD_PROCESSOR(scene.get_aligned_pcd("first"))]
else:
model = DepthAnythingV3("mono")
with Timer("Monocular"):
frel = model.mapping(fabs[:1]).frames_rel[0]
pcds = [frel.to_pcd()]
pcds += [fabs[0].to_pcd()]
pcds[-1].paint_uniform_color([0., 0., 1.])
o3d.visualization.draw_geometries(pcds)
def test_deberta():
from api.encoders import DeBERTa
texts = ["Hello, I love using DeBERTa for NLP tasks!", "Grasp the cup"]
model = DeBERTa()
ret = model(texts)
print(ret)
for k, v in ret.items():
print(f"{k}: {v.shape}")
def test_decompose():
from api import decompose
from tqdm import tqdm
root = Path(r"../assets/objects")
output_dir = Path("runs/coacd")
output_dir.mkdir(parents=True, exist_ok=True)
img_size = (640, 480)
acd = decompose.CoACD(threshold=0.07, max_convex_hull=10, verbose=False)
# Process all *.glb files
for file in tqdm(list(root.rglob("*.glb"))):
output = output_dir / "_".join(file.relative_to(root).parts)
output = output.with_suffix(".bin")
# Load original mesh and render
org = o3d.io.read_triangle_mesh(file, enable_post_processing=True)
rgb_org = o3de.auto_render_color(img_size, [org])
cv2.imwrite(str(output.with_suffix(".png")), rgb_org[..., ::-1])
# Decompose and render parts
parts = output.lazy_obj(lambda: acd(org))
rgb_parts = o3de.auto_render_color(img_size, [decompose.part_to_o3d(parts)])
cv2.imwrite(str(output.with_suffix(".jpg")), rgb_parts[..., ::-1])
# Visualize
for f in output_dir.rglob("*.bin"):
parts = decompose.part_to_o3d(f.binary())
o3d.visualization.draw_geometries([parts])
def test_gdino():
from api.grounding_dino import GroundingDINO
gdino = GroundingDINO(box_thresh=0.5)
image = cv2.imread("/media/tongzj/Data/Downloads/1.jpg")
dets = gdino(image, "robot. bag")
cv2.imwrite("runs/gdino.jpg", sve.annotate(image, dets))
# SAM2
from api.segment_anything import SegmentAnythingV2
sam2 = SegmentAnythingV2("tiny")
dets = sam2.inference_with_box(image, dets)
cv2.imwrite("runs/gsam.png", sve.annotate(image, dets))
# other
mask = dets.mask.any(axis=0)
image[~mask] = 255
cv2.imwrite("runs/debug.png", image)
def test_handeye_calib():
from api.utils.code_det import Charuco, hand_in_eye_calib, hand_to_eye_calib
len_marker = .021
detector = Charuco(len_marker=len_marker, len_square=.028, shape=(5, 7))
# cv2.imwrite("runs/charuco.png", detector.to_image(100)), exit()
def info_result(T):
print(T)
T = T[:3].flatten().tolist()
print([round(i, 8) for i in T])
if 0:
root = Path("runs/handeye1")
cam = D435I
bgrs = [cv2.imread(str(file)) for file in (root / "color").iterdir()]
Teb = np.stack([Path(file).binary()["transform"]["T"] for file in (root / "metadata").iterdir()])
Toc = [detector.pose_global(bgr, cam) for bgr in bgrs]
Tce = hand_in_eye_calib(Toc, Teb)
info_result(Tce)
else:
root = Path("runs/handeye2")
cam = Camera.from_yaml("config/camera/RS-L515.yaml")
frames = [Path(f).binary() for f in root.iterdir()]
bgrs = [f["cam2"][1] for f in frames]
Teb = np.stack([f["transform"]["T"] for f in frames])
Toc = np.stack([detector.pose_global(bgr, cam) for bgr in bgrs])
Tcb = hand_to_eye_calib(Toc, Teb)
info_result(Tcb)
for f in frames:
frame = Frame(cam, *f["cam2"][1:], Tcw=Tcb)
o3d.visualization.draw_geometries([
frame.to_pcd(), o3d.geometry.TriangleMesh.create_coordinate_frame(0.2)
])
def test_obj_tracker():
from api.grounding_dino import GroundingDINO
from api.segment_anything import SegmentAnythingV2
from api.utils.object_tracker import ArucoObjectTracker
tracker = ArucoObjectTracker("config/aruco_obj.yaml")
gdino = GroundingDINO()
sam2 = SegmentAnythingV2("tiny")
frame = Frame(
camera=D435I,
color="assets/aruco_obj/c.png",
depth="assets/aruco_obj/d.png",
)
# track objects
tracker.track(frame)
geos = [obj.o3dmesh() for obj in tracker.objects]
geos = list(filter(None, geos))
if geos:
# segment objects
dets = sam2.inference_with_box(frame.color, THREAD_POOL.submit(gdino, frame.color, caption="bag"))
color_anno = sve.annotate(frame.color, dets, anno_box=False)
cv2.imwrite("runs/obj_tracker.png", color_anno)
# visualize
pcds = [frame.to_pcd(mask) for mask in dets.mask]
idx = np.arange(len(pcds))
for i, color in zip(idx, plt.get_cmap("tab20")(idx)[:, :3]):
pcds[i].paint_uniform_color(color)
# save objects
Path("runs/shelf.bin").binary((tracker.objects[0].pose, [o3de.to_pcd_array(x) for x in pcds]))
o3d.visualization.draw_geometries(geos + pcds)
def test_o3de():
camera = D435I
camera.depth_scale = 5000
camera.depth_range = None
color = cv2.imread("assets/desktop-c.png")
depth = camera.process_depth("assets/desktop-d.png")
pcd = o3de.rgbd2pcd(camera.pointmap(depth), color, mask=depth > 0)
pcd = o3de.estimate_pcd_normal(pcd) # , cam_pos=[0] * 3)
o3d.visualization.draw_geometries([pcd])
def test_radio():
from api.encoders import RADIO
color = cv2.imread("assets/cat.jpg")
color = torch.as_tensor(color / 255).permute(2, 0, 1)[None]
model = RADIO(enable_sam=True)
ret = model.encode_images(color)
if model.enable_text:
feat_text = model.encode_texts(["cats", "cat", "dog"])
print(torch.cosine_similarity(ret[1][0], feat_text))
if model.enable_sam:
model.sam()
def test_sam2():
from api.segment_anything import SegmentAnythingV2
from api.utils.realsense import RGBDCamera
sam2 = SegmentAnythingV2("tiny")
image = cv2.imread("assets/desktop-c.png")
point = np.array([[310, 160]])
# Automatic mask generation
pred = sam2.build_predictor(image, auto_mode=True)
dets = pred()
cv2.imwrite("runs/sam2_all.png", sve.annotate(image, dets, anno_box=False))
# Predict with prompts
dets = pred(point_coords=point, point_labels=np.ones(len(point), dtype=np.bool_))
cv2.imwrite("runs/sam2.png", sve.annotate(image, dets, anno_box=False))
for c, d in RGBDCamera():
dets = sam2(c)
print(dets.confidence)
plt.imshow(sve.annotate(c, dets))
plt.pause(1e-3)
def test_server():
import logging
import sys
import time
from api.utils import func_invoker
# config
FUNCTIONS = {
"add": lambda x, y: x + y,
"sleep": lambda x: time.sleep(x) or x,
}
# FastAPI
if len(sys.argv) > 0 and sys.argv[0].split("/")[-1] == "uvicorn":
import fastapi
SERVER = func_invoker.FastapiServer(functions=FUNCTIONS, loginfo=logging.getLogger("uvicorn").info)
# uvicorn server:app
app = fastapi.FastAPI(
title="Functions API",
description="author: [TongZJ](https://github.com/Instinct323)\n\n" + SERVER.func_docs(),
version="1.0.0"
)
@app.post("/invoke")
async def invoke(request):
return await SERVER.get_response(request)
# Zmq
else:
import zmq
from api.utils.zmq_socket import ZmqSocket
SERVER = func_invoker.ZmqSocketServer(
functions=FUNCTIONS, loginfo=print,
sock=ZmqSocket(zmq.REP, addr="0.0.0.0:5555", queue_size=10)
)
print("Ready")
SERVER.main()
def test_tag2text():
from api.grounding_dino import GroundingDINO
from api.tag2text import Tag2Text
gdino = GroundingDINO()
tt = Tag2Text(tag_thresh=0.5)
image = cv2.imread("/media/tongzj/Data/Workbench/Lab/Lan-grasp/assets/green-pot/c1.png")
text = tt(sv.cv2_to_pillow(image))
print(text)
dets = gdino(image, text)
cv2.imwrite("runs/tt-gdino.jpg", sve.annotate(image, dets))
def test_zero123plus():
from api.zero123plus import Zero123plus
from api.grounding_dino import GroundingDINO
from api.segment_anything import SegmentAnythingV2
zero123p = Zero123plus()
gdino = GroundingDINO()
sam2 = SegmentAnythingV2("large")
target = "cup"
img = cv2.imread("assets/cup2-c.png")
with Timer("Main"):
# Ground and segment the image
mask = sam2(img, box=gdino(img, target).xyxy[0][None]).mask[0]
img[~mask] = 114
# Inference
result = zero123p(Image.fromarray(img), 75)
bboxes = [gdino(r, target)[0] for r in result]
# Show the results
for i, (img, box) in enumerate(zip(result, bboxes)):
plt.subplot(2, 3, i + 1)
plt.imshow(sve.annotate(img, box))
plt.show()
if __name__ == '__main__':
test_ai_client()