Video Analytics &

Motion Detection  

AI Code for Business


Video analytics make use of artificial intelligence to complete various tasks by applying computer vision and deep learning analysis on video footage or live video streams. Video analytics is also sometimes referred to as video content analysis or intelligent video analytics.

Importance

Novel video analytics solutions are quickly gaining traction. Key adopters are companies seeking to use the latest AI technologies to solve long-standing problems, and those that have been operating video surveillance systems long before the emergence of Artificial intelligence (AI). Advances made in Deep | Machine Learning, both subsets of AI, have enabled video analytics to revolutionise the task automation landscape by allowing tasks that previously required human intervention to be automated. Video Intelligence can make use of pre-trained machine learning models that automatically recognize a vast number of objects, places, and actions that can be seen in stored and streaming videos.

Video Analytics in Specified Industries

Vertical Motion Detection

A specific use-case example of video analytics for security is a fence-climbing detection system. Security staff are usually trained to know that people walking outside of a fenced area is considered normal, but climbing on top of or struggling with a fence is irregular. Video analytics software trained to recognize the subtle differences in motion direction between the normal and irregular behavior involving the fence are actively linked to the real-time video feed from security cameras.

Loitering Detection

In Smart Cities, video analytics are trained to notice when people or vehicles remain in a defined zone longer than the user-defined time allows. For the safety of the area, an alarm could be activated depending on the preferences of the program implementer. This behavior detection is effective with the real-time notification of suspicious behavior around pharmacy departments, ATMs, dispensaries, and other locations.

People counting

People counting can also be conducted using video analytics. Retail involves a lot of experimenting with displays and marketing strategies. Observing or having access to how many customers come in and when is helpful for stores to know what is working in terms of marketing and product overview. In addition, noticing how many customers spend prolonged periods of time near which displays are useful for the store because it can be used to improve the customer experience and business for the store. In terms of people counting, video analytics provides operational insights and branding insights and reveals a host of other aspects of customer relationships.

Home monitoring

Home monitoring of elderly adults or people with health issues is another example of an application that provides great value. For instance, falls are a major cause of injury and death in the older population. Although personal medical devices can detect falls, they must be worn and are frequently disregarded by the consumer. A video analytics solution can process the signals of home cameras to detect in real time if a person has fallen. With proper setup, such a system can also determine if a person did take a given medication when they were supposed to.

Example

In this example, we will perform a road traffic count and perform ///....


Follow the steps below and run the code on the colab notebook linked here. (To run the code, click on the round ▶️ next to each cell)

Cell 1: Install the required python library and download video for processing.

!pip install sk-video>=1.1.8

import os

if not os.path.exists('road.mp4'):

    !wget https://learnml.s3.eu-north-1.amazonaws.com/road.mp4

Cell 2: Imports the python libraries needed. Define a function that will be used for logging, a function to remove the background, and a function to apply a filter.

import os

import csv

import numpy as np

import logging

import logging.handlers

import math

import sys

import random

import numpy as np

import skvideo.io

import cv2

import matplotlib.pyplot as plt


from IPython.display import HTML

from base64 import b64encode


cv2.ocl.setUseOpenCL(False)

random.seed(123)


def init_logging(level=logging.INFO):

    main_logger = logging.getLogger()

    for hnd in main_logger.handlers:

        main_logger.removeHandler(hnd)

    formatter = logging.Formatter(

        fmt='%(asctime)s.%(msecs)03d %(levelname)-8s [%(name)s] %(message)s', datefmt='%Y-%m-%d %H:%M:%S')

    handler_stream = logging.StreamHandler(sys.stdout)

    handler_stream.setFormatter(formatter)

    main_logger.addHandler(handler_stream)

    main_logger.setLevel(level)

    return main_logger


def train_bg_subtractor(inst, cap, num=500):

    print ('Training BG Subtractor...')

    i = 0

    for frame in cap:

        inst.apply(frame, None, 0.001)

        i += 1

        if i >= num:

            return cap


def filter_mask(img):

    kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (2, 2))

    closing = cv2.morphologyEx(img, cv2.MORPH_CLOSE, kernel)

    opening = cv2.morphologyEx(closing, cv2.MORPH_OPEN, kernel)

    dilation = cv2.dilate(opening, kernel, iterations=2)

    return dilation

Cell 3: Perform background subtraction, apply filtering, and display the results.

VIDEO_SOURCE = "road.mp4"

bg_subtractor = cv2.createBackgroundSubtractorMOG2(

        history=500, detectShadows=True)

cap = skvideo.io.vreader(VIDEO_SOURCE)

train_bg_subtractor(bg_subtractor, cap, num=500)

frame = next(cap)

fg_mask = bg_subtractor.apply(frame, None, 0.001)

fg_mask[fg_mask < 240] = 0

fg_mask = filter_mask(fg_mask)

plt.figure(figsize=(12,12))

plt.imshow(fg_mask)

plt.show()

Cell 4: A copy of the dataframe is made. This allows for the original data to be undisturbed and kept as a backup.

df_copy = df.copy(deep=True)

