Skip to content

Commit

Permalink
Merge pull request #444 from rakheshkrishna2005/cnn-branch
Browse files Browse the repository at this point in the history
Contribute: Vehicles Classification using CNN
  • Loading branch information
UppuluriKalyani authored Oct 18, 2024
2 parents 078b377 + 64d748f commit 77979e8
Show file tree
Hide file tree
Showing 6 changed files with 922 additions and 0 deletions.
58 changes: 58 additions & 0 deletions Neural Networks/Vehicle Classifier/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# Vehicle Classifier AI

## Description

- Developed a high-performance **Vehicle Image Recognizer** web app using **deep learning techniques**.
- Designed and implemented a **Convolutional Neural Network (CNN)** with **TensorFlow** and **Keras** to classify various vehicle types.
- Integrated a **Flask** server for backend functionality, allowing users to upload vehicle images and receive real-time predictions.
- Supports recognition of vehicles including **bus, family sedan, fire engine, heavy truck, jeep, minibus, racing car, SUV, taxi, and truck**.

## Features

- **Real-Time Predictions:** Utilized Flask to manage image uploads and provide on-the-fly predictions for the type of vehicle.
- **Interactive Web App:** Designed an intuitive web interface where users can easily upload images and get immediate classification results.
- **Highly Accurate Model:** Trained the CNN on a dataset of 1,400 images for training and 200 images for validation, leading to a highly performant model.

## Tech Stack

- **Programming Language:** Python
- **Machine Learning Libraries:** TensorFlow, Keras
- **Web Framework:** Flask
- **Frontend Technologies:** HTML5, CSS3, JavaScript
- **Dataset:**
- Vehicle classification dataset from Kaggle: [Kaggle Dataset](https://www.kaggle.com/datasets/marquis03/vehicle-classification)

## Installation Instructions

`Run all the cells in jupyter, you will have vehicle_model.h5. Now follow the instructions`

1. Clone the GitHub repository:
```bash
git clone https://github.com/UppuluriKalyani/ML-Nexus.git
```

2. Navigate to the project directory:
```bash
cd Neural Networks
cd Vehicle Classifier
```

3. Install the required Python packages:
```bash
pip install -r requirements.txt
```

4. Run the Flask app:
```bash
python app.py
```

5. Open your web browser and go to `http://127.0.0.1:5000` to use the app.

## Screenshot

![Screenshot 2024-10-18 204728](https://github.com/user-attachments/assets/4706df55-48be-4aca-a606-2c7546318a00)

## Demo Video

https://github.com/user-attachments/assets/c9c5d5f9-ba2c-4969-8685-beed10b1e39a
62 changes: 62 additions & 0 deletions Neural Networks/Vehicle Classifier/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
from flask import Flask, render_template, request, redirect, url_for
from tensorflow.keras.preprocessing.image import ImageDataGenerator # type: ignore
from tensorflow.keras.models import load_model # type: ignore
from tensorflow.keras.preprocessing import image # type: ignore
import tensorflow as tf
import numpy as np
import os

app = Flask(__name__)

train_dir = 'dataset/train'

model = load_model('vehicle_model.h5')

batch_size = 32

train_datagen = ImageDataGenerator(rescale=1/255)

classes =['bus', 'family sedan', 'fire engine', 'heavy truck', 'jeep', 'minibus', 'racing car', 'SUV', 'taxi', 'truck']

train_generator = train_datagen.flow_from_directory(
train_dir,
target_size=(600, 600),
batch_size=batch_size,
classes = classes,
class_mode='categorical'
)

loaded_model = tf.keras.models.load_model('vehicle_model.h5')

def predict_vehicle_with_loaded_model(img_path):
img = image.load_img(img_path, target_size=(600, 600))
img_array = image.img_to_array(img)
img_array = np.expand_dims(img_array, axis=0)
img_array /= 255.0

predictions = loaded_model.predict(img_array)
class_idx = np.argmax(predictions[0])
class_label = list(train_generator.class_indices.keys())[class_idx]
return class_label

@app.route('/', methods=['GET', 'POST'])
def upload_file():
if request.method == 'POST':
print("Received POST request")
if 'file' not in request.files:
print("No file part in request")
return redirect(request.url)
file = request.files['file']
if file.filename == '':
print("No selected file")
return redirect(request.url)
if file:
file_path = os.path.join('static', file.filename)
print(f"Saving file to {file_path}")
file.save(file_path)
label = predict_vehicle_with_loaded_model(file_path)
return render_template('app.html', label=label, file_path=file.filename)
return render_template('app.html')

if __name__ == '__main__':
app.run(debug=True)
4 changes: 4 additions & 0 deletions Neural Networks/Vehicle Classifier/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Flask
tensorflow
numpy
keras
Loading

0 comments on commit 77979e8

Please sign in to comment.