使用远程连接Linux服务器运行Python代码时出现如下错误:
Traceback (most recent call last):
  File "./tools/demo.py", line 153, in <module>
    demo(sess, net, im_name)
  File "./tools/demo.py", line 100, in demo
    vis_detections(im, cls, dets, thresh=CONF_THRESH)
  File "./tools/demo.py", line 50, in vis_detections
    fig, ax = plt.subplots(figsize=(12, 12))
  File "/home/yoshie/anaconda3/envs/mwx/lib/python2.7/site-packages/matplotlib/pyplot.py", line 1184, in subplots
    fig = figure(**fig_kw)
  File "/home/yoshie/anaconda3/envs/mwx/lib/python2.7/site-packages/matplotlib/pyplot.py", line 533, in figure
    **kwargs)
  File "/home/yoshie/anaconda3/envs/mwx/lib/python2.7/site-packages/matplotlib/backend_bases.py", line 161, in new_figure_manager
    return cls.new_figure_manager_given_figure(num, fig)
  File "/home/yoshie/anaconda3/envs/mwx/lib/python2.7/site-packages/matplotlib/backend_bases.py", line 167, in new_figure_manager_given_figure
    canvas = cls.FigureCanvas(figure)
  File "/home/yoshie/anaconda3/envs/mwx/lib/python2.7/site-packages/matplotlib/backends/backend_qt5agg.py", line 24, in __init__
    super(FigureCanvasQTAgg, self).__init__(figure=figure)
  File "/home/yoshie/anaconda3/envs/mwx/lib/python2.7/site-packages/matplotlib/backends/backend_qt5.py", line 234, in __init__
    _create_qApp()
  File "/home/yoshie/anaconda3/envs/mwx/lib/python2.7/site-packages/matplotlib/backends/backend_qt5.py", line 125, in _create_qApp
    raise RuntimeError('Invalid DISPLAY variable')
RuntimeError: Invalid DISPLAY variable错误原因是Linux没有GUI界面,本地terminal也无法显示图片。 **解决方法:**在导入matplotlib.pyplot库之前,先执行
import matplotlib as mpl
mpl.use('Agg')再执行
import matplotlib.pyplot as plt错误就不再发生了。 输出图片也可以选择保存在相应目录
plt.savefig("/xxx/xxx.jpg") 想要实时查看图片也推荐使用jupyter notebook。