File size: 3,534 Bytes
c181513 2cd0fed 5b24d89 c181513 0c0dd5a c181513 f42acc8 2cd0fed f42acc8 0c0dd5a f42acc8 0c0dd5a f42acc8 0c0dd5a f42acc8 0c0dd5a f42acc8 0c0dd5a f42acc8 0c0dd5a f42acc8 0c0dd5a f42acc8 0c0dd5a c181513 f42acc8 c181513 f42acc8 c181513 f42acc8 c181513 f42acc8 2cd0fed c181513 f42acc8 0c0dd5a f42acc8 c181513 f42acc8 0c0dd5a f42acc8 0c0dd5a c181513 f42acc8 0c0dd5a c181513 f42acc8 c181513 f42acc8 c181513 f42acc8 c181513 f42acc8 c181513 f42acc8 c181513 f42acc8 c181513 f42acc8 c181513 f42acc8 c181513 4d1f9bc 0c0dd5a c181513 f42acc8 0c0dd5a f42acc8 0c0dd5a c181513 f42acc8 0c0dd5a |
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 |
import tensorflow as tf
import numpy as np
from tensorflow.keras.utils import CustomObjectScope
from PIL import Image
import gradio as gr
import os
from huggingface_hub import hf_hub_download
# Function to calculate absolute difference
def absolute_difference(x):
return tf.abs(x[0] - x[1])
# Download model from Hugging Face Hub
def load_model_from_hub():
try:
print("Downloading model from Hugging Face Hub...")
model_path = hf_hub_download(
repo_id="Nithikorn/Signature_Detection",
filename="final_signature_model1.keras"
)
print(f"β
Model downloaded successfully at: {model_path}")
# Load model with CustomObjectScope
with CustomObjectScope({'absolute_difference': absolute_difference}):
model = tf.keras.models.load_model(model_path)
print("β
Model loaded successfully!")
return model
except Exception as e:
print(f"β Error loading model: {str(e)}")
raise e
# Load model
try:
model = load_model_from_hub()
except Exception as e:
print(f"β Cannot load model: {str(e)}")
model = None
def preprocess_image(image):
image = image.convert("RGB") # Convert to RGB
image = image.resize((224, 224)) # Resize
image = np.array(image, dtype=np.float32) / 255.0 # Normalize
image = np.expand_dims(image, axis=0) # Add batch dimension
return image
# Signature comparison function
def predict_signature(image1, image2):
print("Starting prediction...")
if image1 is None or image2 is None:
print("No images found")
return "β Please upload both images"
if model is None:
return "β Unable to load model. Please check if you've uploaded the model to Hugging Face Hub"
try:
print("Processing image 1...")
img1_processed = preprocess_image(image1)
print("Processing image 2...")
img2_processed = preprocess_image(image2)
print("Sending data to model...")
prediction = model.predict([img1_processed, img2_processed])
print(f"Received result: {prediction}, shape: {prediction.shape}")
# Convert result to numeric value
if prediction.shape == (1, 1):
prediction_value = float(prediction[0][0])
elif prediction.shape == (1,):
prediction_value = float(prediction[0])
else:
prediction_value = float(prediction.flatten()[0])
print(f"Value obtained: {prediction_value}")
threshold = 0.43
result = "β
Genuine Signature" if prediction_value >= threshold else "β Forged Signature"
print(f"Verification result: {result}")
return f"Similarity Score: {prediction_value:.4f}\n{result}"
except Exception as e:
print(f"Error occurred: {str(e)}")
import traceback
traceback.print_exc() # Show full error details
return f"β Error occurred: {str(e)}"
# Create Gradio UI
with gr.Blocks() as demo:
gr.Markdown("# Signature Verification System")
with gr.Row():
img1 = gr.Image(type="pil", label="Signature 1", show_label=True)
img2 = gr.Image(type="pil", label="Signature 2", show_label=True)
btn_predict = gr.Button("Verify Signatures")
output = gr.Textbox(label="Result")
btn_predict.click(predict_signature, inputs=[img1, img2], outputs=output)
# Run Gradio app
demo.launch() |