Skip to content

dist_calc.py

Matha Goram edited this page Nov 12, 2024 · 2 revisions

NAME

dist_calc.py - estimate the distance between two bounding boxes selected successively by the user

SYNOPSIS

python dist_calc.py

DESCRIPTION

This Python script performs the following steps:

  • Imports the Ultralytics solutions and the OpenCV packages
  • Defines the video source and recording data
  • Processes each frame from the video source
  • Uses the method in the solutions package to estimate the distance between the centroids of the bounding boxes
    Click on any two bounding boxes with Left Mouse click for distance calculation

ENVIRONMENT

A list of all environment variables that affect the program or function and how they affect it.

FILES

A list of the files the program or function uses, such as configuration files, startup files, and files the program directly operates on.

STANDARDS

A description of any standards or conventions that relate to the function or command. Commonly just a list of standards.

HISTORY

Adapted from the Ultralytics website.

NOTES

User Interactions

  • Mouse left-click will draw points between successive selections
  • Mouse right-click will delete all drawn points

VideoWriter_fourcc

A FourCC code is a 4-byte code used to specify the video codec. It's a crucial parameter when initializing a VideoWriter object in OpenCV.

Purpose
  • Codec Selection: The FourCC code determines the codec used to compress and encode the video frames.
  • Compatibility: Different codecs have varying levels of compression, quality, and compatibility with different video players and devices.

Syntax

C++

VideoWriter_fourcc(char c1, char c2, char c3, char c4)

Parameters

c1, c2, c3, c4: The four characters that form the FourCC code

Example Usage

C++
#include <opencv2/opencv.hpp>
using namespace cv;
int main() {
    // Create a VideoWriter object with MJPEG codec
    VideoWriter video("output.avi", VideoWriter::fourcc('M', 'J', 'P', 'G'), 30, Size(640, 480));
    // ... (rest of your code)
}

Common FourCC Codes

  • MJPG: Motion JPEG, a lossy codec that is widely supported
  • XVID: MPEG-4 codec, offering good compression and quality
  • DIVX: Another MPEG-4 codec, known for its high compression ratio
  • H264: A high-efficiency video coding standard, offering excellent compression and quality
  • FFMPEG: A versatile codec that supports a wide range of formats
Choosing the Right Codec:

The best codec for your application depends on several factors, including:

  • Desired Video Quality: Higher quality often requires larger file sizes.
  • Required Compression Ratio: A higher compression ratio results in smaller file sizes, but may impact quality
  • Compatibility: Ensure the chosen codec is supported by your target devices and platforms
  • Real-time Requirements: Some codecs are more computationally intensive than others

2D Distance Calculation

Since the bounding boxes are limited to 2D space, this method cannot calculate the accurate distance between the detected objects owing to the absence of data in the third dimension real-world space.

Advantages of Distance Calculation

  • Localization Precision: Enhances accurate spatial positioning in computer vision tasks
  • Size Estimation: Allows estimation of object size for better contextual understanding
  • Scene Understanding: Enhances 3D scene comprehension, aiding improved decision-making in applications like autonomous driving and surveillance.

Arguments

Name Type Default Description
model str None Path to Ultralytics YOLO Model File
line_width int 2 Line thickness for bounding boxes
show bool False Flag to control whether to display the video stream

CAVEATS

n/a

BUGS

n/a

EXAMPLES

$ python dist_calc.py

AUTHORS

armw

SEE ALSO

Model Prediction with Ultralytics YOLO

OPTIONS

A description of the command-line options accepted by a program and how they change its behavior.

EXIT STATUS

This script uses a recorded video file. If a real-time video source is used then minor modifications to the script will be necessary to permit a graceful exit from processing.

LIBRARY

  • Python package:
  • Ultralytics - solutions
  • Python interpreter - 3.11 or later

ERRORS

The scrpt will not proceed to the calculation of the distance if the input video source file is inaccessible.

VERSIONS

There are no explicit versions of this script. The repository contains trackable commits, pushes and merges.