Update app.py
Browse files
app.py
CHANGED
|
@@ -1,71 +1,37 @@
|
|
| 1 |
-
from PIL import Image
|
| 2 |
-
from difflib import SequenceMatcher
|
| 3 |
-
from skimage.metrics import structural_similarity as ssim
|
| 4 |
import gradio as gr
|
| 5 |
-
import
|
|
|
|
|
|
|
|
|
|
| 6 |
import io
|
| 7 |
|
| 8 |
-
|
| 9 |
# Function to calculate SSIM between two images
|
| 10 |
def calculate_ssim(img1, img2):
|
| 11 |
-
img1_gray = Image.open(img1).convert("L")
|
| 12 |
-
img2_gray = Image.open(img2).convert("L")
|
| 13 |
-
return ssim(img1_gray, img2_gray)
|
| 14 |
-
|
| 15 |
|
| 16 |
-
# Function to
|
| 17 |
-
def extract_text(image):
|
| 18 |
-
image = Image.open(image)
|
| 19 |
-
text = pytesseract.image_to_string(image).lower()
|
| 20 |
-
return text
|
| 21 |
-
|
| 22 |
-
# Function to compare trademarks based on text similarity
|
| 23 |
-
def compare_text(trademark1, trademark2):
|
| 24 |
-
text1 = extract_text(trademark1)
|
| 25 |
-
text2 = extract_text(trademark2)
|
| 26 |
-
similarity_ratio = SequenceMatcher(None, text1, text2).ratio()
|
| 27 |
-
return similarity_ratio
|
| 28 |
-
|
| 29 |
-
# Function to compare trademarks based on color similarity
|
| 30 |
-
def compare_colors(trademark1, trademark2):
|
| 31 |
-
trademark1 = trademark1.convert("RGB")
|
| 32 |
-
trademark2 = trademark2.convert("RGB")
|
| 33 |
-
colors1 = trademark1.getcolors(trademark1.size[0] * trademark1.size[1])
|
| 34 |
-
colors2 = trademark2.getcolors(trademark2.size[0] * trademark2.size[1])
|
| 35 |
-
color_vector1 = sum([(count * np.array(color)) for count, color in colors1]) / (trademark1.size[0] * trademark1.size[1])
|
| 36 |
-
color_vector2 = sum([(count * np.array(color)) for count, color in colors2]) / (trademark2.size[0] * trademark2.size[1])
|
| 37 |
-
color_similarity = 1 - np.linalg.norm(color_vector1 - color_vector2)
|
| 38 |
-
return color_similarity
|
| 39 |
-
|
| 40 |
-
# Function to compare trademarks based on multiple aspects
|
| 41 |
def compare_trademarks(trademark1, trademark2):
|
| 42 |
ssim_score = calculate_ssim(trademark1, trademark2)
|
| 43 |
-
|
| 44 |
-
color_similarity = compare_colors(trademark1, trademark2)
|
| 45 |
-
|
| 46 |
-
# Adjust weights based on the importance of each aspect
|
| 47 |
-
ssim_weight = 0.6
|
| 48 |
-
text_weight = 0.2
|
| 49 |
-
color_weight = 0.2
|
| 50 |
-
|
| 51 |
-
overall_similarity = (ssim_weight * ssim_score) + (text_weight * text_similarity) + (color_weight * color_similarity)
|
| 52 |
-
return overall_similarity
|
| 53 |
|
| 54 |
-
# Function to
|
| 55 |
def prevent_trademark_conflict(trademark1, trademark2):
|
| 56 |
similarity_score = compare_trademarks(trademark1, trademark2)
|
| 57 |
-
|
| 58 |
-
return result
|
| 59 |
|
|
|
|
| 60 |
iface = gr.Interface(
|
| 61 |
fn=prevent_trademark_conflict,
|
| 62 |
inputs=[
|
| 63 |
-
gr.inputs.
|
| 64 |
-
gr.inputs.
|
| 65 |
],
|
| 66 |
outputs="text",
|
| 67 |
-
title="Trademark
|
| 68 |
-
description="
|
| 69 |
)
|
| 70 |
|
|
|
|
| 71 |
iface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import cv2
|
| 3 |
+
import numpy as np
|
| 4 |
+
from skimage.metrics import structural_similarity as ssim
|
| 5 |
+
from PIL import Image
|
| 6 |
import io
|
| 7 |
|
|
|
|
| 8 |
# Function to calculate SSIM between two images
|
| 9 |
def calculate_ssim(img1, img2):
|
| 10 |
+
img1_gray = Image.open(io.BytesIO(img1)).convert("L")
|
| 11 |
+
img2_gray = Image.open(io.BytesIO(img2)).convert("L")
|
| 12 |
+
return ssim(np.array(img1_gray), np.array(img2_gray))
|
|
|
|
| 13 |
|
| 14 |
+
# Function to compare two trademarks
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
def compare_trademarks(trademark1, trademark2):
|
| 16 |
ssim_score = calculate_ssim(trademark1, trademark2)
|
| 17 |
+
return ssim_score
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
+
# Function to handle the Gradio interface
|
| 20 |
def prevent_trademark_conflict(trademark1, trademark2):
|
| 21 |
similarity_score = compare_trademarks(trademark1, trademark2)
|
| 22 |
+
return similarity_score
|
|
|
|
| 23 |
|
| 24 |
+
# Prepare Gradio interface
|
| 25 |
iface = gr.Interface(
|
| 26 |
fn=prevent_trademark_conflict,
|
| 27 |
inputs=[
|
| 28 |
+
gr.inputs.Image(type="file", label="Trademark Image 1"),
|
| 29 |
+
gr.inputs.Image(type="file", label="Trademark Image 2")
|
| 30 |
],
|
| 31 |
outputs="text",
|
| 32 |
+
title="Trademark Similarity",
|
| 33 |
+
description="Compare two trademarks to check for similarity."
|
| 34 |
)
|
| 35 |
|
| 36 |
+
# Launch the interface
|
| 37 |
iface.launch()
|