Ultralytics YOLOv8 Solutions: Quick Walkthrough

Ultralytics
6 min readJan 24, 2024

--

Computer vision continues to expand steadily, unveiling numerous compelling use cases across various industries. Object detection stands out as a foundational and indispensable task when using diverse scenarios such as monitoring different activities or collecting analytics.

However, relying solely on object detection proves insufficient. To cater to the multifaceted requirements of computer vision across industries, one must embrace top-level concepts like object counting, object tracking, vehicle speed estimation, distance calculation, heatmap analytics, and more.

Fig-1.1: Ultralytics YOLOv8 Solutions

Ultralytics YOLOv8 has significantly streamlined the workflow by not only offering robust object detection through YOLOv8 but also providing multiple integrated solutions for additional tasks (i.e. object counting, heatmaps, vehicle speed estimation, and so on …). The convenience is amplified by the fact that there’s no need for the installation of third-party libraries — the Ultralytics pip package.

In this article, we will provide an overview of various solutions offered by Ultralytics and outline their respective advantages.

  • Object Counting: Determining the quantity of objects in a scene, is crucial for tasks like crowd management and inventory monitoring.
  • Heatmaps: Graphical representations highlighting feature intensity in an image, resulting useful in areas like surveillance and medical imaging.
  • Speed Estimation: Analyzing object motion for velocity determination, vital in traffic monitoring and surveillance.
  • Object Blurring: Privacy-enhancing technique concealing specific objects in images or videos for ethical use in surveillance and data sharing.

Let’s get started 🚀

Object Counting using Ultralytics YOLOv8

Object counting involves tallying identified objects within a designated video stream. This method seamlessly integrates with object tracking, assigning each track a unique ID. The procedure entails delineating a centroid line, facilitating object counting as objects traverse this designated line within the frame.

This versatile technique finds applications across various domains, such as retail for monitoring customer traffic, smart cities for optimizing crowd management, and industrial settings for tracking the movement of components on assembly lines.

Get started by employing the provided code, and for a broader array of code samples, don’t hesitate to explore our documentation on object counting.

from ultralytics import YOLO
from ultralytics.solutions import object_counter
import cv2

model = YOLO("yolov8n.pt")
cap = cv2.VideoCapture("path/to/video/file.mp4")
assert cap.isOpened(), "Error reading video file"
w, h, fps = (int(cap.get(x))
for x in (cv2.CAP_PROP_FRAME_WIDTH,
cv2.CAP_PROP_FRAME_HEIGHT,
cv2.CAP_PROP_FPS))

# Define region points
region_points = [(20, 400), (1080, 404), (1080, 360), (20, 360)]

# Video writer
video_writer = cv2.VideoWriter("object_counting_output.avi",
cv2.VideoWriter_fourcc(*'mp4v'),
fps,
(w, h))

# Init Object Counter
counter = object_counter.ObjectCounter()
counter.set_args(view_img=True,
reg_pts=region_points,
classes_names=model.names,
draw_tracks=True)

while cap.isOpened():
success, im0 = cap.read()

if not success:
print("Video frame is empty or video processing has \
been successfully completed.")
break

tracks = model.track(im0, persist=True, show=False)

im0 = counter.start_counting(im0, tracks)
video_writer.write(im0)

cap.release()
video_writer.release()
cv2.destroyAllWindows()
Allow Ultralytics to calculate your pocket money. 😄😄😄

Heatmaps using YOLOv8

Heatmaps deliver a comprehensive visualization of densely populated areas. This involves meticulously tracking objects and, based on this tracking data, generating heatmaps utilizing OpenCV COLORMAPS. This dynamic approach finds practical use in diverse domains, such as urban planning, where it aids in identifying high-traffic zones for optimal city infrastructure development.

In healthcare, heatmaps can be applied to analyze patient movement within a medical facility, enhancing operational efficiency and resource allocation. Additionally, in retail, heatmaps prove invaluable for identifying hotspots within stores, enabling strategic product placement, and improving the overall shopping experience.

You can utilize the mentioned code to get started, for more code samples, you can refer to our Heatmaps documentation.

from ultralytics import YOLO
from ultralytics.solutions import heatmap
import cv2

model = YOLO("yolov8n.pt")
cap = cv2.VideoCapture("path/to/video/file.mp4")
assert cap.isOpened(), "Error reading video file"
w, h, fps = (int(cap.get(x)) for x in (cv2.CAP_PROP_FRAME_WIDTH,
cv2.CAP_PROP_FRAME_HEIGHT, cv2.CAP_PROP_FPS))

# Video writer
video_writer = cv2.VideoWriter("heatmap_output.avi",
cv2.VideoWriter_fourcc(*'mp4v'),
fps,
(w, h))

# Init heatmap
heatmap_obj = heatmap.Heatmap()
heatmap_obj.set_args(colormap=cv2.COLORMAP_PARULA,
imw=w,
imh=h,
view_img=True,
shape="circle")

while cap.isOpened():
success, im0 = cap.read()
if not success:
print("Video frame is empty or video processing has \
been successfully completed.")
break
tracks = model.track(im0, persist=True, show=False)

im0 = heatmap_obj.generate_heatmap(im0, tracks)
video_writer.write(im0)

cap.release()
video_writer.release()
cv2.destroyAllWindows()
😄😄😄

