Я создаю приложение, которое может находить дубликаты изображений и близкие к ним дубликаты изображений из вашей библиотеки изображений.
Для моей текущей реализации я могу взять 2 изображения, извлечь из них встраивания (с помощью CLIP библиотеки), сравните ее косинусное сходство (для точных дубликатов) и евклидово расстояние (для сжатых дубликатов, таких как изображения, загруженные из чатов, или скриншоты изображения).
Любые рекомендации по следующим шагам я могу take ?Моя текущая реализация выглядит так:
main.py
from PIL import Image
import torch
import numpy as np
import cv2 as cv
import clip
from sklearn.cluster import KMeans
device = "cpu"
image_path = "assets/photos/aayush.jpeg"
image_path2 = "assets/photos/aayushResized.png"
# Load CLIP model and preprocess function
model, preprocess = clip.load("ViT-L/14", device, jit=False)
# Preprocess the images and add batch dimension
image = preprocess(Image.open(image_path)).unsqueeze(0).to(device)
image2 = preprocess(Image.open(image_path2)).unsqueeze(0).to(device)
print("The data type of images after being processed is: ", type(image))
def calculate_distance(embedding1, embedding2):
""" Calculates Euclidean Distance between 2 images """
dist = torch.nn.functional.pairwise_distance(embedding1, embedding2)
return (dist)
def calculate_cosine_similarity(embedding1, embedding2):
""" Calculates Cosine Similarity between 2 image embeddings """
return torch.nn.functional.cosine_similarity(embedding1, embedding2)
def resize_with_aspect_ratio(raw_image_path, new_width):
# Read the original image
original_image = cv.imread(raw_image_path)
# Calculate the aspect ratio
aspect_ratio = original_image.shape[1] / original_image.shape[0]
# Determine the new height based on the desired width
calculated_height = int(new_width / aspect_ratio)
# Resize the image
resized_image = cv.resize(original_image, (new_width, calculated_height))
print("Newly Resized images shape: ", resized_image.shape)
cv.imshow("Screen", resized_image)
cv.imwrite("aayushResized.png", resized_image)
cv.waitKey(0)
cv.destroyAllWindows()
return resized_image
def normalize(image):
# Compute the standard deviation for each channel
channel_stds = np.std(image, axis=(0, 1))
print(channel_stds)
# Normalize by dividing image by the channel standard deviations
normalized_image = image / channel_stds
return (normalized_image)
def clutster(image):
kmeans = KMeans(n_clusters=2, random_state=0, n_init="auto").fit(X)
# Get image embeddings from the model
with torch.no_grad():
emb1 = model.encode_image(image)
emb2 = model.encode_image(image2)
print("The data type of embeddings are: ", type(emb1), "And ", type(emb2))
# print(emb1, "\n ", emb2)
# Calculate cosine similarity between embeddings
similarity = calculate_cosine_similarity(emb1, emb2)
distance = calculate_distance(emb1, emb2)
print(f"Cosine Similarity: {similarity.item()} {type(similarity)}")
print(f"Euclidean Distance: {distance.item()} {type(similarity)}")
cluster.py
# using clustering algo[k means] to cluster the tensors together:
import os
import torch
import clip
from PIL import Image
import numpy as np
from sklearn.cluster import KMeans
device = "cpu"
model, preprocess = clip.load("ViT-L/14", device, jit=False)
# labels = ["a", "a", "a", "b", "b", "b", "c", "c", "d"]
name_list = []
np_array = []
# looping through image folder
for photos in os.listdir("assets/photos"):
image_path = os.path.join("assets/photos", photos)
# Pre processing images and getting their tensor
image = preprocess(Image.open(image_path)).unsqueeze(0).to(device)
name_list.append(photos)
# Flatten the higher dimension of tensors to like 2dim
numpyImage = np.array(image).flatten()
np_array.append(numpyImage)
k = 3
kmeans = KMeans(n_clusters=k, random_state=42)
kmeans.fit(np_array)
# getting int like labels(auto generated by KMeans)
cluster_labels = kmeans.labels_
image_cluster_map = {}
# Looping through the images and giving them labels
for i, labels in enumerate(cluster_labels):
# print(f"Image {i} belongs to cluster {labels}")
image_cluster_map[f'image_{name_list}'] = labels
# viewing our final cluster
for i, (key, value) in enumerate(image_cluster_map.items()):
print(key, " labelled as: ", value)
Подробнее здесь: https://stackoverflow.com/questions/790 ... mage-dupli
Рекомендация о подходе, который я могу использовать для создания приложения, которое находит дубликаты изображений и бли ⇐ Python
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение
-
-
Странная рекомендация PEP8 по сравнению логических значений с True или False
Anonymous » » в форуме Python - 0 Ответы
- 21 Просмотры
-
Последнее сообщение Anonymous
-
-
-
Рекомендация по инструменту для тестирования и сравнения ответов API [закрыто]
Anonymous » » в форуме Python - 0 Ответы
- 16 Просмотры
-
Последнее сообщение Anonymous
-