Saturday, November 21, 2026

Sobel vs Canny Edge Detection in Python: An OpenCV Tutorial

How does a computer turn a photograph into lines? In this hands-on Python example, we compare two ways of finding intensity changes: Sobel derivatives and Canny edge detection. You will see the same input image, a gradient-strength map, and a binary edge map.

Watch the visual explanation: Sobel vs Canny Short. For the complete walkthrough, watch the Python and OpenCV tutorial.

What is an edge?

An edge is a location where pixel intensity changes sharply. A dark object against a bright background can create an edge, but so can a shadow, texture, or noise. An edge detector identifies changes in the image; it does not identify an object or understand its meaning.

How Sobel responds to changes

Sobel uses small, fixed kernels to approximate intensity derivatives. One derivative measures change along the x direction and another along y. Combining their values provides a gradient-strength map: brighter pixels in our visualization indicate a stronger response after the gradient magnitude is clipped to the 0–255 display range.

A filter scanning across an image is a useful way to understand the computation. The Sobel kernel is chosen by us; it is not a trained neural-network filter. If you want the background on sliding kernels, see How CNN Filters Find Edges.

What Canny adds

Canny is a multi-stage edge detector. Its stages include gradient estimation, thinning candidate edges through non-maximum suppression, and hysteresis thresholding. In this example, we apply Gaussian blur before Canny to reduce sensitivity to noise. The Sobel result uses unblurred grayscale, matching the course exercise. This makes the demonstration useful for exploring each step, but it is not a comparison with identical preprocessing.

The two Canny thresholds work together: strong candidate edges are kept; weaker candidates can be kept when connected to a strong edge. Changing thresholds can remove noise or detail, but the best settings depend on the input image. cv2.Canny does not automatically perform the separate Gaussian blur shown in this example.

Try it with Python and OpenCV

Install Python, then install the dependencies in your environment:

python -m pip install opencv-python numpy

To recreate the geometric image used in the video, save the following code as generate_input.py and run python generate_input.py in a new folder. It creates input.png; it stops if that file already exists so it does not overwrite your image.

"""Generate the seeded synthetic geometric image used in course lesson 07.

Run: python generate_input.py
Requires: pip install opencv-python numpy
"""
from pathlib import Path
import cv2
import numpy as np

rng = np.random.default_rng(7)
img = np.zeros((480, 640, 3), dtype=np.uint8)
img[:] = (30, 25, 45)
cv2.rectangle(img, (80, 80), (280, 280), (200, 180, 100), -1)
cv2.circle(img, (450, 180), 110, (100, 200, 180), -1)
cv2.ellipse(img, (320, 380), (120, 60), 30, 0, 360, (180, 100, 200), -1)
cv2.line(img, (60, 400), (580, 440), (220, 220, 80), 4)
noise = rng.normal(0, 8, img.shape).astype(np.int16)
img = np.clip(img.astype(np.int16) + noise, 0, 255).astype(np.uint8)
output = Path(__file__).resolve().parent / 'input.png'
if output.exists():
    raise SystemExit(f'File already exists: {output}. Choose a clean folder to avoid overwriting it.')
if not cv2.imwrite(str(output), img):
    raise RuntimeError(f'Unable to write {output}')
print(f'Saved {output}')

Now save the following script as sobel_canny.py in the same folder and run python sobel_canny.py. To use your own image instead, run python sobel_canny.py YOUR_IMAGE.jpg, replacing the name with the actual filename or path. Use an image you have the right to use.

"""Sobel gradient magnitude and Canny edges, based on course lesson 07.

Run: python sobel_canny.py [input_image]
Requires: pip install numpy opencv-python
Outputs are written alongside the script. Canny operates on blurred grayscale;
Sobel operates on unblurred grayscale, matching the lesson notebook.
"""
from pathlib import Path
import json
import sys

import cv2
import numpy as np

HERE = Path(__file__).resolve().parent
source = Path(sys.argv[1]) if len(sys.argv) > 1 else HERE / "input.png"
image = cv2.imread(str(source), cv2.IMREAD_COLOR)
if image is None:
    raise SystemExit(f"Cannot read image: {source}")
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blurred = cv2.GaussianBlur(gray, (5, 5), 0)
gx = cv2.Sobel(gray, cv2.CV_64F, 1, 0, ksize=3)
gy = cv2.Sobel(gray, cv2.CV_64F, 0, 1, ksize=3)
magnitude = np.sqrt(gx**2 + gy**2)
sobel = np.uint8(np.clip(magnitude, 0, 255))

def save(name, pixels):
    path = HERE / name
    if not cv2.imwrite(str(path), pixels):
        raise RuntimeError(f"Failed to write {path}")

save("sobel_magnitude.png", sobel)
measurements = {}
for low, high in [(20, 60), (50, 150), (100, 200), (150, 250)]:
    edges = cv2.Canny(blurred, low, high)
    assert edges.shape == gray.shape and set(np.unique(edges)).issubset({0, 255})
    save(f"canny_{low}_{high}.png", edges)
    measurements[f"{low}/{high}"] = int(np.count_nonzero(edges))
results = {
    "input": str(source.name), "width": int(image.shape[1]), "height": int(image.shape[0]),
    "opencv": cv2.__version__, "numpy": np.__version__,
    "canny_nonzero_pixels": measurements,
    "sobel_nonzero_pixels": int(np.count_nonzero(sobel)),
    "interpretation": "Canny is binary; Sobel shows clipped gradient magnitude. Counts are not accuracy measurements."
}
(HERE / "measurements.json").write_text(json.dumps(results, indent=2), encoding="utf-8")
print(json.dumps(results, indent=2))

The script saves sobel_magnitude.png, four Canny images for threshold pairs 20/60, 50/150, 100/200 and 150/250, and measurements.json in the script folder. Those counts report nonzero pixels; they are not accuracy scores or object counts. The demonstration was tested with OpenCV 4.13.0 and NumPy 2.3.5. Versions and image input can affect results. Re-running the processing script overwrites its output files.

Compare the results

  • Sobel: the displayed map shows the strength of intensity changes; it is not inherently a binary outline.
  • Canny: the output marks selected edge pixels after thinning and thresholding.
  • Try this: change both Canny thresholds and compare the resulting outlines. Also try a more textured image or change the blur. Do not assume that fewer lines always mean a more accurate result.

Frequently asked questions

Is Canny a neural network?
No. It is a classical, multi-stage image-processing algorithm. It does not learn weights from a training dataset.

Which is better, Sobel or Canny?
It depends on the task. Sobel provides derivatives or gradient strength; Canny is often useful when you want a thinner, binary edge map. Both are sensitive to image content and parameter choices.

Can edges be used to count objects?
They may help with later processing, but edges alone do not reliably count objects. Broken boundaries, touching objects, lighting and texture can lead to mistakes.

Continue learning

Explore the computer vision course overview and my English-language course, Computer Vision: OpenCV, Machine Learning and YOLO. The course includes hands-on lessons on image operations, edge detection and further computer vision techniques. This is my instructor referral link.

Further reading: OpenCV: Sobel derivatives and OpenCV: Canny edge detection.

Why Does SQL Skip Rank 3? ROW_NUMBER vs RANK vs DENSE_RANK

Watch the tutorial

Watch the full SQL ranking tutorial or the short introduction.

Two people share second place. Does the next person finish third or fourth? In SQL, the answer depends on the ranking function you choose. That choice can change a leaderboard or a top courses report even when the input data is the same.

We will run a small PostgreSQL example, look at the rows, and then rank courses within each category. The data below is synthetic: it does not change any database tables. You can run each complete SQL block in pgAdmin's Query Tool or another PostgreSQL client.

Four scores, one tie

IDParticipantScore
1Anna100
2Ben90
3Chris90
4Dana80

Ben and Chris have the same score. This self-contained query shows all three rankings side by side:

WITH scores (id, participant, score) AS (
    VALUES (1, 'Anna', 100), (2, 'Ben', 90),
           (3, 'Chris', 90), (4, 'Dana', 80)
)
SELECT participant, score,
       ROW_NUMBER() OVER (ORDER BY score DESC, id) AS row_num,
       RANK()       OVER (ORDER BY score DESC) AS rank_num,
       DENSE_RANK() OVER (ORDER BY score DESC) AS dense_rank_num
FROM scores
ORDER BY score DESC, id;

WITH ... VALUES supplies a temporary set of input rows for this query; it does not create a table. The OVER clause determines how each window function orders its input. The final ORDER BY controls the order in which the query displays the result.

participantscorerow_numrank_numdense_rank_num
Anna100111
Ben90222
Chris90322
Dana80443

ROW_NUMBER: one sequence number per row

ROW_NUMBER() assigns a different position to each row. Both 90-point rows still represent a tie by score, but their row numbers must differ. Here, id is a unique tie-breaker: Ben has a smaller ID, so he receives 2. If we ordered by score alone, PostgreSQL would not promise which tied row gets which number. This is useful when the requirement is to select a fixed number of rows and a deterministic tie-breaker is acceptable.

RANK: equal scores share a rank; gaps remain

RANK() assigns 2 to both 90-point rows. Since two rows have already occupied positions 2 and 3, the next rank is 4. Choose it when equal values should share a rank and the gap reflects the number of preceding rows.

DENSE_RANK: equal scores share a rank; no gaps

DENSE_RANK() also assigns 2 to both 90-point rows, but the next distinct score receives 3. Choose it when you want consecutive ranks for distinct score levels.

The distinction comes from what counts as a peer. The ordering inside RANK() and DENSE_RANK() uses score alone, so the two 90s are peers. Adding a unique id to those two window orderings would make them different peers and eliminate the intended tie. We use id in the ROW_NUMBER() ordering, and separately in the final display ordering; that final sort does not change the ranks. PostgreSQL documents these functions in Window Functions.

