Skip to content

jakhon37/DMS

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Driver Monitoring Systems (DMS) are designed to enhance vehicle safety by continuously monitoring the driver’s attention, alertness, and behavior. These systems aim to prevent accidents caused by driver fatigue, distraction, or inattention by providing alerts or taking corrective actions.

1. Setting Up the Jetson Nano

Hardware Requirements:

  • NVIDIA Jetson Nano Developer Kit
  • MicroSD card (32GB or larger)
  • Power supply (5V 4A recommended)
  • Monitor, keyboard, and mouse
  • USB camera (with IR capability for low-light conditions, if possible)
  • Optional: Cooling fan for the Jetson Nano

Software Requirements:

  • JetPack SDK (which includes the Linux operating system, CUDA, cuDNN, and TensorRT)
  • Python libraries: OpenCV, NumPy, etc.
  • Deep learning frameworks: TensorFlow or PyTorch

2. Initial Setup

  1. Flash the MicroSD Card:

    • Download the latest JetPack image from NVIDIA’s website.
    • Flash the image onto the MicroSD card using a tool like Etcher.
  2. Boot the Jetson Nano:

    • Insert the MicroSD card into the Jetson Nano and connect the peripherals (monitor, keyboard, mouse, and camera).
    • Power on the Jetson Nano and follow the on-screen instructions to complete the initial setup.
  3. Install Required Libraries:

    • Open a terminal and update the system:
      sudo apt-get update
      sudo apt-get upgrade
    • Install OpenCV:
      sudo apt-get install python3-opencv
    • Install other required libraries:
      sudo apt-get install python3-numpy
      pip3 install tensorflow

3. Developing the DMS

Step 1: Capture and Preprocess Video Frames

  1. Initialize the Camera:
    import cv2
    
    cap = cv2.VideoCapture(0)  # Use the correct camera index
    if not cap.isOpened():
        print("Error: Could not open video stream.")
        exit()
    
    while True:
        ret, frame = cap.read()
        if not ret:
            break
    
        # Display the resulting frame
        cv2.imshow('Driver Monitoring System', frame)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    
    cap.release()
    cv2.destroyAllWindows()

Step 2: Implement Face and Eye Detection

  1. Load Pretrained Models:

    • Use OpenCV’s Haar cascades for face and eye detection:
      face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
      eye_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_eye.xml')
  2. Detect Faces and Eyes:

    while True:
        ret, frame = cap.read()
        if not ret:
            break
    
        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        faces = face_cascade.detectMultiScale(gray, 1.3, 5)
    
        for (x, y, w, h) in faces:
            cv2.rectangle(frame, (x, y), (x+w, y+h), (255, 0, 0), 2)
            roi_gray = gray[y:y+h, x:x+w]
            roi_color = frame[y:y+h, x:x+w]
            eyes = eye_cascade.detectMultiScale(roi_gray)
            for (ex, ey, ew, eh) in eyes:
                cv2.rectangle(roi_color, (ex, ey), (ex+ew, ey+eh), (0, 255, 0), 2)
    
        cv2.imshow('Driver Monitoring System', frame)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    
    cap.release()
    cv2.destroyAllWindows()

Step 3: Integrate a Pretrained Deep Learning Model for Drowsiness Detection

  1. Load the Model:

    • Assume you have a pretrained model (e.g., for drowsiness detection using TensorFlow or PyTorch):
      from tensorflow.keras.models import load_model
      
      model = load_model('drowsiness_model.h5')
  2. Predict Drowsiness:

    • Extract the eye region and preprocess it for the model:
      import numpy as np
      
      def preprocess_eye(eye_region):
          eye_region = cv2.resize(eye_region, (24, 24))
          eye_region = eye_region / 255.0
          eye_region = np.expand_dims(eye_region, axis=0)
          eye_region = np.expand_dims(eye_region, axis=-1)
          return eye_region
      
      while True:
          ret, frame = cap.read()
          if not ret:
              break
      
          gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
          faces = face_cascade.detectMultiScale(gray, 1.3, 5)
      
          for (x, y, w, h) in faces:
              roi_gray = gray[y:y+h, x:x+w]
              eyes = eye_cascade.detectMultiScale(roi_gray)
              for (ex, ey, ew, eh) in eyes:
                  eye_region = roi_gray[ey:ey+eh, ex:ex+ew]
                  processed_eye = preprocess_eye(eye_region)
                  prediction = model.predict(processed_eye)
                  if prediction < 0.5:  # Adjust threshold as necessary
                      cv2.putText(frame, "Drowsy", (x, y-10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 0, 255), 2)
                  else:
                      cv2.putText(frame, "Alert", (x, y-10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2)
      
          cv2.imshow('Driver Monitoring System', frame)
          if cv2.waitKey(1) & 0xFF == ord('q'):
              break
      
      cap.release()
      cv2.destroyAllWindows()

4. Optimization and Deployment

  • Optimize the Model:

    • Use NVIDIA TensorRT to optimize the model for better performance on the Jetson Nano.
    • Convert the model to a TensorRT engine:
      import tensorrt as trt
      (Follow TensorRT documentation for model conversion)
  • Deployment:

    • Package your application for deployment.
    • Ensure the system can start automatically on boot if intended for a production vehicle.

5. Further Improvements

  • Improve Detection Algorithms:

    • Experiment with more advanced deep learning models for better accuracy.
    • Use more sophisticated datasets for training your models.
  • Expand Functionality:

    • Integrate with vehicle systems to provide real-time alerts.
    • Add additional monitoring features, such as detecting phone usage or other distractions.

By following these steps, you can develop a functional Driver Monitoring System using the NVIDIA Jetson Nano. This project not only enhances your understanding of AI and machine learning but also contributes to safer driving experiences.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published