# Custom Tracking Models

## Import KOI 2 Library

```python
from future import *
from koi2 import KOI2
```

## Serial Initialization

```python
koi = KOI2(tx, rx)
```

Initializes the serial connection to KOI.

Parameters:

tx: TX Pin, use P2 for Robotbit EDU

rx: RX Pin, use P12 for Robotbit EDU

## Select Custom Tracking Mode

```
koi.setModel(3)
```

Sets the KOI mode to Custom Tracking.

## Load Custom Tracking Model

```python
koi.initCustomModel(name, anchor)
koi.initCustomModel('/sd/ballRGB.kmodel',[1.25,1.25,1.50,1.50,1.72,1.72,1.97,1.97,2.34,2.31]) #example for color balls model
```

Loads a custom model.

Parameters:

name: File name

anchor: A list containing the anchor values.

## Get Item Class Coordinates

```
koi.xywh[0]
```

Returns coordinates for the detected custom class.

Parameters:&#x20;

data: The type of data to return.

<table><thead><tr><th width="324">data</th><th>Meaning</th></tr></thead><tbody><tr><td>0</td><td>X coordinates</td></tr><tr><td>1</td><td>Y coordinates</td></tr><tr><td>2</td><td>Width of item</td></tr><tr><td>3</td><td>Height of item</td></tr></tbody></table>

## Get Item ID

```
koi.strVal
```

Returns the name of the item ID.

## Sample Code: Custom Tracking Model

```
from future import *
from koi2 import KOI2



koi = KOI2('P2', 'P12')
koi.setModel(3)
sleep(15)
koi.initCustomModel(10616832,[1.25,1.25,1.50,1.50,1.72,1.72,1.97,1.97,2.34,2.31])
koi.direction(2)
koi.mirror(0)
screen.sync = 0
while True:
  koi.read_from_uart()
  screen.fill((0, 0, 0))
  screen.text(koi.numberVal,5,10,2,(255, 255, 255))
  screen.text(koi.xywh[0],5,30,2,(255, 255, 255))
  screen.text(koi.xywh[1],5,50,2,(255, 255, 255))
  screen.text(koi.xywh[2],5,70,2,(255, 255, 255))
  screen.text(koi.xywh[3],5,90,2,(255, 255, 255))
  screen.refresh()

```

{% file src="<https://686851495-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F7Bv9xBdKh3R9w6Vp7asd%2Fuploads%2FL1i45X7S42rbcqmmtiZ6%2FCustom%20Ball%20Model.py?alt=media&token=d6bea7d3-d082-4a63-af11-d1122011d424>" %}