Rank courses separately in each category

Suppose a training company wants the two highest ranks in each category, including every course tied at second place. The next example supplies seven fictional courses; the values are an illustration, not counts from a live TrainingHub database.

WITH courses (course_id, category, course_name, enrollments) AS (
    VALUES (1, 'Java', 'Java Basics', 80),
           (2, 'Java', 'Spring Intro', 70),
           (3, 'Java', 'Java Testing', 70),
           (4, 'SQL', 'SQL Basics', 100),
           (5, 'SQL', 'SQL Joins', 90),
           (6, 'SQL', 'SQL Reports', 90),
           (7, 'SQL', 'SQL Views', 80)
), ranked AS (
    SELECT course_id, category, course_name, enrollments,
           ROW_NUMBER() OVER (
               PARTITION BY category ORDER BY enrollments DESC, course_id
           ) AS row_num,
           RANK() OVER (
               PARTITION BY category ORDER BY enrollments DESC
           ) AS course_rank
    FROM courses
)
SELECT category, course_name, enrollments, row_num, course_rank
FROM ranked
WHERE course_rank <= 2
ORDER BY category, enrollments DESC, course_id;

PARTITION BY category starts a separate ranking for Java and SQL. The inner CTE calculates ranks for each course; the outer query filters them. The SQL result is:

categorycourse_nameenrollmentsrow_numcourse_rank
JavaJava Basics8011
JavaSpring Intro7022
JavaJava Testing7032
SQLSQL Basics10011
SQLSQL Joins9022
SQLSQL Reports9032

Each category returns three courses, not two: both courses tied for second place satisfy course_rank <= 2. If the requirement is at most two rows per category, use WHERE row_num <= 2 instead. For this fixture that selects Java Basics, Spring Intro, SQL Basics and SQL Joins. It intentionally excludes one tied course in each category using course_id as the tie-breaker. If a category has fewer than two courses, it returns only the rows available.

DENSE_RANK() <= 2 would select the first two distinct enrollment-count levels per category. It can return more than two rows as well. More generally, RANK() <= N and DENSE_RANK() <= N need not return the same set because RANK can have gaps. Write the business rule first: exactly N rows where available, ranks with gaps, or N distinct value levels.

Common mistakes to avoid

  • Confusing display order with rank order. The final ORDER BY sorts the output. The ORDER BY inside each OVER defines that function's ranking.
  • Accidentally removing ties. A unique identifier inside RANK or DENSE_RANK makes rows with equal scores non-peers. Use an ID only where it is part of the intended ranking rule.
  • Assuming ROW_NUMBER() is reproducible with ties. Provide an explicit, unique tie-breaker if a stable choice matters.
  • Filtering a window value in the same query's WHERE. Calculate it in a CTE or subquery, then filter in the outer query. See the PostgreSQL window-function tutorial.
  • Ignoring missing values. These examples use non-null scores. If real data can contain NULL, decide where those rows belong and specify an appropriate NULLS FIRST or NULLS LAST rule. Do not treat a missing score as zero without a reason.

Try it yourself

Change Dana's score to 90. Predict all three columns before running the first query again. Which function gives three rows the same rank? Which one continues without a gap? For another exercise, change the second query to return at most two rows per category and verify which tied course is excluded.

For more practice with PostgreSQL, SQL and a synthetic training-business database, explore Practical SQL with PostgreSQL: From Queries to Real Projects. This is the instructor's referral link. The example above runs independently of the paid course.

Friday, November 20, 2026

How CNN Filters Find Edges: A 3×3 Convolution Example in Python

How can a convolutional neural network (CNN) begin to find useful patterns in an image? One building block is a small filter that moves across pixel values. At each position, it combines a local patch of pixels with the filter's weights to produce one number.

In this tutorial, we follow a 3×3 edge filter across a tiny image, calculate a value by hand, and reproduce the complete result in Python. The example uses a hand-picked filter to explain an operation used in CNNs; it is not an activation taken from a trained network.

Watch the moving filter and feature map in the YouTube Short.

1. The image and the filter

Our grayscale image is a 5×5 grid. Each row is identical. Here, 0 represents black and 255 represents white:

  0  255  255  255    0
  0  255  255  255    0
  0  255  255  255    0
  0  255  255  255    0
  0  255  255  255    0

The example filter has three identical rows. It subtracts the left side of each patch from the right side:

-1   0   1
-1   0   1
-1   0   1

This filter is sensitive to changes from left to right, so it responds to vertical boundaries. Different filters respond to different patterns.

2. Calculate the first output value

Place the filter over the upper-left 3×3 patch. Multiply each pixel by the weight at the same position and sum all nine products. Because the three rows in this particular patch are identical, we can show the calculation for one row and multiply its result by three:

One row: 0 × -1 + 255 × 0 + 255 × 1 = 255
Three rows: 255 + 255 + 255 = 765

The first output cell is therefore +765. Move the filter one pixel to the right and repeat. In the middle patch, all pixel values are 255, so the positive and negative contributions cancel and the output is 0. At the right boundary the response is -765.

3. From a moving filter to a feature map

We move one pixel at a time (stride 1) and do not add a border (no padding). The filter fits in three positions across and three down, so the 5×5 input produces a 3×3 output: (5 − 3 + 1) × (5 − 3 + 1).

 765   0  -765
 765   0  -765
 765   0  -765

These are signed raw responses. In this example, +765 and -765 indicate opposite directions of the same kind of brightness change. Zero appears for the uniform middle patch; it does not prove that every possible image patch with a zero response is uniform. Taking an absolute value, clipping values, or applying an activation changes how the output is interpreted.

4. Run the calculation in Python

You need Python and NumPy. If NumPy is not installed in your chosen environment, run python -m pip install numpy. Save the code below as cnn_filter_example.py and run python cnn_filter_example.py. No image file, model weights, or GPU is required.

import numpy as np

# Five identical rows make a simple grayscale image.
# Signed integers preserve negative results.
image = np.array([[0, 255, 255, 255, 0]] * 5, dtype=np.int32)
kernel = np.array([[-1, 0, 1], [-1, 0, 1], [-1, 0, 1]], dtype=np.int32)

# Valid cross-correlation: stride 1, no padding, no flipped kernel.
feature_map = np.zeros((3, 3), dtype=np.int32)
for row in range(feature_map.shape[0]):
    for col in range(feature_map.shape[1]):
        patch = image[row:row + 3, col:col + 3]
        feature_map[row, col] = np.sum(patch * kernel)

print('First patch:')
print(image[:3, :3])
print('First output value:', feature_map[0, 0])
print('Signed feature map:')
print(feature_map)
np.testing.assert_array_equal(feature_map, [[765, 0, -765]] * 3)

The assertion checks this particular fixture; it is not a test of a trained model's accuracy. Using a signed integer type matters: unsigned pixel types cannot represent -765 directly.

How does this relate to a CNN?

A convolutional layer applies filters to local patches to create feature maps. A real CNN can use multiple filters, input channels, biases, nonlinear activations and other layers. Its filter weights are usually learned during training. Here we chose the numbers ourselves to make the calculation easy to inspect; we did not train or run a CNN.

Strictly speaking, this example does cross-correlation: it does not flip the kernel before applying it. CNN libraries commonly call this operation convolution. The PyTorch Conv2d documentation describes the cross-correlation operation used by that layer.

Try it yourself

Reverse each filter row to [1, 0, -1] and run the script again after removing or updating the assertion. Can you predict the new signs? Keep the image and all other code unchanged. The positive and negative outputs should exchange signs, while the middle column remains zero.

Frequently asked questions

Why is the output smaller than the input?
With a 3×3 filter, stride 1 and no padding, the filter can start at only three rows and three columns of a 5×5 input. Padding and stride change the output dimensions.

Are CNN filters always edge detectors?
No. Filters are learned for the task; the patterns and their interpretation depend on the trained network, layer and input.

Does a negative value mean an error?
No. A negative value is a valid signed response. Here its sign reflects the direction of the brightness change relative to our chosen filter.

Continue learning

Watch the short animation to follow the moving window. Read the course overview on this blog. My English-language course Computer Vision: OpenCV, Machine Learning and YOLO includes a hands-on lesson on convolution. This is my instructor referral link.

Sunday, November 15, 2026

Why Does SQL SUM Give the Wrong Total After a JOIN?

Watch the tutorial

Watch the full SQL JOIN and SUM tutorial or the short introduction.

Two orders total $200. After joining them to their items, a query reports $300 — with no error. Add DISTINCT inside SUM, and the answer becomes $100. Both results can be wrong for the question we intended to ask.

Let's inspect the rows, reproduce the problem in PostgreSQL and choose a fix based on which orders should count. The example is synthetic and read-only.

The question comes first

Suppose we need the sum of order totals for orders with at least one item. We have two orders:

order_idorder_total
101100
102100

The items are:

item_idorder_id
1101
2101
3102

Each order appears once in the orders data, and both have at least one item. The intended total is 100 + 100 = 200. In this demonstration, an order total is an order-level amount; the item table identifies matches and is not a source of item prices.

Each runnable example below defines its input using WITH and VALUES. Run a complete block in pgAdmin's Query Tool or another PostgreSQL client. It does not create persistent tables or modify data.

Start with the order-level total

WITH orders(order_id, order_total) AS (VALUES (101,100), (102,100))
SELECT SUM(order_total) AS order_total
FROM orders;

For these inputs the result is 200. Since both orders have items, the total of all orders happens to equal the total of orders with at least one item. That will not always be true.

Look at what JOIN really produces

