Python从被调用的python文件中获取列表

时间:2017-12-20 18:44:07

标签: python tensorflow imagenet

task2.py中,我正在执行这样的python文件:

text = os.system("python new_image.py  --image_file " + "/home/roots/" + str(nc) + ".jpg --num_top_predictions 1")

new_image.py的基本行似乎

def main(_):
    maybe_download_and_extract()
    image = (FLAGS.image_file if FLAGS.image_file else
            os.path.join(FLAGS.model_dir, 'cropped_panda.jpg'))
    score = run_inference_on_image(image)
    return score


if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument(
        '--model_dir',
        type=str,
        default='/tmp/imagenet',
        help="""\
        Path to classify_image_graph_def.pb,
        imagenet_synset_to_human_label_map.txt, and
        imagenet_2012_challenge_label_map_proto.pbtxt.\
        """
    )
    parser.add_argument(
        '--image_file',
        type=str,
        default='',
        help='Absolute path to image file.'
    )
    parser.add_argument(
        '--num_top_predictions',
        type=int,
        default=5,
        help='Display this many predictions.'
    )
    FLAGS, unparsed = parser.parse_known_args()
    tf.app.run(main=main, argv=[sys.argv[0]] + unparsed)

我在张量流模型imagenet中修改了classify_image.py,以便run_inference_on_image()返回一个列表。我现在想在task2.py中获取此列表。我该怎么做?

1 个答案:

答案 0 :(得分:0)

一种简单的方法是将列表保存为pickle中的临时new_image.py文件,然后在pickle中加载task2.py文件。即。

# In new_image.py
import pickle
picklefile = open('temp.pkl', 'wb')

# Dump list to the pickle file
pickle.dump(listvariable, picklefile)
picklefile.close()

# In task2.py
import pickle 
picklefile = open('temp.pkl', 'rb')
newlist_variable = pickle.load(picklefile)
picklefile.close()

我不知道是否有办法在两个python进程之间共享动态变量。但如果有的话,我很想知道!