nbv_sim/active_grasp/baselines.py

124 lines
3.7 KiB
Python
Raw Normal View History

2021-07-07 17:46:11 +02:00
import numpy as np
2021-07-08 09:15:09 +02:00
import scipy.interpolate
import rospy
2021-07-07 17:46:11 +02:00
2021-08-05 13:45:22 +02:00
from .policy import BasePolicy
2021-07-07 17:46:11 +02:00
from vgn.utils import look_at
2021-07-08 10:54:29 +02:00
class SingleView(BasePolicy):
2021-07-07 17:46:11 +02:00
"""
Process a single image from the initial viewpoint.
"""
2021-08-03 18:11:30 +02:00
def update(self, img, extrinsic):
self.integrate_img(img, extrinsic)
self.best_grasp = self.predict_best_grasp()
2021-07-07 17:46:11 +02:00
self.done = True
2021-07-08 10:54:29 +02:00
class TopView(BasePolicy):
2021-07-07 17:46:11 +02:00
"""
Move the camera to a top-down view of the target object.
"""
def activate(self, bbox):
super().activate(bbox)
2021-08-03 18:11:30 +02:00
eye = np.r_[self.center[:2], self.center[2] + 0.3]
2021-07-07 17:46:11 +02:00
up = np.r_[1.0, 0.0, 0.0]
2021-08-03 18:11:30 +02:00
self.target = look_at(eye, self.center, up)
2021-07-07 17:46:11 +02:00
2021-08-03 18:11:30 +02:00
def update(self, img, extrinsic):
self.integrate_img(img, extrinsic)
error = extrinsic.translation - self.target.translation
2021-07-07 17:46:11 +02:00
if np.linalg.norm(error) < 0.01:
2021-08-03 18:11:30 +02:00
self.best_grasp = self.predict_best_grasp()
2021-07-07 17:46:11 +02:00
self.done = True
2021-08-03 18:11:30 +02:00
return self.target
2021-07-08 09:15:09 +02:00
2021-07-08 10:54:29 +02:00
class RandomView(BasePolicy):
2021-07-08 09:36:51 +02:00
"""
Move the camera to a random viewpoint on a circle centered above the target.
"""
2021-08-03 18:11:30 +02:00
def __init__(self, intrinsic):
super().__init__(intrinsic)
self.r = 0.06 # radius of the circle
self.h = 0.3 # distance above bbox center
2021-07-08 09:36:51 +02:00
def activate(self, bbox):
super().activate(bbox)
t = np.random.uniform(np.pi, 3.0 * np.pi)
2021-08-03 18:11:30 +02:00
eye = self.center + np.r_[self.r * np.cos(t), self.r * np.sin(t), self.h]
2021-07-08 09:36:51 +02:00
up = np.r_[1.0, 0.0, 0.0]
2021-08-03 18:11:30 +02:00
self.target = look_at(eye, self.center, up)
2021-07-08 09:36:51 +02:00
2021-08-03 18:11:30 +02:00
def update(self, img, extrinsic):
self.integrate_img(img, extrinsic)
error = extrinsic.translation - self.target.translation
2021-07-08 09:36:51 +02:00
if np.linalg.norm(error) < 0.01:
2021-08-03 18:11:30 +02:00
self.best_grasp = self.predict_best_grasp()
2021-07-08 09:36:51 +02:00
self.done = True
2021-08-03 18:11:30 +02:00
return self.target
2021-07-08 09:36:51 +02:00
2021-07-08 10:54:29 +02:00
class FixedTrajectory(BasePolicy):
2021-07-08 09:15:09 +02:00
"""
Follow a pre-defined circular trajectory centered above the target object.
"""
2021-08-03 18:11:30 +02:00
def __init__(self, intrinsic):
super().__init__(intrinsic)
self.r = 0.08
2021-07-08 09:15:09 +02:00
self.h = 0.3
self.duration = 6.0
self.m = scipy.interpolate.interp1d([0, self.duration], [np.pi, 3.0 * np.pi])
def activate(self, bbox):
super().activate(bbox)
self.tic = rospy.Time.now()
2021-08-03 18:11:30 +02:00
def update(self, img, extrinsic):
self.integrate_img(img, extrinsic)
2021-07-08 09:15:09 +02:00
elapsed_time = (rospy.Time.now() - self.tic).to_sec()
if elapsed_time > self.duration:
2021-08-03 18:11:30 +02:00
self.best_grasp = self.predict_best_grasp()
2021-07-08 09:15:09 +02:00
self.done = True
else:
t = self.m(elapsed_time)
2021-08-03 18:11:30 +02:00
eye = self.center + np.r_[self.r * np.cos(t), self.r * np.sin(t), self.h]
2021-07-08 09:15:09 +02:00
up = np.r_[1.0, 0.0, 0.0]
2021-08-03 18:11:30 +02:00
target = look_at(eye, self.center, up)
2021-07-08 09:15:09 +02:00
return target
2021-07-08 10:54:29 +02:00
class AlignmentView(BasePolicy):
"""
Align the camera with an initial grasp prediction as proposed in (Gualtieri, 2017).
"""
def activate(self, bbox):
super().activate(bbox)
2021-08-03 18:11:30 +02:00
self.target = None
def update(self, img, extrinsic):
self.integrate_img(img, extrinsic)
if not self.target:
grasp = self.predict_best_grasp()
if not grasp:
self.done = True
return
R, t = grasp.pose.rotation, grasp.pose.translation
2021-07-08 10:54:29 +02:00
eye = R.apply([0.0, 0.0, -0.16]) + t
2021-08-03 18:11:30 +02:00
center = t
2021-07-08 10:54:29 +02:00
up = np.r_[1.0, 0.0, 0.0]
2021-08-03 18:11:30 +02:00
self.target = look_at(eye, center, up)
2021-07-08 10:54:29 +02:00
2021-08-03 18:11:30 +02:00
error = extrinsic.translation - self.target.translation
2021-07-08 10:54:29 +02:00
if np.linalg.norm(error) < 0.01:
2021-08-03 18:11:30 +02:00
self.best_grasp = self.predict_best_grasp()
2021-07-08 10:54:29 +02:00
self.done = True
2021-08-03 18:11:30 +02:00
return self.target