Other people have this problem, I used their solutions but not solved.
I use virtual env with python3.5.
Matplotlib is installed under virtual env.
I have python3.tkinter installed at system.
When I check
matplotlib.get_backend()
I have
>>> import matplotlib
>>> matplotlib.get_backend()
'TkAgg'
But when I run the code below
for image_path in TEST_IMAGE_PATHS:
image = Image.open(image_path)
# the array based representation of the image will be used later in order to prepare the
# result image with boxes and labels on it.
image_np = load_image_into_numpy_array(image)
# Expand dimensions since the model expects images to have shape: [1, None, None, 3]
image_np_expanded = np.expand_dims(image_np, axis=0)
# Actual detection.
output_dict = run_inference_for_single_image(image_np, detection_graph)
# Visualization of the results of a detection.
vis_util.visualize_boxes_and_labels_on_image_array(
image_np,
output_dict['detection_boxes'],
output_dict['detection_classes'],
output_dict['detection_scores'],
category_index,
instance_masks=output_dict.get('detection_masks'),
use_normalized_coordinates=True,
line_thickness=8)
#plt.figure(figsize=IMAGE_SIZE)
plt.imshow(image_np)
plt.show()
I have problem as
UserWarning: Matplotlib is currently using agg, which is a non-GUI backend, so cannot show the figure.
% get_backend())
I put at header as
from io import StringIO
import matplotlib
matplotlib.rcParams["backend"] = "TkAgg"
from matplotlib import pyplot as plt
from PIL import Image
some people said it was solved, but I still have the same problem and plt doesn't display image.
As you noted, Matplotlib backends sometimes require extra steps to run in virtual environments.
That being said, the documentation linked above also indicates that TkAgg should be available:
[...] the Tk framework (TkAgg backend) does not require any external dependencies and is normally always available.
I use Ubuntu, and I assumed TkAgg would be relying on PyGObject. That option has a note itself that links to build instructions.
Following the PyGObject build instructions, I went to install its system dependencies:
sudo apt-get install -y python3-venv python3-wheel python3-dev
sudo apt-get install -y libgirepository1.0-dev build-essential \
libbz2-dev libreadline-dev libssl-dev zlib1g-dev libsqlite3-dev wget \
curl llvm libncurses5-dev libncursesw5-dev xz-utils tk-dev libcairo2-dev
Then added the following Python dependencies to my project's virtual environment:
# inside my project's virtual environment
pip install pycairo
pip install pygobject
Once that done, running my project as usual displayed the expected graph.
I'm using Ubuntu 18.04.2 and Python 3.6.8 in the virtual environment of my project.
I skipped through most of the build instructions for PyGObject and only did what I described above.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With