0% found this document useful (0 votes)
15 views3 pages

Untitled 0

The document outlines a Python script for driver monitoring using a pre-trained neural network model. It includes functions for loading and preprocessing image data, generating a confusion matrix, testing random images, and applying various image transformations. The script utilizes libraries such as TensorFlow, OpenCV, and Matplotlib for model handling and visualization.

Uploaded by

rameshsv06
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views3 pages

Untitled 0

The document outlines a Python script for driver monitoring using a pre-trained neural network model. It includes functions for loading and preprocessing image data, generating a confusion matrix, testing random images, and applying various image transformations. The script utilizes libraries such as TensorFlow, OpenCV, and Matplotlib for model handling and visualization.

Uploaded by

rameshsv06
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
You are on page 1/ 3

import os

import cv2
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from tensorflow.keras.models import Sequential, load_model
from tensorflow.keras.layers import Flatten, Dense, Dropout
from tensorflow.keras.preprocessing.image import ImageDataGenerator, img_to_array, load_img
from tensorflow.keras.callbacks import EarlyStopping, ReduceLROnPlateau
from tensorflow.keras.applications import VGG16
from sklearn.metrics import confusion_matrix, classification_report
import random

# Set dataset paths


train_data_dir = '/home/rameshkumar/task_phytec/driver_monitoring/dataset_new/train'
test_data_dir = '/home/rameshkumar/task_phytec/driver_monitoring/dataset_new/test'

# Define only the classes we want


class_labels = ['0_Open', '1_Closed'] # Ignore 2 and 3

# Load model
model = load_model('model_eye.h5')

# Model Summary
def print_model_summary():
model.summary()

# Load test dataset


test_datagen = ImageDataGenerator(rescale=1./255)
test_generator = test_datagen.flow_from_directory(
test_data_dir,
target_size=(64, 64),
batch_size=32,
class_mode='sparse',
shuffle=False,
classes=class_labels
)

# Generate Confusion Matrix


def plot_confusion_matrix():
y_true = test_generator.classes
y_pred = model.predict(test_generator)
y_pred_classes = np.argmax(y_pred, axis=1)
cm = confusion_matrix(y_true, y_pred_classes)

plt.figure(figsize=(6, 5))
sns.heatmap(cm, annot=True, fmt='d', cmap='Blues', xticklabels=class_labels,
yticklabels=class_labels)
plt.xlabel('Predicted')
plt.ylabel('Actual')
plt.title('Confusion Matrix')
plt.show()
print("Classification Report:\n", classification_report(y_true, y_pred_classes,
target_names=class_labels))

# Test on Random Images


def test_random_images():
image_paths = random.sample([os.path.join(test_data_dir, class_name, f)
for class_name in class_labels
for f in os.listdir(os.path.join(test_data_dir, class_name))], 5)

for img_path in image_paths:


img = load_img(img_path, target_size=(64, 64))
img_array = img_to_array(img) / 255.0
img_array = np.expand_dims(img_array, axis=0)

prediction = model.predict(img_array)
predicted_class = class_labels[np.argmax(prediction)]

plt.imshow(img)
plt.title(f'Predicted: {predicted_class}')
plt.axis('off')
plt.show()

# Image Transformations
def apply_transformations(image_path):
img = cv2.imread(image_path)
img = cv2.resize(img, (64, 64))

transformations = {
'Original': img,
'Grayscale': cv2.cvtColor(img, cv2.COLOR_BGR2GRAY),
'HSV': cv2.cvtColor(img, cv2.COLOR_BGR2HSV),
'YCrCb': cv2.cvtColor(img, cv2.COLOR_BGR2YCrCb),
'Negative': cv2.bitwise_not(img),
'Red Channel': img[:, :, 2],
'Green Channel': img[:, :, 1],
'Blue Channel': img[:, :, 0]
}

plt.figure(figsize=(12, 8))
for i, (title, transformed_img) in enumerate(transformations.items(), 1):
plt.subplot(3, 3, i)
if len(transformed_img.shape) == 2:
plt.imshow(transformed_img, cmap='gray')
else:
plt.imshow(cv2.cvtColor(transformed_img, cv2.COLOR_BGR2RGB))
plt.title(title)
plt.axis('off')
plt.tight_layout()
plt.show()

# Run functions
print_model_summary()
plot_confusion_matrix()
test_random_images()
apply_transformations(random.choice(os.listdir(test_data_dir)))

You might also like