WITH orders(order_id, order_total) AS (VALUES (101,100), (102,100)),
items(item_id, order_id) AS (VALUES (1,101), (2,101), (3,102))
SELECT o.order_id, o.order_total, i.item_id
FROM orders AS o
JOIN items AS i ON i.order_id = o.order_id
ORDER BY o.order_id, i.item_id;
order_idorder_totalitem_id
1011001
1011002
1021003

The join returns one row per matching order and item pair. Order 101 has two matching items, so its order-level amount appears twice. These rows describe distinct order-item pairs, not duplicate items or duplicate orders in the original input. See PostgreSQL's explanation of joined tables.

SUM adds the joined rows

WITH orders(order_id, order_total) AS (VALUES (101,100), (102,100)),
items(item_id, order_id) AS (VALUES (1,101), (2,101), (3,102))
SELECT SUM(o.order_total) AS joined_sum,
       SUM(DISTINCT o.order_total) AS distinct_amount_sum,
       COUNT(*) AS joined_rows,
       COUNT(DISTINCT o.order_id) AS distinct_order_ids
FROM orders AS o
JOIN items AS i ON i.order_id = o.order_id;
joined_sumdistinct_amount_sumjoined_rowsdistinct_order_ids
30010032

SUM(o.order_total) sums the order total on every joined row: 100 + 100 + 100 = 300. The extra $100 comes from repeating order 101's total once for each item.

SUM(DISTINCT o.order_total) does something different: it sums each distinct amount once, not each order once. Both orders have the amount 100, so this query counts that amount only once and returns 100. Distinct amounts are useful if that is actually the question; they are not a general repair for counting orders. PostgreSQL describes DISTINCT within aggregates in its aggregate-expression documentation.

COUNT(DISTINCT o.order_id) is a helpful diagnostic in this specific example because order_id identifies orders in the supplied data: three matched pairs contain two order IDs. A row-count difference does not automatically mean a join is wrong — it can be expected for a one-to-many relationship.

Fix for “orders with at least one item”: EXISTS

If we only need to check that a matching item exists, EXISTS asks that question without producing one order row per item:

WITH orders(order_id, order_total) AS (VALUES (101,100), (102,100)),
items(item_id, order_id) AS (VALUES (1,101), (2,101), (3,102))
SELECT SUM(o.order_total) AS matched_order_total
FROM orders AS o
WHERE EXISTS (
    SELECT 1 FROM items AS i WHERE i.order_id = o.order_id
);

The result is 200. EXISTS tests whether the subquery returns a row for the current order; its SELECT 1 does not mean that it returns or counts item prices. It includes an order once if at least one matching item exists. See PostgreSQL's EXISTS documentation.

If your report also needs item-level values, you may instead aggregate the item side to one row per order before joining it to orders. Joining a one-row-per-order summary avoids multiplying the order-level amount, provided the order key is unique and the joins and filters reflect the intended population. Whether an order amount should equal the sum of its item amounts depends on business rules such as discounts, tax and shipping; do not assume it without checking.

What about orders with no items?

Add an order 103 worth 50 with no matching item. The total of all orders is now 250. The total of orders with at least one item remains 200. Neither number answers every question:

WITH orders(order_id, order_total) AS (VALUES (101,100), (102,100), (103,50)),
items(item_id, order_id) AS (VALUES (1,101), (2,101), (3,102)),
item_counts AS (
    SELECT order_id, COUNT(*) AS item_count
    FROM items
    GROUP BY order_id
)
SELECT SUM(o.order_total) AS all_order_total,
       SUM(o.order_total) FILTER (WHERE ic.order_id IS NOT NULL) AS matched_order_total
FROM orders AS o
LEFT JOIN item_counts AS ic ON ic.order_id = o.order_id;

For this input the query gives 250 and 200 respectively. The grouped item_counts has at most one row per order ID; the LEFT JOIN retains order 103. Its unmatched ic.order_id is NULL, so the filtered aggregate excludes it. An inner join or the earlier EXISTS query intentionally excludes the order with no items. Choose based on your report's requirements.

With no qualifying non-null input amounts, PostgreSQL's SUM returns NULL rather than zero. If zero is the desired reporting rule, use COALESCE deliberately. See aggregate functions.

A practical debugging checklist

  1. Write down what one row represents in each input and what the final metric must count.
  2. Inspect the joined rows before aggregating, including identifiers from both sides.
  3. Compare joined row counts with distinct order IDs when the key is known and non-null.
  4. Check whether missing matches should be retained or removed.
  5. Compare against a small dataset with known answers, including two different orders that have the same amount.

These are correctness checks, not a performance benchmark. The best execution plan and timing depend on data, indexes, PostgreSQL version and the actual query; measure the relevant workload rather than assuming that EXISTS is always faster.

Try it yourself

Add a third item to order 101. What does the naive joined sum return? What does the EXISTS query return? Then add an order with no items and decide explicitly whether your report should include it. Run the queries and compare the results.

For another join-related surprise, see Why Your LEFT JOIN Loses Rows: ON vs WHERE in PostgreSQL.

For guided SQL practice, explore Practical SQL with PostgreSQL: From Queries to Real Projects. This is the instructor's referral link. The course uses TrainingHub, a synthetic training-business database.

The example uses self-contained PostgreSQL SELECT statements with synthetic data. Verify results in your own environment and on representative inputs.

Saturday, November 14, 2026

Why Does SQL NOT IN Return No Rows? The NULL Trap and NOT EXISTS

Watch the tutorial

Watch the full NOT IN and NULL tutorial or the short introduction.

Your query should find customers without a matching order. Instead, PostgreSQL returns no rows. There is no syntax error. The cause can be a single NULL in the subquery used by NOT IN.

In this tutorial, you will reproduce the problem with five small rows, see why it happens, and compare two ways to find customers with no order whose known customer ID matches. The examples run in PostgreSQL and do not change your database.

The data: three customers and two orders

We have customers Anna (1), Ben (2), and Cara (3). The order data contains a row for customer 1 and another row whose customer ID is NULL.

Customer IDName
1Anna
2Ben
3Cara
Order customer ID
1
NULL

The question is: which customers have no order with a matching known customer ID? Anna has a match. Ben and Cara do not. This does not prove that they never placed an order: the order with an unknown customer ID could belong to either of them. Without more data, we cannot assign it to anyone.

In the examples below, each query uses common table expressions (WITH) and VALUES to create temporary input rows for that statement. Paste any complete query into pgAdmin's Query Tool and execute it. Nothing is inserted, updated or deleted.

The surprising query: NOT IN

WITH customers(id, name) AS (
    VALUES (1, 'Anna'), (2, 'Ben'), (3, 'Cara')
),
orders(customer_id) AS (
    VALUES (1), (NULL::integer)
)
SELECT c.id, c.name
FROM customers AS c
WHERE c.id NOT IN (SELECT o.customer_id FROM orders AS o)
ORDER BY c.id;

Result in PostgreSQL: zero rows. Why don't Ben and Cara appear?

Why does one NULL change the result?

NOT IN compares a customer's ID with all values produced by the subquery. For Ben, whose ID is 2, the condition behaves like:

2 <> 1 AND 2 <> NULL

2 <> 1 is true, but 2 <> NULL is unknown, not true. Consequently, the complete condition is unknown. SQL's WHERE keeps rows for which its condition is true; both false and unknown fail the filter. Cara's comparison behaves the same way. Anna matches 1, so her NOT IN condition is false.

This is not a PostgreSQL error. It follows SQL's treatment of NULL as an unknown or missing value in comparisons. See PostgreSQL's documentation of NOT IN.

Fix 1: exclude NULL from the subquery

When unknown order customer IDs should not count as known matches, filter them out on the right-hand side:

WITH customers(id, name) AS (
    VALUES (1, 'Anna'), (2, 'Ben'), (3, 'Cara')
),
orders(customer_id) AS (
    VALUES (1), (NULL::integer)
)
SELECT c.id, c.name
FROM customers AS c
WHERE c.id NOT IN (
    SELECT o.customer_id
    FROM orders AS o
    WHERE o.customer_id IS NOT NULL
)
ORDER BY c.id;

Result:

idname
2Ben
3Cara

The subquery now produces only 1. Each ID on the left in this example is non-null. Filtering the subquery alone does not settle what should happen if a left-hand ID can also be NULL; see the separate case below.

Fix 2: ask whether a matching row exists

NOT EXISTS asks a different, often clearer question: for this customer, is there no order row whose customer ID equals this customer's ID?

WITH customers(id, name) AS (
    VALUES (1, 'Anna'), (2, 'Ben'), (3, 'Cara')
),
orders(customer_id) AS (
    VALUES (1), (NULL::integer)
)
SELECT c.id, c.name
FROM customers AS c
WHERE NOT EXISTS (
    SELECT 1
    FROM orders AS o
    WHERE o.customer_id = c.id
)
ORDER BY c.id;

The result is again Ben and Cara. The correlated condition uses the current customer's ID; the order with a NULL customer ID does not satisfy an equality comparison to that ID. SELECT 1 is enough because EXISTS checks whether the subquery returns a row, not the selected value. See PostgreSQL: EXISTS.

Neither this small example nor the name of an operator proves which query is faster. Performance depends on the query, data, indexes and execution plan; measure on the relevant workload.

What if the left-hand ID is NULL too?

The example deliberately uses non-null customer IDs. For nullable values on the left, the two approaches can differ even after filtering NULL from the subquery:

WITH orders(customer_id) AS (VALUES (1), (NULL::integer))
SELECT
    NULL::integer NOT IN (
        SELECT customer_id FROM orders WHERE customer_id IS NOT NULL
    ) AS null_not_in,
    NOT EXISTS (
        SELECT 1 FROM orders WHERE customer_id = NULL::integer
    ) AS null_not_exists;

