python - 在数千个文件夹中查找图像相似性

标签 python

我已经拼凑/编写了一些代码(感谢 stackoverflow 用户!),使用 imagehash 检查图像的相似性,但现在我在检查数千个图像(大约 16,000 个)时遇到问题。我是否可以通过代码(或完全不同的路线)改进任何内容,以更准确地找到匹配项和/或减少所需的时间?谢谢!

我首先将创建的列表更改为 itertools 组合,因此它仅比较图像的唯一组合。

new_loc = os.chdir(r'''myimagelocation''')
dirloc = os.listdir(r'''myimagelocation''')

duplicates = []
dup = []

for f1, f2 in itertools.combinations(dirloc,2):
    #Honestly not sure which hash method to use, so I went with dhash.
    dhash1 = imagehash.dhash(Image.open(f1))
    dhash2 = imagehash.dhash(Image.open(f2))
    hashdif = dhash1 - dhash2


    if hashdif < 5:  #May change the 5 to find more accurate matches
            print("images are similar due to dhash", "image1", f1, "image2", f2)
            duplicates.append(f1)
            dup.append(f2)

    #Setting up a CSV file with the similar images to review before deleting
    with open("duplicates.csv", "w") as myfile:
        wr = csv.writer(myfile)
        wr.writerows(zip(duplicates, dup)) 

目前,此代码可能需要几天时间才能处理文件夹中的图像数量。如果可能的话,我希望将其减少到几个小时。

最佳答案

尝试一下,不要在比较时对每个图像进行散列(127,992,000 个散列),而是提前散列并比较散列,因为这些散列不会改变(16,000 个散列)。

new_loc = os.chdir(r'''myimagelocation''')
dirloc = os.listdir(r'''myimagelocation''')

duplicates = []
dup = []

hashes = []

for file in dirloc:
    hashes.append((file, imagehash.dhash(Image.open(file))))

for pair1, pair2 in itertools.combinations(hashes,2):
    f1, dhash1 = pair1
    f2, dhash2 = pair2
    #Honestly not sure which hash method to use, so I went with dhash.
    hashdif = dhash1 - dhash2


    if hashdif < 5:  #May change the 5 to find more accurate matches
            print("images are similar due to dhash", "image1", f1, "image2", f2)
            duplicates.append(f1)
            dup.append(f2)

#Setting up a CSV file with the similar images to review before deleting
with open("duplicates.csv", "w") as myfile: # also move this out of the loop so you arent rewriting the file every time
    wr = csv.writer(myfile)
    wr.writerows(zip(duplicates, dup)) 

关于python - 在数千个文件夹中查找图像相似性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57100668/

相关文章:

python - 使用 Python 的日志记录模块记录错误的问题

python - 将文本渲染到 kivy Canvas

python - numpy中的不规则切片

python - 如何在 pkl 文件中编写可在 python 脚本中使用的 write() 方法?

python - 在 Python 中从单元测试中输出数据

python - 将字符串拆分为字母和标点符号,正则表达式除外

python替代正则表达式搜索返回无的元组

python - 使用列表作为名称和列表中元素的字符串

python - 导入 pbnt (贝叶斯网络模块)并获取 AttributeError

python - 在 Python OOP 中调用类变量时应该使用 self 还是类名?