# Color Blob and Line Tracking

## 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 Color Blob Tracking Mode

```
koi.setModel(16)
```

Sets the KOI mode to Color Blob Tracking.

## Set Color to Track

```
koi.colorSwitch(color)
```

Parameters:

color: Color to track

<table><thead><tr><th width="286">color</th><th width="249">meaning</th></tr></thead><tbody><tr><td>0</td><td>Red</td></tr><tr><td>1</td><td>Green</td></tr><tr><td>2</td><td>Blue</td></tr><tr><td>3</td><td>Yellow</td></tr><tr><td>9</td><td>Custom(requires calibration)</td></tr></tbody></table>

## Calibrate Custom Color

```
koi.colorCalibration()
```

Calibrates KOI to track a custom color.

## Get Color Blob Data

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

Returns data for the tracked color blob.

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

## Sample Program: Color Blob Tracking

```
from future import *
from koi2 import KOI2



koi = KOI2('P2', 'P12')
koi.setModel(16)
screen.sync = 0
while True:
  koi.read_from_uart()
  if sensor.btnValue('a'):
    koi.colorSwitch(0)
  if sensor.btnValue('b'):
    koi.colorCalibration()
    koi.colorSwitch(9)
  screen.fill((0, 0, 0))
  screen.text(koi.xywh[0],5,10,1,(255, 255, 255))
  screen.text(koi.xywh[1],5,30,1,(255, 255, 255))
  screen.text(koi.xywh[2],5,50,1,(255, 255, 255))
  screen.text(koi.xywh[3],5,70,1,(255, 255, 255))
  screen.refresh()

```

{% file src="/files/iYbXRpmdyFHBxHNz9oIe" %}

## Select Line Tracking Mode

```
koi.setModel(32)
```

Sets the KOI mode to Color Blob Tracking.

## Set Color to Track

```
koi.lineSwitch(color)
```

Parameters:

color: Color to track

<table><thead><tr><th width="286">color</th><th width="249">meaning</th></tr></thead><tbody><tr><td>0</td><td>Red</td></tr><tr><td>1</td><td>Green</td></tr><tr><td>2</td><td>Blue</td></tr><tr><td>3</td><td>Yellow</td></tr><tr><td>9</td><td>Custom(requires calibration)</td></tr></tbody></table>

## Calibrate Custom Color

```
koi.colorCalibration()
```

Calibrates KOI to track a custom color.

## Get Line Data

```
koi.xy12[data]
```

Returns data for the tracked line.

Parameters:&#x20;

data: The type of data to return.

| data | Meaning        |
| ---- | -------------- |
| 0    | X1 coordinates |
| 1    | Y1 coordinates |
| 2    | X2 coordinates |
| 3    | Y2 coordinates |

## Sample Program: Line Tracking

```
from future import *
from koi2 import KOI2



koi = KOI2('P2', 'P12')
koi.setModel(32)
screen.sync = 0
while True:
  koi.read_from_uart()
  if sensor.btnValue('a'):
    koi.lineSwitch(3)
  if sensor.btnValue('b'):
    koi.colorCalibration()
    koi.lineSwitch(4)
  screen.fill((0, 0, 0))
  screen.text(koi.xy12[0],5,10,1,(255, 255, 255))
  screen.text(koi.xy12[1],5,30,1,(255, 255, 255))
  screen.text(koi.xy12[2],5,50,1,(255, 255, 255))
  screen.text(koi.xy12[3],5,70,1,(255, 255, 255))
  screen.refresh()

```

{% file src="/files/2ZN3A7HjzRnz3jYWveE8" %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://sharinghub-eng.kittenbot.hk/airelated/ai-camera-koi-2/micropython-programming/color-blob-and-line-tracking.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
