1 申明一个类变量 如 value
2 赋值 self.value = 需要的函数值
3 在另一个脚本中通过类的实例对象调用
class Visualizer:
# To obtain the Position and Rotation Michael 20210511
posrot = []
def __init__(self):
args = DatasetCatalog.get(cfg.test.dataset)
self.ann_file = args['ann_file']
self.coco = coco.COCO(self.ann_file)
# def visualize(self, output, batch):
def visualize(self, output, batch):
inp = img_utils.unnormalize_img(batch['inp'][0], mean, std).permute(1, 2, 0)
kpt_2d = output['kpt_2d'][0].detach().cpu().numpy()
img_id = int(batch['img_id'][0])
anno = self.coco.loadAnns(self.coco.getAnnIds(imgIds=img_id))[0]
kpt_3d = np.concatenate([anno['fps_3d'], [anno['center_3d']]], axis=0)
K = np.array(anno['K'])
pose_gt = np.array(anno['pose'])
pose_pred = pvnet_pose_utils.pnp(kpt_3d, kpt_2d, K) # Position and Rotation here Michael
# To have the value by self.* Michael 20210511
self.posrot = pose_pred
# print("fun")
# print(pose_pred)
调用pose_pred值
def run_visualize():
from lib.networks import make_network
from lib.datasets import make_data_loader
from lib.utils.net_utils import load_network
import tqdm
import torch
from lib.visualizers import make_visualizer
network = make_network(cfg).cuda()
load_network(network, cfg.model_dir, resume=cfg.resume, epoch=cfg.test.epoch)
network.eval()
data_loader = make_data_loader(cfg, is_train=False) # here Michael
visualizer = make_visualizer(cfg)
for batch in tqdm.tqdm(data_loader):
for k in batch:
if k != 'meta':
batch[k] = batch[k].cuda() #
with torch.no_grad():
output = network(batch['inp'], batch)
visualizer.visualize(output, batch)
# get position and rotation Michael 20210511
# print("poem")
print(visualizer.posrot)
调用打印这个值