Algorithm: Custom Image Prediction from Video Using Trained CNN Model
Input:
- A trained deep learning model (e.g., MobileNetV2 .h5 file)
- A video file (e.g., .mp4)
- Desired frame extraction rate (frames per second)
Output:
- Class predictions (e.g., open, close, yawn, no_yawn) for each extracted image frame
- Visualization of predictions with confidence scores
Steps:
1. Start
2. Import necessary libraries (TensorFlow, OpenCV, Matplotlib, OS, etc.)
3. Extract frames from the video:
- Load the video using cv2.VideoCapture()
- Get the frame rate (FPS) of the video
- Calculate frame interval = FPS / desired output rate
- Loop through frames: save every frame_interval-th frame as an image in output folder
4. Create full file paths for extracted images
5. Load the trained model using tf.keras.models.load_model()
6. Define preprocessing pipeline: read, decode, normalize, resize, expand dimensions
7. Create TensorFlow Dataset for custom images and batch it
8. Make predictions using model.predict()
9. Convert predicted probabilities to class labels using np.argmax()
10. Visualize each image with its predicted label using matplotlib.pyplot
11. End
Example Output:
For frame_0012.jpg -> Prediction: Yawn (92%)
For frame_0045.jpg -> Prediction: Open (89%)