# Built-in 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 Road Sign Tracking Mode

```
koi.setModel(1)
```

Sets the KOI mode to Road Sign Tracking.

## Get Road Sign Coordinates

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

Returns coordinates for the detected road sign.

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 road sign</td></tr><tr><td>3</td><td>Height of road sign</td></tr></tbody></table>

## Get Road Sign Name

```
koi.strVal
```

Returns the name of the road sign.

Values Returned: U-Turn, forward, left, right, limit-30, stop, tunnel

## Sample Code: Road Sign Tracking

```
from future import *
from koi2 import KOI2



koi = KOI2('P2', 'P12')
koi.setModel(1)
sleep(15)
koi.direction(2)
koi.mirror(0)
screen.sync = 0
while True:
  koi.read_from_uart()
  screen.fill((0, 0, 0))
  screen.text(koi.strVal,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%2F3rW76v2TzgBOOY8r0MVG%2FRoad%20Sign.py?alt=media&token=ed6a7599-3859-4d94-8339-da1cff5546b1>" %}

## Select Common Items Tracking Mode

```
koi.setModel(2)
```

Sets the KOI mode to Common Items Tracking.

## Get Item Coordinates

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

Returns coordinates for the detected item.

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 Name

```
koi.strVal
```

Returns the name of the item.

Values Returned: aeroplane, bicycle, bird, boat, bottle, bus, car, cat, chair, cow, diningtable, dog, horse, motorbike, person, pottedplant, sheep, sofa, train, tvmonitor

## Sample Code: Item Tracking

```
from future import *
from koi2 import KOI2



koi = KOI2('P2', 'P12')
koi.setModel(2)
sleep(15)
koi.direction(2)
koi.mirror(0)
screen.sync = 0
while True:
  koi.read_from_uart()
  screen.fill((0, 0, 0))
  screen.text(koi.strVal,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%2FxA9iKffpgNqlirEJdQXP%2FItem%20Tracking.py?alt=media&token=327cbacd-d1fc-469f-86e0-dd858e130d56>" %}

## Select Alphabets Tracking Mode

```
koi.setModel(6)
```

Sets the KOI mode to Alphabet Tracking.

## Get Alphabet Coordinates

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

Returns coordinates for the detected alphabet.

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 alphabet</td></tr><tr><td>3</td><td>Height of alphabet</td></tr></tbody></table>

## Get Alphabet Name

```
koi.strVal
```

Returns the name of the alphabet.

Values Returned: A, B, C, D, E, F

## Sample Code: Alphabet Tracking

```
from future import *
from koi2 import KOI2



koi = KOI2('P2', 'P12')
koi.setModel(6)
sleep(15)
koi.direction(2)
koi.mirror(0)
screen.sync = 0
while True:
  koi.read_from_uart()
  screen.fill((0, 0, 0))
  screen.text(koi.strVal,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%2FKxFPDD7Z1DfPfUmdRkNX%2FAlphabet%20Tracking.py?alt=media&token=d394aa4e-b404-4381-afe0-a383176f64f9>" %}

## Select Numbers Tracking Mode

```
koi.setModel(4)
```

Sets the KOI mode to Numbers Tracking.

## Get Number Coordinates

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

Returns coordinates for the detected number.

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 number</td></tr><tr><td>3</td><td>Height of number</td></tr></tbody></table>

## Get Number Name

```
koi.numberVal
```

Returns the name of the number.

Values Returned: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9

## Sample Code: Number Tracking

```
from future import *
from koi2 import KOI2



koi = KOI2('P2', 'P12')
koi.setModel(4)
sleep(15)
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()

```
