|
import gradio as gr |
|
import cv2 |
|
import numpy as np |
|
from skimage.metrics import structural_similarity as ssim |
|
from PIL import Image |
|
import pytesseract |
|
import matplotlib.pyplot as plt |
|
|
|
|
|
def calculate_ssim(img1, img2): |
|
img1_gray = img1.convert("L") |
|
img2_gray = img2.convert("L") |
|
img1_resized = img1_gray.resize((256, 256)) |
|
img2_resized = img2_gray.resize((256, 256)) |
|
return ssim(np.array(img1_resized), np.array(img2_resized)) |
|
|
|
|
|
def extract_text(image): |
|
text = pytesseract.image_to_string(image) |
|
return text |
|
|
|
|
|
def calculate_color_difference(img1, img2): |
|
hist1 = cv2.calcHist([np.array(img1)], [0, 1, 2], None, [8, 8, 8], [0, 256, 0, 256, 0, 256]) |
|
hist2 = cv2.calcHist([np.array(img2)], [0, 1, 2], None, [8, 8, 8], [0, 256, 0, 256, 0, 256]) |
|
return cv2.compareHist(hist1, hist2, cv2.HISTCMP_CHISQR) |
|
|
|
|
|
def compare_trademarks(trademark1, trademark2): |
|
ssim_score = calculate_ssim(trademark1, trademark2) |
|
text1 = extract_text(trademark1) |
|
text2 = extract_text(trademark2) |
|
text_similarity = calculate_text_similarity(text1, text2) |
|
color_difference = calculate_color_difference(trademark1, trademark2) |
|
return ssim_score, text_similarity, color_difference |
|
|
|
|
|
def calculate_text_similarity(text1, text2): |
|
if text1 == "" or text2 == "": |
|
return 0.0 |
|
text1 = text1.lower() |
|
text2 = text2.lower() |
|
intersection = len(set(text1.split()) & set(text2.split())) |
|
union = len(set(text1.split()) | set(text2.split())) |
|
return intersection / union |
|
|
|
|
|
def prevent_trademark_conflict(trademark1, trademark2): |
|
ssim_score, text_similarity, color_difference = compare_trademarks(trademark1, trademark2) |
|
result = f"SSIM Score: {ssim_score:.2f}\nText Similarity: {text_similarity:.2f}\nColor Difference: {color_difference:.2f}" |
|
return result |
|
|
|
|
|
iface = gr.Interface( |
|
fn=prevent_trademark_conflict, |
|
inputs=[ |
|
gr.inputs.Image(type="pil", label="Trademark Image 1"), |
|
gr.inputs.Image(type="pil", label="Trademark Image 2") |
|
], |
|
outputs="text", |
|
title="Trademark Similarity", |
|
description="Compare two trademarks to check for similarity." |
|
) |
|
|
|
|
|
iface.launch() |
|
|