PostgreSQL returns NULL for null_not_in and true for null_not_exists. In the second expression, equality to NULL does not produce a matching row. Decide deliberately how the application should treat an unknown customer ID; add an appropriate left-side condition if needed. Do not assume the approaches are interchangeable for all nullable inputs. The treatment of an empty subquery is another case to consider when designing a query.

Quick questions

Does NOT IN always return zero rows? No. The issue above arises when the subquery contains a NULL, in this single-column example. With no right-hand NULL, nonmatching non-null IDs can pass. If the subquery returns no rows, NOT IN has its own defined behavior.

Can I just replace every NOT IN with NOT EXISTS? Not without checking the intended behavior for NULL on both sides. For the demonstrated non-null customer IDs and equality match, the filtered NOT IN and NOT EXISTS produce the same result.

Does a row with a NULL customer ID belong to nobody? No. The ID is unknown in this dataset, so the row cannot be assigned to a specific customer from this information alone.

Does the example modify my data? No. The statements use VALUES and SELECT; they do not create tables or write rows.

Try it yourself

Change the second order from NULL to 2, run each query again, and predict which customers remain. Then change it back to NULL and explain why the first query loses the unmatched rows. In real work, check the business meaning of unknown IDs before choosing how to handle them.

For another SQL filtering surprise, read Why Your LEFT JOIN Loses Rows: ON vs WHERE in PostgreSQL.

To practice SQL and PostgreSQL with guided examples, explore Practical SQL with PostgreSQL: From Queries to Real Projects. This is the instructor's referral link. The course uses TrainingHub, a synthetic training-business database.

Verified against PostgreSQL 18.6 using a self-contained, read-only example. Behavior and plans for other workloads should be checked in their own environments.

Friday, November 13, 2026

Why PostgreSQL Does Not Always Use Your Index: EXPLAIN ANALYZE

Watch the tutorial

Watch the full PostgreSQL EXPLAIN ANALYZE tutorial or the short introduction.

You created an index, but PostgreSQL still chooses a sequential scan. Is the index broken? Not necessarily. In this tutorial, you will use EXPLAIN ANALYZE to compare two filters on the same temporary table and learn why an index can be useful for one query while the planner chooses a sequential scan for another.

The answer depends on the data and the work a query needs to do. We will inspect an actual local PostgreSQL execution, not guess from the presence of an index.

What you will need

Use PostgreSQL and a SQL client such as psql or pgAdmin's Query Tool. Run the complete setup and both queries in one database session: the sample table is temporary, so it disappears at the end of that session. The demonstrated run used PostgreSQL 18.6 on Linux in a separate disposable Docker container; the commands below were executed by psql. No existing course tables were changed. Creating temporary tables requires the appropriate database privileges.

The sample is deliberately artificial: 200,000 orders distributed evenly across 1,000 customer IDs. We compare a filter for a single customer with a filter for customers 1 through 900. These queries return different amounts of data; their elapsed times are not a benchmark of the same workload with and without an index.

Build the sample and the index

CREATE TEMP TABLE demo_orders (
    order_id integer NOT NULL,
    customer_id integer NOT NULL,
    amount numeric(10,2) NOT NULL
);

INSERT INTO demo_orders (order_id, customer_id, amount)
SELECT n, ((n - 1) % 1000) + 1, ((n % 10000)::numeric / 100)
FROM generate_series(1, 200000) AS g(n);

CREATE INDEX demo_orders_customer_id_idx ON demo_orders(customer_id);
ANALYZE demo_orders;

CREATE TEMP TABLE creates a session-scoped table. generate_series provides the sample values. The B-tree index is on customer_id; ANALYZE collects statistics the planner can use to estimate how many rows meet each condition. Other PostgreSQL settings, versions, hardware, data distributions and cached pages can produce different plans and times.

Confirm the inputs:

SELECT count(*) AS total_rows,
       count(*) FILTER (WHERE customer_id = 42) AS narrow_rows,
       count(*) FILTER (WHERE customer_id BETWEEN 1 AND 900) AS broad_rows
FROM demo_orders;

In our run the result was 200,000 total rows, 200 for customer 42 and 180,000 in the 1–900 range. The first filter matches 0.1% of this dataset; the second matches 90%.

One customer: inspect an index-supported plan

EXPLAIN (ANALYZE, BUFFERS)
SELECT sum(amount)
FROM demo_orders
WHERE customer_id = 42;

In one local execution, the relevant part of the plan was:

Aggregate  (cost=515.05..515.06 rows=1 width=32) (actual time=0.328..0.329 rows=1.00 loops=1)
  ->  Bitmap Heap Scan on demo_orders  (cost=5.84..514.55 rows=199 width=6) (actual time=0.068..0.304 rows=200.00 loops=1)
        Recheck Cond: (customer_id = 42)
        ->  Bitmap Index Scan on demo_orders_customer_id_idx  (cost=0.00..5.79 rows=199 width=0) (actual time=0.035..0.036 rows=200.00 loops=1)
              Index Cond: (customer_id = 42)
Execution Time: 0.351 ms

Read the tree from its child nodes upward: in this run, a Bitmap Index Scan used the customer index to identify candidate locations, and a Bitmap Heap Scan visited the table to obtain the matching rows. The aggregate summed their amounts. The planner estimated 199 rows at the bitmap heap scan; EXPLAIN ANALYZE reported 200 actual rows. The cost numbers are planner units, not milliseconds; actual time and Execution Time report timing from that run.

EXPLAIN ANALYZE actually executes its statement. This example uses SELECT, but do not assume it is harmless for statements with side effects. See the PostgreSQL EXPLAIN documentation.

Most customers: why a sequential scan can make sense

EXPLAIN (ANALYZE, BUFFERS)
SELECT sum(amount)
FROM demo_orders
WHERE customer_id BETWEEN 1 AND 900;

In the same run:

Aggregate  (cost=4532.63..4532.64 rows=1 width=32) (actual time=24.887..24.889 rows=1.00 loops=1)
  ->  Seq Scan on demo_orders  (cost=0.00..4082.00 rows=180253 width=6) (actual time=0.030..14.546 rows=180000.00 loops=1)
        Filter: ((customer_id >= 1) AND (customer_id <= 900))
        Rows Removed by Filter: 20000
Execution Time: 24.967 ms

The index still exists. Here the chosen plan reads the temporary table sequentially and filters its rows. The condition retains 180,000 of 200,000 rows, so an index is not automatically the most attractive way to retrieve the needed data. In another environment PostgreSQL might choose a different plan; costs and data distributions matter. See Examining Index Usage and Using EXPLAIN.

This is not evidence that the sequential scan was proven fastest across all workloads. We inspected the planner's choice and the observed execution of each query. The two example queries have different filters and result sizes: do not divide their times and claim an index speedup. Cache state and measurement overhead also affect reported times.

Read a plan without guessing

  1. Check the actual node: is it a sequential, index, bitmap index or bitmap heap scan? An index being present does not imply it was used.
  2. Compare estimates with observations: find estimated and actual rows in the same node; interpret loops when nodes run more than once.
  3. Read the condition: Index Cond, Recheck Cond and Filter tell you where predicates appear in this plan.
  4. Look at buffers and timing in context: cache state, scale, configuration and query execution affect observations. Repeat tests on representative data and hardware.

The full raw plans and a repeat run were retained alongside the example. A second local session produced the same row counts and scan types with different execution times; it does not establish behavior in other environments.

Try it yourself

Change the upper boundary from 900 to 10, then to 500, and inspect each plan. Which scan does your environment choose? Run each query more than once and record the data distribution, PostgreSQL version and result. Do not force a particular plan and assume it will remain optimal in production. To finish, close the session; its temporary table is removed automatically.

Continue learning

For more hands-on SQL, query plans and PostgreSQL, see Practical SQL with PostgreSQL: From Queries to Real Projects. Coupon availability may change. The course uses the synthetic TrainingHub database, while this article uses a separate temporary dataset.

See the PostgreSQL course overview for the complete learning path, prerequisites and more free SQL tutorials.

Monday, November 9, 2026

Why Your LEFT JOIN Loses Rows: ON vs WHERE in PostgreSQL

Watch the tutorial

See the SQL queries and results in pgAdmin in the full YouTube tutorial.

You want a list of every course, including courses with no active enrollments. You write a LEFT JOIN, add a filter, and suddenly some courses disappear. The SQL runs without an error, but it answers a different question.

In this guide, we reproduce the issue and compare conditions in WHERE and ON. We also test why OR ... IS NULL is not always a complete repair.

What you need

Use PostgreSQL and a SQL client such as the pgAdmin Query Tool. Each self-contained SELECT uses WITH and VALUES to create temporary input sets for that statement only. No persistent tables are created or changed. Run one complete block at a time. The example was verified on PostgreSQL 18.6; client formatting may differ.

The courses CTE is the left input and enrollments is the right input; aliases c and e refer to them. Our goal is to keep every course and show its active enrollments, if any.

1. Start with the input

WITH courses (course_id, title) AS (
    VALUES (101, 'SQL Foundations'),
           (102, 'Java API'),
           (103, 'Data Modeling'),
           (104, 'PostgreSQL Performance')
), enrollments (enrollment_id, course_id, status) AS (
    VALUES (1, 101, 'active'),
           (2, 101, 'cancelled'),
           (3, 102, 'cancelled'),
           (4, 104, 'active')
)
SELECT c.course_id, c.title, e.enrollment_id, e.status
FROM courses AS c
LEFT JOIN enrollments AS e ON e.course_id = c.course_id
ORDER BY c.course_id, e.enrollment_id;

Result: five rows.

course_idtitleenrollment_idstatus
101SQL Foundations1active
101SQL Foundations2cancelled
102Java API3cancelled
103Data ModelingNULLNULL
104PostgreSQL Performance4active