df_copy.drop('CustomerID', axis=1, inplace=True)

Cell 5: Using the seaborn visualization libary, display a count bar plot of the gender in the data.

sns.countplot(x='Gender', data=df_copy)

plt.xlabel('Gender')

plt.ylabel('Count')


Observation and Analysis: There is ~10% more female than male customers.

Cell 6: Using the matplotlib visualization libary, display a histogram of the age in the data. We use a bin or bucket of 10. Feel free to change this number to visualize with different bin sizes.

plt.hist(x=df_copy['Age'], bins=10, orientation='vertical', color='red')

plt.xlabel('Age')

plt.ylabel('Count')

plt.show()


Observation and Analysis: There top 3 customer age groups are 15-22 years, 30-40 years and 45-50 years.

Cell 7: Using seaborn, perform a scatter plot and a joint plot on age and spending score and differentiate by gender.

sns.scatterplot(data=df_copy, x='Age', y='Spending Score (1-100)', hue='Gender')

sns.jointplot(data=df_copy, x='Age', y='Spending Score (1-100)')


Observation and Analysis: Per the joint plot, customers whose spending score is more than 65 have ages in the range of 15-42 years. From the scatter plot, it is observed that customers whose spending score is more than 65 consists of more Females than Males. Customers having average spending score is comprised of the full age group range from 15-75 years with an approximate Female to Male ratio.

Cell 8: Using seaborn, perform a scatter plot and a joint plot on annual income and spending score and differentiate by gender.

sns.scatterplot(data=df_copy, x='Annual Income (k$)', y='Spending Score (1-100)', hue='Gender')

sns.jointplot(data=df_copy, x='Annual Income (k$)', y='Spending Score (1-100)')


Observation and Analysis: Five clusters are seen and can be categorized as high income high spending (top right cluster), high income low spending (bottom right cluster), average income average spending (center cluster), low income high spending (top left cluster), and low income low spending (bottom left cluster).

Cell 9: We extract the annual income and spending score data and prepare this to be used for k-means clustering analysis. The first step is to find the optimal number of clusters for this dataset using the elbow method.

X = df_copy.iloc[:, [2,3]]

X.columns


wcss = []

for i in range(1,11):

  kmeans_model = KMeans(n_clusters=i, init='k-means++', random_state=42)

  kmeans_model.fit(X)

  wcss.append(kmeans_model.inertia_)


plt.plot(range(1,11), wcss)

plt.title('Elbow Method')

plt.xlabel('Number of Clusters')

plt.ylabel('WCSS')

plt.show()


Observation and Analysis: Five clusters is optimal for this dataset.

Cell 10: We construct the kmeans model, perform fit and prediction, and visualize the results.

scaler = StandardScaler()

X = scaler.fit_transform(X)

kmeans_model = KMeans(n_clusters=5, init='k-means++', random_state=42)

y_kmeans = kmeans_model.fit_predict(X)


plt.scatter(X[y_kmeans == 0, 0], X[y_kmeans == 0, 1], s = 30, c = 'yellow', label = 'Cluster 1')

plt.scatter(X[y_kmeans == 1, 0], X[y_kmeans == 1, 1], s = 30, c = 'cyan', label = 'Cluster 2')

plt.scatter(X[y_kmeans == 2, 0], X[y_kmeans == 2, 1], s = 30, c = 'lightgreen', label = 'Cluster 3')

plt.scatter(X[y_kmeans == 3, 0], X[y_kmeans == 3, 1], s = 30, c = 'orange', label = 'Cluster 4')

plt.scatter(X[y_kmeans == 4, 0], X[y_kmeans == 4, 1], s = 30, c = 'red', label = 'Cluster 5')

plt.scatter(x=kmeans_model.cluster_centers_[:, 0], y=kmeans_model.cluster_centers_[:, 1], s=100, c='black', marker='+', label='Cluster Centers')

plt.legend()

plt.title('Clusters of customers')

plt.xlabel('Annual Income')

plt.ylabel('Spending Score')

plt.show()


Observation and Recommendations:

a)  High Income, High Spending Score (Cluster 5) - Target these customers by sending new product alerts which would lead to   increase in the revenue collected by the mall as they are loyal customers.

b)  High Income, Low Spending Score (Cluster 3) - Target these customers by asking for feedback and increase advertising with the   aim to convert them into Cluster 5 customers.

c)  Average Income, Average Spending Score (Cluster 2) - Can target these set of customers by providing them with lower cost   financing or other attractive buy options.

d)  Low Income, High Spending Score (Cluster 1) - May or may not target these group of customers based on the policy of the mall.

e)  Low Income, Low Spending Score (Cluster 4) - Do not target.


Conclusion

Finding and extracting actionable insights from data is what machine learning does best. By employing the vast and growing data science modeling techniques, businesses can minimize sales and marketing expenditure with targeted approaches. Clustering analysis is but only one of the many algorithms readily available today. In consideration of your business, how can clustering of your data help?

Check out the other articles to see more applications and related code on maching learning. If you need support and would like to find out more, get in touch with the contact link.