Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -0,0 +1,144 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gdown
|
2 |
+
import cv2
|
3 |
+
import numpy as np
|
4 |
+
import gradio as gr
|
5 |
+
import matplotlib.pyplot as plt
|
6 |
+
from gradio_client import Client, handle_file
|
7 |
+
|
8 |
+
# β
Dictionary of public Google Drive links for reference signatures
|
9 |
+
drive_links = {
|
10 |
+
1: "https://drive.google.com/file/d/1xtzL6-TpN4EVyaFUF4MM4ssjAZqZutF8/view?usp=drive_link",
|
11 |
+
2: "https://drive.google.com/file/d/1UpPfOlDXoWwB5Ub530uhUrOVnUnWYvpQ/view?usp=drive_link",
|
12 |
+
3: "https://drive.google.com/file/d/1-M_PND4PK3tSLY705olnsswOk5bNoOFa/view?usp=drive_link",
|
13 |
+
4: "https://drive.google.com/file/d/1FL1uLEXlWW-nQYNoaBARiVs0N0XAwsvW/view?usp=drive_link",
|
14 |
+
5: "https://drive.google.com/file/d/1nZhl1CkvuH-KA4ErAslD-91W2QnBajhx/view?usp=drive_link",
|
15 |
+
6: "https://drive.google.com/file/d/1SHEgykTZN9lGdDaR6PTl9P01-Zlpu6cZ/view?usp=drive_link",
|
16 |
+
7: "https://drive.google.com/file/d/1gRE9SmvT7OBw8JYCyx7ehMs3lBpiX-Bp/view?usp=drive_link"
|
17 |
+
}
|
18 |
+
|
19 |
+
# β
Function to extract file ID from Google Drive link
|
20 |
+
def extract_file_id(drive_url):
|
21 |
+
return drive_url.split("/d/")[1].split("/view")[0]
|
22 |
+
|
23 |
+
# β
Function to download a file from Google Drive
|
24 |
+
def download_from_drive(file_id, save_path):
|
25 |
+
gdown.download(f"https://drive.google.com/uc?id={file_id}", save_path, quiet=False)
|
26 |
+
return save_path
|
27 |
+
|
28 |
+
# β
Function to extract the signature from a document image
|
29 |
+
def extract_signature(document_image_path):
|
30 |
+
client = Client("tech4humans/signature-detection")
|
31 |
+
result = client.predict(
|
32 |
+
image=handle_file(document_image_path),
|
33 |
+
conf_thres=0.25,
|
34 |
+
iou_thres=0.5,
|
35 |
+
api_name="/process_image"
|
36 |
+
)
|
37 |
+
|
38 |
+
extracted_signature_info = result[0]
|
39 |
+
extracted_signature_path = (
|
40 |
+
extracted_signature_info.get("path") if isinstance(extracted_signature_info, dict)
|
41 |
+
else extracted_signature_info if isinstance(extracted_signature_info, str)
|
42 |
+
else None
|
43 |
+
)
|
44 |
+
|
45 |
+
if extracted_signature_path:
|
46 |
+
image = cv2.imread(extracted_signature_path)
|
47 |
+
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
48 |
+
thresh = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
|
49 |
+
cv2.THRESH_BINARY_INV, 11, 2)
|
50 |
+
contours, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
|
51 |
+
|
52 |
+
valid_contours = []
|
53 |
+
for cnt in contours:
|
54 |
+
x, y, w, h = cv2.boundingRect(cnt)
|
55 |
+
area = w * h
|
56 |
+
aspect_ratio = w / float(h)
|
57 |
+
if 500 < area < 50000 and 0.2 < aspect_ratio < 5.0:
|
58 |
+
valid_contours.append((x, y, w, h))
|
59 |
+
|
60 |
+
if valid_contours:
|
61 |
+
x, y, w, h = max(valid_contours, key=lambda b: b[2] * b[3])
|
62 |
+
cropped_signature = image[y:y+h, x:x+w]
|
63 |
+
return cropped_signature
|
64 |
+
return None
|
65 |
+
|
66 |
+
# β
ORB Feature Matching for Signature Comparison
|
67 |
+
def orb_similarity(img1, img2, distance_threshold=50):
|
68 |
+
gray1, gray2 = [
|
69 |
+
cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) if len(img.shape) == 3 else img
|
70 |
+
for img in [img1, img2]
|
71 |
+
]
|
72 |
+
|
73 |
+
orb = cv2.ORB_create()
|
74 |
+
kp1, des1 = orb.detectAndCompute(gray1, None)
|
75 |
+
kp2, des2 = orb.detectAndCompute(gray2, None)
|
76 |
+
|
77 |
+
if des1 is None or des2 is None:
|
78 |
+
return 0, None
|
79 |
+
|
80 |
+
bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True)
|
81 |
+
matches = sorted(bf.match(des1, des2), key=lambda x: x.distance)
|
82 |
+
|
83 |
+
good_matches = [m for m in matches if m.distance < distance_threshold]
|
84 |
+
similarity = len(good_matches) / len(matches) if matches else 0
|
85 |
+
return similarity, (kp1, kp2, good_matches, matches)
|
86 |
+
|
87 |
+
# β
Function to process the uploaded document image and selected reference number
|
88 |
+
def verify_signature(document_image, reference_number):
|
89 |
+
if reference_number not in drive_links:
|
90 |
+
return "Invalid reference number selected.", None
|
91 |
+
|
92 |
+
# Download reference signature
|
93 |
+
file_id = extract_file_id(drive_links[reference_number])
|
94 |
+
reference_image_path = f"reference_signature_{reference_number}.jpg"
|
95 |
+
download_from_drive(file_id, reference_image_path)
|
96 |
+
|
97 |
+
# Extract signature from the document
|
98 |
+
cropped_signature = extract_signature(document_image)
|
99 |
+
if cropped_signature is None:
|
100 |
+
return "Signature extraction failed.", None
|
101 |
+
|
102 |
+
# Load reference signature
|
103 |
+
reference_img = cv2.imread(reference_image_path)
|
104 |
+
if reference_img is None:
|
105 |
+
return "Error: Could not load the reference image.", None
|
106 |
+
|
107 |
+
# Compute similarity
|
108 |
+
similarity, details = orb_similarity(cropped_signature, reference_img)
|
109 |
+
similarity_percentage = round(similarity * 100, 2)
|
110 |
+
|
111 |
+
# Classification based on similarity score
|
112 |
+
if similarity_percentage > 55:
|
113 |
+
classification = "β
Matched"
|
114 |
+
elif 40 <= similarity_percentage <= 55:
|
115 |
+
classification = "β οΈ Manual Check Recommended"
|
116 |
+
else:
|
117 |
+
classification = "β Not Matched"
|
118 |
+
|
119 |
+
# Generate visualization of matches
|
120 |
+
matched_img = None
|
121 |
+
if details is not None:
|
122 |
+
kp1, kp2, good_matches, _ = details
|
123 |
+
matched_img = cv2.drawMatches(cropped_signature, kp1, reference_img, kp2, good_matches, None, flags=2)
|
124 |
+
|
125 |
+
return f"π Similarity Score: {similarity_percentage}%\nπ {classification}", matched_img
|
126 |
+
|
127 |
+
# β
Gradio Interface
|
128 |
+
interface = gr.Interface(
|
129 |
+
fn=verify_signature,
|
130 |
+
inputs=[
|
131 |
+
gr.Image(type="filepath", label="Upload Document Image"),
|
132 |
+
gr.Number(label="Enter Reference Signature Number (1-7)", precision=0)
|
133 |
+
],
|
134 |
+
outputs=[
|
135 |
+
gr.Textbox(label="Verification Result"),
|
136 |
+
gr.Image(label="Signature Matching Visualization")
|
137 |
+
],
|
138 |
+
title="ποΈ Signature Verification System",
|
139 |
+
description="Upload a document with a signature, select a reference number (1-7), and verify its authenticity.",
|
140 |
+
theme="compact"
|
141 |
+
)
|
142 |
+
|
143 |
+
# β
Launch Gradio App
|
144 |
+
interface.launch()
|