opencv
  1. opencv-human-activity-recognition-with-opencv

Human Activity Recognition with OpenCV - ( Advanced Applications in OpenCV )

Heading h2

Syntax

# Load saved model from disk
model = keras.models.load_model('activity_recognition_model.h5')

# Classify the input video frames
input_frames = []
# logic to append input video frames goes here
input_frames = np.array(input_frames)
input_frames = np.expand_dims(input_frames, axis=0)
predictions = model.predict(input_frames)

Example

import cv2
import numpy as np
import keras

# Load saved model from disk
model = keras.models.load_model('activity_recognition_model.h5')

# Start the webcam feed
cap = cv2.VideoCapture(0)

# Define the classes in the activity recognition model
classes = ['walking', 'standing', 'sitting', 'running', 'clapping', 'jumping']

while True:
    # Capture and preprocess the video frames
    ret, frame = cap.read()
    frame = cv2.resize(frame, (224, 224))
    frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
    frame = np.expand_dims(frame, axis=0)

    # Classify the video frames and display the results
    predictions = model.predict(frame)
    activity = classes[np.argmax(predictions[0])]
    cv2.putText(frame, activity, (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
    cv2.imshow('Activity Recognition', frame)

    # Wait for key press to stop the process
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

# Release the resources
cap.release()
cv2.destroyAllWindows()

Output

The output will be a live video feed from the webcam with the detected human activity displayed on the screen.

Explanation

Human activity recognition is an important application of computer vision that involves identifying the actions and behaviors of individuals from video data. OpenCV provides various tools and techniques for building human activity recognition systems.

In the above example, we use a pre-trained deep learning model to classify human activities from live video frames. We load the saved activity recognition model from disk and define the classes in the model. We then start the webcam feed and capture and preprocess the video frames. Finally, we classify the video frames using the loaded model and display the results on the screen.

Use

Human activity recognition with OpenCV can be used for a variety of applications, such as surveillance, health monitoring, and sports analysis. It can help detect and prevent crimes, monitor the health and fitness of individuals, and improve the performance of athletes.

Important Points

  • Human activity recognition is an important application of computer vision
  • OpenCV provides various tools and techniques for building human activity recognition systems
  • Pre-trained deep learning models can be used to classify human activities from video data
  • The output of a human activity recognition system can be used for a variety of applications, such as surveillance, health monitoring, and sports analysis

Summary

In conclusion, human activity recognition with OpenCV is a powerful application of computer vision that has various real-world use cases. Pre-trained deep learning models can be used to classify human activities from live video data. OpenCV provides various tools and techniques for building human activity recognition systems that can help improve public safety, monitor health and fitness, and enhance sports performance.

Published on: