elisemercury / Duplicate-Image-Finder

difPy - Python package for finding duplicate or similar images within folders
https://difpy.readthedocs.io
MIT License
448 stars 67 forks source link

AttributeError: 'PosixPath' object has no attribute 'is_relative_to' #52

Closed FabioMD1972 closed 1 year ago

FabioMD1972 commented 1 year ago

Hi, on Google Colab I got this error: `--------------------------------------------------------------------------- AttributeError Traceback (most recent call last) in ----> 1 src_res= dif("/content/02_Draw_append", "/content/02_Draw")

1 frames /usr/local/lib/python3.8/dist-packages/difPy/dif.py in init(self, directory_A, directory_B, recursive, similarity, px_size, show_progress, show_output, delete, silent_del) 70 directory_A = dif._process_directory(directory_A) 71 directory_B = dif._process_directory(directory_B) ---> 72 dif._path_validation([directory_A, directory_B]) 73 img_matrices_A, folderfiles_A = dif._create_imgs_matrix(directory_A, px_size, recursive, show_progress) 74 img_matrices_B, folderfiles_B = dif._create_imgs_matrix(directory_B, px_size, recursive, show_progress)

/usr/local/lib/python3.8/dist-packages/difPy/dif.py in _path_validation(paths) 143 raise ValueError('An attempt to compare the directory with itself.') 144 path1, path2 = paths --> 145 if path1.is_relative_to(path2) or path2.is_relative_to(path1): 146 raise ValueError('One directory belongs to another.') 147

AttributeError: 'PosixPath' object has no attribute 'is_relative_to'`

the dif call is for two different folders: src_res= dif("/content/02_Draw_append", "/content/02_Draw")

All works fine if I call it on the some folder like: src_res= dif("/content/02_Draw_append")

Here you can find pip instal info: Looking in indexes: https://pypi.org/simple, https://us-python.pkg.dev/colab-wheels/public/simple/ Requirement already satisfied: difPy in /usr/local/lib/python3.8/dist-packages (2.4.5) Requirement already satisfied: opencv-python in /usr/local/lib/python3.8/dist-packages (from difPy) (4.6.0.66) Requirement already satisfied: matplotlib in /usr/local/lib/python3.8/dist-packages (from difPy) (3.2.2) Requirement already satisfied: scikit-image in /usr/local/lib/python3.8/dist-packages (from difPy) (0.18.3) Requirement already satisfied: numpy in /usr/local/lib/python3.8/dist-packages (from difPy) (1.21.6) Requirement already satisfied: kiwisolver>=1.0.1 in /usr/local/lib/python3.8/dist-packages (from matplotlib->difPy) (1.4.4) Requirement already satisfied: python-dateutil>=2.1 in /usr/local/lib/python3.8/dist-packages (from matplotlib->difPy) (2.8.2) Requirement already satisfied: pyparsing!=2.0.4,!=2.1.2,!=2.1.6,>=2.0.1 in /usr/local/lib/python3.8/dist-packages (from matplotlib->difPy) (3.0.9) Requirement already satisfied: cycler>=0.10 in /usr/local/lib/python3.8/dist-packages (from matplotlib->difPy) (0.11.0) Requirement already satisfied: pillow!=7.1.0,!=7.1.1,>=4.3.0 in /usr/local/lib/python3.8/dist-packages (from scikit-image->difPy) (7.1.2) Requirement already satisfied: tifffile>=2019.7.26 in /usr/local/lib/python3.8/dist-packages (from scikit-image->difPy) (2022.10.10) Requirement already satisfied: imageio>=2.3.0 in /usr/local/lib/python3.8/dist-packages (from scikit-image->difPy) (2.9.0) Requirement already satisfied: networkx>=2.0 in /usr/local/lib/python3.8/dist-packages (from scikit-image->difPy) (2.8.8) Requirement already satisfied: PyWavelets>=1.1.1 in /usr/local/lib/python3.8/dist-packages (from scikit-image->difPy) (1.4.1) Requirement already satisfied: scipy>=1.0.1 in /usr/local/lib/python3.8/dist-packages (from scikit-image->difPy) (1.7.3) Requirement already satisfied: six>=1.5 in /usr/local/lib/python3.8/dist-packages (from python-dateutil>=2.1->matplotlib->difPy) (1.15.0)

Tks.

elisemercury commented 1 year ago

Hi @FabioMD1972, Thanks fort raising the issue! That's indeed something that should be fixed and is likely because Google Colab uses a different path format than the Path library supports, therefore it's throwing the error.

I will make sure to fix this issue in the next difPy update.

Again, thanks for opening the issue! All the best, Elise

guites commented 1 year ago

I do believe this happens in linux environments, as I'm getting this same error on my docker container:

root@ca26ae3edc8f:/workspace# cat /etc/*-release PRETTY_NAME="Debian GNU/Linux 11 (bullseye)" NAME="Debian GNU/Linux" VERSION_ID="11" VERSION="11 (bullseye)" VERSION_CODENAME=bullseye ID=debian

But not on my macos machine.

Edit: this might also be related to Python versions or pathlib version.

On my macos

guites@macos project % python3
Python 3.10.9 (main, Dec 15 2022, 17:11:09) [Clang 14.0.0 (clang-1400.0.29.202)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from pathlib import Path
>>> directory = Path("images")
>>> directory
PosixPath('images')
>>> directory.is_relative_to
<bound method PurePath.is_relative_to of PosixPath('images')>

on the debian docker container:

root@ca26ae3edc8f:/workspace# python3
Python 3.8.16 (default, Dec  8 2022, 02:10:02)
[GCC 10.2.1 20210110] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from pathlib import Path
>>> directory = Path("images")
>>> directory
PosixPath('images')
>>> directory.is_relative_to
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'PosixPath' object has no attribute 'is_relative_to'
guites commented 1 year ago

Ah shoot turns out its related to the python version. ìs_relative_to needs python 3.9. Source: https://docs.python.org/3/library/pathlib.html#pathlib.PurePath.is_relative_to .

I'll open a PR with a python 3.8 compatible alternative for those who cannot update the python version

elisemercury commented 1 year ago

Hi all,

Thanks a lot for reporting the issue and thanks @guites for the solution suggestion! This issue is now fixed with difPy v3.0.0.

All the best, Elise