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.

Wednesday, November 4, 2026

Remove an Image Background with Python and YOLO Segmentation

How can you remove the background around one object without cutting it out by hand? In this Python tutorial, a pretrained YOLO segmentation model predicts a bus mask on a synthetic city image. We turn that mask into an alpha channel and save a PNG with a transparent background.

Our example selects one predicted bus on a 1024 × 1024 image and runs locally on a Windows CPU. In this run the model returned 12 predictions; those are model outputs, not a verified count of physical objects. A cyclist partly blocks the bus, and the predicted mask does not reconstruct the hidden part.

Watch the Python and YOLO tutorial

Watch the full tutorial or see the short demonstration.

Synthetic city image showing the selected predicted bus mask in green and its bounding box in yellow
Actual selected instance from the demonstration. The yellow rectangle and green mask are different outputs of the same segmentation prediction.

Box versus mask versus alpha

A bounding box gives a rectangular region. It can contain background or another object, as the cyclist in this image illustrates. An instance segmentation mask marks pixels assigned to one predicted object. That boundary is an estimate; inspect it around occlusion, fine details and similar colors. See the Ultralytics instance segmentation documentation.

A PNG alpha channel controls transparency. In this binary example, an alpha value of 0 means transparent and 255 means opaque. A checkerboard is only a way to visualize transparent pixels: it is not the content of the alpha channel. This code keeps the image's full canvas size instead of cropping to the box.

What you need

The demonstrated run used Windows, Python 3.13.9, Ultralytics 8.4.24, PyTorch 2.10.0+cpu, OpenCV 4.13.0 and NumPy 2.3.5. Other versions or hardware can produce different results. Create a project folder and a virtual environment using Windows Command Prompt:

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

The command pins Ultralytics, not the full set of transitive dependencies; the exact fresh online installation has not been independently tested. Package installation and obtaining the checkpoint require an internet connection unless you already have suitable local files. Once the tested environment and inputs were present, the prediction and export were repeated with network access blocked.

Put an image and a segmentation checkpoint beside the script

Place a permitted color image as image.png and the official YOLOv8n-seg checkpoint as yolov8n-seg.pt alongside the Python file. This example image is included in lesson 37 of my Computer Vision course. For a different permitted input, predictions and output will differ.

background-demo/
├── remove_background.py
├── image.png
└── yolov8n-seg.pt

The tested checkpoint has SHA-256 a7cd8f929e1903d78a12a48efecab430209f18dc46cb96c3599a5980c63c423c. A hash identifies file bytes; it does not by itself grant usage rights. The model file is not hosted by this article.

Complete runnable example

Save the following as remove_background.py in the project folder. The script checks that the local files exist, selects the highest-scoring bus prediction and stops with an explicit message if there is no usable mask or no bus. The target class is an intentional choice for this image; change it deliberately for another object.

# SPDX-License-Identifier: AGPL-3.0-only
# Copyright (c) 2026 ARTSCI - Author of the AI/ML/CV course materials.
"""Isolate the highest-scoring bus prediction as a transparent PNG."""
from pathlib import Path
import os

ROOT = Path(__file__).resolve().parent
config_dir = ROOT / 'config'
config_dir.mkdir(exist_ok=True)
os.environ['YOLO_CONFIG_DIR'] = str(config_dir)
os.environ['YOLO_OFFLINE'] = 'true'

import cv2
import numpy as np
import torch
from ultralytics import YOLO


def save_png(path, image):
    if not cv2.imwrite(str(path), image):
        raise OSError(f'Could not save {path}')