Course 101 has two enrollments, 102 has only a cancelled enrollment, 103 has none, and 104 has one active enrollment. The unmatched course 103 is retained with NULL in the right-hand columns.

2. A WHERE condition removes rows

WITH courses (course_id, title) AS (
    VALUES (101, 'SQL Foundations'),
           (102, 'Java API'),
           (103, 'Data Modeling'),
           (104, 'PostgreSQL Performance')
), enrollments (enrollment_id, course_id, status) AS (
    VALUES (1, 101, 'active'),
           (2, 101, 'cancelled'),
           (3, 102, 'cancelled'),
           (4, 104, 'active')
)
SELECT c.course_id, c.title, e.enrollment_id, e.status
FROM courses AS c
LEFT JOIN enrollments AS e ON e.course_id = c.course_id
WHERE e.status = 'active'
ORDER BY c.course_id, e.enrollment_id;

Result: two rows.

course_idtitleenrollment_idstatus
101SQL Foundations1active
104PostgreSQL Performance4active

After matching on course ID, WHERE retains rows for which the status condition is true. Cancelled rows fail it; comparing NULL with 'active' yields unknown, not true. Courses 102 and 103 disappear. This query is appropriate if the goal is only active matches; it does not satisfy our requirement to keep every course. This describes logical semantics; the optimizer can choose a different physical plan.

3. Why OR IS NULL is not always enough

WITH courses (course_id, title) AS (
    VALUES (101, 'SQL Foundations'),
           (102, 'Java API'),
           (103, 'Data Modeling'),
           (104, 'PostgreSQL Performance')
), enrollments (enrollment_id, course_id, status) AS (
    VALUES (1, 101, 'active'),
           (2, 101, 'cancelled'),
           (3, 102, 'cancelled'),
           (4, 104, 'active')
)
SELECT c.course_id, c.title, e.enrollment_id, e.status
FROM courses AS c
LEFT JOIN enrollments AS e ON e.course_id = c.course_id
WHERE e.status = 'active' OR e.enrollment_id IS NULL
ORDER BY c.course_id, e.enrollment_id;

Result: three rows.

course_idtitleenrollment_idstatus
101SQL Foundations1active
103Data ModelingNULLNULL
104PostgreSQL Performance4active

Course 103 returns because it had no matching enrollment. Course 102 still disappears: its cancelled enrollment matched the join, so no null-extended row was generated. Its enrollment ID is non-null and its status is not active. This example uses non-null enrollment IDs; nullable identifiers can make this test ambiguous.

4. Put the match condition in ON

WITH courses (course_id, title) AS (
    VALUES (101, 'SQL Foundations'),
           (102, 'Java API'),
           (103, 'Data Modeling'),
           (104, 'PostgreSQL Performance')
), enrollments (enrollment_id, course_id, status) AS (
    VALUES (1, 101, 'active'),
           (2, 101, 'cancelled'),
           (3, 102, 'cancelled'),
           (4, 104, 'active')
)
SELECT c.course_id, c.title, e.enrollment_id, e.status
FROM courses AS c
LEFT JOIN enrollments AS e
    ON e.course_id = c.course_id
   AND e.status = 'active'
ORDER BY c.course_id, e.enrollment_id;

Result: four rows.

course_idtitleenrollment_idstatus
101SQL Foundations1active
102Java APINULLNULL
103Data ModelingNULLNULL
104PostgreSQL Performance4active

Now a match requires the same course ID and an active status. Courses 102 and 103 are both retained with null right-hand values: neither has an active match. This result alone does not distinguish a cancelled-only course from a course with no enrollments.

A LEFT JOIN does not guarantee one row per course: two active enrollments produce two rows. If you need exactly one, decide whether to aggregate, test existence, or select one enrollment by a defined rule.

Try it yourself

Add a second active enrollment for course 101 and run the final query. Which course appears twice? Then try WHERE e.enrollment_id IS NULL after the final join to find courses with no active match. That is a different question from finding courses with no enrollment of any status.

References

For more practical SQL and PostgreSQL, see Practical SQL with PostgreSQL: From Queries to Real Projects. The course uses the synthetic TrainingHub database for guided examples.

See the PostgreSQL course overview for the complete learning path, prerequisites and more free SQL tutorials.

YOLO Oriented Bounding Boxes: Detect Rotated Objects with Python

Why can a rectangular object detector box include so much empty background? When a ship appears at an angle, a rectangle aligned with the image edges may be much larger than a rotated rectangle around the same prediction. In this hands-on Python example, we use a YOLO oriented bounding box (OBB) model, inspect its four corner points and compare two ways of drawing that same prediction.

Important: this is a comparison of box geometry, not a test of two different detectors or a measurement of detection accuracy. A predicted box may miss part of an object, identify the wrong object, or overlap another prediction.

Watch the tutorial

Watch the full YouTube tutorial for the prediction, the geometry and its limitations.

Prefer a quick demonstration? Watch the YouTube Short.

See the difference

Two crops of the same harbor image: an orange horizontal bounding rectangle on the left and a rotated green bounding rectangle on the right
The same predicted rectangle drawn two ways. Left: its orange axis-aligned enclosing rectangle. Right: the model's green oriented box. This is the highest-confidence prediction in this run.

For this selected prediction, the orange rectangle has about 3.75 times the area of the green one. This ratio is calculated from the predicted coordinates and describes this single example. It does not say how accurately either box matches the true ship.

What is an oriented bounding box?

An axis-aligned bounding box is parallel to the image edges. An OBB can rotate to follow an object's orientation. Ultralytics exposes the four corners through result.obb.xyxyxyxy and a center, width, height and rotation representation through result.obb.xywhr. The angles in the latter are in radians. An OBB has no inherent front or back direction: rotating a rectangle by 180 degrees produces the same rectangle. See the Ultralytics OBB documentation; online documentation may describe versions newer than the one tested here.

Neither a standard box nor an OBB is a segmentation mask. Both are rectangles and can contain background. A smaller rectangle is not automatically a better or more accurate detection; that requires comparison with appropriate ground-truth annotations.

The image and model

The input below is a synthetic harbor image from the course. It is shown so you can see exactly what the program receives. You can save the image for this demonstration or use your own permitted image; other inputs may yield different results.

Synthetic overhead view of a harbor with ships at different angles
Input: a synthetic 1024 × 1024 harbor image from the course.

We used the official YOLOv8n-OBB checkpoint locally. It is an aerial-image-oriented model, so the example uses ships. The tested model's SHA-256 is fa6e4cd2691f132875c143135affaa66b5d89394ebb1d07d19770a9b6382c1b8. Choose a model appropriate to the objects you want to detect.

Set up the example

The actual run used Windows CPU, Python 3.13.9, Ultralytics 8.4.117, OpenCV 5.0.0, NumPy 2.5.1 and PyTorch 2.14.1+cpu. The code below was run in that existing environment; a fresh installation has not been independently verified. On Windows, create an environment and install the direct package:

py -3.13 -m venv .venv
.venv\Scripts\python.exe -m pip install ultralytics==8.4.117

Installing packages and obtaining the checkpoint require internet access unless they are already available locally. The command pins the direct package but not every dependency. Place the image in your project folder as input.png, the downloaded checkpoint as yolov8n-obb.pt, and the complete example below as obb_demo.py.

Complete Python example

# SPDX-License-Identifier: AGPL-3.0-only
# Copyright (c) 2026 ARTSCI - Author of the AI/ML/CV course materials.
"""Run one oriented detector and draw its axis-aligned enclosing rectangles.

The horizontal boxes are computed from the *same* oriented predictions; they
are not predictions from a second model. Model detections are not ground truth.
"""
import argparse
import hashlib
import json
from pathlib import Path

import cv2
import numpy as np
from ultralytics import YOLO


def sha256(path):
    return hashlib.sha256(path.read_bytes()).hexdigest()


