Skip to main content

Tools for scanning tire treads.

Project description

treadscan

PyPI version Documentation Status Downloads

This package provides utilities for extracting tire tread (pattern) from images, or even from video of passing vehicles.

treadscan

Three main classes are
treadscan.Detector - picks out stopped vehicles from continoous footage (like at an intersection, under a traffic light). treadscan.Segmentor - finds tires in an image, represented by an ellipse (using basic image processing methods) or keypoints (using a Keypoint-RCNN model). treadscan.TireModel - creates a tire model from keypoints (or an ellipse), then "unwraps" the tire tread.

There is a pre-trained Keypoint-RCNN model for detecting tire keypoints available in this repository, or you can train your own. Before training a model, you need annotated training data. To annotate images, you can use the model_annotation.py, which conveniently saves the keypoints and bounding boxes as a JSON in the same format as used in the training notebook. This script is included in this folder.

model annotation

This GUI utilises the OpenCV window, using mouse position and keyboard input to place the points. So to place a point, user can hover over a location and press a specific key, or hold the key while dragging with the mouse. The points which have to be placed manually are:

  • T - top of vehicle's rim
  • B - bottom of vehicle's rim
  • R - third point on the rim (best to always use the same location)
  • S - sidewall height (recommended location is above the T point)
  • W - inner side of tire

Other controls are:

  • SPACEBAR - show a preview of tire tread
  • BACKSPACE - hide tread preview
  • N - submit keypoints, keep annotating (you might wish to annotate ALL tires in image if there are multiple)
  • ENTER - to export keypoints to JSON file
  • ESCAPE - quit annotation without exporting

The 5 keypoints are used to construct a tire model, defined by outer and inner ellipses.

Example usage

import cv2
import treadscan

# Grayscale picture of background
background_sample = cv2.imread('background.png', cv2.IMREAD_GRAYSCALE)
# BackgroundSubtractorSimple can be substituted with any cv2 background subtractor, for example cv2.BackgroundSubtractorKNN
background_subtractor = treadscan.BackgroundSubtractorSimple(background_sample)
# Pre-recorded video, could also be a live stream from the camera
frame_extractor = treadscan.FrameExtractor('recording.mp4', treadscan.InputType.VIDEO)

# Detects stopped vehicles from footage
detector = treadscan.Detector(backsub=background_subtractor, frame_extractor=frame_extractor)
# Keypoint-RCNN finds five keypoints per tire in image
segmentor = treadscan.SegmentorRCNN('RCNN_model/saved_model.pth')

i = 1
# For each stopped vehicle from footage:
for image in detector.detect():
    # Save detected vehicle
    cv2.imwrite(f'image{i:04d}.jpg', image)
    # Find all tires in image
    list_of_keypoints = segmentor.find_keypoints(image)
    # For each tire in image:
    for j in range(len(list_of_keypoints)):
        keypoints = list_of_keypoints[j]
        # Construct tire model
        tire_model = treadscan.TireModel(image.shape)
        tire_model.from_keypoints(*keypoints)
        # Unwrap tire tread from the model
        tread = tire_model.unwrap(image)
        # Tread postprocessing
        tread = treadscan.remove_gradient(tread)
        tread = treadscan.clahe(tread)
        # Save unwrapped tread
        cv2.imwrite(f'image{i:04d}_tread{j+1:02d}.jpg', tread)
    i += 1

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

treadscan-1.0.5.tar.gz (135.1 kB view details)

Uploaded Source

File details

Details for the file treadscan-1.0.5.tar.gz.

File metadata

  • Download URL: treadscan-1.0.5.tar.gz
  • Upload date:
  • Size: 135.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.8.14

File hashes

Hashes for treadscan-1.0.5.tar.gz
Algorithm Hash digest
SHA256 e402b5935950fe0eb4128d7bc3ce9a3ad96b041d108c126bd5121fcf8b04a535
MD5 74145f429c13e5efc979e72ec64036a7
BLAKE2b-256 57cacc753419f86d84d45baf23b8e16564fc40591cd98d4603e2e4f09b33d0b6

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page