-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcobot_pvnet.py
186 lines (151 loc) · 6.81 KB
/
cobot_pvnet.py
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
"""
Purpose: This is a non-ROS script that contains the pose estimation pipeline. Pose estimation is a two stage process: 1) Mask R-CNN + 2) PVNet
Author: Hameed Abdul ([email protected]) and Yangfei Dai ([email protected])
"""
import os
import json
import argparse
import yaml
import torch
import cv2
import gin
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
import matplotlib.patches as patches
from PIL import Image
from pathlib import Path
from yacs.config import CfgNode as CN
from lib.config import args, cfgs
from lib.networks import make_network
from lib.utils.net_utils import load_network
from lib.visualizers import make_visualizer
from lib.datasets.transforms import make_transforms
from lib.datasets import make_data_loader
from lib.utils.pvnet import pvnet_pose_utils
from mrcnn.utils.maskrcnnWrapper import MaskRCNNWrapper
# Configs/Models in the order 0: mainshell, 1: topshell, 2: insert_mold
def make_and_load_pvnet(cfg):
net = make_network(cfg).cuda()
load_network(net, cfg.model_dir, resume=cfg.resume, epoch=cfg.test.epoch)
return net
def call_pvnet(data, is_vis=True):
cam_u = 2694.112343
cam_v = 1669.169773
W, H = int(5472), int(3648)
crop_size = 128
# shift the uv from original camera uv to cropped image center
shifted_u = cam_u + (W//2 - data['uv'][0]) - (W//2 - crop_size//2)
shifted_v = cam_v + (H//2 - data['uv'][1]) - (H//2 - crop_size//2)
K_cam = np.array([[10704.062350, 0, shifted_u],
[0, 10727.438047, shifted_v],
[0, 0, 1]])
cat_idx = data['class']
cur_pvnet = pvnets[cat_idx]
cur_cfg = cfgs[cat_idx]
cur_roi = data['image_128x128']
return run_inference(cur_pvnet, cur_cfg, cur_roi, K_cam, is_vis)
def predict_to_pose(pvnet_output, cfg, K_cam, input_img, is_vis: bool=False, is_pose_H: bool=True):
kpt_3d = np.concatenate([cfg.fps_3d, [cfg.center_3d]], axis=0)
kpt_2d = pvnet_output['kpt_2d'][0].detach().cpu().numpy()
pose_pred = pvnet_pose_utils.pnp(kpt_3d, kpt_2d, K_cam)
if is_vis:
visualize_pose(input_img, cfg, pvnet_output, K_cam, pose_pred)
if is_pose_H:
# return pose as 4x4 matrix
return np.c_[pose_pred.T, np.array([0, 0, 0, 1])].T
# return pose as 3x4 matrix
return pose_pred
def draw_axis(img, R, t, K, scale=0.006, dist=None):
"""
Draw a 6dof axis (XYZ -> RGB) in the given rotation and translation
:param img - rgb numpy array
:R - Rotation matrix, 3x3
:t - 3d translation vector, in meters (dtype must be float)
:K - intrinsic calibration matrix , 3x3
:scale - factor to control the axis lengths
:dist - optional distortion coefficients, numpy array of length 4. If None distortion is ignored.
"""
img = img.astype(np.float32)
rotation_vec, _ = cv2.Rodrigues(R) #euler rotations
dist = np.zeros(4, dtype=float) if dist is None else dist
points = scale * np.float32([[1, 0, 0], [0, 1, 0], [0, 0, 1], [0, 0, 0]]).reshape(-1, 3)
axis_points, _ = cv2.projectPoints(points, rotation_vec, t, K, dist)
axis_points = axis_points.astype(int)
corner = tuple(axis_points[3].ravel())
img = cv2.line(img, corner, tuple(axis_points[0].ravel()), (255, 0, 0), 1)
# img = cv2.putText(img, "X", tuple(axis_points[0].ravel()), cv2.FONT_HERSHEY_SIMPLEX, 5, (255, 0, 0), 1)
img = cv2.line(img, corner, tuple(axis_points[1].ravel()), (0, 255, 0), 1)
# img = cv2.putText(img, "Y", tuple(axis_points[1].ravel()), cv2.FONT_HERSHEY_SIMPLEX, 5, (0, 255, 0), 1)
img = cv2.line(img, corner, tuple(axis_points[2].ravel()), (0, 0, 255), 1)
img = img.astype(np.uint8)
return img
def visualize_pose(input_img, cfg, pvnet_output, K_cam, pose_pred):
corner_3d = cfg.corner_3d
kpt_2d = pvnet_output['kpt_2d'][0].detach().cpu().numpy()
segmentation = pvnet_output['seg'][0].detach().cpu().numpy()
mask = pvnet_output['mask'][0].detach().cpu().numpy()
corner_2d_pred = pvnet_pose_utils.project(corner_3d, K_cam, pose_pred)
###########################
# overall result
###########################
plt.figure(0)
plt.subplot(221)
plt.imshow(input_img)
plt.axis('off')
plt.title('Input Image')
plt.subplot(222)
plt.imshow(mask)
plt.axis('off')
plt.title('Predicted Mask')
plt.subplot(223)
plt.imshow(input_img)
plt.scatter(kpt_2d[:8, 0], kpt_2d[:8, 1], color='red', s=10)
plt.axis('off')
plt.title('Key points detection')
ax = plt.subplot(224)
# ax.imshow(input_img)
ax.imshow(draw_axis(input_img, pose_pred[:3, :3], pose_pred[:3, 3], K_cam))
plt.axis('off')
plt.title('Pose Prediction')
# plt.savefig("/pvnet/data/evaluation/topshell.png")
from scipy.spatial.transform import Rotation
R = pose_pred[:3, :3]
euler_angles = Rotation.from_matrix(R).as_euler('xyz', degrees=True)
euler_angles_rounded = [int(angle) for angle in euler_angles]
print("Euler angles for Estimated Pose in camera frame:", euler_angles_rounded)
plt.show()
def run_inference(pvnet, cfg, image, K_cam, is_vis=False):
pvnet.eval()
transform = make_transforms(cfg, is_train=False)
processed_image, _, _ = transform(image)
processed_image = np.array(processed_image).astype(np.float32)
# Convert the preprocessed image to a tensor and move it to GPU
input_tensor = torch.from_numpy(processed_image).unsqueeze(0).cuda().float()
with torch.no_grad():
pvnet_output = pvnet(input_tensor)
return predict_to_pose(pvnet_output, cfg, K_cam, image, is_vis)
if __name__ == '__main__':
# load instance segmentaino results from mask r-cnn
"""
'class': an integer representing the class.
'uv': a numpy array containing two float values.
'score': a float representing the score.
'image_128x128': a 2D numpy array representing an image.
"""
# Load all need models and configs
gin.parse_config_file('./mrcnn/simple_output.gin')
gin.constant('CobotPoseEstNode.T_camera_in_base', np.load("./data/11_23_image_dataset/T_camera_in_base.npy"))
T_tagboard_in_cam = np.load("./data/11_23_image_dataset/T_tagboard_in_cam.npy")
mrcnn = MaskRCNNWrapper()
pvnets = tuple([make_and_load_pvnet(c) for c in cfgs])
img_path = Path("./data/11_23_image_dataset")
for img_path in img_path.glob("*.png"):
print(img_path)
img = cv2.imread(str(img_path))[:,:,::-1]
data_for_pvnet, _, _ = mrcnn(img, is_vis=True)
poses = [call_pvnet(data, is_vis=True) for data in data_for_pvnet]
[d.update({'T_part_in_cam': T_part_in_cam, 'T_part_in_tag': np.linalg.inv(T_tagboard_in_cam) @ T_part_in_cam}) for d, T_part_in_cam in zip(data_for_pvnet, poses)]
data = {str(i):d for i, d in enumerate(data_for_pvnet)}
output_path = img_path.parent / (img_path.name[:-4] + ".npz")
np.savez_compressed(output_path, **data)