Spaces:
Sleeping
Sleeping
File size: 27,512 Bytes
9f6bfc6 657e67e 7c0f80b fe8cd4e 0414ba1 fe8cd4e 9f6bfc6 fe8cd4e 0414ba1 9f6bfc6 fe8cd4e 0414ba1 9f6bfc6 fe8cd4e 0414ba1 9f6bfc6 fe8cd4e 0414ba1 9f6bfc6 fe8cd4e 9f6bfc6 fe8cd4e 9f6bfc6 0414ba1 9f6bfc6 0414ba1 9f6bfc6 0414ba1 9f6bfc6 0414ba1 9f6bfc6 0414ba1 9f6bfc6 0414ba1 9f6bfc6 0414ba1 9f6bfc6 0414ba1 9f6bfc6 0414ba1 9f6bfc6 0414ba1 fe8cd4e 9f6bfc6 0414ba1 fe8cd4e 9f6bfc6 0414ba1 9f6bfc6 fe8cd4e 9f6bfc6 0414ba1 9f6bfc6 0414ba1 9f6bfc6 fe8cd4e 9f6bfc6 fe8cd4e 9f6bfc6 fe8cd4e 9f6bfc6 | 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 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 | # import os
# os.environ["CUDA_VISIBLE_DEVICES"] = "-1"
# import os
# import tempfile
# import numpy as np
# import cv2
# import gradio as gr
# from tensorflow.keras.applications import ResNet50
# from tensorflow.keras.applications.resnet50 import preprocess_input
# from tensorflow.keras.preprocessing import image
# from skimage.metrics import structural_similarity as ssim
# from PIL import Image
# from io import BytesIO
# # Disable GPU for TensorFlow
# os.environ["TF_ENABLE_ONEDNN_OPTS"] = "0"
# os.environ["CUDA_VISIBLE_DEVICES"] = "-1"
# class ImageCharacterClassifier:
# def __init__(self, similarity_threshold=0.5):
# self.model = ResNet50(weights='imagenet', include_top=False, pooling='avg')
# self.similarity_threshold = similarity_threshold
# def load_and_preprocess_image(self, img):
# # Convert image to array and preprocess it
# img = img.convert('RGB')
# img_array = np.array(img)
# img_array = cv2.resize(img_array, (224, 224)) # Ensure correct size
# img_array = np.expand_dims(img_array, axis=0)
# img_array = preprocess_input(img_array)
# return img_array
# def extract_features(self, img):
# preprocessed_img = self.load_and_preprocess_image(img)
# features = self.model.predict(preprocessed_img)
# return features
# def calculate_ssim(self, img1, img2):
# img1_gray = cv2.cvtColor(img1, cv2.COLOR_RGB2GRAY)
# img2_gray = cv2.cvtColor(img2, cv2.COLOR_RGB2GRAY)
# img2_gray = cv2.resize(img2_gray, (img1_gray.shape[1], img1_gray.shape[0]))
# return ssim(img1_gray, img2_gray)
# def process_images(reference_image, comparison_images, similarity_threshold):
# try:
# if reference_image is None:
# return "Please upload a reference image.", []
# if not comparison_images:
# return "Please upload comparison images.", []
# classifier = ImageCharacterClassifier(similarity_threshold)
# # Convert reference image to NumPy array
# ref_image = Image.fromarray(reference_image)
# ref_features = classifier.extract_features(ref_image)
# results = []
# html_output = "<h3>Comparison Results:</h3>"
# # for comp_image in comparison_images:
# # try:
# # # Read image file as PIL Image
# # comp_pil = Image.open(comp_image)
# # comp_pil = comp_pil.convert("RGB")
# # # Convert to NumPy format for SSIM
# # comp_array = np.array(comp_pil)
# for comp_image in comparison_images:
# try:
# with open(comp_image.name, "rb") as f:
# comp_pil = Image.open(BytesIO(f.read()))
# comp_pil = comp_pil.convert("RGB")
# comp_array = np.array(comp_pil)
# # Calculate SSIM score
# ssim_score = classifier.calculate_ssim(reference_image, comp_array)
# # Extract features
# comp_features = classifier.extract_features(comp_pil)
# max_feature_diff = np.max(np.abs(ref_features - comp_features))
# is_similar = max_feature_diff < 6.0
# status_text = "SIMILAR" if is_similar else "NOT SIMILAR"
# status_color = "green" if is_similar else "red"
# html_output += f"<p style='color:{status_color};'>{comp_image.name}: {status_text}</p>"
# results.append(comp_array)
# except Exception as e:
# html_output += f"<p style='color:red;'>Error processing {comp_image.name}: {str(e)}</p>"
# return html_output, results
# except Exception as e:
# return f"<p style='color:red;'>Error: {str(e)}</p>", []
# def create_interface():
# with gr.Blocks() as interface:
# gr.Markdown("# Image Similarity Classifier")
# gr.Markdown("Upload a reference image and multiple comparison images.")
# with gr.Row():
# with gr.Column():
# reference_input = gr.Image(label="Reference Image", type="numpy")
# comparison_input = gr.Files(label="Comparison Images", type="file")
# threshold_slider = gr.Slider(minimum=0.0, maximum=1.0, value=0.5, step=0.05, label="Similarity Threshold")
# submit_button = gr.Button("Compare Images")
# with gr.Column():
# output_html = gr.HTML(label="Results")
# output_gallery = gr.Gallery(label="Processed Images", columns=3)
# submit_button.click(
# fn=process_images,
# inputs=[reference_input, comparison_input, threshold_slider],
# outputs=[output_html, output_gallery]
# )
# return interface
# if __name__ == "__main__":
# interface = create_interface()
# interface.launch(share=True)
# import os
# os.environ["CUDA_VISIBLE_DEVICES"] = "-1"
# import os
# import tempfile
# import numpy as np
# import cv2
# import gradio as gr
# from tensorflow.keras.applications import ResNet50
# from tensorflow.keras.applications.resnet50 import preprocess_input
# from tensorflow.keras.preprocessing import image
# from skimage.metrics import structural_similarity as ssim
# from PIL import Image
# from io import BytesIO
# # Disable GPU for TensorFlow
# os.environ["TF_ENABLE_ONEDNN_OPTS"] = "0"
# os.environ["CUDA_VISIBLE_DEVICES"] = "-1"
# # --- DOCUMENTATION STRINGS (English Only) ---
# GUIDELINE_SETUP = """
# ## 1. Quick Start Guide: Setup and Run Instructions
# This application uses a combination of advanced feature extraction (ResNet50) and structural analysis (SSIM) to determine if comparison images are structurally and semantically similar to a reference image.
# 1. **Upload Reference:** Upload the main image you want to compare against in the 'Reference Image' box.
# 2. **Upload Comparisons:** Upload one or more images you want to test for similarity in the 'Comparison Images' file upload area.
# 3. **Set Threshold:** Adjust the 'Similarity Threshold' slider. This primarily affects the structural (SSIM) component, but the feature comparison also plays a role (currently fixed).
# 4. **Run:** Click the **"Compare Images"** button.
# 5. **Review:** Results will appear in the 'Results' panel, indicating if each comparison image is "SIMILAR" or "NOT SIMILAR".
# """
# GUIDELINE_INPUT = """
# ## 2. Expected Inputs
# | Input Field | Purpose | Requirement |
# | :--- | :--- | :--- |
# | **Reference Image** | The baseline image against which all others will be compared. | Must be a single image file (JPG, PNG). |
# | **Comparison Images** | One or more images to be tested for similarity. | Must be multiple image files. Upload them using the file selector. |
# | **Similarity Threshold** | A slider controlling the sensitivity (0.0 to 1.0) for structural similarity (SSIM). | Higher values (closer to 1.0) mean stricter similarity requirements. Default is 0.5. |
# **Image Preprocessing:** All uploaded images are automatically resized to 224x224 pixels and standardized according to the requirements of the ResNet model before feature extraction.
# """
# GUIDELINE_OUTPUT = """
# ## 3. Expected Outputs (Similarity Results)
# The application provides two main outputs:
# 1. **Results (HTML Panel):**
# * A list detailing the outcome for each comparison image.
# * Status: **SIMILAR** (Green) or **NOT SIMILAR** (Red).
# * Similarity is determined by a combined metric: Structural Similarity (SSIM) AND feature vector distance (ResNet features).
# 2. **Processed Images (Gallery):**
# * A gallery displaying the input comparison images after they have been processed.
# ### How Similarity is Determined:
# The classification relies on two checks:
# 1. **Feature Distance:** The distance between the deep features extracted by the ResNet50 model (checking semantic content).
# 2. **Structural Similarity (SSIM):** A metric comparing the structural fidelity between the reference and comparison images (checking visual layout and quality).
# An image is typically marked "SIMILAR" only if both checks suggest a close match.
# """
# # --- CLASSIFIER CLASS ---
# class ImageCharacterClassifier:
# def __init__(self, similarity_threshold=0.5):
# # Setting include_top=False loads the ResNet50 convolutional layers
# self.model = ResNet50(weights='imagenet', include_top=False, pooling='avg')
# self.similarity_threshold = similarity_threshold
# def load_and_preprocess_image(self, img):
# # Convert image to array and preprocess it
# img = img.convert('RGB')
# img_array = np.array(img)
# img_array = cv2.resize(img_array, (224, 224)) # Ensure correct size
# img_array = np.expand_dims(img_array, axis=0)
# img_array = preprocess_input(img_array)
# return img_array
# def extract_features(self, img):
# preprocessed_img = self.load_and_preprocess_image(img)
# # Use predict_on_batch for potentially better memory usage
# features = self.model.predict(preprocessed_img, verbose=0)
# return features
# def calculate_ssim(self, img1, img2):
# # Ensure images are in numpy array format for cv2 and SSIM
# img1_gray = cv2.cvtColor(img1, cv2.COLOR_RGB2GRAY)
# img2_gray = cv2.cvtColor(img2, cv2.COLOR_RGB2GRAY)
# # Resize comparison image to match reference image size for SSIM calculation
# img2_gray = cv2.resize(img2_gray, (img1_gray.shape[1], img1_gray.shape[0]))
# # Ensure data types are consistent (usually float/uint8 works)
# # SSIM calculation
# return ssim(img1_gray, img2_gray, data_range=img1_gray.max() - img1_gray.min())
# def process_images(reference_image_array, comparison_images, similarity_threshold):
# try:
# if reference_image_array is None:
# return "<p style='color:red;'>Please upload a reference image.</p>", []
# if not comparison_images:
# return "<p style='color:red;'>Please upload comparison images.</p>", []
# classifier = ImageCharacterClassifier(similarity_threshold)
# # 1. Process Reference Image
# ref_image_pil = Image.fromarray(reference_image_array).convert("RGB")
# ref_features = classifier.extract_features(ref_image_pil)
# # Convert array back to RGB for SSIM comparison later
# ref_image_for_ssim = cv2.cvtColor(reference_image_array, cv2.COLOR_BGR2RGB)
# results = []
# html_output = "<h3>Comparison Results:</h3>"
# # 2. Process Comparison Images
# for comp_file in comparison_images:
# try:
# # Open image file using PIL
# with open(comp_file.name, "rb") as f:
# comp_pil = Image.open(BytesIO(f.read())).convert("RGB")
# comp_array = np.array(comp_pil)
# # --- Similarity Checks ---
# # A. SSIM Check (Structural Similarity)
# ssim_score = classifier.calculate_ssim(ref_image_for_ssim, comp_array)
# ssim_match = ssim_score >= similarity_threshold
# # B. Feature Check (Semantic Similarity using ResNet features)
# comp_features = classifier.extract_features(comp_pil)
# # Using a hardcoded feature difference threshold (6.0 in original code)
# max_feature_diff = np.max(np.abs(ref_features - comp_features))
# feature_match = max_feature_diff < 6.0
# # Combined Result
# is_similar = feature_match # The original logic primarily used the feature match
# # If you want to require both SSIM and Feature Match:
# # is_similar = ssim_match and feature_match
# status_text = f"SIMILAR (SSIM: {ssim_score:.3f})" if is_similar else f"NOT SIMILAR (SSIM: {ssim_score:.3f})"
# status_color = "green" if is_similar else "red"
# html_output += f"<p style='color:{status_color};'>{os.path.basename(comp_file.name)}: {status_text}</p>"
# results.append(comp_array) # Add the numpy array of the comparison image
# except Exception as e:
# html_output += f"<p style='color:red;'>Error processing {os.path.basename(comp_file.name)}: {str(e)}</p>"
# results.append(None) # Add None to keep list consistent
# return html_output, [r for r in results if r is not None]
# except Exception as e:
# return f"<p style='color:red;'>Critical Error: {str(e)}</p>", []
# def create_interface():
# with gr.Blocks(title="Image Similarity Classifier") as interface:
# gr.Markdown("# Image Similarity Classifier (ResNet + SSIM)")
# gr.Markdown("Tool to compare a reference image against multiple comparison images based on structural and deep feature similarity.")
# # 1. Guidelines Section
# with gr.Accordion("Tips & Guidelines ", open=False):
# gr.Markdown(GUIDELINE_SETUP)
# gr.Markdown("---")
# gr.Markdown(GUIDELINE_INPUT)
# gr.Markdown("---")
# gr.Markdown(GUIDELINE_OUTPUT)
# gr.Markdown("---")
# # 2. Application Interface
# with gr.Row():
# with gr.Column():
# gr.Markdown("## Step 1: Upload a Reference Image ")
# reference_input = gr.Image(label="Reference Image", type="numpy", height=300)
# gr.Markdown("## Step 2: Upload Multiple Images to Compair with Reference Image ")
# comparison_input = gr.Files(label="Comparison Images", type="file")
# gr.Markdown("## Step 3: Set the Confidence Score (Optional) ")
# threshold_slider = gr.Slider(minimum=0.0, maximum=1.0, value=0.5, step=0.05, label="Similarity Threshold (SSIM)")
# gr.Markdown("## Step 4: Click Compare Images ")
# submit_button = gr.Button("Compare Images", variant="primary")
# gr.Markdown("# Results ")
# gr.Markdown("## Comparison Result ")
# output_html = gr.HTML(label="Comparison Results")
# gr.Markdown("## Processed Comparison Images")
# output_gallery = gr.Gallery(label="Processed Comparison Images", columns=3)
# # 3. Event Handling
# submit_button.click(
# fn=process_images,
# inputs=[reference_input, comparison_input, threshold_slider],
# outputs=[output_html, output_gallery]
# )
# # Example data setup (Requires placeholder images to exist)
# gr.Markdown("---")
# gr.Markdown("## Sample Data for Testing")
# # Note: You would need to provide actual file paths for reference and comparison samples
# # Example setup demonstrating how to structure inputs for gr.Examples:
# example_data = [
# [np.zeros((100, 100, 3), dtype=np.uint8), [gr.File("sample_data/license3.jpg"), gr.File("sample_data/licence.jpeg")], 0.6], # Placeholder example
# ]
# # Since examples for Files/Gallery can be complex to set up without actual files,
# # we will use a simple explanation here instead of a runnable Example block.
# gr.Markdown("Due to the multi-file input requirement, please manually upload a reference image and several comparison images to test.")
# return interface
# if __name__ == "__main__":
# interface = create_interface()
# # Note: Using share=True might expose the app publicly if run without authorization.
# interface.launch()
import os
import numpy as np
import cv2
import gradio as gr
from tensorflow.keras.applications import ResNet50
from tensorflow.keras.applications.resnet50 import preprocess_input
from skimage.metrics import structural_similarity as ssim
from PIL import Image
from io import BytesIO
# Disable GPU for TensorFlow
os.environ["TF_ENABLE_ONEDNN_OPTS"] = "0"
os.environ["CUDA_VISIBLE_DEVICES"] = "-1"
# --- DOCUMENTATION STRINGS (English Only) ---
GUIDELINE_SETUP = """
## 1. Quick Start Guide: Setup and Run Instructions
This application uses a combination of advanced feature extraction (ResNet50) and structural analysis (SSIM) to determine if comparison images are structurally and semantically similar to a reference image.
1. **Upload Reference:** Upload the main image you want to compare against in the 'Reference Image' box.
2. **Upload Comparisons:** Upload one or more images you want to test for similarity in the 'Comparison Images' file upload area.
3. **Set Threshold:** Adjust the 'Similarity Threshold' slider. This controls the sensitivity for structural similarity (SSIM).
4. **Run:** Click the **"Compare Images"** button.
5. **Review:** Results will appear in the 'Results' panel, indicating if each comparison image is "SIMILAR" or "NOT SIMILAR".
"""
GUIDELINE_INPUT = """
## 2. Expected Inputs and Preprocessing
| Input Field | Purpose | Requirement |
| :--- | :--- | :--- |
| **Reference Image** | The baseline image against which all others will be compared. | Must be a single image file (JPG, PNG). |
| **Comparison Images** | One or more images to be tested for similarity. | Must be multiple image files. Upload them using the file selector. |
| **Similarity Threshold** | A slider controlling the sensitivity (0.0 to 1.0) for structural similarity (SSIM). | Higher values (closer to 1.0) mean stricter similarity requirements. Default is 0.5. |
**Image Preprocessing:** All uploaded images are automatically resized to 224x224 pixels and standardized according to the requirements of the ResNet model before feature extraction.
"""
GUIDELINE_OUTPUT = """
## 3. Expected Outputs (Similarity Results)
The application provides two main outputs:
1. **Results (HTML Panel):**
* A list detailing the outcome for each comparison image.
* Status: **SIMILAR** (Green) or **NOT SIMILAR** (Red).
2. **Processed Images (Gallery):**
* A gallery displaying the input comparison images after they have been processed.
### How Similarity is Determined:
The classification relies on two checks: Structural Similarity (SSIM) and Deep Feature Distance (ResNet). An image is marked "SIMILAR" if both structural and semantic properties suggest a close match.
"""
# --- CLASSIFIER CLASS ---
class ImageCharacterClassifier:
def __init__(self, similarity_threshold=0.5):
self.model = ResNet50(weights='imagenet', include_top=False, pooling='avg')
self.similarity_threshold = similarity_threshold
def load_and_preprocess_image(self, img):
img = img.convert('RGB')
img_array = np.array(img)
img_array = cv2.resize(img_array, (224, 224))
img_array = np.expand_dims(img_array, axis=0)
img_array = preprocess_input(img_array)
return img_array
def extract_features(self, img):
preprocessed_img = self.load_and_preprocess_image(img)
features = self.model.predict(preprocessed_img, verbose=0)
return features
def calculate_ssim(self, img1, img2):
img1_gray = cv2.cvtColor(img1, cv2.COLOR_RGB2GRAY)
img2_gray = cv2.cvtColor(img2, cv2.COLOR_RGB2GRAY)
img2_gray = cv2.resize(img2_gray, (img1_gray.shape[1], img1_gray.shape[0]))
return ssim(img1_gray, img2_gray, data_range=img1_gray.max() - img1_gray.min())
def process_images(reference_image_array, comparison_files, similarity_threshold):
try:
if reference_image_array is None:
return "<p style='color:red;'>Please upload a reference image.</p>", []
if not comparison_files:
return "<p style='color:red;'>Please upload comparison images.</p>", []
classifier = ImageCharacterClassifier(similarity_threshold)
ref_image_pil = Image.fromarray(reference_image_array).convert("RGB")
ref_features = classifier.extract_features(ref_image_pil)
ref_image_for_ssim = cv2.cvtColor(reference_image_array, cv2.COLOR_BGR2RGB)
results = []
html_output = "<h3>Comparison Results:</h3>"
for comp_file_item in comparison_files:
try:
# FIX: Extract file path correctly regardless of whether it's a dict (internal Gradio)
# or a gr.File object (returned by our custom loader function).
if isinstance(comp_file_item, str):
file_path = comp_file_item
elif hasattr(comp_file_item, 'name'):
file_path = comp_file_item.name
elif isinstance(comp_file_item, dict) and 'name' in comp_file_item:
file_path = comp_file_item['name']
else:
raise ValueError("Invalid file object structure.")
with open(file_path, "rb") as f:
comp_pil = Image.open(BytesIO(f.read())).convert("RGB")
comp_array = np.array(comp_pil)
# SSIM Check
ssim_score = classifier.calculate_ssim(ref_image_for_ssim, comp_array)
# Feature Check
comp_features = classifier.extract_features(comp_pil)
max_feature_diff = np.max(np.abs(ref_features - comp_features))
feature_match = max_feature_diff < 6.0
is_similar = feature_match # Primary criterion
status_text = f"SIMILAR (SSIM: {ssim_score:.3f})" if is_similar else f"NOT SIMILAR (SSIM: {ssim_score:.3f})"
status_color = "green" if is_similar else "red"
html_output += f"<p style='color:{status_color};'>{os.path.basename(file_path)}: {status_text}</p>"
results.append(comp_array)
except Exception as e:
# Use the path for logging the error
error_name = os.path.basename(file_path) if 'file_path' in locals() else 'Unknown File'
html_output += f"<p style='color:red;'>Error processing {error_name}: {str(e)}</p>"
return html_output, [r for r in results if r is not None]
except Exception as e:
return f"<p style='color:red;'>Critical Error: {str(e)}</p>", []
# --- SAMPLE DATA DEFINITION ---
# Placeholder file paths (MUST EXIST for examples to work)
# NOTE: Adjusted paths to match your provided snippet structure 'sample_data/filename'
SAMPLE_FILES_SET1 = {
"reference": "sample_data/license3.jpg",
"comparisons": ["sample_data/license3.jpg", "sample_data/license3.jpg", "sample_data/licence.jpeg"]
}
SAMPLE_FILES_SET2 = {
"reference": "sample_data/licence.jpeg",
"comparisons": ["sample_data/licence.jpeg", "sample_data/license3.jpg", "sample_data/licence.jpeg", "sample_data/licence.jpeg"]
}
# --- GRADIO UI SETUP ---
def create_interface():
with gr.Blocks(title="Image Similarity Classifier") as interface:
gr.Markdown("# Image Similarity Classifier (ResNet + SSIM)")
gr.Markdown("Tool to compare a reference image against multiple comparison images based on structural and deep feature similarity.")
# 1. Guidelines Section
with gr.Accordion("User Guidelines and Documentation", open=False):
gr.Markdown(GUIDELINE_SETUP)
gr.Markdown("---")
gr.Markdown(GUIDELINE_INPUT)
gr.Markdown("---")
gr.Markdown(GUIDELINE_OUTPUT)
gr.Markdown("---")
# 2. Application Interface
with gr.Row():
with gr.Column():
gr.Markdown("## Step 1: Upload a Reference Image ")
reference_input = gr.Image(label="Reference Image", type="numpy", height=300)
gr.Markdown("## Step 2: Upload Multiple Images to Compair with Reference Image ")
comparison_input = gr.Files(label="Comparison Images", type="file")
gr.Markdown("## Step 3: Set the Confidence Score (Optional) ")
threshold_slider = gr.Slider(minimum=0.0, maximum=1.0, value=0.5, step=0.05, label="Similarity Threshold (SSIM)")
gr.Markdown("## Step 4: Click Compare Images ")
submit_button = gr.Button("Compare Images", variant="primary")
gr.Markdown("---")
gr.Markdown("# Results ")
gr.Markdown("## Comparison Result ")
output_html = gr.HTML(label="Comparison Results")
gr.Markdown("## Processed Comparison Images")
output_gallery = gr.Gallery(label="Processed Comparison Images", columns=3)
# 3. Example Loading Setup
gr.Markdown("---")
gr.Markdown("## Sample Data for Testing")
gr.Markdown("### Click on any of these two set to run the test set ")
def load_and_run_set(reference_path, comparison_paths, threshold_value=0.5):
"""Loads data into inputs, triggers processing, and returns all results."""
# 1. Load Reference Image as NumPy array
ref_img_pil = Image.open(reference_path).convert("RGB")
ref_img_array = np.array(ref_img_pil)
# 2. Comparison Files: Prepare the list of paths (strings) for the processor
# We return a list of strings/paths here, which Gradio's gr.Files component accepts
comparison_file_paths = comparison_paths
# 3. Process the images immediately using the paths
html, gallery = process_images(ref_img_array, comparison_file_paths, threshold_value)
# 4. Return inputs and outputs for component update
return ref_img_array, comparison_file_paths, threshold_value, html, gallery
with gr.Row():
btn_set1 = gr.Button("Load & Run Sample Set 1 (Similar Docs)", size="sm")
btn_set2 = gr.Button("Load & Run Sample Set 2 (Dissimilar Docs)", size="sm")
# 4. Event Handling
submit_button.click(
fn=process_images,
inputs=[reference_input, comparison_input, threshold_slider],
outputs=[output_html, output_gallery]
)
# Event handlers for example buttons: load data into inputs/outputs
btn_set1.click(
fn=lambda: load_and_run_set(SAMPLE_FILES_SET1['reference'], SAMPLE_FILES_SET1['comparisons'], 0.6),
inputs=[],
outputs=[reference_input, comparison_input, threshold_slider, output_html, output_gallery]
)
btn_set2.click(
fn=lambda: load_and_run_set(SAMPLE_FILES_SET2['reference'], SAMPLE_FILES_SET2['comparisons'], 0.4),
inputs=[],
outputs=[reference_input, comparison_input, threshold_slider, output_html, output_gallery]
)
return interface
if __name__ == "__main__":
# Ensure the 'sample_data/' directory exists with 'license3.jpg' and 'licence.jpeg'
# and any other necessary files.
interface = create_interface()
interface.queue()
interface.launch() |