This documentation page is also available as an interactive notebook. You can launch the notebook in
Kaggle or Colab, or download it for use with an IDE or local Jupyter installation, by clicking one of the
above links.
Automatically identify and locate objects in images using YOLOX object
detection models.This notebook uses pxt.create_table() so you can run cells without a
project. An app puts the same columns on a TableModel in app.py and
creates the tables with pxt schema update.
Problem
You have images that need object detection—identifying what objects are
present and where they’re located. Manual labeling is slow and
expensive.
Solution
What’s in this recipe:
Detect objects using YOLOX models (runs locally, no API needed)
Get bounding boxes and class labels
Filter detections by confidence threshold
You add a computed column that runs YOLOX on each image. Detection
happens automatically when you insert new images.
Setup
%pip install -qU pixeltable pixeltable-yolox
Load images
import pixeltable as pxtfrom pixeltable.functions.yolox import yolox# Create a fresh directorypxt.drop_dir('detection_demo', force=True)pxt.create_dir('detection_demo')
Connected to Pixeltable database at: postgresql+psycopg://postgres:@/pixeltable?host=/Users/pjlb/.pixeltable/pgdata
Created directory ‘detection_demo’.
<pixeltable.catalog.dir.Dir at 0x1413e9ed0>
# Create table for imagesimages = pxt.create_table('detection_demo/images', {'image': pxt.Image})
Created table ‘images’.
# Insert sample images (COCO dataset samples with common objects)image_urls = [ 'https://raw.githubusercontent.com/pixeltable/pixeltable/main/docs/resources/images/000000000036.jpg', 'https://raw.githubusercontent.com/pixeltable/pixeltable/main/docs/resources/images/000000000090.jpg', 'https://raw.githubusercontent.com/pixeltable/pixeltable/main/docs/resources/images/000000000106.jpg',]images.insert([{'image': url} for url in image_urls])
Inserting rows into `images`: 0 rows [00:00, ? rows/s]
Inserting rows into `images`: 3 rows [00:00, 523.85 rows/s]
Inserted 3 rows with 0 errors.
3 rows inserted, 6 values computed.
# View imagesimages.collect()
Run object detection
Add a computed column that runs YOLOX on each image:
Parse the detection output to get object counts and classes:
# Extract number of detections@pxt.udfdef count_objects(detections: dict) -> int: """Count the number of detected objects.""" return len(detections.get('labels', []))images.add_computed_column(object_count=count_objects(images.detections))