# 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="<https://686851495-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F7Bv9xBdKh3R9w6Vp7asd%2Fuploads%2FoagQwQV4FSy5GRkg3qsJ%2FImage%20Classifier%20Save.py?alt=media&token=164b3c58-640b-4fed-bec1-2a177219e60d>" %}

## 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="<https://686851495-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F7Bv9xBdKh3R9w6Vp7asd%2Fuploads%2Ft26ykyZRKvJ4gEWRhDyO%2FImage%20Classifier%20Load.py?alt=media&token=ef147e09-241d-41f5-a7d6-a92f5d5f7b84>" %}