def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('--image', type=Path, required=True)
    parser.add_argument('--weights', type=Path, required=True)
    parser.add_argument('--output', type=Path, default=Path('output'))
    args = parser.parse_args()
    for path in (args.image, args.weights):
        if not path.is_file():
            raise FileNotFoundError(path)
    image = cv2.imread(str(args.image))
    if image is None:
        raise ValueError(f'Cannot read image: {args.image}')
    args.output.mkdir(parents=True, exist_ok=True)
    model = YOLO(str(args.weights))
    result = model.predict(source=str(args.image), conf=0.25, imgsz=1024,
                           device='cpu', verbose=False)[0]
    if result.obb is None or len(result.obb) == 0:
        raise ValueError('No oriented detections in this image')
    corners = result.obb.xyxyxyxy.cpu().numpy()
    xywhr = result.obb.xywhr.cpu().numpy()
    scores = result.obb.conf.cpu().numpy()
    classes = result.obb.cls.cpu().numpy().astype(int)
    lower = corners.min(axis=1)
    upper = corners.max(axis=1)
    obb_area = xywhr[:, 2] * xywhr[:, 3]
    span = upper - lower
    axis_area = span[:, 0] * span[:, 1]
    if np.any(obb_area <= 0):
        raise ValueError('Invalid oriented box area')
    ratios = axis_area / obb_area
    if np.any(ratios < 0.999):
        raise ValueError('Axis-aligned box should enclose its oriented box')
    rotated = image.copy()
    aligned = image.copy()
    rows = []
    for i, points in enumerate(corners):
        poly = np.rint(points).astype(np.int32)
        lo = tuple(np.floor(lower[i]).astype(int))
        hi = tuple(np.ceil(upper[i]).astype(int))
        cv2.polylines(rotated, [poly], True, (34, 220, 66), 3, cv2.LINE_AA)
        cv2.rectangle(aligned, lo, hi, (0, 192, 255), 3, cv2.LINE_AA)
        rows.append({'id': i + 1, 'class': result.names[int(classes[i])],
                     'confidence': float(scores[i]), 'corners': points.tolist(),
                     'axis_box_xyxy': [*lower[i].tolist(), *upper[i].tolist()],
                     'obb_area': float(obb_area[i]), 'axis_area': float(axis_area[i]),
                     'axis_over_obb': float(ratios[i]),
                     'angle_deg': float(np.degrees(xywhr[i, 4]))})
    def save(path, img):
        if not cv2.imwrite(str(path), img):
            raise RuntimeError(f'Failed to write {path}')
    save(args.output / 'annotated_obb.png', rotated)
    save(args.output / 'annotated_axis.png', aligned)
    # Highest-scoring detection is used as an example, not as ground truth.
    best = int(np.argmax(scores))
    x1, y1 = np.floor(lower[best] - 50).astype(int)
    x2, y2 = np.ceil(upper[best] + 50).astype(int)
    h, w = image.shape[:2]
    x1, y1, x2, y2 = max(0, x1), max(0, y1), min(w, x2), min(h, y2)
    if x1 >= x2 or y1 >= y2:
        raise ValueError('Could not crop selected detection')
    save(args.output / 'crop_original.png', image[y1:y2, x1:x2])
    # Draw only selected detection on each crop so nearby boxes do not mislead.
    a = image.copy()
    b = image.copy()
    cv2.rectangle(a, tuple(np.floor(lower[best]).astype(int)),
                  tuple(np.ceil(upper[best]).astype(int)), (0, 192, 255), 4, cv2.LINE_AA)
    cv2.polylines(b, [np.rint(corners[best]).astype(np.int32)], True,
                  (34, 220, 66), 4, cv2.LINE_AA)
    save(args.output / 'crop_axis.png', a[y1:y2, x1:x2])
    save(args.output / 'crop_obb.png', b[y1:y2, x1:x2])
    save(args.output / 'comparison.png', np.hstack((a[y1:y2, x1:x2], b[y1:y2, x1:x2])))
    report = {'image_sha256': sha256(args.image), 'weights_sha256': sha256(args.weights),
              'image_shape_hw': [h, w], 'model': 'yolov8n-obb', 'device': 'cpu',
              'confidence_threshold': 0.25, 'imgsz': 1024,
              'count': len(rows), 'classes': sorted(set(x['class'] for x in rows)),
              'mean_axis_over_obb': float(ratios.mean()),
              'selected_id': best + 1, 'selected_axis_over_obb': float(ratios[best]),
              'selected_crop_xyxy': [int(x1), int(y1), int(x2), int(y2)],
              'detections': rows,
              'note': 'Axis boxes computed from the same predicted OBB corners, not a second detector. No ground truth assessed.'}
    (args.output / 'result.json').write_text(json.dumps(report, indent=2), encoding='utf-8')
    print(json.dumps({k: v for k, v in report.items() if k != 'detections'}, indent=2))


if __name__ == '__main__':
    main()

Run the example from that directory:

.venv\Scripts\python.exe obb_demo.py --image input.png --weights yolov8n-obb.pt --output output

The script saves the original crop, an orange horizontal-box view, a green OBB view, a side-by-side comparison and the coordinates and ratios in output/result.json. It explicitly checks that the image and weights exist, then runs a single CPU prediction at confidence 0.25 and image size 1024.

How the comparison is calculated

  1. The model returns four corner points for each predicted oriented box.
  2. The code takes the minimum and maximum x and y values of those same points to obtain an enclosing axis-aligned rectangle.
  3. It divides the area of that horizontal rectangle by the area of the oriented box.

This calculation compares the enclosing geometry of an OBB prediction, not the output of a conventional object-detection model. Rounding the outline for drawing can make rendered line positions differ slightly from the floating-point coordinates used for areas.

What this run actually found

On this one synthetic image, the model returned 9 boxes labelled ship. The mean ratio of axis-aligned enclosing area to OBB area was about 2.73 for those predictions. The numbers are not a count of verified ships, a recall score, an accuracy benchmark or a prediction for other images.

Synthetic harbor with the model's oriented green detection boxes
All oriented predictions from the same run. The upper-right ship has overlapping boxes, including one that covers only part of the ship.

The overlapping boxes on the upper-right ship illustrate a real limitation: even if the boxes are rotated, predictions can be duplicated or incomplete. Confidence is a model score, not proof that a box is correct. Inspect the image and evaluate against labelled data before relying on a model for an application.

Where can this help?

Rotated boxes can be useful for aerial images, rotated packages, vehicles and other elongated objects. Suitability depends on the model and its training data; this specific example does not establish that the supplied checkpoint can detect every object or domain. Try your own permitted image and compare the resulting boxes with the actual objects.

Explore the course lesson on oriented boxes and other practical computer vision projects: see the current promotional offer. Coupon terms and availability may change. You can also use the course link.

Code and component licenses

The complete example script above is © 2026 ARTSCI - Author of the AI/ML/CV course materials, licensed under GNU AGPL-3.0 only, without warranty. Preserve applicable notices and provide corresponding source when sharing covered modifications. This code notice does not automatically license the harbor image, videos or other course media.

The Ultralytics software and pretrained weights retain their own license terms; see the assets license, Ultralytics source and licensing information for your intended use. Other dependencies have their own licenses.

Explore the wider Computer Vision course: Python, OpenCV and YOLO.

Sunday, November 8, 2026

Practical SQL with PostgreSQL: A New Hands-On Course

I have prepared a new English-language course, Practical SQL with PostgreSQL: From Queries to Real Projects, for people who want to build database skills through practical examples.

What does the course cover?

The course begins with relational database foundations, tables, keys and SQL queries, then moves through topics used in everyday database work:

  • Filtering and joining data, aggregation, subqueries, common table expressions and window functions.
  • Working with NULL, dates, text, regular expressions, views, JSON and composite types.
  • Understanding query plans, indexes and performance, as well as exploring planner controls and scheduling.
  • Introducing PL/pgSQL functions, procedures and triggers, followed by roles, privileges, data exchange and backup and restore.

Practice with TrainingHub

The examples use TrainingHub, a synthetic database for a training business. You can practice working with courses, customers, enrollments and payments while following demonstrations in pgAdmin. The course includes practical exercises, downloadable materials, worked solutions and short knowledge checks.

The demonstrated local setup uses Docker Desktop and Windows. Some optional extension labs require additional configuration.

View the course page on Udemy.

Some course materials and narration were created with AI assistance, including AI-generated speech.

How to Blur People in Video with Python, YOLO and OpenCV

Can Python blur people in a video automatically? This demonstration uses a pretrained YOLO detector and OpenCV to find person bounding boxes in each frame and blur the detected rectangles. It creates a new video locally.

This is a visual-effect demonstration, not a guarantee of anonymity. A detector can miss a person, a rectangle can leave identifying detail outside it, and the blur may be too weak. Check all output carefully and use an appropriate, independently validated privacy process before sharing real footage.

Watch the tutorial

See the full demonstration and the missed-detection example in the YouTube tutorial, or watch the short demonstration.

Before and after

Both images below are from frame 120 of the same synthetic input. The first is the source frame; the second is an actual frame from the processed video, not a simulated illustration. The rectangular area comes from one detected person and includes padding around the box.

Original synthetic video frame showing a person exercising
Frame 120 of the permitted synthetic input, before detection and blurring.
Same frame from the output video, with the detected person blurred
Frame 120 of the processed MP4 at confidence threshold 0.25; the detected person rectangle and some surrounding background have been blurred.

How the example works

  1. Read the input. A video is a sequence of images called frames. OpenCV reads its frames, dimensions and frames per second (FPS).
  2. Detect people. YOLO predicts boxes in each frame. A bounding box is a rectangle around a predicted object; the script requests class 0, which is person for this checkpoint. Confidence is a model score, not a guarantee that a person or all of their pixels are detected.
  3. Blur the selected region. The code expands each box by 8% in each direction, clips it to the frame, and applies a 71 × 71 Gaussian blur to the nonempty rectangle. This is a region of interest, or ROI. The rest of the frame remains outside that operation.
  4. Write the result. OpenCV writes the processed frames to a new MP4 at the input video's reported FPS. The sample does not track identities across frames, and it does not copy an audio track to the output.

Bounding boxes cover rectangles, not exact person outlines: some background can be blurred as well. A person-class detector is not automatically a face or licence-plate detector. Different videos, versions and settings can produce different detections.

What you need

The actual run used Windows CPU with Python 3.13.9, Ultralytics 8.4.24, OpenCV 4.13.0, NumPy 2.3.5 and PyTorch 2.10.0+cpu. Use a permitted video and the official YOLOv8n checkpoint. The demonstration input is a synthetic exercise clip; course students can find squat_demo.mp4 in the 04_zasoby folder of the lesson 52 practice ZIP and copy it to the project folder as input.mp4. A different input can produce different results.

On Windows Command Prompt, create a virtual environment and install the packages:

py -3.13 -m venv .venv
.venv\Scripts\python.exe -m pip install ultralytics==8.4.24

Package installation and acquiring weights require an internet connection unless already cached. This command pins the direct package, not every dependency; a fresh online installation has not been independently verified. The demonstrated prediction was repeated on Windows CPU with the available environment and local files.

Place the permitted source video as input.mp4, the model as yolov8n.pt, and the code below as blur_people.py in the project directory. The script checks that the input and weights exist; it doesn't silently choose an alternative model or video.

Complete Python example

# SPDX-License-Identifier: AGPL-3.0-only
# Copyright (c) 2026 ARTSCI - Author of the AI/ML/CV course materials.
"""Detect people and blur their detected rectangles in a video on CPU.

This example demonstrates a visual effect. Missed detections or weak blur may
leave identifying details visible: do not treat output as anonymized footage.
"""
import argparse
import json
import math
import os
import time
from pathlib import Path

