Update app.py
Browse files
app.py
CHANGED
|
@@ -2,65 +2,78 @@ import gradio as gr
|
|
| 2 |
import cv2
|
| 3 |
import numpy as np
|
| 4 |
from skimage.metrics import structural_similarity as ssim
|
| 5 |
-
from
|
| 6 |
-
import
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
-
# Function to calculate SSIM between two images
|
| 10 |
def calculate_ssim(img1, img2):
|
| 11 |
-
img1_gray =
|
| 12 |
-
img2_gray =
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
def compare_trademarks(trademark1, trademark2):
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
text_similarity = calculate_text_similarity(text1, text2)
|
| 34 |
-
color_difference = calculate_color_difference(trademark1, trademark2)
|
| 35 |
-
return ssim_score, text_similarity, color_difference
|
| 36 |
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
text2 = text2.lower()
|
| 43 |
-
intersection = len(set(text1.split()) & set(text2.split()))
|
| 44 |
-
union = len(set(text1.split()) | set(text2.split()))
|
| 45 |
-
return intersection / union
|
| 46 |
-
|
| 47 |
-
# Function to handle the Gradio interface
|
| 48 |
def prevent_trademark_conflict(trademark1, trademark2):
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
|
| 53 |
-
#
|
| 54 |
-
|
| 55 |
fn=prevent_trademark_conflict,
|
| 56 |
inputs=[
|
| 57 |
gr.inputs.Image(type="pil", label="Trademark Image 1"),
|
| 58 |
-
gr.inputs.Image(type="pil", label="Trademark Image 2")
|
| 59 |
],
|
| 60 |
outputs="text",
|
| 61 |
-
title="Trademark
|
| 62 |
-
description="Compare two trademarks
|
| 63 |
)
|
| 64 |
|
| 65 |
# Launch the interface
|
| 66 |
-
|
|
|
|
| 2 |
import cv2
|
| 3 |
import numpy as np
|
| 4 |
from skimage.metrics import structural_similarity as ssim
|
| 5 |
+
from transformers import BertForSequenceClassification, BertTokenizer
|
| 6 |
+
import torch
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
# Load pre-trained model and tokenizer
|
| 10 |
+
model = BertForSequenceClassification.from_pretrained("bert-base-uncased")
|
| 11 |
+
tokenizer = BertTokenizer.from_pretrained("bert-base-uncased")
|
| 12 |
+
|
| 13 |
|
|
|
|
| 14 |
def calculate_ssim(img1, img2):
|
| 15 |
+
img1_gray = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)
|
| 16 |
+
img2_gray = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY)
|
| 17 |
+
return ssim(img1_gray, img2_gray)
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
def calculate_text_similarity(text1, text2):
|
| 21 |
+
encoded_text1 = tokenizer(text1, truncation=True, padding=True, return_tensors="pt")
|
| 22 |
+
encoded_text2 = tokenizer(text2, truncation=True, padding=True, return_tensors="pt")
|
| 23 |
+
|
| 24 |
+
with torch.no_grad():
|
| 25 |
+
outputs_text1 = model(**encoded_text1)
|
| 26 |
+
outputs_text2 = model(**encoded_text2)
|
| 27 |
+
|
| 28 |
+
embeddings_text1 = outputs_text1.last_hidden_state.squeeze(0)
|
| 29 |
+
embeddings_text2 = outputs_text2.last_hidden_state.squeeze(0)
|
| 30 |
+
|
| 31 |
+
text_similarity = ssim(embeddings_text1.numpy(), embeddings_text2.numpy())
|
| 32 |
+
|
| 33 |
+
return text_similarity
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
def calculate_color_similarity(img1, img2):
|
| 37 |
+
img1_lab = cv2.cvtColor(img1, cv2.COLOR_BGR2Lab)
|
| 38 |
+
img2_lab = cv2.cvtColor(img2, cv2.COLOR_BGR2Lab)
|
| 39 |
+
|
| 40 |
+
color_similarity = ssim(img1_lab, img2_lab, multichannel=True)
|
| 41 |
+
|
| 42 |
+
return color_similarity
|
| 43 |
+
|
| 44 |
+
|
| 45 |
def compare_trademarks(trademark1, trademark2):
|
| 46 |
+
img1 = cv2.imread(trademark1)
|
| 47 |
+
img2 = cv2.imread(trademark2)
|
| 48 |
+
|
| 49 |
+
ssim_score = calculate_ssim(img1, img2)
|
| 50 |
+
|
| 51 |
+
text1 = "Trademark text 1"
|
| 52 |
+
text2 = "Trademark text 2"
|
| 53 |
+
|
| 54 |
text_similarity = calculate_text_similarity(text1, text2)
|
|
|
|
|
|
|
| 55 |
|
| 56 |
+
color_similarity = calculate_color_similarity(img1, img2)
|
| 57 |
+
|
| 58 |
+
return ssim_score, text_similarity, color_similarity
|
| 59 |
+
|
| 60 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
def prevent_trademark_conflict(trademark1, trademark2):
|
| 62 |
+
similarity_scores = compare_trademarks(trademark1, trademark2)
|
| 63 |
+
return similarity_scores
|
| 64 |
+
|
| 65 |
|
| 66 |
+
# Interface
|
| 67 |
+
trademark_comparison_interface = gr.Interface(
|
| 68 |
fn=prevent_trademark_conflict,
|
| 69 |
inputs=[
|
| 70 |
gr.inputs.Image(type="pil", label="Trademark Image 1"),
|
| 71 |
+
gr.inputs.Image(type="pil", label="Trademark Image 2"),
|
| 72 |
],
|
| 73 |
outputs="text",
|
| 74 |
+
title="Trademark Comparison",
|
| 75 |
+
description="Compare two trademarks based on SSIM, text similarity, and color similarity.",
|
| 76 |
)
|
| 77 |
|
| 78 |
# Launch the interface
|
| 79 |
+
trademark_comparison_interface.launch()
|