Skip to content

Commit

Permalink
Signature verification using opencv
Browse files Browse the repository at this point in the history
  • Loading branch information
09viv committed Oct 14, 2024
1 parent 847b0f8 commit e133134
Show file tree
Hide file tree
Showing 9 changed files with 225 additions and 0 deletions.
19 changes: 19 additions & 0 deletions Signature Verification/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
### Signature Verification System
This project is a web-based Signature Verification System built using Flask and OpenCV. It allows users to upload two signature images, compares them, and verifies if the signatures are likely from the same person based on a similarity score.

### Features
Upload two signature images.
Real-time comparison using OpenCV's image processing techniques.
Displays the result indicating whether the signatures are likely the same.
Simple and intuitive web interface.
Result shown in bold text for easy identification.

### Technologies Used
Python (Flask, OpenCV, NumPy)
HTML/CSS/JavaScript
Jinja2 (Flask templating engine)

### How to Use
Upload two signature images using the form on the homepage.
Click on Submit to compare the signatures.
The system will display a message indicating whether the signatures are likely from the same person or not.
68 changes: 68 additions & 0 deletions Signature Verification/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
from flask import Flask, render_template, request
import cv2
import numpy as np
import os

app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = 'uploads/'
os.makedirs(app.config['UPLOAD_FOLDER'], exist_ok=True)

def preprocess_signature(image_path):
# Read the image
image = cv2.imread(image_path)
# Convert to grayscale
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Resize the image to a standard size
resized_image = cv2.resize(gray_image, (128, 128))
# Threshold the image (binary)
_, binary_image = cv2.threshold(resized_image, 128, 255, cv2.THRESH_BINARY_INV)
return binary_image

def compute_histogram(image):
# Compute the histogram (256 bins for grayscale image)
hist = cv2.calcHist([image], [0], None, [256], [0, 256])
# Normalize the histogram
hist = cv2.normalize(hist, hist).flatten()
return hist

def compare_signatures(signature1, signature2):
# Compute histograms for both signatures
hist1 = compute_histogram(signature1)
hist2 = compute_histogram(signature2)
# Compare histograms using correlation method
score = cv2.compareHist(hist1, hist2, cv2.HISTCMP_CORREL)
return score

@app.route('/')
def index():
return render_template('index.html')

@app.route('/compare', methods=['POST'])
def compare():
if 'signature1' not in request.files or 'signature2' not in request.files:
return "No files uploaded", 400

file1 = request.files['signature1']
file2 = request.files['signature2']

path1 = os.path.join(app.config['UPLOAD_FOLDER'], file1.filename)
path2 = os.path.join(app.config['UPLOAD_FOLDER'], file2.filename)

file1.save(path1)
file2.save(path2)

# Process the signatures
sig1 = preprocess_signature(path1)
sig2 = preprocess_signature(path2)

# Compare signatures
similarity_score = compare_signatures(sig1, sig2)

# Set a threshold for verification
threshold = 0.7
result = "<b>The signatures matched successfully.</b>" if similarity_score > threshold else "<b>The signatures are likely to be different.</b>"

return result

if __name__ == '__main__':
app.run(debug=True)
65 changes: 65 additions & 0 deletions Signature Verification/methods.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Methods - Signature Verification</title>
<link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}">
</head>
<body>

<header>
<h1>Signature Verification System</h1>
<nav>
<ul>
<li><a href="{{ url_for('index') }}">Home</a></li>
<li><a href="{{ url_for('methods') }}">Methods</a></li>
<li><a href="{{ url_for('compare') }}">Compare</a></li>
</ul>
</nav>
</header>

<main>
<section>
<h2>Methods Used in Signature Verification</h2>
<p>This Signature Verification System uses several steps to analyze and compare the signatures. Below are the main steps involved:</p>

<h3>1. Preprocessing the Signatures</h3>
<p>The first step is to preprocess the uploaded images. The following preprocessing techniques are applied:</p>
<ul>
<li><b>Grayscale Conversion:</b> The color images are converted into grayscale to reduce complexity.</li>
<li><b>Thresholding:</b> Thresholding is applied to the image to create a binary image that distinguishes the signature from the background.</li>
<li><b>Contour Detection:</b> Contours are detected in both the uploaded signatures using OpenCV to extract the structure of the signatures.</li>
</ul>

<h3>2. Comparing Signatures</h3>
<p>The core method of comparison involves calculating a similarity score between the two signatures:</p>
<ul>
<li><b>Histogram Comparison:</b> We use OpenCV’s <code>cv2.compareHist()</code> function to compare the histograms of the two signatures. This gives a score indicating the degree of similarity between the two images.</li>
<li><b>Correlation Method:</b> The correlation method is used to compute the similarity between the two histograms. A higher correlation score means the signatures are more similar.</li>
</ul>

<h3>3. Decision Making</h3>
<p>Once the similarity score is calculated, a threshold is applied to determine if the signatures match:</p>
<ul>
<li>If the similarity score is above the predefined threshold, the signatures are considered to be from the same person.</li>
<li>If the score is below the threshold, the signatures are likely different.</li>
</ul>
</section>

<section>
<h2>Technologies Used</h2>
<ul>
<li><b>Flask:</b> Flask is used to handle the backend logic and serve the web application.</li>
<li><b>OpenCV:</b> OpenCV is used for image processing and signature comparison.</li>
<li><b>Jinja2:</b> Flask’s templating engine Jinja2 is used to render dynamic HTML pages.</li>
</ul>
</section>
</main>

<footer>
<p>&copy; 2024 Signature Verification System. All rights reserved.</p>
</footer>

</body>
</html>
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
73 changes: 73 additions & 0 deletions Signature Verification/templates/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<!DOCTYPE html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />

<!-- Bootstrap CSS -->
<link
href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"
rel="stylesheet"
integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC"
crossorigin="anonymous"
/>

<title>Signature Verification Tool</title>
</head>
<body>
<div class="container">
<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
<div class="container-fluid">
<a class="navbar-brand" href="#">Dashboard</a>
<button
class="navbar-toggler"
type="button"
data-bs-toggle="collapse"
data-bs-target="#navbarSupportedContent"
aria-controls="navbarSupportedContent"
aria-expanded="false"
aria-label="Toggle navigation"
>
<span class="navbar-toggler-icon"></span>
</button>
</div>
</nav>
<div class = "container my-3">
<form
id="uploadForm"
action="/compare"
method="post"
enctype="multipart/form-data"
>
<div class="mb-3">
<label for="signature1" class="form-label">Upload signature 1</label>
<input
class="form-control"
type="file"
name="signature1"
accept="image/*"
required
/>
</div>
<div class="mb-3">
<label for="signature2" class="form-label">Upload signature 2</label>
<input
class="form-control"
type="file"
name="signature2"
accept="image/*"
required
/>
</div>
<button type="submit" class="btn btn-outline-success">Verify</button>
</form>
</div>
</div>
<script
src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"
integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM"
crossorigin="anonymous"
></script>
</body>
</html>
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit e133134

Please sign in to comment.