Untitled 0
Untitled 0
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
# Load model
model = load_model('model_eye.h5')
# Model Summary
def print_model_summary():
model.summary()
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))
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)))