File size: 2,471 Bytes
1a43383 b12bd2d 2550810 7559e30 86e69fa 0d2d614 0986dc7 c1da91a 623fe69 2550810 b12bd2d 0d2d614 f4eb306 2550810 0d2d614 b12bd2d 0d2d614 2550810 27b7cb5 b12bd2d 690ada3 0d2d614 690ada3 0986dc7 690ada3 b12bd2d 690ada3 b12bd2d a7129dd |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 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 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
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
# Function to calculate SSIM between two images
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))
# Function to extract text from an image
def extract_text(image):
text = pytesseract.image_to_string(image)
return text
# Function to calculate color difference between two images
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)
# Function to compare two trademarks
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
# Function to calculate text similarity ratio
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
# Function to handle the Gradio interface
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
# Prepare Gradio interface
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."
)
# Launch the interface
iface.launch()
|