Speed Estimation using YOLOv8

Speed estimation, a pivotal process in computer vision applications, involves determining the rate of movement of an object within a specified context. With the capabilities of Ultralytics YOLOv8, one can seamlessly calculate the speed of objects by integrating object tracking with distance and time data. This functionality is particularly valuable in applications such as traffic management and surveillance, where understanding the speed of moving objects is essential for informed decision-making.

The precision of speed estimation plays a critical role in shaping the effectiveness and dependability of diverse applications. It stands as a fundamental component in advancing intelligent systems, contributing to the enhancement of real-time decision-making processes. As computer vision technologies continue to evolve, accurate speed estimation becomes increasingly integral to the seamless operation of systems that rely on understanding and responding to dynamic movements in their surroundings.

To initiate your work, you can make use of the provided code. For additional code samples, you can refer to our documentation page for Speed Estimation.

from ultralytics import YOLO
from ultralytics.solutions import speed_estimation
import cv2

model = YOLO("yolov8n.pt")
names = model.model.names

cap = cv2.VideoCapture("path/to/video/file.mp4")
assert cap.isOpened(), "Error reading video file"
w, h, fps = (int(cap.get(x)) for x in (cv2.CAP_PROP_FRAME_WIDTH,
cv2.CAP_PROP_FRAME_HEIGHT, cv2.CAP_PROP_FPS))

# Video writer
video_writer = cv2.VideoWriter("speed_estimation.avi",
cv2.VideoWriter_fourcc(*'mp4v'),
fps,
(w, h))

line_pts = [(0, 360), (1280, 360)]

# Init speed-estimation obj
speed_obj = speed_estimation.SpeedEstimator()
speed_obj.set_args(reg_pts=line_pts,
names=names,
view_img=True)

while cap.isOpened():

success, im0 = cap.read()
if not success:
print("Video frame is empty or video processing has been \
successfully completed.")
break

tracks = model.track(im0, persist=True, show=False)

im0 = speed_obj.estimate_speed(im0, tracks)
video_writer.write(im0)

cap.release()
video_writer.release()
cv2.destroyAllWindows()
220 KM/PH 😄😄😄

Object Blurring using YOLOv8

In the current digital era, prioritizing personal identity protection is crucial as privacy concerns become increasingly prevalent. The implementation of object blurring serves as a robust solution to shield individuals’ identities across diverse visual content. Leveraging the object blurring feature in Ultralytics YOLOv8, sensitive information, including faces or identifiable features, can be strategically concealed, ensuring the disguise of personal details while maintaining the overall integrity of the content.

This approach is commonly employed in scenarios like image and video sharing, enhancing privacy and mitigating the risk of unauthorized identification. Object blurring stands as a practical and ethical solution in the realm of personal identity protection, contributing to a safer and more secure online environment.

To kickstart your project, you can utilize the provided code. For more examples of code, you can refer to our documentation page for Object blurring.

from ultralytics import YOLO
from ultralytics.utils.plotting import Annotator, colors
import cv2

model = YOLO("yolov8n.pt")
names = model.names

cap = cv2.VideoCapture("path/to/video/file.mp4")
assert cap.isOpened(), "Error reading video file"
w, h, fps = (int(cap.get(x)) for x in (cv2.CAP_PROP_FRAME_WIDTH,
cv2.CAP_PROP_FRAME_HEIGHT,
cv2.CAP_PROP_FPS))
# Blur ratio
blur_ratio = 50

# Video writer
video_writer = cv2.VideoWriter("object_blurring_output.avi",
cv2.VideoWriter_fourcc(*'mp4v'),
fps, (w, h))

while cap.isOpened():
success, im0 = cap.read()
if not success:
print("Video frame is empty or video processing has \
been successfully completed.")
break

results = model.predict(im0, show=False)
boxes = results[0].boxes.xyxy.cpu().tolist()
clss = results[0].boxes.cls.cpu().tolist()
annotator = Annotator(im0, line_width=2, example=names)

if boxes is not None:
for box, cls in zip(boxes, clss):
annotator.box_label(box, color=colors(int(cls), True),
label=names[int(cls)])

obj = im0[int(box[1]):int(box[3]), int(box[0]):int(box[2])]
blur_obj = cv2.blur(obj, (blur_ratio, blur_ratio))

im0[int(box[1]):int(box[3]),
int(box[0]):int(box[2])] = blur_obj

cv2.imshow("ultralytics", im0)
video_writer.write(im0)
if cv2.waitKey(1) & 0xFF == ord('q'):
break

cap.release()
video_writer.release()
cv2.destroyAllWindows()
Then why not utilize Ultralytics YOLOv8 😄😄😄

That’s a wrap! We encourage you to explore the capabilities of Ultralytics YOLOv8 solutions. We look forward to witnessing your projects’ utilization of these tools. Share your advancements and findings with the community — dive in and showcase your work! 🚀

Ultralytics

Ultralytics is on a mission to empower people and companies to unleash the positive potential of vision AI. With a commitment to simplicity, flexibility, and usability, Ultralytics YOLO and Ultralytics HUB lower the barriers to getting started solving the world’s challenges. Ultralytics technology enables businesses to transform their operations, enhance customer experiences, and drive innovation by seamlessly integrating state-of-the-art models and user-friendly platforms. It’s been a remarkable journey, but we’re just getting started.

--

--