python中在一个脚本调用另一个脚本类中函数变量值

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)

调用打印这个值

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值