Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
import numpy as np
|
| 3 |
import tensorflow as tf
|
| 4 |
import logging
|
|
@@ -82,6 +82,7 @@ def extract_clip_features(image_path, model, preprocess):
|
|
| 82 |
def compare_images(image1, image2, method):
|
| 83 |
similarity = None
|
| 84 |
start_time = time.time()
|
|
|
|
| 85 |
if method == 'pHash':
|
| 86 |
img1 = Image.open(image1)
|
| 87 |
img2 = Image.open(image2)
|
|
@@ -89,21 +90,26 @@ def compare_images(image1, image2, method):
|
|
| 89 |
hash2 = phashstr(img2)
|
| 90 |
distance = hamming_distance(hash1, hash2)
|
| 91 |
similarity = hamming_to_similarity(distance, len(hash1) * 4)
|
|
|
|
| 92 |
elif method == 'ResNet50':
|
| 93 |
features1 = extract_features(image1, resnet_model, resnet_preprocess)
|
| 94 |
features2 = extract_features(image2, resnet_model, resnet_preprocess)
|
| 95 |
similarity = cosine_similarity(features1, features2)
|
|
|
|
| 96 |
elif method == 'VGG16':
|
| 97 |
features1 = extract_features(image1, vgg_model, vgg_preprocess)
|
| 98 |
features2 = extract_features(image2, vgg_model, vgg_preprocess)
|
| 99 |
similarity = cosine_similarity(features1, features2)
|
|
|
|
| 100 |
elif method == 'CLIP':
|
| 101 |
features1 = extract_clip_features(image1, clip_model, preprocess_clip)
|
| 102 |
features2 = extract_clip_features(image2, clip_model, preprocess_clip)
|
| 103 |
similarity = cosine_similarity(features1, features2)
|
| 104 |
-
|
| 105 |
logging.info(f"AI based Supporting Documents comparison using {method} completed in {time.time() - start_time:.4f} seconds")
|
| 106 |
-
|
|
|
|
|
|
|
| 107 |
|
| 108 |
# Gradio interface
|
| 109 |
demo = gr.Interface(
|
|
@@ -113,9 +119,14 @@ demo = gr.Interface(
|
|
| 113 |
gr.Image(type="filepath", label="Upload Second Image"),
|
| 114 |
gr.Radio(["pHash", "ResNet50", "VGG16", "CLIP"], label="Select Comparison Method")
|
| 115 |
],
|
| 116 |
-
outputs=gr.
|
| 117 |
-
title="AI
|
| 118 |
-
description=
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
examples=[
|
| 120 |
["Snipaste_2024-05-31_16-18-31.jpg", "Snipaste_2024-05-31_16-18-52.jpg"],
|
| 121 |
["example1.png", "example2.png"]
|
|
@@ -123,3 +134,4 @@ demo = gr.Interface(
|
|
| 123 |
)
|
| 124 |
|
| 125 |
demo.launch()
|
|
|
|
|
|
| 1 |
+
# import gradio as gr
|
| 2 |
import numpy as np
|
| 3 |
import tensorflow as tf
|
| 4 |
import logging
|
|
|
|
| 82 |
def compare_images(image1, image2, method):
|
| 83 |
similarity = None
|
| 84 |
start_time = time.time()
|
| 85 |
+
|
| 86 |
if method == 'pHash':
|
| 87 |
img1 = Image.open(image1)
|
| 88 |
img2 = Image.open(image2)
|
|
|
|
| 90 |
hash2 = phashstr(img2)
|
| 91 |
distance = hamming_distance(hash1, hash2)
|
| 92 |
similarity = hamming_to_similarity(distance, len(hash1) * 4)
|
| 93 |
+
|
| 94 |
elif method == 'ResNet50':
|
| 95 |
features1 = extract_features(image1, resnet_model, resnet_preprocess)
|
| 96 |
features2 = extract_features(image2, resnet_model, resnet_preprocess)
|
| 97 |
similarity = cosine_similarity(features1, features2)
|
| 98 |
+
|
| 99 |
elif method == 'VGG16':
|
| 100 |
features1 = extract_features(image1, vgg_model, vgg_preprocess)
|
| 101 |
features2 = extract_features(image2, vgg_model, vgg_preprocess)
|
| 102 |
similarity = cosine_similarity(features1, features2)
|
| 103 |
+
|
| 104 |
elif method == 'CLIP':
|
| 105 |
features1 = extract_clip_features(image1, clip_model, preprocess_clip)
|
| 106 |
features2 = extract_clip_features(image2, clip_model, preprocess_clip)
|
| 107 |
similarity = cosine_similarity(features1, features2)
|
| 108 |
+
|
| 109 |
logging.info(f"AI based Supporting Documents comparison using {method} completed in {time.time() - start_time:.4f} seconds")
|
| 110 |
+
|
| 111 |
+
# Return similarity with HTML formatting for bold and colorful text
|
| 112 |
+
return f"<span style='font-weight:bold; color:#4CAF50;'>Similarity Score: {similarity:.2f}%</span>"
|
| 113 |
|
| 114 |
# Gradio interface
|
| 115 |
demo = gr.Interface(
|
|
|
|
| 119 |
gr.Image(type="filepath", label="Upload Second Image"),
|
| 120 |
gr.Radio(["pHash", "ResNet50", "VGG16", "CLIP"], label="Select Comparison Method")
|
| 121 |
],
|
| 122 |
+
outputs=gr.HTML(label="Similarity"), # Use HTML for bold and colorful text
|
| 123 |
+
title="AI Based Customs Supporting Documents Comparison",
|
| 124 |
+
description=(
|
| 125 |
+
"Upload two images of supporting documents and select the comparison method.\n"
|
| 126 |
+
"Fraud documents like invoices are used by custom brokers with the same templates. "
|
| 127 |
+
"This tool helps identify similar document templates used in two different consignments.\n"
|
| 128 |
+
"Developed by NCTC."
|
| 129 |
+
),
|
| 130 |
examples=[
|
| 131 |
["Snipaste_2024-05-31_16-18-31.jpg", "Snipaste_2024-05-31_16-18-52.jpg"],
|
| 132 |
["example1.png", "example2.png"]
|
|
|
|
| 134 |
)
|
| 135 |
|
| 136 |
demo.launch()
|
| 137 |
+
|