BeginnerComputer VisionFull notebook

Drowsiness Detection Notebook Material using TensorFlow Image Preprocessing

Notebook-based material from the uploaded drowsiness file, including TensorFlow image loading, OpenCV/PIL preprocessing and MobileNetV2 prediction outputs.

This download currently contains the uploaded notebook Drowsiness_Detection_druingVideo.ipynb. Replace it later with your final cleaned code ZIP if required.

Drowsiness Detection Notebook Material using TensorFlow Image Preprocessing

Overview

This tutorial is updated from your uploaded Drowsiness_Detection_druingVideo.ipynb file. The current notebook content mainly demonstrates image-loading methods, TensorFlow/Keras preprocessing and MobileNet/MobileNetV2 prediction rather than a complete eye-closure drowsiness pipeline. I have still placed all code cells and outputs into the drowsiness tutorial page so you can later replace or extend them with your final drowsiness-detection code and result screenshots.

Editing tip: This page is controlled from data/tutorials.ts. Change the title, text, images, Python code, YouTube link, and download link there.

What you will learn

  • Load images using IPython, Keras, OpenCV and PIL
  • Resize images to 224 x 224
  • Prepare tensors for MobileNet/MobileNetV2
  • Decode ImageNet predictions
  • Use this material as the base for a later drowsiness pipeline

Suggested tutorial flow

01

Image loading methods

Compare IPython display, Keras loader, OpenCV and PIL.

02

Image resizing

Resize image inputs to 224 x 224 for deep-learning models.

03

MobileNet/MobileNetV2 model loading

Load pretrained model weights.

04

Prediction decoding

Run prediction and decode ImageNet top classes.

05

Future drowsiness integration

Replace classification examples with eye/face drowsiness logic later.

Text explanation template

Notebook-based tutorial material

This tutorial is updated from your uploaded Drowsiness_Detection_druingVideo.ipynb file. The current notebook content mainly demonstrates image-loading methods, TensorFlow/Keras preprocessing and MobileNet/MobileNetV2 prediction rather than a complete eye-closure drowsiness pipeline. I have still placed all code cells and outputs into the drowsiness tutorial page so you can later replace or extend them with your final drowsiness-detection code and result screenshots.

How this page is organized

The page shows the full notebook workflow as tutorial sections. Each code cell is rendered as a separate Python code block. If the notebook cell produced an image, graph, screenshot or display output, it is shown under the matching code block and also in the results gallery.

How to edit this later

Open data/tutorials.ts and edit the object with this tutorial slug. Replace resultImages with your final screenshots, update codeBlocks with cleaned code, change videoUrl to the exact YouTube link, and replace the download URL with your final source-code ZIP or GitHub link.

Images, screenshots and results

Add your result screenshots, graphs, model outputs, detected objects, confusion matrices, or interface snapshots here.

Python code explanation

Use one or more code blocks. After each block, explain what the code does in simple teaching language.

Notebook cell 0: import tensorflow as tf ## pip install tensorflow

pythonexample
import tensorflow as tf  ## pip install tensorflow

This code block is extracted from the uploaded Jupyter notebook. Replace or rewrite the explanation later with your final teaching notes for this step.

Notebook cell 1: import numpy as np ## pip install numpy ### shift + enter

pythonexample
import numpy as np ## pip install numpy    ### shift  + enter

This code block is extracted from the uploaded Jupyter notebook. Replace or rewrite the explanation later with your final teaching notes for this step.

Notebook cell 2: filename = 'image_data//000031.jpg'

pythonexample
filename = 'image_data//000031.jpg'

This code block is extracted from the uploaded Jupyter notebook. Replace or rewrite the explanation later with your final teaching notes for this step.

Notebook cell 4: from IPython.display import Image ### first method to read image

pythonexample
from IPython.display import Image  ### first method to read image
Image(filename = 'image_data//000031.jpg',width= 224, height= 224)
drowsiness-detection-opencv notebook output cell 4
Notebook output from cell 4: from IPython.display import Image ### first method to read image

This code block is extracted from the uploaded Jupyter notebook. Replace or rewrite the explanation later with your final teaching notes for this step.

Notebook cell 6: from tensorflow.keras.preprocessing import image

pythonexample
from tensorflow.keras.preprocessing import image
img = image.load_img(filename, target_size = (224,224))

This code block is extracted from the uploaded Jupyter notebook. Replace or rewrite the explanation later with your final teaching notes for this step.

Notebook cell 7: import matplotlib.pyplot as plt ##conda install -c conda-forge matplotlib

pythonexample
import matplotlib.pyplot as plt  ##conda install -c conda-forge matplotlib

This code block is extracted from the uploaded Jupyter notebook. Replace or rewrite the explanation later with your final teaching notes for this step.

Notebook cell 8: plt.imshow(img)