import cv2


def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('--video', type=Path, required=True)
    parser.add_argument('--weights', type=Path, required=True)
    parser.add_argument('--output', type=Path, default=Path('blur_output'))
    parser.add_argument('--confidence', type=float, default=0.25)
    args = parser.parse_args()
    if not (0 <= args.confidence <= 1):
        raise ValueError('confidence must be between 0 and 1')
    for name, path in (('Video', args.video), ('Weights', args.weights)):
        if not path.is_file():
            raise FileNotFoundError(f'{name} not found: {path}')
    args.output.mkdir(parents=True, exist_ok=True)
    config = args.output / 'config'
    config.mkdir(exist_ok=True)
    os.environ.setdefault('YOLO_CONFIG_DIR', str(config.resolve()))
    os.environ.setdefault('YOLO_OFFLINE', 'true')
    os.environ.setdefault('YOLO_AUTOINSTALL', 'false')
    from ultralytics import YOLO
    import torch
    torch.set_num_threads(4)
    model = YOLO(str(args.weights))

    cap = cv2.VideoCapture(str(args.video))
    if not cap.isOpened():
        raise RuntimeError(f'Cannot open video: {args.video}')
    fps = cap.get(cv2.CAP_PROP_FPS)
    width, height = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)), int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
    if not math.isfinite(fps) or fps <= 0 or width <= 0 or height <= 0:
        raise ValueError('Video FPS and dimensions must be valid')
    out = args.output / 'blurred.mp4'
    writer = cv2.VideoWriter(str(out), cv2.VideoWriter_fourcc(*'mp4v'), fps, (width, height))
    if not writer.isOpened():
        raise RuntimeError(f'Cannot create video: {out}')
    frame_no = 0
    person_boxes = 0
    frames_with_detections = 0
    records = []
    begin = time.perf_counter()
    try:
        while True:
            ok, frame = cap.read()
            if not ok:
                break
            result = model.predict(frame, classes=[0], conf=args.confidence, imgsz=640,
                                   device='cpu', verbose=False)[0]
            boxes = []
            for box in result.boxes:
                x1, y1, x2, y2 = map(float, box.xyxy[0].tolist())
                # Expand by 8 percent to reduce unblurred edges, and clamp to the image.
                pad_x, pad_y = (x2 - x1) * 0.08, (y2 - y1) * 0.08
                left = max(0, math.floor(x1 - pad_x))
                top = max(0, math.floor(y1 - pad_y))
                right = min(width, math.ceil(x2 + pad_x))
                bottom = min(height, math.ceil(y2 + pad_y))
                if right <= left or bottom <= top:
                    continue
                boxes.append({'xyxy': [left, top, right, bottom],
                              'confidence': round(float(box.conf.item()), 6)})
                region = frame[top:bottom, left:right]
                frame[top:bottom, left:right] = cv2.GaussianBlur(region, (71, 71), 0)
            if boxes:
                frames_with_detections += 1
                person_boxes += len(boxes)
            records.append({'frame': frame_no, 'boxes': boxes})
            writer.write(frame)
            frame_no += 1
    finally:
        cap.release()
        writer.release()
    seconds = time.perf_counter() - begin
    if frame_no == 0:
        raise ValueError('Input contained no decodable frames')
    summary = {'frames': frame_no, 'frames_with_person_detections': frames_with_detections,
               'frames_without_person_detections': frame_no - frames_with_detections,
               'person_boxes': person_boxes, 'fps_source': fps, 'width': width, 'height': height,
               'confidence': args.confidence, 'imgsz': 640, 'box_padding_fraction': 0.08,
               'gaussian_kernel': [71, 71], 'device': 'cpu', 'processing_seconds': round(seconds, 3),
               'processing_frames_per_second': round(frame_no / seconds, 3),
               'output': str(out), 'warning': 'Blur is not a privacy or anonymization guarantee.'}
    (args.output / 'result.json').write_text(json.dumps(summary, indent=2), encoding='utf-8')
    (args.output / 'detections.json').write_text(json.dumps(records, indent=2), encoding='utf-8')
    print(json.dumps(summary, indent=2))


if __name__ == '__main__':
    main()

Run the script from the project directory:

.venv\Scripts\python.exe blur_people.py --video input.mp4 --weights yolov8n.pt --output blur_output

The script writes blur_output/blurred.mp4, a machine-readable blur_output/result.json summary, and blur_output/detections.json with per-frame box coordinates and scores. The detection JSON reports predictions: its presence does not establish that the output is anonymous or correct.

Read the result

For this specific test, the input has 241 frames at 24 FPS, with a frame size of 1280 × 720. With a confidence threshold of 0.25, image size 640, and this particular checkpoint, the program returned one person box in each of the 241 frames. This is a count of model outputs in this sample. It is not recall, pixel coverage, a guarantee of complete concealment, or evidence for other recordings.

The example clamps box coordinates before slicing to avoid indexing outside the frame. cv2.GaussianBlur smooths pixels inside that rectangle, while VideoWriter saves the resulting frames. See the OpenCV guide to image smoothing and the Ultralytics object-blurring guide for further background; the current online documentation can describe a later version than the one tested here.

What happens when a person is not detected?

In a separate run we deliberately raised --confidence to 0.99. The detector returned zero person boxes in all 241 frames, so no person rectangle was blurred. In the 0.25 run, the frame-120 person box had a model score of approximately 0.938850, below the deliberately selected 0.99 threshold. This controlled threshold experiment is deliberately different from the 0.25 demonstration. It illustrates a missed detection, not an estimate of the model's error rate in real use. To reproduce this test, use the same input and model:

.venv\Scripts\python.exe blur_people.py --video input.mp4 --weights yolov8n.pt --output high_threshold_output --confidence 0.99
Frame from the high confidence experiment with no person rectangle detected
Frame 120 of the output at the deliberately raised confidence threshold 0.99: no person rectangle was returned and the source remains visible.

If no box is returned for a frame, this script does not infer a hidden location from earlier frames: that frame is written without that ROI being blurred. Even if a box is returned, it may be misplaced or insufficient. Appearance, clothing, posture, faces outside the box, reflections or background context can still identify someone. Strong visual obscuration does not by itself establish legal anonymization.

Try it yourself

  1. Start with footage you have permission to use, without real personal data where possible.
  2. Run the script with the tested settings and inspect the original alongside the output, including difficult frames, the start and end, small subjects and partial occlusions.
  3. Open detections.json and compare its boxes to what is actually visible. Detection count alone is not a ground-truth assessment.
  4. Try a different confidence threshold using --confidence and compare the same frames. A lower threshold may add detections as well as false positives; it does not guarantee that all people are hidden.

See also my guide to removing an image background with YOLO segmentation, and explore more practical applications in my Computer Vision: OpenCV, Machine Learning and YOLO course.

Code and component licenses

The complete example script on this page is © 2026 ARTSCI - Author of the AI/ML/CV course materials, licensed under GNU AGPL-3.0 only, without warranty. Preserve applicable notices and provide corresponding source when sharing covered modifications. This code notice does not automatically license the sample video, course content or other media.

Ultralytics software and model weights retain their own terms; review the Ultralytics assets license and licensing information for your intended use. Other dependencies have their own licenses.

Explore the wider Computer Vision course: Python, OpenCV and YOLO.

Saturday, November 7, 2026

Estimate Car Speed from Video with Python, YOLO and OpenCV

Can you estimate how fast a vehicle moves using a video? In this Python example, YOLO detects vehicles, a tracker connects detections between frames, and we divide a tracked point’s image displacement by the elapsed video time.

The output in this demonstration is the magnitude of average image-plane velocity in pixels per second (px/s), not road speed in km/h. It is computed from net image displacement, not the distance travelled along the full path. The source is a synthetic traffic video. It has no established real-world scale, so a physical speed cannot be recovered from it just by naming a conversion factor.

Watch the tutorial

Watch the complete Python and YOLO tutorial or see the Short demonstration.

Synthetic highway video with a tracked car, its image-plane trail and the screen displacement result in pixels per second
Output from frame 96 of the sample: the yellow box surrounds the selected track, while red points trace its detected box centers. The displayed value is the magnitude of net image displacement divided by four seconds.

From video to a measurable result

  1. Open the video and read its frame rate. A frame is one image in a sequence, and FPS is the number of frames per second.
  2. Detect vehicles and associate detections across frames. The tracker assigns an ID to an association; it is not a permanent identity or a guarantee of correct tracking.
  3. For one continuous track, find the chosen point at the start and end of the measurement interval. In this example the point is the center of its bounding box.
  4. Divide the distance in image pixels by the elapsed video time. Label the answer in px/s.

The sample is 1280 × 720 pixels at 24 frames per second. We use the vehicle associated with track ID 2 in frames 0–96. The straight-line screen displacement of its box center is about 302.85 pixels in 4 seconds, or 75.7 px/s. The calculation and output concern this track and interval, not every vehicle in the scene.

How is speed calculated?

For two observations from the same tracked object at frame indices f0 and f1, a constant-frame-rate video gives elapsed_seconds = (f1 - f0) / fps. The two image positions are (x0, y0) and (x1, y1). This example uses the straight-line displacement sqrt((x1 - x0)² + (y1 - y0)²). Dividing it by elapsed seconds gives the magnitude of average image-plane velocity in pixels per second over that interval.

This is net displacement, not total path length, and it is not an instantaneous speed. A curved or back-and-forth path can yield a smaller result than summing every successive movement. FPS must describe the source video correctly; variable-frame-rate footage needs appropriate presentation timestamps rather than assuming constant spacing.

