2022-03-08 19:51:28 +08:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
"""
|
|
|
|
@author:XuMing(xuming624@qq.com)
|
|
|
|
@description:
|
|
|
|
"""
|
|
|
|
import glob
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import unittest
|
2022-03-11 21:36:30 +08:00
|
|
|
from PIL import Image
|
2022-03-08 19:51:28 +08:00
|
|
|
|
|
|
|
sys.path.append('..')
|
|
|
|
|
|
|
|
from similarities.imagesim import ClipSimilarity, ImageHashSimilarity, SiftSimilarity
|
|
|
|
|
|
|
|
pwd_path = os.path.abspath(os.path.dirname(__file__))
|
|
|
|
|
2022-03-11 21:36:30 +08:00
|
|
|
img1 = Image.open(os.path.join(pwd_path, '../examples/data/image1.png'))
|
|
|
|
img2 = Image.open(os.path.join(pwd_path, '../examples/data/image8-like-image1.png'))
|
2022-03-08 19:51:28 +08:00
|
|
|
image_dir = os.path.join(pwd_path, '../examples/data/')
|
2022-03-11 21:36:30 +08:00
|
|
|
corpus_imgs = [Image.open(i) for i in glob.glob(os.path.join(image_dir, '*.png'))]
|
2022-03-08 19:51:28 +08:00
|
|
|
|
|
|
|
|
|
|
|
class ImageSimCase(unittest.TestCase):
|
2022-03-11 21:36:30 +08:00
|
|
|
|
2022-03-08 19:51:28 +08:00
|
|
|
def test_clip(self):
|
2022-03-11 21:36:30 +08:00
|
|
|
m = ClipSimilarity()
|
2022-03-08 19:51:28 +08:00
|
|
|
print(m)
|
2022-03-11 21:36:30 +08:00
|
|
|
s = m.similarity(img1, img2)
|
2022-03-09 20:25:40 +08:00
|
|
|
print(s)
|
|
|
|
self.assertTrue(s > 0.5)
|
2022-03-11 21:36:30 +08:00
|
|
|
r = m.most_similar(img1)
|
2022-03-09 20:25:40 +08:00
|
|
|
print(r)
|
|
|
|
self.assertTrue(not r[0])
|
2022-03-11 21:36:30 +08:00
|
|
|
m.add_corpus(corpus_imgs)
|
|
|
|
|
|
|
|
r = m.most_similar(img1)
|
|
|
|
print(r)
|
|
|
|
self.assertTrue(len(r) > 0)
|
2022-03-08 19:51:28 +08:00
|
|
|
|
2022-03-11 21:36:30 +08:00
|
|
|
def test_clip_dict(self):
|
|
|
|
m = ClipSimilarity()
|
|
|
|
print(m)
|
|
|
|
corpus_dict = {i.filename: i for i in corpus_imgs}
|
|
|
|
queries = {i.filename: i for i in corpus_imgs[:3]}
|
|
|
|
m.add_corpus(corpus_dict)
|
|
|
|
r = m.most_similar(queries)
|
2022-03-08 19:51:28 +08:00
|
|
|
print(r)
|
|
|
|
self.assertTrue(len(r) > 0)
|
|
|
|
|
|
|
|
def test_sift(self):
|
|
|
|
m = SiftSimilarity(corpus=glob.glob(f'{image_dir}/*.jpg'))
|
|
|
|
print(m)
|
2022-03-11 21:36:30 +08:00
|
|
|
print(m.similarity(img1, img2))
|
|
|
|
r = m.most_similar(img1)
|
2022-03-09 20:25:40 +08:00
|
|
|
print(r)
|
|
|
|
self.assertTrue(not r[0])
|
2022-03-11 21:36:30 +08:00
|
|
|
m.add_corpus(corpus_imgs)
|
|
|
|
m.add_corpus(corpus_imgs)
|
|
|
|
r = m.most_similar(img1)
|
2022-03-08 19:51:28 +08:00
|
|
|
print(r)
|
|
|
|
self.assertTrue(len(r) > 0)
|
|
|
|
|
|
|
|
def test_phash(self):
|
2022-03-11 21:36:30 +08:00
|
|
|
m = ImageHashSimilarity(hash_function='phash')
|
2022-03-08 19:51:28 +08:00
|
|
|
print(m)
|
2022-03-11 21:36:30 +08:00
|
|
|
print(m.similarity(img1, img2))
|
|
|
|
m.most_similar(img1)
|
|
|
|
m.add_corpus(corpus_imgs)
|
|
|
|
r = m.most_similar(img1)
|
2022-03-08 19:51:28 +08:00
|
|
|
print(r)
|
|
|
|
|
2022-03-11 21:36:30 +08:00
|
|
|
m = ImageHashSimilarity(hash_function='average_hash')
|
2022-03-08 19:51:28 +08:00
|
|
|
print(m)
|
2022-03-11 21:36:30 +08:00
|
|
|
print(m.similarity(img1, img2))
|
|
|
|
m.most_similar(img1)
|
|
|
|
m.add_corpus(corpus_imgs)
|
|
|
|
m.add_corpus(corpus_imgs)
|
|
|
|
r = m.most_similar(img1)
|
2022-03-08 19:51:28 +08:00
|
|
|
print(r)
|
|
|
|
self.assertTrue(len(r) > 0)
|
|
|
|
|
|
|
|
def test_hamming_distance(self):
|
|
|
|
m = ImageHashSimilarity(hash_function='phash', hash_size=128)
|
2022-03-11 21:36:30 +08:00
|
|
|
s = m.similarity(img1, img2)
|
2022-03-08 19:51:28 +08:00
|
|
|
print(s)
|
2022-03-09 20:25:40 +08:00
|
|
|
self.assertTrue(s[0] > 0)
|
2022-03-08 19:51:28 +08:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|