Image Classifier

Import KOI 2 Library

from future import *
from koi2 import KOI2

Serial Initialization

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:

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')

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:

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

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()

Last updated