What you need

Use Python and a permitted traffic video. The demonstration ran on Windows, Python 3.13.9, Ultralytics 8.4.24, OpenCV 4.13.0, NumPy 2.3.5, PyTorch 2.10.0+cpu and lap 0.5.13. Other versions, input videos and hardware may change detections and IDs. Install the required packages in a virtual environment; downloading packages and model weights needs an internet connection unless they are already available locally.

py -3.13 -m venv .venv
.venv\Scripts\python.exe -m pip install ultralytics==8.4.24 lap==0.5.13

This command pins two direct packages, not the complete dependency tree. A clean online install of this exact command has not been independently tested; platform, version and hardware differences can affect compatibility and results.

Place an input video and the official yolov8n.pt checkpoint where the script expects them. Course students can find the sample traffic_counter_highway_clip.mp4 in 04_zasoby and yolov8n.pt in 02_praktyka of the lesson 51 practice ZIP. Copy them into the project folder as input.mp4 and yolov8n.pt, respectively; do not assume the video is otherwise licensed for public redistribution. Use your own permitted video if the sample is unavailable: exact tracks and speeds will differ.

Complete Python example

Save the following as measure_screen_speed.py. The source is the same file used for the demonstration. Check the paths and parameters before running.

# SPDX-License-Identifier: AGPL-3.0-only
# Copyright (c) 2026 ARTSCI - Author of the AI/ML/CV course materials.
"""Measure image-plane displacement of one tracked car; no real-world calibration."""
import argparse
import json
import math
import os
from pathlib import Path

import cv2


def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('--video', type=Path, required=True)
    parser.add_argument('--weights', type=Path, required=True)
    parser.add_argument('--output', type=Path, default=Path('speed_output'))
    parser.add_argument('--track-id', type=int, default=2)
    parser.add_argument('--end-frame', type=int, default=96)
    args = parser.parse_args()
    for name, path in [('Video', args.video), ('Weights', args.weights)]:
        if not path.is_file():
            raise FileNotFoundError(f'{name} not found: {path}')
    args.output.mkdir(parents=True, exist_ok=True)
    (args.output / 'config').mkdir(exist_ok=True)
    os.environ.setdefault('YOLO_CONFIG_DIR', str((args.output / 'config').resolve()))
    os.environ.setdefault('YOLO_OFFLINE', 'true')
    os.environ.setdefault('YOLO_AUTOINSTALL', 'false')
    from ultralytics import YOLO
    import torch
    torch.set_num_threads(4)
    model = YOLO(str(args.weights))
    cap = cv2.VideoCapture(str(args.video))
    if not cap.isOpened():
        raise RuntimeError('Could not open video')
    fps = cap.get(cv2.CAP_PROP_FPS)
    if not math.isfinite(fps) or fps <= 0:
        raise ValueError('Invalid video frame rate')
    width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
    height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
    writer = cv2.VideoWriter(str(args.output / 'annotated.mp4'),
                             cv2.VideoWriter_fourcc(*'mp4v'), fps, (width, height))
    if not writer.isOpened():
        raise RuntimeError('Could not create output video')
    start = None
    path = []
    result = None
    try:
        for frame_number in range(args.end_frame + 1):
            ok, frame = cap.read()
            if not ok:
                raise RuntimeError(f'Video ended at frame {frame_number}')
            prediction = model.track(frame, persist=True, device='cpu', imgsz=640,
                conf=0.20, classes=[2, 3, 5, 7], tracker='bytetrack.yaml',
                verbose=False)[0]
            boxes = prediction.boxes
            matches = [] if boxes.id is None else [box for box in boxes
                       if box.id is not None and int(box.id.item()) == args.track_id]
            if len(matches) != 1:
                raise ValueError(f'Expected one track ID {args.track_id} in frame {frame_number}; got {len(matches)}')
            xyxy = [float(v) for v in matches[0].xyxy[0].tolist()]
            center = ((xyxy[0]+xyxy[2])/2, (xyxy[1]+xyxy[3])/2)
            if start is None:
                start = center
            path.append(tuple(round(x) for x in center))
            elapsed = frame_number / fps
            displacement = math.dist(start, center)
            mean_px_s = displacement / elapsed if elapsed else None
            cv2.rectangle(frame, (round(xyxy[0]),round(xyxy[1])),
                          (round(xyxy[2]),round(xyxy[3])), (0,220,255), 2)
            for pt in path:
                cv2.circle(frame, pt, 2, (0,0,255), -1)
            cv2.circle(frame, path[-1], 6, (0,0,255), -1)
            cv2.rectangle(frame, (10,18), (710,102), (30,30,30), -1)
            cv2.putText(frame, f'Track ID {args.track_id} | frame {frame_number} | {elapsed:.2f} s',
                        (25,55), cv2.FONT_HERSHEY_SIMPLEX, .8, (255,255,255), 2)
            label = 'Screen displacement / elapsed time: ' + (f'{mean_px_s:.1f} px/s' if mean_px_s is not None else 'N/A')
            cv2.putText(frame, label, (25,88), cv2.FONT_HERSHEY_SIMPLEX, .72, (255,255,255), 2)
            writer.write(frame)
            if frame_number in (0, 48, args.end_frame):
                if not cv2.imwrite(str(args.output / f'frame_{frame_number:03d}.png'), frame):
                    raise RuntimeError('Could not write frame')
            if frame_number == args.end_frame:
                result = {'track_id': args.track_id, 'first_frame': 0, 'last_frame': frame_number,
                          'fps': fps, 'width': width, 'height': height, 'elapsed_s': elapsed,
                          'start_xy': start, 'end_xy': center, 'displacement_px': displacement,
                          'mean_screen_displacement_px_per_s': mean_px_s,
                          'definition': 'straight-line image-plane displacement divided by elapsed time',
                          'not_kmh': True}
    finally:
        writer.release()
        cap.release()
    (args.output/'result.json').write_text(json.dumps(result, indent=2), encoding='utf-8')
    print(json.dumps(result, indent=2))


if __name__ == '__main__':
    main()

From the project directory run:

.venv\Scripts\python.exe measure_screen_speed.py --video input.mp4 --weights yolov8n.pt --output speed_output

The defaults select track ID 2 from frames 0 through 96 for this specific example. The script produces speed_output/annotated.mp4, sample annotated PNG frames and speed_output/result.json. It raises an error if the chosen ID is missing in any frame rather than silently guessing an association.

Follow the code

  1. Read the source. cv2.VideoCapture opens the input and reads the recorded FPS, width and height. The loop uses zero-based frame numbers and stops at the requested end frame.
  2. Detect and associate. model.track(..., persist=True, tracker='bytetrack.yaml') detects the selected vehicle classes and retains tracker state across frames. The code requires exactly one match for the selected ID in every frame; if an association is absent, it stops instead of filling gaps.
  3. Pick a point. For each matching box, the midpoint between its left and right coordinates and between its top and bottom coordinates is the box center. The first center becomes the starting position; subsequent centers add visible points to the red trail.
  4. Calculate and save. For each later frame, math.dist(start, center) returns the straight-line pixel distance from the starting center. Dividing by elapsed source-video time gives the running net displacement per second; the script saves an annotated MP4 and the final coordinates and numbers in JSON.

The defaults are chosen for this particular clip: ID 2 must be present already in frame 0 and remain present through frame 96. For another video, inspect its tracks and set --track-id and --end-frame to an association that satisfies that condition. The same numeric ID in another run is not necessarily the same physical object. This small script is deliberately strict; it does not automatically find an arbitrary new car later in a clip.

How to check the number

Compare the same vehicle at both endpoints. Check that the displayed ID remains associated with that vehicle throughout the measured interval. Read the endpoint coordinates, frame indices and source FPS; calculate the displacement and elapsed time independently. A different track, box jitter, occlusion or camera movement can change the answer. An ID switch can make an apparently precise calculation describe the wrong association.

For this run, frame 0 gives center approximately (692.45, 306.86), and frame 96 gives (966.27, 436.23). The elapsed time is (96 − 0) / 24 = 4 seconds; sqrt((966.27 − 692.45)² + (436.23 − 306.86)²) is approximately 302.85 pixels. Thus net displacement per second is about 302.85 / 4 = 75.7 px/s. These rounded coordinates are for illustration; the program computes with full precision. We visually inspected sampled frames of the association; this is not a ground-truth accuracy measurement.

Why this is not a speedometer

A pixel is a location in the image, not a fixed distance on the road. Perspective means the same road distance can occupy a different number of pixels in different parts of the image. A moving camera, distortion, an inaccurate frame rate and a box center that shifts as the view changes introduce further errors.

A physical speed estimate requires a calibrated mapping between image and ground-plane positions, a valid time base and a suitable known reference. A single global meters-per-pixel factor is valid only under suitable geometric conditions and assumptions. Even after calibration, compare estimates with independently measured ground truth across representative cases. The Ultralytics speed-estimation guide also describes FPS and scale as inputs and calls the output an estimate.

This demonstration is educational; it is not a validated instrument for enforcement or safety-critical decisions.

Explore further

For a related application, read Count Cars with YOLO: Why Track IDs Are Not Vehicle Counts. For more practical computer vision projects, see my Computer Vision: OpenCV, Machine Learning and YOLO course on Udemy.

Code and component licenses

The example script is © 2026 ARTSCI - Author of the AI/ML/CV course materials, licensed under GNU AGPL-3.0 only, without warranty. Keep applicable notices and provide corresponding source when sharing covered modifications. This notice does not automatically license the source video or other media.

Ultralytics software and model weights have their own terms; review the Ultralytics assets license and Ultralytics licensing information for your intended use. Other dependencies and media may have separate terms.

Explore the wider Computer Vision course: Python, OpenCV and YOLO.