我已经拼凑/编写了一些代码(感谢 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/