本文是对这篇文章
imagededup图片去重的补充。
步骤:
1、conda创建虚拟环境
2、安装imagededup,此处会安装很多依赖库。
pip install imagededup
3、安装ipykernel
我使用jupyter Notebook完成的。所以要把conda的虚拟环境在jupyter Notebook中打开。
pip install ipykernel
4、将虚拟环境加入内核
python -m ipykernel install --name=env_name(想要保存的环境名)
5、在jupyter中导入
打开jupyter notebook,点击新建,会出现入下图所示的内容:
6、代码
from imagededup.methods import PHash
phasher = PHash() ##导入方法
这里是导入检测重复图片的方法,根据官方教程还有如下图所示其他方法
duplicates_list = phasher.find_duplicates_to_remove(
image_dir='D:\数据集\VOC2020\JPEGImages')
find_duplicates_to_remove()是官方函数。image_dir:数据集的文件夹名
Returns a list of files in the image directory that are considered as duplicates. Does NOT remove the said files.
函数返回一个列表,里面是重复图片的文件名,这个函数不会自己删除文件。如下图所示
import os
import shutil
path = "D:\数据集\VOC2020\images"
for file_name in duplicates_list:
file_path = os.path.join(path, file_name)
# 运用os.path.join(path, file_name)将文件路径与文件名拼接,形成一个新路径
os.remove(file_path)
print('finish!!')
删除path文件夹里面和duplicates_list里文件名相同的文件。
至此,完成。
开源地址:
https://github.com/idealo/imagededup
API教程:
https://idealo.github.io/imagededup/