Description
Starting from matplotlib 3.6+, FigureCanvas.tostring_rgb() was removed (replaced by buffer_rgba()). This causes AttributeError when trying to render segmentation results in both fastsam/prompt.py and utils/tools.py.
Error
AttributeError: 'FigureCanvasAgg' object has no attribute 'tostring_rgb'. Did you mean: 'tostring_argb'?
Locations
fastsam/prompt.py line 172 (in plot_to_result())
utils/tools.py line 183 (in fast_process())
Both follow the same pattern:
try:
buf = fig.canvas.tostring_rgb()
except AttributeError:
fig.canvas.draw()
buf = fig.canvas.tostring_rgb()
Note: The except block retries the same failing call, which will never succeed.
Suggested Fix
Replace tostring_rgb() with buffer_rgba() which is the modern matplotlib API:
try:
buf = fig.canvas.buffer_rgba()
except AttributeError:
fig.canvas.draw()
buf = fig.canvas.buffer_rgba()
Also update the reshape from 3 channels (RGB) to 4 channels (RGBA), and switch color conversion accordingly.
Additional note
When running on a headless server (no display), matplotlib may also fail due to missing TkAgg backend. A robust fix would add matplotlib.use("Agg") or switch backend before plot calls.
Description
Starting from matplotlib 3.6+,
FigureCanvas.tostring_rgb()was removed (replaced bybuffer_rgba()). This causesAttributeErrorwhen trying to render segmentation results in bothfastsam/prompt.pyandutils/tools.py.Error
Locations
fastsam/prompt.pyline 172 (inplot_to_result())utils/tools.pyline 183 (infast_process())Both follow the same pattern:
Note: The except block retries the same failing call, which will never succeed.
Suggested Fix
Replace
tostring_rgb()withbuffer_rgba()which is the modern matplotlib API:Also update the reshape from 3 channels (RGB) to 4 channels (RGBA), and switch color conversion accordingly.
Additional note
When running on a headless server (no display), matplotlib may also fail due to missing TkAgg backend. A robust fix would add
matplotlib.use("Agg")or switch backend before plot calls.