This project is part of my undergraduate thesis, focusing on utilizing machine learning for image classification.
This repository contains a Flask web application that performs image classification using a pre-trained Keras model. The application allows users to upload an image and get a prediction about the freshness of the image based on the model's inference.
- Image Upload: Users can upload images in
png,jpeg, orjpgformats. - Image Classification: The application uses a Keras model to classify the uploaded image into one of four categories:
segar,tidak segar,cukup segar, orsangat tidak segar. - Dynamic Results: The prediction result is displayed along with the uploaded image on the web interface.
Before running the application, ensure you have the following installed:
- Python 3.x
- Flask
- Keras
- TensorFlow
- NumPy
You can install the dependencies using pip:
pip install flask keras tensorflow numpyapp.py: The main application script containing the Flask app.templates/index.html: The HTML template for the web interface.static/: Directory containing static files such as uploaded images and the pre-trained model (model 1.0.h5).
Convolutional Neural Networks (CNNs) are the foundation of this application. A CNN is a class of deep learning models specifically designed for image processing tasks. The core operations in a CNN include:
-
Convolution: Extracts features from the input image by applying a set of learnable filters (kernels). The convolution operation can be represented as:
$$(I * K)(x, y) = Σ Σ I(x+m, y+n) * K(m, n)$$ Where:
-
$$I$$ is the input image. -
$$K$$ is the kernel/filter. -
$$(x, y)$$ are the coordinates of the pixel. - The summation computes the dot product of the kernel with the overlapping region of the image.
-
-
Activation Function: Introduces non-linearity into the network. A common choice is the ReLU (Rectified Linear Unit) function:
$$f(x) = max(0, x)$$ -
Pooling: Reduces the spatial dimensions of the image while retaining important features. Common pooling methods include max pooling and average pooling.
-
Flattening and Dense Layers: After feature extraction, the resulting feature map is flattened into a vector and passed through fully connected (dense) layers to make the final prediction.
-
Softmax Function: Converts the final layer's output into probabilities for each class:
$$σ(z)_i = e^(z_i) / Σ e^(z_j)$$ Where z_i is the input to the softmax function for class i, and N is the total number of classes.
In this application, the uploaded image is processed and passed through a pre-trained CNN model. The model outputs a probability distribution over the predefined categories, which is interpreted and displayed as the final classification result.
- Home Page: The user is presented with a form to upload an image.
- Image Validation: The application validates the uploaded file to ensure it is in the allowed formats.
- Image Processing: The image is preprocessed to match the input requirements of the model (resize to 224x224, convert to array, and normalize).
- Prediction: The pre-trained Keras model makes a prediction on the processed image.
- Result Display: The result is displayed on the same page along with the uploaded image.
- Clone the repository:
git clone https://github.com/your-repo-url- Navigate to the project directory:
cd your-project-directory- Run the Flask application:
python app.py- Open your web browser and navigate to:
http://127.0.0.1:5000/
- Upload an image and view the prediction results.
Checks if the uploaded file has a valid extension.
GET /: Renders the home page (index.html).POST /: Handles the image upload, processes the image, and returns the prediction result.
- Load the pre-trained Keras model (
model 1.0.h5). - Preprocess the image:
- Resize to 224x224.
- Convert to a NumPy array.
- Normalize using
preprocess_input.
- Predict the class of the image.
- Map the prediction to a human-readable label (
segar,tidak segar, etc.).
- Displays a flash message if the uploaded file is not valid.
- Handles cases where the model cannot classify the image.