试图与欧几里得距离找到最接近的距离

发布于 2025-01-31 02:12:06 字数 1364 浏览 1 评论 0原文

import imagehash
from PIL import Image
import glob
import numpy as np
import csv
from scipy.spatial import distance

image_list = []
list_rows = []

for filename in glob.glob('/home/folder1/*.jpg'):
    im = Image.open(filename)
    image_list.append(im)
    img_hash = imagehash.average_hash(im)
    print(img_hash)
    list_rows.append([img_hash])

np.savetxt("numpy_test.csv", list_rows, delimiter=",", fmt='% s')

with open('numpy_test.csv', newline='') as f:
    reader = csv.reader(f)
    data = list(reader)

print(data)

for hashes in data:
    print(hashes)

mouni = []
mouni_rows = []
for file in glob.glob('/home/folder2/test.jpg'):
    img = Image.open(file)
    mouni.append(img)
    img_hash = imagehash.average_hash(img)
    print(img_hash)
    mouni_rows.append([img_hash])

np.savetxt("numpy_test_mouni.csv", mouni_rows, delimiter=",", fmt='% s')

with open('numpy_test_mouni.csv', newline='') as f:
    reader = csv.reader(f)
    mouni = list(reader)

print(mouni)

for k in mouni:
    print(k)

for i in hashes:
    for y in k:
        dist = np.linalg.norm(i - y)
        print(dist)

-folder1包含一些图像,从其中imagehashes fulacetd并保存到.csv file

-folder2仅包含1个图像,并将imageHash提取并保存到另一个.csv文件中

- 目标是将1 imagehash从1 image中进行比较。使用欧几里得距离的散列,以找到最接近的哈希距离。但是错误是:typeError:未支撑的操作数类型 - :'str'和'str'

真正赞赏的任何建议。先感谢您

import imagehash
from PIL import Image
import glob
import numpy as np
import csv
from scipy.spatial import distance

image_list = []
list_rows = []

for filename in glob.glob('/home/folder1/*.jpg'):
    im = Image.open(filename)
    image_list.append(im)
    img_hash = imagehash.average_hash(im)
    print(img_hash)
    list_rows.append([img_hash])

np.savetxt("numpy_test.csv", list_rows, delimiter=",", fmt='% s')

with open('numpy_test.csv', newline='') as f:
    reader = csv.reader(f)
    data = list(reader)

print(data)

for hashes in data:
    print(hashes)

mouni = []
mouni_rows = []
for file in glob.glob('/home/folder2/test.jpg'):
    img = Image.open(file)
    mouni.append(img)
    img_hash = imagehash.average_hash(img)
    print(img_hash)
    mouni_rows.append([img_hash])

np.savetxt("numpy_test_mouni.csv", mouni_rows, delimiter=",", fmt='% s')

with open('numpy_test_mouni.csv', newline='') as f:
    reader = csv.reader(f)
    mouni = list(reader)

print(mouni)

for k in mouni:
    print(k)

for i in hashes:
    for y in k:
        dist = np.linalg.norm(i - y)
        print(dist)

-Folder1 contains some images from which imagehashes are extracetd and saved to a .csv file

-Folder2 contains only 1 image and imagehash is extracted and saved to an other .csv file

-The goal is to compare the imagehash from the 1 image with the rest of the hashes using the euclidean distance in order to find the closest ones. But the error is: TypeError: unsupported operand type(s) for -: 'str' and 'str'

Any advice really appreciated. Thank you in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文