def main():
    image_path = ROOT / 'image.png'
    weights_path = ROOT / 'yolov8n-seg.pt'
    for path in (image_path, weights_path):
        if not path.is_file():
            raise FileNotFoundError(f'Required local file: {path}')

    image = cv2.imread(str(image_path), cv2.IMREAD_COLOR)
    if image is None:
        raise ValueError(f'Cannot decode image: {image_path}')
    torch.set_num_threads(4)
    model = YOLO(str(weights_path))
    result = model.predict(
        source=image, conf=0.25, iou=0.70, imgsz=640, device='cpu',
        retina_masks=True, max_det=300, verbose=False,
    )[0]
    if result.masks is None or result.boxes is None:
        raise ValueError('No segmentation masks were returned for this image.')

    candidates = [i for i, box in enumerate(result.boxes)
                  if result.names[int(box.cls.item())] == 'bus']
    if not candidates:
        raise ValueError('No bus prediction passed the confidence threshold.')
    selected = max(candidates, key=lambda i: float(result.boxes[i].conf.item()))
    box = result.boxes[selected]
    mask = result.masks.data[selected].cpu().numpy().astype(np.uint8)
    if mask.shape != image.shape[:2]:
        raise ValueError(f'Mask shape {mask.shape} differs from image {image.shape[:2]}.')

    alpha = mask * 255
    isolated = cv2.cvtColor(image, cv2.COLOR_BGR2BGRA)
    isolated[:, :, 3] = alpha
    isolated[mask == 0, :3] = 0  # Do not keep hidden background pixels.
    output = ROOT / 'output'
    output.mkdir(exist_ok=True)
    save_png(output / 'isolated_bus.png', isolated)
    save_png(output / 'mask.png', alpha)

    highlighted = image.copy()
    highlighted[mask == 1] = (
        image[mask == 1].astype(np.float32) * 0.55
        + np.array([45, 190, 70], dtype=np.float32) * 0.45
    ).astype(np.uint8)
    left, top, right, bottom = [round(float(v)) for v in box.xyxy[0].tolist()]
    cv2.rectangle(highlighted, (left, top), (right, bottom), (40, 220, 255), 3)
    save_png(output / 'selected_mask_and_box.png', highlighted)

    print('Image width x height:', image.shape[1], 'x', image.shape[0])
    print('Total returned predictions:', len(result.boxes))
    print('Selected class:', result.names[int(box.cls.item())])
    print('Selected confidence score:', round(float(box.conf.item()), 4))
    print('Selected box xyxy:', [round(float(v), 1) for v in box.xyxy[0].tolist()])
    print('Foreground pixels:', int(mask.sum()))
    print('Background pixels:', int(mask.size - mask.sum()))
    print('Saved:', output / 'isolated_bus.png')


if __name__ == '__main__':
    main()

From the project directory, run:

.venv\Scripts\python.exe remove_background.py

The script writes output/isolated_bus.png with four BGRA channels, output/mask.png as the alpha mask and output/selected_mask_and_box.png for inspection. The rectangle and colored mask are diagnostic overlays; they are not drawn onto the transparent PNG.

Follow the image through the code

  1. Read and predict. OpenCV reads a color image, then the local segmentation model runs on the CPU. The example fixes conf=0.25, iou=0.70 and imgsz=640. retina_masks=True requests masks at the original image resolution; the explicit shape check rejects a mismatch instead of silently combining misaligned arrays.
  2. Select one instance. The script filters predictions labelled bus, then chooses the highest confidence among those candidates. It uses the same selected index for the box and mask. Confidence is a model score, not a calibrated guarantee that the label or every mask pixel is correct.
  3. Make transparency. The returned mask in this run contains 0 and 1. Multiplying it by 255 creates the alpha plane. OpenCV adds an alpha channel to its BGR image to make BGRA; the code assigns the alpha values and clears hidden background color pixels. It then saves PNG, which preserves transparency.
Actual transparent PNG output, showing the selected bus with the background removed
Actual transparent PNG displayed over a page-provided checkerboard. The image keeps its original 1024 × 1024 canvas. The bicycle rider occludes part of the bus; the script does not invent the unseen portion.

Read and verify the result

The recorded run returned 12 predictions in total. The selected bus had score approximately 0.9458 and box [48.2, 412.2, 476.4, 716.6] in original-image coordinates. Its binary mask produced 98,828 opaque pixels and 949,748 transparent pixels on a 1024 × 1024 canvas. These are measurements of this output, not a pixel-accuracy evaluation against a ground-truth mask.

To check your saved file instead of relying on the checkerboard display, save this separate snippet as verify_alpha.py in the project folder. It reads the PNG with its alpha channel intact and reports its shape and distinct alpha values:

import cv2
import numpy as np

image = cv2.imread("output/isolated_bus.png", cv2.IMREAD_UNCHANGED)
if image is None or image.ndim != 3 or image.shape[2] != 4:
    raise ValueError("Expected a readable image with four BGRA channels")
print("Shape:", image.shape)
print("Alpha values:", np.unique(image[:, :, 3]).tolist())

From the project folder, run .venv\Scripts\python.exe verify_alpha.py.

For the demonstrated output it reports a shape of (1024, 1024, 4) and alpha values [0, 255]. A different image or mask can produce different dimensions and counts. A viewer with a solid background may not make transparency obvious; verify the alpha channel or place the image on a contrasting background.

What this example does not do

Segmentation predicts the visible instance; it does not recover bus pixels hidden behind the cyclist. The binary alpha can also produce rough edges, and the model can include background or miss the object. If there is no suitable bus, inspect the input and class label; lowering the threshold may expose weaker candidates but does not guarantee a correct mask. Validate with representative images and an appropriate reference before relying on the output in a real application.

For another practical view of model outputs, see detecting objects from text labels with YOLO-World. The Ultralytics object-isolation guide discusses segmentation masks and transparent PNG export.

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 course image, narration or video.

Ultralytics software and model weights retain their own terms; review the Ultralytics assets license and licensing information for your use. Other dependencies have their own licenses. My full course covers more practical applications: Computer Vision: OpenCV, Machine Learning and YOLO.

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