pythonexample
plt.imshow(img)
Notebook output
<matplotlib.image.AxesImage at 0x163ff5d5f88>
drowsiness-detection-opencv notebook output cell 8
Notebook output from cell 8: plt.imshow(img)

This code block is extracted from the uploaded Jupyter notebook. Replace or rewrite the explanation later with your final teaching notes for this step.

Notebook cell 10: import cv2 ### pip install opencv-python

pythonexample
import cv2  ### pip install opencv-python

This code block is extracted from the uploaded Jupyter notebook. Replace or rewrite the explanation later with your final teaching notes for this step.

Notebook cell 11: imgg = cv2.imread(filename)

pythonexample
imgg = cv2.imread(filename)

This code block is extracted from the uploaded Jupyter notebook. Replace or rewrite the explanation later with your final teaching notes for this step.

Notebook cell 12: plt.imshow(imgg)

pythonexample
plt.imshow(imgg)
Notebook output
<matplotlib.image.AxesImage at 0x163ff8b0e48>
drowsiness-detection-opencv notebook output cell 12
Notebook output from cell 12: plt.imshow(imgg)

This code block is extracted from the uploaded Jupyter notebook. Replace or rewrite the explanation later with your final teaching notes for this step.

Notebook cell 13: imgg =cv2.resize(imgg, (224,224))

pythonexample
imgg =cv2.resize(imgg, (224,224))

This code block is extracted from the uploaded Jupyter notebook. Replace or rewrite the explanation later with your final teaching notes for this step.

Notebook cell 14: plt.imshow(imgg)

pythonexample
plt.imshow(imgg)
Notebook output
<matplotlib.image.AxesImage at 0x16382639608>
drowsiness-detection-opencv notebook output cell 14
Notebook output from cell 14: plt.imshow(imgg)

This code block is extracted from the uploaded Jupyter notebook. Replace or rewrite the explanation later with your final teaching notes for this step.

Notebook cell 15: plt.imshow(cv2.cvtColor(imgg, cv2.COLOR_BGR2RGB))

pythonexample
plt.imshow(cv2.cvtColor(imgg, cv2.COLOR_BGR2RGB))
Notebook output
<matplotlib.image.AxesImage at 0x16382066488>
drowsiness-detection-opencv notebook output cell 15
Notebook output from cell 15: plt.imshow(cv2.cvtColor(imgg, cv2.COLOR_BGR2RGB))

This code block is extracted from the uploaded Jupyter notebook. Replace or rewrite the explanation later with your final teaching notes for this step.

Notebook cell 17: from PIL import Image ## pip install Pillow

pythonexample
from PIL import Image ## pip install Pillow

This code block is extracted from the uploaded Jupyter notebook. Replace or rewrite the explanation later with your final teaching notes for this step.

Notebook cell 18: im = Image.open(filename) ## PIL

pythonexample
im = Image.open(filename)  ## PIL

This code block is extracted from the uploaded Jupyter notebook. Replace or rewrite the explanation later with your final teaching notes for this step.

Notebook cell 19: im = im.resize((224,224))

pythonexample
im = im.resize((224,224))

This code block is extracted from the uploaded Jupyter notebook. Replace or rewrite the explanation later with your final teaching notes for this step.

Notebook cell 20: plt.imshow(im)

pythonexample
plt.imshow(im)
Notebook output
<matplotlib.image.AxesImage at 0x163820d1ec8>
drowsiness-detection-opencv notebook output cell 20
Notebook output from cell 20: plt.imshow(im)

This code block is extracted from the uploaded Jupyter notebook. Replace or rewrite the explanation later with your final teaching notes for this step.

Notebook cell 22: mobile = tf.keras.applications.mobilenet.MobileNet() ## deep learning model weights -...

pythonexample
mobile = tf.keras.applications.mobilenet.MobileNet() ## deep learning model weights - pre-trained

This code block is extracted from the uploaded Jupyter notebook. Replace or rewrite the explanation later with your final teaching notes for this step.

Notebook cell 23: ## Creating a model

pythonexample
## Creating a model
## Training a modle
## test or validate
# Predict

This code block is extracted from the uploaded Jupyter notebook. Replace or rewrite the explanation later with your final teaching notes for this step.

Notebook cell 24: mobile = tf.keras.applications.mobilenet_v2.MobileNetV2()

pythonexample
mobile = tf.keras.applications.mobilenet_v2.MobileNetV2()

This code block is extracted from the uploaded Jupyter notebook. Replace or rewrite the explanation later with your final teaching notes for this step.

Notebook cell 25: anyone = tf.keras.applications.

pythonexample
anyone = tf.keras.applications.

This code block is extracted from the uploaded Jupyter notebook. Replace or rewrite the explanation later with your final teaching notes for this step.

Notebook cell 27: filename = 'image_data//ILSVRC2012_val_00003606.jpeg'

pythonexample
filename = 'image_data//ILSVRC2012_val_00003606.jpeg'

