# Image Classifier

## 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 Image Classifier Mode

```
koi.setModel(5)
```

Sets the KOI mode to Face Mask Detection.

## Image Classifier Add Tag

```
koi.classifierAddTag(class)
```

Adds a sample into the image classifier model.

Parameters:&#x20;

class: The name for the class.

## Sample Code: Image Classfier Model Training

```
from future import *
from koi2 import KOI2



koi = KOI2('P2', 'P12')
koi.setModel(5)
sleep(15)
koi.direction(2)
koi.mirror(0)
screen.sync = 0
while True:
  koi.read_from_uart()
  if sensor.btnValue('a'):
    koi.classifierAddTag('A')
    sleep(0.2)
  if sensor.btnValue('b'):
    koi.classifierAddTag('B')
    sleep(0.2)
  if koi.getBtnState('A'):
    koi.classifierSave('/flash/'+'abc.json')

```

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

## Get Image Classifier Results

```
koi.strVal
```

Returns the result from the image classifier.

## Get Similarity Value

```
koi.getSimilarity()
```

Returns the similarity value of the image classifier result.

## Save Image Classifier Model

```
koi.classifierSave(location'+json)
```

Saves the classifier onto the location specified.

Parameters:

location: '/sd/' to save on SD card, '/flash/' to save on onboard memory.

json: file name, must end in '.json'

## Load Image Classifier Model

```
koi.classifierLoad(location'+json)
```

Saves the classifier onto the location specified.

Parameters:

location: '/sd/' to save on SD card, '/flash/' to save on onboard memory.

json: file name, must end in '.json'

## Set Target for Similarity Value to Most Similar Result

```
koi.classifierGetMostSimilarResults()
```

Sets the similarity value to return the similarity to the result with highest similarity.

## Specify Target for Similarity Value

```
koi.classifierSetDetectionTarget(class)
```

Sets the similarity value to return the similarity to the class specified.

Parameters:&#x20;

class: The name for the class.

## Reset Image Classifier

```
koi.classifierReset()
```

Reset the Image Classifier and clears the unsaved training results.

## Sample Code: Image Classifier Model Load

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



koi = KOI2('P2', 'P12')
koi.setModel(5)
sleep(15)
koi.direction(2)
koi.mirror(0)
screen.sync = 0
while True:
  koi.read_from_uart()
  if sensor.btnValue('a'):
    koi.classifierLoad('/flash/'+'abc.json')
    sleep(0.5)
  screen.fill((0, 0, 0))
  screen.text(koi.strVal,5,10,2,(255, 255, 255))
  screen.text(koi.getSimilarity(),5,40,2,(255, 255, 255))
  screen.refresh()

```

{% file src="/files/7ZnW5p5DumsirJ8uVt84" %}


---

# 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/image-classifier.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.
