> For the complete documentation index, see [llms.txt](https://sharinghub-eng.kittenbot.hk/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://sharinghub-eng.kittenbot.hk/airelated/ai-camera-koi-2/micropython-programming/color-blob-and-line-tracking.md).

# 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
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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, and the optional `goal` query parameter:

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

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
