Shriinivas / gimpsegany

Gimp plugin for integration with Meta Segment Anything
GNU Affero General Public License v3.0
23 stars 0 forks source link

Registering the plugin with my GIMP 2.10 on macOS 10.15.7 #1

Open PALYGAP opened 10 months ago

PALYGAP commented 10 months ago

Thanks for the plugin, though I am having trouble registering it with my GIMP 2.10 on macOS 10.15.7. I have run your install validation script (/path/to/python3/python ./seganybridge.py vit_h /path/to/checkpoint/model/sam_vit_h_4b8939.pth) and got a Success. I have put the seganybridge.py and seganyplugin.py in the /Users/me/Library/Application Support/GIMP/2.10/plug-ins repository. I also changed the mode so that these files are executable (chmod 777). Made sure I restarted GIMP. I also made sure SAM was properly installled by running the script amg.py from the SAM github and generated the segmentation masks. So seems my only trouble is registering your plugin with GIMP. Do you have any idea why it is not registering with GIMP and would you know of any way to check/debug the registration process ?

Shriinivas commented 10 months ago

I have modified the script on github to accept spaces in the directory, please use the new script. Here are a couple other things to note 1) Make sure the segment-anything folder you created with git clone is in the PYTHONPATH 2) You need to have Python-Fu enabled. Also, check the python version from Python-Fu console, it should be 2.7.x. The script will run with only python2 since, to my knowledge, GIMP 2.10 has not yet upgraded to python3. Please let me know if you still face any issues. 3) You should make only seganyplugin.py executable, making the other also executable won't break anything, but you would get an error on console after start-up.

Since GIMP 2.10 works with python2 and Segment Anything needs Python3, I am calling the seganybridge as an OS command from within python. So it's important that python2 and python3 are on your system and you specify the python3 path used by Segment Anything in the pop-up dialog of the plugin.

8o050 commented 7 months ago

Thank you so much for creating this plugin!

I had to make a couple of modifications to make this work on my mac (M3). The gimp version is 2.10.36 when I am writing this. I am using miniconda to set the SegmentAnything model up.

  1. I ran the GIMP in the terminal to get the errors. Otherwise, it just silently fails to load:
/Applications/GIMP.app/Contents/MacOS/gimp --verbose
  1. Add the following lines to the top of the seganyplugin.py just like other plugins like gradients-save-as-css:
#! /usr/bin/env python
# -*- coding: utf-8 -*-

Without this, GIMP was giving me an error like this.

GIMP-WARNING: gimp: gimp_wire_read(): error
  1. Clear the env_vars with the shellRun. In seganyplugin.py , I modified the following lines to pass env_vars:
    env_vars = {
        'PYTHONPATH': ""
    }
    shellRun(cmd, env_vars=env_vars)

Without this, there was a numpy conflict error like these:

