
Bank Note Authentication in Python with Streamlit
16 May, 2023
0
0
0
Contributors
Introduction
Bank note authentication is critical in ensuring the security and integrity of financial transactions. With the advancement of technology, the need for efficient and accurate methods of verifying the authenticity of banknotes has become increasingly important. In this article, we will explore how to build a bank note authentication system using python and streamlit.
Python, a versatile and popular programming language, provides many tools and libraries for machine learning and data analysis. Streamlit, on the other hand, is a user-friendly framework that allows us to create interactive web applications with minimal code quickly. By combining these two powerful tools, we can develop a user interface for bank note authentication that is both intuitive and efficient.
The goal of this article is to guide you through the process of building a bank note authentication system step-by-step. We will start by discussing the importance of bank note authentication and its associated challenges. Next, we will explore the dataset used for training our model and provide an overview of the machine learning techniques involved.
We will then dive into the implementation details, covering data preparation, model training, and evaluation. We will use python's machine learning libraries, such as scikit-learn, to build and train a classification model capable of distinguishing between real and fake bank notes.
Once our model is trained and ready, we will integrate it into a streamlit web application. Streamlit's simplicity and interactivity will allow us to create an intuitive user interface where users can input bank note features and receive instant predictions about their authenticity. Machine learning algorithms learn from datasets. Therefore, to determine whether a banknote is real or not, we need a dataset of real and fake bank notes and their different features. You can access the dataset used for this project here. Throughout the article, I will provide code snippets and explanations to help you understand the concepts and techniques involved.
By the end of this article, you will have a solid understanding of how to build a bank note authentication system using python and streamlit.
So, let's dive into the fascinating world of bank note authentication and unleash the power of python and streamlit to create a robust and user-friendly application.
Prerequisites required
- Python installed on your system
- Jupyter notebook on your system
- Visual studio on your system
Importance of bank note authentication
- Protecting the economy: Authentic banknotes maintain trust in the currency system, ensuring the economy's stability. Fake money can disrupt financial markets, lead to inflation, and cause financial losses for individuals, businesses, and governments.
- Safeguarding businesses and individuals: Banknote authentication helps businesses and individuals avoid accepting fake banknotes, protecting them from financial losses. It ensures that transactions are conducted with genuine currency, reducing the risk of fraud.
- Preserving public trust: Banknote authentication measures assure the public that their currency is legitimate, fostering confidence in the monetary system. This trust is essential for maintaining a functioning economy and encouraging economic activity.
Challenges associated with bank note authentication
- Advancements in counterfeiting techniques: Fraudsters continually develop sophisticated methods to replicate banknotes, making it challenging to detect fake currency. They may use high-quality printing techniques, security features, or advanced materials that mimic genuine banknotes.
- Detection complexity: Authenticating banknotes requires expertise and specialized equipment. Some security features, such as holograms, UV inks, or watermarks, may require specific tools or knowledge to detect accurately. The complexity of detection methods can pose a challenge for individuals or businesses without access to advanced authentication resources.
- Time and cost implications: Verifying banknotes manually can be time-consuming and costly, especially for businesses that handle large volumes of cash. Automating the authentication process with technology can help overcome this challenge, but it requires investment in equipment and training.
- Education and awareness: Ensuring widespread knowledge and awareness of banknote security features is essential. A lack of education about authenticating banknotes can make individuals and businesses more susceptible to accepting fake currency unknowingly.
We need to prepare our data. We will use the banknote authentication data Set from the UCI Machine Learning Repository. This dataset contains 1372 samples of banknote images that have been processed to extract features such as variance, entropy, skewness, and curtosis. Each sample is labelled as either real or fake.
We will use a random forest model to classify the banknotes as real or fake. We will train the model using 70% of the dataset and test it using the remaining 30%.
Let's get started
Importing the necessary libraries
import pandas as pd
import pickle
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score
Data Preparation
We will load the bank note dataset and split it into training and testing datasets.
# Load the dataset
def load_data():
data = pd.read_csv("Banknote_Authentication.csv")
return data
banknote_data = load_data()
banknote_data.head()
# Split the data into features and labels
X = banknote_data.drop("class", axis=1)
y = banknote_data["class"]
# Split the data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
Training the model
# Train a random forest classifier
rf_classifier = RandomForestClassifier(n_estimators=100)
#fit model to the training data
rf_classifier.fit(X_train, y_train)
Evaluating the model
After training our random forest model, we will evaluate its performance on the testing dataset. We will make predictions on our testing data and compute the accuracy of our model.
#make predictions on the testing data
y_pred = rf_classifier.predict(X_test)
#compute the accuracy of the model
accuracy = accuracy_score(y_test, y_pred)
print(f" Accuracy: {accuracy}")
Integrating random forest classification model in streamlit
Finally, we will integrate our model into streamlit.
Let's define the web app using streamlit. We set the page title and icon and added a title to the page. We then define input fields for the user to input the variance, skewness, curtosis, and entropy of a banknote.
Finally, we add a button for the user to click to authenticate the banknote. When the button is clicked, the input data is used to make a prediction using the random forest classifier, and the result is displayed to the user using the write method of the st
object.
import streamlit as st
from my_utils import predict
st.set_page_config(page_title = 'Bank Note Authentication App', page_icon = ':money_with_wings:')
st.title("Bank Note Authentication")
variance = st.number_input("Variance")
skewness = st.number_input("Skewness")
curtosis = st.number_input("curtosis")
entropy = st.number_input("Entropy")
#Make a prediction
if st.button("Authenticate", key = 'submit_button'):
X_test = [variance, skewness, curtosis, entropy]
prediction = predict(X_test = X_test)
st.write(f"The banknote is {'real' if prediction == 0 else 'fake'}")
Then you write the following command on your terminal
streamlit run app.py
You should see something like this
And that is it!
FAQs
- What is Bank note authentication? Bank note authentication is the process of determining if a banknote is real or fake.
- How can I authenticate a banknote using python with streamlit? You can authenticate a banknote using python with streamlit by building a machine learning model that can classify the banknote as genuine or fake based on specific features such as variance, skewness, curtosis, and entropy.
- What libraries do I need to use python with streamlit for bank note authentication? You need libraries such as pandas, numpy, scikit-learn, and streamlit to build the machine learning model and create the user interface with Streamlit.
- How accurate is the bank note authentication model built with python and streamlit? The model's accuracy depends on several factors such as the quality and quantity of the training data, the choice of features, and the machine learning algorithm used. However, with proper data preprocessing, feature selection, and algorithm tuning, you can achieve high accuracy in banknote authentication.
- What are the benefits of using python with streamlit for bank note authentication? Python with streamlit provides a user-friendly and interactive interface for banknote authentication. It also allows for quick prototyping and testing of machine learning models.
- Can I use the bank note authentication model built with python and streamlit in production? Yes, you can use the model in production after thorough testing and validation. However, you must ensure the model is robust and accurate enough to handle real-world scenarios.
Conclusion
We have demonstrated how to build a bank note authentication web application using python and streamlit. Our application uses a machine learning model to classify bank notes as real or fake based on their features.
We have explained the steps in building the model, including data preprocessing, model training, and evaluation. We have also discussed deploying the model using streamlit, an open-source Python library for building web applications.
Our application provides a user-friendly interface for users to upload images of bank notes and get the prediction of the model. We have shown that our model achieves high accuracy in classifying the bank notes and is suitable for practical applications.
Overall, our project demonstrates the power and flexibility of python and streamlit for building machine learning applications. With further development, this application could be used in real-world scenarios to help prevent financial fraud and improve security in the banking industry.
Thank you for reading.