Sitemap

Real-Time Plotting with CVZone

3 min readOct 21, 2024

--

Press enter or click to view image in full size

Introduction

In the field of data analysis and computer vision, the capacity to generate real-time plots is a vital tool for visualising evolving data streams in real time. The capacity to create plots in real time is beneficial in a number of contexts, including the tracking of live sensor data, the monitoring of system performance, and the visualisation of algorithm results. The ability to create plots in real time makes data more accessible and more actionable.

In a previous blog post, I examined the means of achieving Real-Time Plotting using solely OpenCV and Matplotlib. Although this approach is both powerful and flexible, it does entail a certain degree of manual setup and integration between libraries. In this instance, we will examine CVZone, an extremely useful library that streamlines numerous common computer vision operations, including real-time data visualisation.

CVZone is constructed upon the OpenCV framework and Mediapipe, yet it provides a more sophisticated level of abstraction, thereby facilitating the implementation of intricate tasks with a reduced amount of code. Whether one is a novice or an experienced programmer, CVZone provides a streamlined pathway to rapid development, encompassing real-time plotting, face detection, and hand tracking, all with intuitive functions and minimal boilerplate code.

Get Mert’s stories in your inbox

Join Medium for free to get updates from this writer.

The objective of this blog post is to demonstrate the utilisation of CVZone for the real-time plotting of data. To illustrate the methodology, we will be employing it to monitor the performance of our application by plotting Frames Per Second (FPS) data. However, it should be noted that the approach is highly flexible, allowing the plotting of any kind of data, including sensor readings, algorithm outputs, or system metrics. While FPS is the example used here, the possibilities are indeed limitless.

Getting Started

Step 1: Installing the necessary libraries

pip install opencv-python numpy cvzone

Step 2: Importing the libaries

import time
import cv2
from cvzone.PlotModule import LivePlot
import numpy as np

Step 3: Initialization

cap = cv2.VideoCapture(r"YourVideoPath")

# Initializing the Plot
xPlot = LivePlot(w=640, yLimit=[0, 500], interval=0.01)

# Previous Time (used to calculate FPS)
pTime = 0

Step 4: Plot FPS in real time

while True:

success, img = cap.read()

img = cv2.resize(img, (640, 480))

if not success:
break


# Calculating the fps
cTime = time.time()
fps = 1 / (cTime - pTime)
pTime = cTime

# Updating the plot
imgPlot = xPlot.update(fps)

# Concatenating input image and the plot
result_img = np.hstack([img, imgPlot])

cv2.imshow("Image and Plot", result_img)

if cv2.waitKey(1) & 0xFF == ord('q'):
break

The whole Code:

import time
import cv2
from cvzone.PlotModule import LivePlot
import numpy as np

cap = cv2.VideoCapture(r"YourVideoPath")

# Initializing the Plot
xPlot = LivePlot(w=640, yLimit=[0, 500], interval=0.01)

# Previous Time (used to calculate FPS)
pTime = 0

while True:

success, img = cap.read()

img = cv2.resize(img, (640, 480))

if not success:
break


# Calculating the fps
cTime = time.time()
fps = 1 / (cTime - pTime)
pTime = cTime

# Updating the plot
imgPlot = xPlot.update(fps)

# Concatenating input image and the plot
result_img = np.hstack([img, imgPlot])

cv2.imshow("Image and Plot", result_img)

if cv2.waitKey(1) & 0xFF == ord('q'):
break

Conclusion

In this tutorial we have learned how to use CVZone for real-time plotting. If you found this code helpful, please clap your hands and comment on this post! I would also love for you to follow me to learn more about Data Science and other related topics. Thanks for reading!

--

--

Mert
Mert

Written by Mert

Bioinformatics grad, now Master's in Informatics. Passionate about Computer Vision & Deep Learning.