import cv2
import torch
import open_clip
from skimage import metrics
from image_similarity_measures.evaluate import evaluation
from sentence_transformers import util
from PIL import Image
def histogram_based(image1, image2):
hist_img1 = cv2.calcHist([image1], [0, 1, 2], None, [256, 256, 256], [0, 256, 0, 256, 0, 256])
hist_img1[255, 255, 255] = 0 # ignore all white pixels
cv2.normalize(hist_img1, hist_img1, alpha=0, beta=1, norm_type=cv2.NORM_MINMAX)
hist_img2 = cv2.calcHist([image2], [0, 1, 2], None, [256, 256, 256], [0, 256, 0, 256, 0, 256])
hist_img2[255, 255, 255] = 0 # ignore all white pixels
cv2.normalize(hist_img2, hist_img2, alpha=0, beta=1, norm_type=cv2.NORM_MINMAX)
# Find the metric value
metric_val = cv2.compareHist(hist_img1, hist_img2, cv2.HISTCMP_CORREL)
return round(metric_val, 2)
def structural_similarity_index(image1, image2):
image2 = cv2.resize(image2, (image1.shape[1], image1.shape[0]), interpolation=cv2.INTER_AREA)
# Convert images to grayscale
image1_gray = cv2.cvtColor(image1, cv2.COLOR_BGR2GRAY)
image2_gray = cv2.cvtColor(image2, cv2.COLOR_BGR2GRAY)
# Calculate SSIM
ssim_score = metrics.structural_similarity(image1_gray, image2_gray, full=True)
return round(ssim_score[0], 2)
def clip_cnn(image1, image2):
device = "cuda" if torch.cuda.is_available() else "cpu"
model, _, preprocess = open_clip.create_model_and_transforms('ViT-B-16-plus-240', pretrained="laion400m_e32")
model.to(device)
def image_encoder(img):
img1 = Image.fromarray(img).convert('RGB')
img1 = preprocess(img1).unsqueeze(0).to(device)
img1 = model.encode_image(img1)
return img1
def generate_score(test_img, data_img):
img1 = image_encoder(test_img)
img2 = image_encoder(data_img)
cos_scores = util.pytorch_cos_sim(img1, img2)
score = round(float(cos_scores[0][0]) * 100, 2)
return score
return round(generate_score(image1, image2), 2)
COMPARATORS = [
histogram_based,
structural_similarity_index,
clip_cnn
]
print("Different tweet. Lower is better")
image_path_1 = 'data/3/photo_2023-09-22_07-46-25.jpg'
image_path_2 = 'data/3/photo_2023-09-22_13-49-42.jpg'
image1 = cv2.imread(image_path_1)
image2 = cv2.imread(image_path_2)
for comparator in COMPARATORS:
print(comparator(image1, image2))
print("Similar images but with padding. 1 is for ideal")
image1 = cv2.imread('data/2/photo_2023-09-26_22-50-15.jpg')
image2 = cv2.imread('data/2/photo_2023-09-26_22-54-42.jpg')
for comparator in COMPARATORS:
print(comparator(image1, image2))
print("A different photo, but with slightly different text. Lower is better")
image1 = cv2.imread('data/1/photo_2023-10-11_10-02-11.jpg')
image2 = cv2.imread('data/1/photo_2023-10-11_10-02-14.jpg')
for comparator in COMPARATORS:
print(comparator(image1, image2))
Я для своей компании сделал бот, который считает статистику каждую неделю по лайкам, сообщениям, и ищет мем недели.
Использовал telegram core api, обычный bot api не предоставляет информацию по лайкам
О, у вас тоже есть мем-чат, где вы проводите конкурсы?
Можно к вам в мем-чат?
Ахаха, ну если устроитесь к нам на работу, то велком)