Python implementation

Requirements and support

Documentation

Main module

Implementation of the TraX protocol. The current implementation is written in pure Python and is therefore a bit slow.

class trax.MessageType

The message type container class

exception trax.TraXError

A protocol error class

Server-side communication

Implementation of the TraX sever. This module provides implementation of the server side of the protocol and is therefore meant to be used in the tracker.

class trax.server.Request

A container class for client requests. Contains fileds type, image, region and parameters.

class trax.server.Server(options, verbose=False)

TraX server implementation class.

quit()

Sends quit message and end terminates communication.

status(region, properties=None)

Reply to client with a status region and optional properties.

Parameters:
  • region (trax.region.Region) – Resulting region object.
  • properties (dict) – Optional arguments as a dictionary.
wait()

Wait for client message request. Recognize it and parse them when received .

Returns:A request structure
Return type:trax.server.Request
class trax.server.ServerOptions(region, image, name=None, identifier=None)

TraX server options

Image module

Image description classes.

trax.image.BUFFER = 'buffer'

Constant for encoded memory buffer image

class trax.image.BufferImage(data=None, format='unknown')

Image encoded in a memory buffer stored in JPEG or PNG file format

class trax.image.FileImage(path=None)

Image saved in a local file

Variables:path – Path to the image file
class trax.image.Image(type)

Base class for all image containers

Variables:type – Type constant for the image
trax.image.MEMORY = 'memory'

Constant for raw memory image

class trax.image.MemoryImage(image)

Image saved in memory as a numpy array

trax.image.PATH = 'path'

Constant for file path image

trax.image.URL = 'url'

Constant for remote or local URL image

class trax.image.URLImage(url=None)

Image saved in a local or remote resource

Variables:url – URL of the image
trax.image.parse(string)

Parses string image representation to one of the containers

Region module

Region description classes.

trax.region.POLYGON = 'polygon'

Constant for polygon region type

class trax.region.Polygon(points)

Polygon region

Variables:
  • points (list) – List of points as tuples [(x1,y1), (x2,y2),…,(xN,yN)]
  • count (int) – number of points
trax.region.RECTANGLE = 'rectangle'

Constant for rectangle region type

class trax.region.Rectangle(x=0, y=0, width=0, height=0)

Rectangle region

Variables:
  • x – top left x coord of the rectangle region
  • y (float) – top left y coord of the rectangle region
  • w (float) – width of the rectangle region
  • h (float) – height of the rectangle region
class trax.region.Region(type)

Base class for all region containers

Variables:type – type of the region
trax.region.SPECIAL = 'special'

Constant for special region type

class trax.region.Special(code)

Special region

Variables:code – Code value
trax.region.convert(region, to)

Perform conversion from one region type to another (if possible).

Parameters:
  • region (Region) – original region
  • to (str) – type of desired result
Result:

converter region or None if conversion is not possible

Integration example

Below is a simple example of a Python code skeleton for a tracker, exposing its tracking loop but hidding all tracker-specific details to keep things clear.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
tracker = Tracker()
trajectory = [];
i = 1

rectangle = read_bounding_box()
image = read_image(i)
rectangle = tracker.initialize(rectangle, image)

trajectory.append(rectangle)

while True:
  i = i + 1
  image = read_image(i)
  rectangle = tracker.update(image)
  trajectory.append(rectangle)

write_trajectory(trajectory)

To make the tracker work with the TraX protocol you have to modify the above code in the following way and also make sure that the trax module will be available at runtime.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
import trax.server
import trax.region
import trax.image
import time

tracker = Tracker()

options = trax.server.ServerOptions(trax.region.RECTANGLE, trax.image.PATH)

with trax.server.Server(options) as server:
  while True:
    request = server.wait()
    if request.type in ["quit", "error"]:
      break
    if request.type == "initialize":
      rectangle = tracker.initialize(get_rectangle(request.region),
            load_image(request.image))
    else:
      rectangle = tracker.update(load_image(request.image))

    server.status(get_region(rectangle))