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