Update app.py
Browse files
app.py
CHANGED
|
@@ -3,26 +3,52 @@ import cv2
|
|
| 3 |
import numpy as np
|
| 4 |
from skimage.metrics import structural_similarity as ssim
|
| 5 |
from PIL import Image
|
| 6 |
-
import
|
|
|
|
| 7 |
|
| 8 |
# Function to calculate SSIM between two images
|
| 9 |
def calculate_ssim(img1, img2):
|
| 10 |
img1_gray = img1.convert("L")
|
| 11 |
img2_gray = img2.convert("L")
|
| 12 |
-
# Resize images to a common size
|
| 13 |
img1_resized = img1_gray.resize((256, 256))
|
| 14 |
img2_resized = img2_gray.resize((256, 256))
|
| 15 |
return ssim(np.array(img1_resized), np.array(img2_resized))
|
| 16 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
# Function to compare two trademarks
|
| 18 |
def compare_trademarks(trademark1, trademark2):
|
| 19 |
ssim_score = calculate_ssim(trademark1, trademark2)
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
# Function to handle the Gradio interface
|
| 23 |
def prevent_trademark_conflict(trademark1, trademark2):
|
| 24 |
-
|
| 25 |
-
|
|
|
|
| 26 |
|
| 27 |
# Prepare Gradio interface
|
| 28 |
iface = gr.Interface(
|
|
|
|
| 3 |
import numpy as np
|
| 4 |
from skimage.metrics import structural_similarity as ssim
|
| 5 |
from PIL import Image
|
| 6 |
+
import pytesseract
|
| 7 |
+
import matplotlib.pyplot as plt
|
| 8 |
|
| 9 |
# Function to calculate SSIM between two images
|
| 10 |
def calculate_ssim(img1, img2):
|
| 11 |
img1_gray = img1.convert("L")
|
| 12 |
img2_gray = img2.convert("L")
|
|
|
|
| 13 |
img1_resized = img1_gray.resize((256, 256))
|
| 14 |
img2_resized = img2_gray.resize((256, 256))
|
| 15 |
return ssim(np.array(img1_resized), np.array(img2_resized))
|
| 16 |
|
| 17 |
+
# Function to extract text from an image
|
| 18 |
+
def extract_text(image):
|
| 19 |
+
text = pytesseract.image_to_string(image)
|
| 20 |
+
return text
|
| 21 |
+
|
| 22 |
+
# Function to calculate color difference between two images
|
| 23 |
+
def calculate_color_difference(img1, img2):
|
| 24 |
+
hist1 = cv2.calcHist([np.array(img1)], [0, 1, 2], None, [8, 8, 8], [0, 256, 0, 256, 0, 256])
|
| 25 |
+
hist2 = cv2.calcHist([np.array(img2)], [0, 1, 2], None, [8, 8, 8], [0, 256, 0, 256, 0, 256])
|
| 26 |
+
return cv2.compareHist(hist1, hist2, cv2.HISTCMP_CHISQR)
|
| 27 |
+
|
| 28 |
# Function to compare two trademarks
|
| 29 |
def compare_trademarks(trademark1, trademark2):
|
| 30 |
ssim_score = calculate_ssim(trademark1, trademark2)
|
| 31 |
+
text1 = extract_text(trademark1)
|
| 32 |
+
text2 = extract_text(trademark2)
|
| 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 |
+
# Function to calculate text similarity ratio
|
| 38 |
+
def calculate_text_similarity(text1, text2):
|
| 39 |
+
if text1 == "" or text2 == "":
|
| 40 |
+
return 0.0
|
| 41 |
+
text1 = text1.lower()
|
| 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 |
+
ssim_score, text_similarity, color_difference = compare_trademarks(trademark1, trademark2)
|
| 50 |
+
result = f"SSIM Score: {ssim_score:.2f}\nText Similarity: {text_similarity:.2f}\nColor Difference: {color_difference:.2f}"
|
| 51 |
+
return result
|
| 52 |
|
| 53 |
# Prepare Gradio interface
|
| 54 |
iface = gr.Interface(
|