以图搜图的原理:将图片转成向量,而后通过欧式间隔等等比拟向量的间隔,获取两个图片的类似度
那最要害的一步:『将图片转成向量』,如何应用 python 实现呢?
能够应用 image2vector
装置形式很简略
pip install image2vector
用起来就更简略了,你不必关怀任何深度学习的货色,只有指定 image 就能无脑生成图片的向量
from pathlib import Path
from typing import List
from iv import ResNet, l2
# Initialize a residual neural network
resnet: ResNet = ResNet(weight_file='weight/gl18-tl-resnet50-gem-w-83fdc30.pth')
# Generate a vector of specified images
# The generated vector is a List[float] data structure,
# the length of the list is 512, which means the vector is of 512 dimensions
vector_1: List[float] = resnet.gen_vector('example-1.jpg')
vector_2: List[float] = resnet.gen_vector('example-2.jpg')
# Compare the Euclidean distance of two vectors
distance: float = l2(vector_1, vector_2)
print('Euclidean Distance is', distance)
参考:
- 如何应用 resnet 生成图片向量?
- image2vector github
- pypi image2vector