OpenCV and Keras | Traffic Sign Classification for Self-Driving Car

OpenCV and Keras | Traffic Sign Classification for Self-Driving Car

Building a traffic sign classification model for self-driving cars involves several key steps, including data preprocessing, model training using a convolutional neural network (CNN), and evaluation. In this example, I'll use the German Traffic Sign Recognition Benchmark (GTSRB) dataset, but you can adapt the code to your own dataset if needed.

Step 1: Install Necessary Libraries

You will need opencv-python, tensorflow, keras, and related libraries.

pip install opencv-python tensorflow keras matplotlib 

Step 2: Import Libraries

import cv2 import numpy as np import matplotlib.pyplot as plt from tensorflow.keras.models import Sequential from tensorflow.keras.layers import Conv2D, MaxPooling2D, Dense, Flatten, Dropout from tensorflow.keras.utils import to_categorical from sklearn.model_selection import train_test_split 

Step 3: Load and Preprocess the Data

Load your dataset, and preprocess the images (resize, normalize, etc.). Here, I'll assume you have a function to load your dataset into X (features) and Y (labels).

# Replace with your own data loading code X, Y = load_traffic_sign_data() # This should be replaced with actual data loading # Convert and normalize images X = np.array([cv2.resize(image, (32, 32)) for image in X]) / 255.0 Y = to_categorical(Y) # One-hot encoding of labels # Split the dataset X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.2, random_state=42) 

Step 4: Define the CNN Model

model = Sequential() # Convolutional base model.add(Conv2D(32, (3, 3), activation='relu', input_shape=(32, 32, 3))) model.add(MaxPooling2D((2, 2))) model.add(Dropout(0.25)) model.add(Conv2D(64, (3, 3), activation='relu')) model.add(MaxPooling2D((2, 2))) model.add(Dropout(0.25)) model.add(Conv2D(128, (3, 3), activation='relu')) model.add(Dropout(0.4)) # Flattening and Fully Connected Layer model.add(Flatten()) model.add(Dense(128, activation='relu')) model.add(Dropout(0.3)) model.add(Dense(Y.shape[1], activation='softmax')) # Y.shape[1] should be the number of classes # Compile the model model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy']) 

Step 5: Train the Model

history = model.fit(X_train, Y_train, epochs=20, validation_data=(X_test, Y_test), batch_size=64) 

Step 6: Evaluate the Model

# Evaluate the model on test data test_loss, test_accuracy = model.evaluate(X_test, Y_test) print(f"Test accuracy: {test_accuracy}") # Plot training history plt.plot(history.history['accuracy'], label='accuracy') plt.plot(history.history['val_accuracy'], label='val_accuracy') plt.xlabel('Epoch') plt.ylabel('Accuracy') plt.ylim([0, 1]) plt.legend(loc='lower right') plt.show() 

Step 7: Save the Model (Optional)

model.save('traffic_sign_model.h5') 

Step 8: Use the Model for Real-Time Prediction (Optional)

def classify_traffic_sign(image): # Preprocess the image image = cv2.resize(image, (32, 32)) / 255.0 image = np.expand_dims(image, axis=0) # Predict the class prediction = model.predict(image) class_id = np.argmax(prediction) return class_id # Using OpenCV to open webcam and predict in real-time cap = cv2.VideoCapture(0) while True: ret, frame = cap.read() # Add preprocessing if needed # For instance, detect the traffic sign area and extract that region # ... # Predict the traffic sign in the frame class_id = classify_traffic_sign(frame) # Display the resulting frame with class name (assuming you have a mapping of class IDs to names) cv2.putText(frame, f'Class: {class_id}', (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2) cv2.imshow('frame', frame) if cv2.waitKey(1) & 0xFF == ord('q'): break cap.release() cv2.destroyAllWindows() 

This code will continuously capture frames from your webcam, classify them,


More Tags

port nodemon threadpool maps data-science create-react-native-app airflow axios maven-jaxb2-plugin on-screen-keyboard

More Programming Guides

Other Guides

More Programming Examples