Update app.py
Browse files
app.py
CHANGED
@@ -1,19 +1,25 @@
|
|
1 |
-
import numpy as np
|
2 |
-
from skimage.metrics import structural_similarity as ssim
|
3 |
from PIL import Image
|
4 |
-
import gradio as gr
|
5 |
from difflib import SequenceMatcher
|
|
|
|
|
|
|
6 |
|
7 |
# Function to calculate SSIM between two images
|
8 |
def calculate_ssim(img1, img2):
|
9 |
img1_gray = img1.convert("L")
|
10 |
img2_gray = img2.convert("L")
|
11 |
-
return ssim(
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
|
13 |
# Function to compare trademarks based on text similarity
|
14 |
def compare_text(trademark1, trademark2):
|
15 |
-
text1 =
|
16 |
-
text2 =
|
17 |
similarity_ratio = SequenceMatcher(None, text1, text2).ratio()
|
18 |
return similarity_ratio
|
19 |
|
@@ -23,14 +29,8 @@ def compare_colors(trademark1, trademark2):
|
|
23 |
trademark2 = trademark2.convert("RGB")
|
24 |
colors1 = trademark1.getcolors(trademark1.size[0] * trademark1.size[1])
|
25 |
colors2 = trademark2.getcolors(trademark2.size[0] * trademark2.size[1])
|
26 |
-
color_vector1 = np.
|
27 |
-
color_vector2 = np.
|
28 |
-
for count, color in colors1:
|
29 |
-
color_vector1 += np.array(color) * count
|
30 |
-
for count, color in colors2:
|
31 |
-
color_vector2 += np.array(color) * count
|
32 |
-
color_vector1 /= trademark1.size[0] * trademark1.size[1]
|
33 |
-
color_vector2 /= trademark2.size[0] * trademark2.size[1]
|
34 |
color_similarity = 1 - np.linalg.norm(color_vector1 - color_vector2)
|
35 |
return color_similarity
|
36 |
|
@@ -57,8 +57,8 @@ def prevent_trademark_conflict(trademark1, trademark2):
|
|
57 |
iface = gr.Interface(
|
58 |
fn=prevent_trademark_conflict,
|
59 |
inputs=[
|
60 |
-
gr.inputs.
|
61 |
-
gr.inputs.
|
62 |
],
|
63 |
outputs="text",
|
64 |
title="Trademark Conflict Prevention",
|
|
|
|
|
|
|
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 pytesseract
|
6 |
|
7 |
# Function to calculate SSIM between two images
|
8 |
def calculate_ssim(img1, img2):
|
9 |
img1_gray = img1.convert("L")
|
10 |
img2_gray = img2.convert("L")
|
11 |
+
return ssim(img1_gray, img2_gray)
|
12 |
+
|
13 |
+
# Function to extract text from an image using OCR
|
14 |
+
def extract_text(image):
|
15 |
+
image = Image.open(image)
|
16 |
+
text = pytesseract.image_to_string(image).lower()
|
17 |
+
return text
|
18 |
|
19 |
# Function to compare trademarks based on text similarity
|
20 |
def compare_text(trademark1, trademark2):
|
21 |
+
text1 = extract_text(trademark1)
|
22 |
+
text2 = extract_text(trademark2)
|
23 |
similarity_ratio = SequenceMatcher(None, text1, text2).ratio()
|
24 |
return similarity_ratio
|
25 |
|
|
|
29 |
trademark2 = trademark2.convert("RGB")
|
30 |
colors1 = trademark1.getcolors(trademark1.size[0] * trademark1.size[1])
|
31 |
colors2 = trademark2.getcolors(trademark2.size[0] * trademark2.size[1])
|
32 |
+
color_vector1 = sum([(count * np.array(color)) for count, color in colors1]) / (trademark1.size[0] * trademark1.size[1])
|
33 |
+
color_vector2 = sum([(count * np.array(color)) for count, color in colors2]) / (trademark2.size[0] * trademark2.size[1])
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
color_similarity = 1 - np.linalg.norm(color_vector1 - color_vector2)
|
35 |
return color_similarity
|
36 |
|
|
|
57 |
iface = gr.Interface(
|
58 |
fn=prevent_trademark_conflict,
|
59 |
inputs=[
|
60 |
+
gr.inputs.ImagePicker(label="Trademark Image 1"),
|
61 |
+
gr.inputs.ImagePicker(label="Trademark Image 2")
|
62 |
],
|
63 |
outputs="text",
|
64 |
title="Trademark Conflict Prevention",
|