├── src ├── __init__.py ├── utils │ ├── __init__.py │ ├── utils.py │ └── metrics.py ├── core │ ├── __init__.py │ ├── train.py │ ├── preprocess.py │ ├── segment.py │ └── features.py ├── train_test.py └── extract_gui.py ├── .github ├── FUNDING.yml └── ISSUE_TEMPLATE │ └── bug_report.md ├── res └── drawable │ └── bg.png ├── models └── decision-tree.pkl ├── requirements.txt ├── .gitignore ├── LICENSE ├── _config.yml └── README.md /src/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | github: saifkhichi96 2 | custom: paypal.me/saifkhichi06 3 | -------------------------------------------------------------------------------- /res/drawable/bg.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/saifkhichi96/signature-extraction/HEAD/res/drawable/bg.png -------------------------------------------------------------------------------- /src/utils/__init__.py: -------------------------------------------------------------------------------- 1 | from .metrics import jaccard_score, f1_score 2 | from .utils import list_images 3 | -------------------------------------------------------------------------------- /models/decision-tree.pkl: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/saifkhichi96/signature-extraction/HEAD/models/decision-tree.pkl -------------------------------------------------------------------------------- /src/core/__init__.py: -------------------------------------------------------------------------------- 1 | from .features import extract_features 2 | from .segment import extract_signature, extract_signatures 3 | from .train import train 4 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | joblib~=1.0.0 2 | numpy~=1.21.5 3 | opencv-contrib-python<=3.4.2.17 4 | Pillow~=9.0.0 5 | scikit-learn~=1.0.2 6 | scipy~=1.7.3 7 | tqdm~=4.62.3 8 | tk~=0.1.0 9 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Ignore IntelliJ IDEA project files 2 | .idea/ 3 | 4 | # Ignore dataset files 5 | data/ 6 | 7 | # Ignore compiled binary files 8 | build/ 9 | out/ 10 | 11 | # Ignore compiled Python files and virtual environments 12 | __pycache__/ 13 | *.pyc 14 | venv/ 15 | 16 | # Ignore log files 17 | logs/ 18 | 19 | # Ignore OS cache files 20 | .DS_Store 21 | ._* 22 | -------------------------------------------------------------------------------- /src/utils/utils.py: -------------------------------------------------------------------------------- 1 | import os 2 | 3 | 4 | def list_images(path, formats=['jpeg', 'jpg', 'png', 'tif', 'tiff']): 5 | """Lists all images in a directory (including sub-directories). 6 | 7 | Images in JPG, PNG and TIF format are listed. 8 | """ 9 | images = [] 10 | for f in os.listdir(path): 11 | fn = os.path.join(path, f) 12 | if os.path.isdir(fn): 13 | images += list_images(fn) 14 | else: 15 | ext = f.split('.')[-1] 16 | for format in formats: 17 | if ext.lower() == format.lower(): 18 | images.append(fn) 19 | break 20 | 21 | return sorted(images) 22 | -------------------------------------------------------------------------------- /src/train_test.py: -------------------------------------------------------------------------------- 1 | from core import extract_features, extract_signatures, train 2 | from utils import jaccard_score, f1_score 3 | 4 | train_data = 'data/train/' 5 | valid_data = 'data/test/y/' 6 | 7 | feats = 'out/features/' 8 | tmp = 'out/temp/' 9 | model = 'out/models/decision-tree.pkl' 10 | 11 | # Extract features, train model and segment test data 12 | extract_features(train_data, preprocess=False, out_dir=feats) 13 | train(feats, model) 14 | extract_signatures(valid_data, tmp, model) 15 | 16 | # Calculate accuracy of segmentation 17 | predictions = f'out/temp/' 18 | groundtruth = 'data/test/y_true' 19 | dice = f1_score(predictions, groundtruth) 20 | iou = jaccard_score(predictions, groundtruth) 21 | 22 | print('F1 Score: %.02f%%' % (dice * 100)) 23 | print('Jaccard Index: %.02f%%' % (iou * 100)) 24 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/bug_report.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Bug report 3 | about: Create a report to help us improve 4 | title: '' 5 | labels: '' 6 | assignees: '' 7 | 8 | --- 9 | 10 | **Describe the bug** 11 | A clear and concise description of what the bug is. 12 | 13 | **To Reproduce** 14 | Steps to reproduce the behavior: 15 | 1. Go to '...' 16 | 2. Click on '....' 17 | 3. Scroll down to '....' 18 | 4. See error 19 | 20 | **Expected behavior** 21 | A clear and concise description of what you expected to happen. 22 | 23 | **Screenshots** 24 | If applicable, add screenshots to help explain your problem. 25 | 26 | **Desktop (please complete the following information):** 27 | - OS: [e.g. iOS] 28 | - Browser [e.g. chrome, safari] 29 | - Version [e.g. 22] 30 | 31 | **Smartphone (please complete the following information):** 32 | - Device: [e.g. iPhone6] 33 | - OS: [e.g. iOS8.1] 34 | - Browser [e.g. stock browser, safari] 35 | - Version [e.g. 22] 36 | 37 | **Additional context** 38 | Add any other context about the problem here. 39 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Muhammad Saif Ullah Khan 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /src/utils/metrics.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | 4 | from .utils import list_images 5 | 6 | 7 | def iou(y_pred, y_true): 8 | intersection = np.count_nonzero(cv2.bitwise_and(y_pred, y_true)) 9 | union = np.count_nonzero(cv2.bitwise_or(y_pred, y_true)) 10 | return intersection / union 11 | 12 | 13 | def dice(y_pred, y_true): 14 | a = iou(y_pred, y_true) 15 | return 2 * a / (a + 1) 16 | 17 | 18 | def jaccard_score(predictions, groundtruth): 19 | y_preds = list_images(predictions) 20 | y_trues = list_images(groundtruth) 21 | if len(y_preds) != len(y_trues): 22 | raise Exception() 23 | 24 | score = [] 25 | for i, y_pred in enumerate(y_preds): 26 | y_pred = cv2.imread(y_pred, 0) 27 | y_true = cv2.imread(y_trues[i], 0) 28 | score.append(iou(y_pred, y_true)) 29 | 30 | return np.mean(score) 31 | 32 | 33 | def f1_score(predictions, groundtruth): 34 | y_preds = list_images(predictions) 35 | y_trues = list_images(groundtruth) 36 | if len(y_preds) != len(y_trues): 37 | raise Exception() 38 | 39 | score = [] 40 | for i, y_pred in enumerate(y_preds): 41 | y_pred = cv2.imread(y_pred, 0) 42 | y_true = cv2.imread(y_trues[i], 0) 43 | score.append(dice(y_pred, y_true)) 44 | 45 | return np.mean(score) 46 | -------------------------------------------------------------------------------- /_config.yml: -------------------------------------------------------------------------------- 1 | remote_theme: saifkhichi96/simple-material 2 | logo: /../assets/images/logo.png 3 | 4 | breadcrumbs: true 5 | navigation_footer: true 6 | 7 | # ----------------------------------------------------------------------------- 8 | # Site Navigation 9 | # ----------------------------------------------------------------------------- 10 | menu_main: 11 | - title: Home 12 | icon: roofing 13 | url: /../ 14 | - title: About Me 15 | icon: contact_page 16 | url: /../resume/ 17 | - title: My Services 18 | icon: design_services 19 | url: /../services/ 20 | - title: Coding Projects 21 | icon: cases 22 | url: /../projects/ 23 | - title: Research Work 24 | icon: science 25 | url: /../research/ 26 | - title: 27 | - title: Contact 28 | url: /../contact/ 29 | 30 | 31 | menu_social: 32 | email: saifkhichi96@gmail.com 33 | github: saifkhichi96 34 | linkedin: saifkhichi96 35 | stackoverflow: 4690466 36 | youtube: /channel/UCj4yPuKHi9pZ9WbKbfh5MEQ 37 | playconsole: 7722501168179136426 38 | 39 | 40 | navigation_footer: true 41 | breadcrubms: true 42 | installable: true 43 | -------------------------------------------------------------------------------- /src/core/train.py: -------------------------------------------------------------------------------- 1 | import os 2 | 3 | import joblib 4 | import numpy as np 5 | from sklearn.tree import DecisionTreeClassifier 6 | 7 | 8 | def load_dataset(dataset): 9 | """Loads dataset from given path. 10 | 11 | Parameters: 12 | dataset (str) : Path of the dataset. 13 | 14 | Returns: 15 | The dataset as (data, labels) tuple, where 'data' is the feature array 16 | and 'labels' is the label vector. 17 | """ 18 | ext = '.npy' 19 | classes = [x.split('.')[0] for x in os.listdir(dataset) if x.lower().endswith(ext)] 20 | 21 | data = [] 22 | labels = [] 23 | for idx, cls in enumerate(sorted(classes)): 24 | data = np.load(os.path.join(dataset, f'{cls}{ext}')) 25 | data.append(data) 26 | 27 | labels = np.ones((data.shape[0], 1)) * idx 28 | labels.append(labels) 29 | 30 | data = np.vstack(data) 31 | labels = np.vstack(labels).ravel() 32 | return data, labels 33 | 34 | 35 | def train(dataset, outfile): 36 | """Trains a classifier on the given dataset. 37 | 38 | A Decision Tree classifier with entropy criterion is trained on the dataset 39 | to distinguish between signature and non-signature components. Optionally, 40 | the trained model can also be saved to a file. 41 | 42 | Parameters: 43 | dataset (str) : Path of the dataset. This folder must contain extracted 44 | features for each class in the dataset, saved as 45 | separate .npy files. 46 | outfile (str) : Optional. Save path for the trained model. 47 | 48 | Returns: 49 | The classifier trained on given dataset. 50 | """ 51 | # Load the dataset 52 | print('Reading the dataset... ') 53 | data, labels = load_dataset(dataset) 54 | assert data.shape[0] == labels.shape[0] 55 | print(f'{data.shape[0]} samples found\n') 56 | 57 | # Train a decision tree classifier 58 | print(f'Training the classifier...') 59 | clf = DecisionTreeClassifier(criterion='entropy') 60 | clf.fit(data, labels) 61 | 62 | if outfile is not None: 63 | print(f'Saving trained model...') 64 | out_dir = os.path.dirname(outfile) 65 | if not os.path.exists(out_dir): 66 | os.makedirs(out_dir) 67 | 68 | joblib.dump(clf, outfile) 69 | 70 | return clf 71 | -------------------------------------------------------------------------------- /src/core/preprocess.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | 4 | 5 | def remove_lines_x(im): 6 | lines = [] 7 | 8 | h, w = im.shape 9 | columns = range(w) 10 | rows = range(h) 11 | 12 | for r in rows: # For each horizontal scan line 13 | mean = np.mean(im[r, columns]) # calculate pixel density (i.e. 14 | pxdt = mean / 255.0 # percentage of white pixels) 15 | 16 | if pxdt > 0.25: # For >25% white pixels, we make the 17 | lines.append(r) # whole scan line (FIXME: Should be 18 | im[r, columns] = 0 # the actual line only) black. 19 | 20 | return im, lines 21 | 22 | 23 | def remove_lines_y(im): 24 | lines = [] 25 | 26 | h, w = im.shape 27 | columns = range(w) 28 | rows = range(h) 29 | 30 | for c in columns: # For each vertical scan line 31 | mean = np.mean(im[rows, c]) # calculate pixel density (i.e. 32 | pxdt = mean / 255.0 # percentage of white pixels) 33 | 34 | if pxdt > 0.25: # For >25% white pixels, we make the 35 | lines.append(c) # whole scan line (FIXME: Should be 36 | im[rows, c] = 0 # the actual line only) black. 37 | 38 | return lines 39 | 40 | 41 | def remove_lines(image, kernel=(25, 25)): 42 | # Make a copy (original image will be needed later) 43 | copy = np.copy(image) 44 | 45 | # Remove all lines (horizontal and vertical) 46 | x_lines = remove_lines_x(copy) 47 | y_lines = remove_lines_y(copy) 48 | 49 | # Remove noise (removes any parts of lines not removed) 50 | blurred = cv2.GaussianBlur(copy, kernel, 0) 51 | ret3, copy = cv2.threshold(blurred, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU) 52 | 53 | # Guassian filtering for noise removal thickens all strokes 54 | # and filling can sometimes color pixels which were unfilled 55 | # in original image. These side effects are reversed by 56 | # taking an intersection of the processed image with the 57 | # original image 58 | return cv2.bitwise_and(copy, image) 59 | 60 | 61 | def threshold(im): 62 | blur = cv2.GaussianBlur(im, (25, 25), 0) 63 | im_bin_1 = cv2.adaptiveThreshold(blur, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV, 121, 7) 64 | im_bin_2 = cv2.adaptiveThreshold(im, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV, 35, 2) 65 | return np.bitwise_and(im_bin_1, im_bin_2) 66 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | --- 2 | layout: project 3 | categories: Machine Learning 4 | code: saifkhichi96/signature-extraction 5 | demo: mSPeYTF9J4Q 6 | drawer: true 7 | permalink: /index.html 8 | --- 9 | 10 | # Extracting Signatures from Bank Checks 11 | 12 | ## Introduction 13 | Ahmed et al. [1] suggest a connected components-based method for segmenting signatures in document images. For this purpose, they train and evaluate their models on Tobacco-800 documents dataset to extract patch-level signatures. 14 | 15 | In this project, we have taken their proposal and modified it to work specifically for bank checks, and be able to segment signatures on a stroke level instead of patch level. Both these are inherently more difficult problems. 16 | 17 | Bank checks have complex backgrounds which are not known beforehand. This was not the case in Tobacco-800 dataset where the documents have a simple white background. Similarly, extracting stroke-level position of the signature is again a more difficult task compared to patch level location. 18 | 19 | ## Demo 20 | You can watch a demo of the results in the following [Youtube video](https://www.youtube.com/watch?v=mSPeYTF9J4Q)
21 | [![Demo Video](https://img.youtube.com/vi/mSPeYTF9J4Q/0.jpg)](https://www.youtube.com/watch?v=mSPeYTF9J4Q) 22 | 23 | ## Datasets 24 | One reason why [1] worked on patch-level was a lack of publicly available datasets with groundtruth at stroke-level. In this project, we have created our own dataset of bank checks with stroke level groundtruth available as binary segmentation masks. We use this dataset for testing. 25 | 26 | We divide our training data into two classes: `signatures` and `non-signature`. For `non-signature` class, we took 64 documents from Tobacco-800 [3, 4] to manually generate new images which which didn't contain any signatures or handwritten text. Some of these documents contained logos, as well. For the `signature` class, we combined all images from the UTSig signature database [2], which contains 8280 Persian language signatures. 27 | 28 | ## Installation 29 | Clone the project and `cd` to project directory. Then, execute the following commands once to set-up your environment 30 | ``` 31 | python3 -m venv venv/ 32 | source venv/bin/activate 33 | pip install -r requirements.txt 34 | ``` 35 | 36 | Then you can run the GUI app with `python3 src/extract_gui.py`. 37 | 38 | The repository comes with trained models in the `models/` folder. To train a new model with your own data, look at the [train_test.py](./src/train_test.py) script. 39 | 40 | ## References 41 | 1. Ahmed, S., Malik, M. I., Liwicki, M., & Dengel, A. (2012, September). Signature segmentation from document images. In 2012 International Conference on Frontiers in Handwriting Recognition (pp. 425-429). IEEE. 42 | 43 | 2. Soleimani, A., Fouladi, K., & Araabi, B. N. (2016). UTSig: A Persian offline signature dataset. IET Biometrics, 6(1), 1-8. 44 | 45 | 3. Zhu, G., & Doermann, D. (2007, September). Automatic document logo detection. In Ninth International Conference on Document Analysis and Recognition (ICDAR 2007) (Vol. 2, pp. 864-868). IEEE. 46 | 47 | 4. Zhu, G., Zheng, Y., Doermann, D., & Jaeger, S. (2007, June). Multi-scale structural saliency for signature detection. In 2007 IEEE Conference on Computer Vision and Pattern Recognition (pp. 1-8). IEEE. 48 | -------------------------------------------------------------------------------- /src/core/segment.py: -------------------------------------------------------------------------------- 1 | import os 2 | 3 | import cv2 4 | import joblib 5 | import numpy as np 6 | from tqdm import tqdm 7 | 8 | from utils import list_images 9 | from .features import get_components 10 | 11 | 12 | def extract_signature(img, clf, preprocess=True): 13 | """Segments signatures in an image. 14 | 15 | This function receives a grayscale image as input, from which all connected 16 | components are extracted and then classified as either a signature or not. 17 | A mask image of same size as the input image is generated, with signature 18 | pixels in white (intensity 255) and background in black (intensity 0). 19 | 20 | Parameters: 21 | img : the image to extract signature from 22 | clf : the classifier to use for predicting component class 23 | preprocess : whether to apply additional preprocessing to the image 24 | 25 | Returns: 26 | the segmentation mask with signature pixels in white 27 | """ 28 | # Extract SURF features of connected components 29 | components = get_components(img, preprocess) 30 | 31 | # Classify each component as signature/background 32 | mask = np.zeros(img.shape, np.uint8) 33 | for (descriptors, idx) in components: 34 | # A component may have multiple descriptors. Classify each 35 | # of them separately. 36 | n_descriptors = descriptors.shape[0] 37 | predictions = np.zeros(n_descriptors) 38 | for i in range(n_descriptors): 39 | predictions[i] = clf.predict(descriptors[i].reshape(1, -1)) 40 | 41 | # Component is signature if at least 50% of the descriptors 42 | # are classified as signature. 43 | n_votes = len(predictions) 44 | n_yes_v = n_votes - np.count_nonzero(predictions) 45 | confidence = n_yes_v / n_votes 46 | if 0.5 < confidence < 0.99: 47 | mask[idx] = 255 48 | 49 | return mask 50 | 51 | 52 | def extract_signatures(dataset, out_dir, model, preprocess=True, use_color=False): 53 | """Extracts signatures from all images in the dataset. 54 | 55 | Segmentation masks for each input image are generated and saved in the 56 | specified directory. 57 | 58 | Parameters: 59 | dataset (str) : Path of the test dataset. 60 | out_dir (str) : Path to save the segmentation output. 61 | model (str) : Path of the extraction model to use. 62 | preprocess (bool) : Optional. Additional preprocessing happens before 63 | feature extraction if True. Default is True. 64 | use_color (bool) : Optional. Whether to use color images. Default is 65 | False. 66 | """ 67 | # Load extraction model 68 | print("Loading segmentation model...") 69 | clf = joblib.load(model) 70 | 71 | # Get list of input files 72 | images = list_images(dataset) 73 | print("Found", len(images), "images. Starting segmentation...") 74 | 75 | # Create output directory if doesn't already exist 76 | if not os.path.exists(out_dir): 77 | os.makedirs(out_dir) 78 | 79 | if not os.path.exists(out_dir + "/masks/"): 80 | os.makedirs(out_dir + "/masks/") 81 | 82 | for image_f in tqdm(images): 83 | if use_color: 84 | im = cv2.imread(image_f) 85 | b, g, r = cv2.split(im) 86 | # mask_r = extract_signature(r, clf, preprocess) 87 | mask = extract_signature(g, clf, preprocess) 88 | # mask_b = extract_signature(b, clf, preprocess) 89 | # mask = OR(OR(AND(mask_r, mask_g), AND(mask_g, mask_b)), AND(mask_b, mask_r)) 90 | else: 91 | im = cv2.imread(image_f, 0) 92 | mask = extract_signature(im, clf, preprocess) 93 | 94 | outfile = os.path.split(image_f)[1] 95 | outfile = os.path.splitext(outfile)[0] + ".png" 96 | outfile = os.path.join(out_dir, outfile) 97 | cv2.imwrite(outfile, mask) 98 | 99 | overlay = np.copy(im) 100 | overlay[np.where(mask != 0)] = (0, 0, 255) 101 | 102 | points = np.argwhere(mask != 0) # find where the black pixels are 103 | points = np.fliplr(points) # store them in x,y coordinates instead of row,col indices 104 | x, y, w, h = cv2.boundingRect(points) # create a rectangle around those points 105 | cv2.rectangle(overlay, (x, y), (x + w, y + h), (0, 255, 0), 2) 106 | 107 | outfile = os.path.split(image_f)[1] 108 | outfile = os.path.splitext(outfile)[0] + ".png" 109 | outfile = os.path.join(out_dir + "/masks/", outfile) 110 | cv2.imwrite(outfile, overlay) 111 | -------------------------------------------------------------------------------- /src/extract_gui.py: -------------------------------------------------------------------------------- 1 | # import the necessary packages 2 | import time 3 | import tkinter as tk 4 | from tkinter import filedialog 5 | 6 | import cv2 7 | import joblib 8 | import numpy as np 9 | from PIL import Image, ImageTk 10 | 11 | from core import extract_signature 12 | 13 | 14 | def resize(image, size): 15 | w, h = image.size 16 | if w == 0 or h == 0: 17 | return Image.fromarray(np.ones(size) * 255.0) 18 | 19 | _w, _h = size 20 | if w > h: 21 | h = int(h * float(_w) / w) 22 | w = int(_w) 23 | else: 24 | w = int(w * float(_h) / h) 25 | h = int(_h) 26 | image = image.resize((w, h), Image.ANTIALIAS) 27 | 28 | max_w, max_h = size 29 | img_w, img_h = image.size 30 | 31 | img = np.array(image) 32 | canvas = np.ones(shape=(max_h, max_w, 3), dtype=img.dtype) * 255 33 | x = int((max_w - img_w) / 2) 34 | y = int((max_h - img_h) / 2) 35 | 36 | canvas[y:y + img_h, x:x + img_w, :] = img[0:img_h, 0:img_w, :] 37 | return Image.fromarray(canvas) 38 | 39 | 40 | def detect_signature(): 41 | global app 42 | 43 | if len(app.current_file) > 0: 44 | clf = app.model 45 | 46 | app.status("Extracting signature...") 47 | start_time = time.time() 48 | 49 | im = cv2.imread(app.current_file, 0) 50 | mask = extract_signature(im, clf, preprocess=True) 51 | 52 | im = cv2.imread(app.current_file) 53 | im[np.where(mask == 255)] = (0, 0, 255) 54 | 55 | # Draw bounding box on image 56 | points = np.argwhere(mask == 255) # find where the black pixels are 57 | points = np.fliplr(points) # store them in x,y coordinates instead of row,col indices 58 | x, y, w, h = cv2.boundingRect(points) # create a rectangle around those points 59 | cv2.rectangle(im, (x, y), (x + w, y + h), (0, 255, 0), 2) 60 | 61 | im = cv2.cvtColor(im, cv2.COLOR_BGR2RGB) 62 | app.show(im, app.input_view) 63 | app.status("Done in %.2fs." % (time.time() - start_time)) 64 | 65 | 66 | def open_image(): 67 | global app 68 | 69 | # open a file chooser dialog and allow the user to select an input image 70 | current_file = filedialog.askopenfilename() 71 | 72 | # ensure a file path was selected 73 | if len(current_file) > 0: 74 | app.status("Opening " + current_file.split("/")[-1] + "...") 75 | app.current_file = current_file 76 | 77 | # Open and display selected image 78 | src = cv2.imread(app.current_file) 79 | src = cv2.cvtColor(src, cv2.COLOR_BGR2RGB) 80 | app.show(src, app.input_view) 81 | app.status("Step 2: Detect Signature") 82 | 83 | 84 | class SignatureExtractor: 85 | 86 | def __init__(self): 87 | self.__root = tk.Tk() 88 | self.__root.configure(background="white") 89 | self.__root.title("Signature Extractor") 90 | self.__root.resizable(width=False, height=False) 91 | self.__root.geometry('{}x{}'.format(960, 720)) 92 | tk.Grid.rowconfigure(self.__root, 0, weight=1) 93 | tk.Grid.columnconfigure(self.__root, 0, weight=1) 94 | self.__center() 95 | 96 | # Add a grid 97 | mainframe = tk.Frame(self.__root) 98 | mainframe.grid(rowspan=12, columnspan=4, sticky=(tk.N, tk.W, tk.E, tk.S)) 99 | tk.Grid.rowconfigure(mainframe, 0, weight=1) 100 | tk.Grid.columnconfigure(mainframe, 0, weight=1) 101 | 102 | # Create a Tkinter variable 103 | self.model = joblib.load("models/decision-tree.pkl") 104 | 105 | tk.Button(mainframe, text="Open an Image", command=open_image).grid(row=0, column=0, sticky=tk.E) 106 | tk.Button(mainframe, text="Detect Signature", command=detect_signature).grid(row=0, column=1, sticky=tk.E) 107 | 108 | # Create canvas where source image will be displayed 109 | self.input_view = tk.Label(mainframe) 110 | self.input_view.grid(row=1, column=0, columnspan=2) 111 | self.show(np.ones((100, 100)) * 255, self.input_view) 112 | 113 | self.__status = tk.Label(mainframe, text="Step 1: Open an Image") 114 | self.__status.grid(row=2, column=0, sticky=tk.W) 115 | 116 | self.current_file = "" 117 | 118 | def __center(self): 119 | self.__root.update_idletasks() 120 | w = self.__root.winfo_screenwidth() 121 | h = self.__root.winfo_screenheight() 122 | size = tuple(int(_) for _ in self.__root.geometry().split('+')[0].split('x')) 123 | x = w / 2 - size[0] / 2 124 | y = h / 2 - size[1] / 2 125 | self.__root.geometry("%dx%d+%d+%d" % (size + (x, y))) 126 | 127 | def show(self, im, target): 128 | try: 129 | im = Image.fromarray(im).convert("RGB") 130 | im = resize(im, (960, 640)) 131 | except Exception as ex: 132 | im = Image.fromarray(np.ones((960, 640)) * 255.0) 133 | 134 | im = ImageTk.PhotoImage(im) 135 | target.configure(image=im) 136 | target.image = im 137 | 138 | def status(self, text): 139 | self.__status['text'] = text 140 | 141 | def start(self): 142 | self.__root.mainloop() 143 | 144 | 145 | if __name__ == '__main__': 146 | app = SignatureExtractor() 147 | app.start() 148 | -------------------------------------------------------------------------------- /src/core/features.py: -------------------------------------------------------------------------------- 1 | import os 2 | 3 | import cv2 4 | import numpy as np 5 | from tqdm import tqdm 6 | 7 | from utils import list_images 8 | from .preprocess import remove_lines, threshold 9 | 10 | 11 | def get_components(img, preprocess=False): 12 | """Finds and describes connected components in an image as SURF features. 13 | 14 | Grana's (BBDT) 8-way connectivity algorithm is used to find all the 15 | connected components in the image. Then, 128-bit extended SURF descriptors 16 | (as detailed in [1]) with a Hessian threshold of 400 are used to describe 17 | these components. These descriptors are the feature vectors. 18 | 19 | [1] Dengel, Andreas & Ahmed, Sheraz & Malik, Muhammad Imran & Liwicki, 20 | Marcus. (2012). Signature Segmentation from Document Images. Proceedings 21 | - International Workshop on Frontiers in Handwriting Recognition, IWFHR. 22 | 10.1109/ICFHR.2012.271. 23 | 24 | Parameters: 25 | img (np.array) : A single-channel grayscale image as a numpy array. 26 | preprocess (bool) : Optional. If this is True, image is preprocessed 27 | before feature extraction. Default is False. 28 | 29 | Returns: 30 | list of feature arrays for all detected connected components, and their 31 | locations in the image 32 | """ 33 | # Binarize to get a two-color image 34 | img = threshold(img) 35 | 36 | # If specified, perform additional pre-processing steps 37 | if preprocess: 38 | # Based on our prior knowlegde that signature is usually found in the 39 | # bottom-right quarter of the bank check, make rest of image white 40 | h, w = img.shape 41 | img[:int(h / 2), :w] = 0 42 | img[:h, :int(w / 2)] = 0 43 | 44 | # Use a heuristic to try and remove horizontal lines from the image 45 | # This step is intended to remove the guidelines on bank checks 46 | img = remove_lines(img) 47 | 48 | # Find all connected components 49 | count, labels, stats, _ = cv2.connectedComponentsWithStats(img, 8, cv2.CV_32S) 50 | 51 | # For each individual component, do 52 | components = [] 53 | for idx in range(1, count): # (0 is background, so ignore) 54 | # Crop out the component 55 | x, y, w, h = stats[idx, 0], stats[idx, 1], stats[idx, 2], stats[idx, 3] 56 | component = img[y: y + h, x: x + w] 57 | 58 | # Locate pixels belonging to the component 59 | idx = np.where(labels == idx) 60 | 61 | # Extract SURF features from the component 62 | surf = cv2.xfeatures2d.SURF_create(hessianThreshold=400, 63 | nOctaves=4, 64 | nOctaveLayers=3, 65 | extended=True, 66 | upright=True) 67 | _, descriptors = surf.detectAndCompute(component, None) 68 | 69 | # Save descriptors and indices of the component 70 | if descriptors is not None: 71 | component = (np.array(descriptors), idx) 72 | components.append(component) 73 | 74 | return components 75 | 76 | 77 | def extract(path, preprocess=False): 78 | """Extract features from all images in a directory. 79 | 80 | This function should be used to extract features for a single training class 81 | in the dataset. 82 | 83 | Parameters: 84 | path (str) : Location of the input directory. 85 | preprocess (bool) : Optional. If this is True, image is preprocessed 86 | before feature extraction. Default is False. 87 | 88 | Returns: 89 | all extracted features as a Nx128 dimensional array, where N is the sum 90 | of number of all detected components in all images 91 | """ 92 | components = None 93 | for image_f in tqdm(list_images(path)): 94 | try: 95 | # Open the image in OpenCV 96 | im = cv2.imread(image_f, 0) 97 | if im is None: 98 | raise IOError(f'{image_f} could not be opened.') 99 | 100 | for descriptors, idx in get_components(im, preprocess): 101 | component = np.vstack(descriptors) 102 | if components is None: 103 | components = component 104 | else: 105 | components = np.vstack((components, component)) 106 | except Exception as e: 107 | print(e) 108 | 109 | return components 110 | 111 | 112 | def extract_features(dataset, preprocess=False, out_dir=None): 113 | """Extract features from all images in a dataset. 114 | 115 | This function should be used to extract features for all classes in a 116 | dataset. The dataset must be organized such that, the dataset directory 117 | contains one subdirectory for each class, with each of these subdirectory 118 | containing images for that class. 119 | 120 | Classes will be sorted alphabetically and assigned labels [0,K-1], where K 121 | is the number of classes. 122 | 123 | Extracted features can optionally be stored by passing in a path to save 124 | the features in `out_dir` parameter. Features will be saved as arrays in .npy 125 | files with names same as the class name. 126 | 127 | Parameters: 128 | dataset (str) : Location of the input directory. 129 | preprocess (bool) : If this is True, image is preprocessed before 130 | feature extraction. Default: False. 131 | out_dir (str|None) : Save path for the feature arrays. Default: None. 132 | 133 | Returns: 134 | The dataset as (X, y) tuple, where X is the feature array and y is the 135 | label vector. 136 | """ 137 | # Get list of all training classes 138 | classes = [x for x in os.listdir(dataset) if os.path.isdir(os.path.join(dataset, x))] 139 | print(f'Detected {len(classes)} Classes:', classes) 140 | 141 | X = [] 142 | y = [] 143 | for i, c in enumerate(sorted(classes)): 144 | # Extract features 145 | print(f'Extracting \'{c}\' features...') 146 | data = np.vstack(extract(os.path.join(dataset, c))) 147 | X.append(data) 148 | 149 | # Generate labels 150 | labels = np.ones((data.shape[0], 1)) * i 151 | y.append(labels) 152 | print(f'{data.shape[0]} feature vectors extracted') 153 | 154 | # Save features and labels if specified 155 | if out_dir is not None: 156 | print('Saving extracted features...') 157 | if not os.path.exists(out_dir): 158 | os.makedirs(out_dir) 159 | 160 | outfile = os.path.join(out_dir, f'{c}.npy') 161 | np.save(outfile, data) 162 | 163 | X = np.vstack(X) 164 | y = np.vstack(y).ravel() 165 | return X, y 166 | --------------------------------------------------------------------------------