2024-02-10 13:07:27 - INFO - Running command: /Users/user/miniconda3/envs/gimp/bin/python /Applications/GIMP.app/Contents/Resources/lib/gimp/2.0/plug-ins/seganyplugin/seganybridge.py vit_h /Users/user/Documents/segment_anything/sam_vit_h_4b8939.pth /var/folders/3l/.../T/__seg__5GZQCr.png Selection Multiple /var/folders/3l/.../T/__seg__mask__ True /var/folders/3l/.../T/__seg__sel__.txt
2024-02-10 13:07:27 - ERROR - Command failed with the following error:
 Traceback (most recent call last):
  File "/Applications/GIMP.app/Contents/Resources/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/numpy/core/__init__.py", line 40, in <module>
    from . import multiarray
  File "/Applications/GIMP.app/Contents/Resources/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/numpy/core/multiarray.py", line 13, in <module>
    from . import overrides
  File "/Applications/GIMP.app/Contents/Resources/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/numpy/core/overrides.py", line 6, in <module>
    from numpy.core._multiarray_umath import (
ImportError: dlopen(/Applications/GIMP.app/Contents/Resources/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/numpy/core/_multiarray_umath.so, 0x0002): symbol not found in flat namespace '_PyBuffer_Type'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Applications/GIMP.app/Contents/Resources/lib/gimp/2.0/plug-ins/seganyplugin/seganybridge.py", line 24, in <module>
    import torch
  File "/Users/user/miniconda3/envs/gimp/lib/python3.9/site-packages/torch/__init__.py", line 1209, in <module>
    from .storage import _StorageBase, TypedStorage, _LegacyStorage, UntypedStorage, _warn_typed_storage_removal
  File "/Users/user/miniconda3/envs/gimp/lib/python3.9/site-packages/torch/storage.py", line 14, in <module>
    import numpy as np
  File "/Applications/GIMP.app/Contents/Resources/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/numpy/__init__.py", line 142, in <module>
    from . import core
  File "/Applications/GIMP.app/Contents/Resources/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/numpy/core/__init__.py", line 71, in <module>
    raise ImportError(msg)
ImportError:

IMPORTANT: PLEASE READ THIS FOR ADVICE ON HOW TO SOLVE THIS ISSUE!

Importing the multiarray numpy extension module failed.  Most
likely you are trying to import a failed build of numpy.
Here is how to proceed:
- If you're working with a numpy git repository, try `git clean -xdf`
  (removes all files not under version control) and rebuild numpy.
- If you are simply trying to use the numpy version that you have installed:
  your installation is broken - please reinstall numpy.
- If you have already reinstalled and that did not fix the problem, then:
  1. Check that you are using the Python you expect (you're using /Users/user/miniconda3/envs/gimp/bin/python),
     and that you have no directories in your PATH or PYTHONPATH that can
     interfere with the Python and numpy versions you're trying to use.
  2. If (1) looks fine, you can open a new issue at
     https://github.com/numpy/numpy/issues.  Please include details on:
     - how you installed Python
     - how you installed numpy
     - your operating system
     - whether or not you have multiple versions of Python installed
     - if you built from source, your compiler versions and ideally a build log

     Note: this error has many possible causes, so please don't comment on
     an existing issue about this - open a new one instead.

Original error was: dlopen(/Applications/GIMP.app/Contents/Resources/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/numpy/core/_multiarray_umath.so, 0x0002): symbol not found in flat namespace '_PyBuffer_Type'

2024-02-10 13:07:27 - DEBUG - Finished creating segments!

Hope this helps someone!

Popshells commented 1 week ago

Hi, thank you for this! I followed all these guidelines and I got the "Success!" message in the console after running seganybridge.py in my M1, made seganyplugin.py executable and added export PYTHONPATH="/path/to/segment-anything:$PYTHONPATH" to my SAM-env activate command, the venv has pytorch.org/whl/nightly/cpu , but the plugin crashes starting up Gimp 2.10.38 (revision 1), the following error is from the terminal and I don't know how to troubleshoot it, any suggestions are welcome, thank you in advance:

Querying plug-in: '/Users/me/Library/Application Support/GIMP/2.10/plug-ins/seganyplugin.py'
/Applications/GIMP.app/Contents/MacOS/gimp: LibGimpBase-WARNING: gimp: gimp_wire_read(): error
Terminating plug-in: '/Users/me/Library/Application Support/GIMP/2.10/plug-ins/seganyplugin.py'
Shriinivas commented 1 week ago

Hi @Popshells I suppose your issue is fixed after adding shebang as suggested by @8o050, if not please let me know. I have been avoiding installing python GIMP since the latest re-installion of OS as python 2 is outdated (discontinued since 2020) and installing it on Linux is quite a bit of a hassle. And don't want to include anything that I have not tested on my machine, so not including this change in the code for now. I hope GIMP 3 arrives soon with its python 3 support. That would make this plugin much simpler and a lot more streamlined.

Popshells commented 1 week ago

Thanks for the rapid response! Yes! After adding the shebang at the very top, and replacing shellRun(cmd) as @8o050 suggested it's working fine! Thank you! Awesome tool integration!