Facene
Facene
import numpy as np
from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D, Dense, Dropout, Flatten,
BatchNormalization
from keras.optimizers import Adam
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from keras.callbacks import ReduceLROnPlateau
import matplotlib.pyplot as plt
import cv2
import os
# Preprocess all images (train and validation from the same directory)
train_generator = train_data_gen.flow_from_directory(
dataset_dir,
target_size=(64, 64),
batch_size=32,
color_mode="rgb",
class_mode='categorical',
subset='training', # 80% for training
shuffle=True
)
validation_generator = train_data_gen.flow_from_directory(
dataset_dir,
target_size=(64, 64),
batch_size=32,
color_mode="rgb",
class_mode='categorical',
subset='validation', # 20% for validation
shuffle=True
)
# Convolutional layers
model.add(Conv2D(32, kernel_size=(3, 3), activation='relu', input_shape=(64, 64,
3)))
model.add(BatchNormalization())
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Conv2D(64, kernel_size=(3, 3), activation='relu'))
model.add(BatchNormalization())
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
# Make prediction
prediction = model.predict(img)
predicted_class = np.argmax(prediction)
confidence = prediction[0][predicted_class]
# Return result
if confidence > 0.7: # Adjustable threshold
return class_labels[predicted_class], confidence
else:
return "Unknown", confidence"""
"""
# Example usage of prediction function
if __name__ == "__main__":
# Test the prediction function with a sample image
test_image_path = 'data/person1/sample_image.jpg' # Replace with an actual
image path
if os.path.exists(test_image_path):
name, confidence = predict_face(test_image_path)
print(f"Predicted: {name} with confidence {confidence:.2f}")
else:
print("Test image not found. Provide a valid image path to test the
prediction.")"""