This code block is extracted from the uploaded Jupyter notebook. Replace or rewrite the explanation later with your final teaching notes for this step.

Notebook cell 28: from tensorflow.keras.preprocessing import image

pythonexample
from tensorflow.keras.preprocessing import image
img = image.load_img(filename, target_size = (224,224))

This code block is extracted from the uploaded Jupyter notebook. Replace or rewrite the explanation later with your final teaching notes for this step.

Notebook cell 30: plt.imshow(img)

pythonexample
plt.imshow(img)
Notebook output
<matplotlib.image.AxesImage at 0x1638e7b09c8>
drowsiness-detection-opencv notebook output cell 30
Notebook output from cell 30: plt.imshow(img)

This code block is extracted from the uploaded Jupyter notebook. Replace or rewrite the explanation later with your final teaching notes for this step.

Notebook cell 31: resized_img = image.img_to_array(img)

pythonexample
resized_img = image.img_to_array(img)
final_image =  np.expand_dims(resized_img,axis =0)  ## need fourth dimension  
final_image=tf.keras.applications.mobilenet.preprocess_input(final_image)

This code block is extracted from the uploaded Jupyter notebook. Replace or rewrite the explanation later with your final teaching notes for this step.

Notebook cell 32: final_image.shape

pythonexample
final_image.shape
Notebook output
(1, 224, 224, 3)

This code block is extracted from the uploaded Jupyter notebook. Replace or rewrite the explanation later with your final teaching notes for this step.

Notebook cell 33: predictions = mobile.predict(final_image)

pythonexample
predictions = mobile.predict(final_image)

This code block is extracted from the uploaded Jupyter notebook. Replace or rewrite the explanation later with your final teaching notes for this step.

Notebook cell 34: from tensorflow.keras.applications import imagenet_utils

pythonexample
from tensorflow.keras.applications import imagenet_utils

This code block is extracted from the uploaded Jupyter notebook. Replace or rewrite the explanation later with your final teaching notes for this step.

Notebook cell 35: results = imagenet_utils.decode_predictions(predictions)

pythonexample
results = imagenet_utils.decode_predictions(predictions)

This code block is extracted from the uploaded Jupyter notebook. Replace or rewrite the explanation later with your final teaching notes for this step.

Notebook cell 36: print(results)## mobilenet v2 of 2018

pythonexample
print(results)## mobilenet v2 of 2018
Notebook output
[[('n01514668', 'cock', 0.95692945), ('n01514859', 'hen', 0.032631382), ('n01807496', 'partridge', 0.0003097788), ('n02437616', 'llama', 0.00014671635), ('n01616318', 'vulture', 0.00013141289)]]

This code block is extracted from the uploaded Jupyter notebook. Replace or rewrite the explanation later with your final teaching notes for this step.

Notebook cell 37: print(results) ## mobilenet v1 of 2017

pythonexample
print(results)  ## mobilenet v1 of 2017
Notebook output
[[('n01514859', 'hen', 0.5734376), ('n01514668', 'cock', 0.42456666), ('n01807496', 'partridge', 0.00065948046), ('n02342885', 'hamster', 0.00016566401), ('n02364673', 'guinea_pig', 8.980193e-05)]]

This code block is extracted from the uploaded Jupyter notebook. Replace or rewrite the explanation later with your final teaching notes for this step.

Notebook cell 38: plt.imshow(img)

pythonexample
plt.imshow(img)
Notebook output
<matplotlib.image.AxesImage at 0x1638e9761c8>
drowsiness-detection-opencv notebook output cell 38
Notebook output from cell 38: plt.imshow(img)

This code block is extracted from the uploaded Jupyter notebook. Replace or rewrite the explanation later with your final teaching notes for this step.

Notebook cell 39: predictions = mobile.predict(final_image)

pythonexample
predictions = mobile.predict(final_image)

This code block is extracted from the uploaded Jupyter notebook. Replace or rewrite the explanation later with your final teaching notes for this step.

Notebook cell 40: results = imagenet_utils.decode_predictions(predictions)

pythonexample
results = imagenet_utils.decode_predictions(predictions)

This code block is extracted from the uploaded Jupyter notebook. Replace or rewrite the explanation later with your final teaching notes for this step.

Notebook cell 41: print(results)

pythonexample
print(results)
Notebook output
[[('n03769881', 'minibus', 0.41444352), ('n04335435', 'streetcar', 0.21348298), ('n02701002', 'ambulance', 0.11573718), ('n06874185', 'traffic_light', 0.04023983), ('n02930766', 'cab', 0.019706426)]]

This code block is extracted from the uploaded Jupyter notebook. Replace or rewrite the explanation later with your final teaching notes for this step.

Download source code

Use this area to provide the complete source code, GitHub repository, Google Drive ZIP, notebook, dataset, or premium download link.

Download Notebook / Source Code
Related Tutorials

Keep learning