Пример #1
0
 def __init__(self, model_path, ui, KNN=3, data_path='data'):
   TeachableMachine.__init__(self, model_path, ui)
   self._buffer = deque(maxlen = 4)
   self._engine = KNNEmbeddingEngine(model_path, KNN)
   
   self.cls_nums = KNN+1
   self.data_path = data_path
   self.trg_folder = []    # trg_folder = './data/{Class}'
   self.img_nums = [0, 0, 0, 0]    # img_nums = [ x, x, x, x], count each class's images
   self.check_dir()
   
   if sum(self.img_nums) != 0:
     t_reload = self.log_t('Reload Data', end=' ... ')
     self.reload_data()
     self.log_t('Done.', t_reload)
Пример #2
0
    def __init__(self, model_path, ui, KNN=3):
        TeachableMachine.__init__(self, model_path, ui)
        self._buffer = deque(maxlen=4)
        self._engine = KNNEmbeddingEngine(model_path, KNN)

        ### Modify by Chun
        self.cls_nums = KNN + 1
        self.data_path = 'data'
        self.trg_folder = []  # trg_folder = './data/{Class}'
        self.img_nums = [
            0, 0, 0, 0
        ]  # img_nums = [ x, x, x, x], count each class's images
        self.check_dir()

        if sum(self.img_nums) != 0:
            print('\n', 'Reload Data', end=' ... ')
            self.reload_data()
Пример #3
0
class TeachableMachineKNN(TeachableMachine):
    def __init__(self, model_path, ui, KNN=3):
        TeachableMachine.__init__(self, model_path, ui)
        self._buffer = deque(maxlen=4)
        self._engine = KNNEmbeddingEngine(model_path, KNN)

    def classify(self, img, svg):
        # Classify current image and determine
        emb = self._engine.DetectWithImage(img)
        self._buffer.append(self._engine.kNNEmbedding(emb))
        classification = Counter(self._buffer).most_common(1)[0][0]
        # Interpret user button presses (if any)
        debounced_buttons = self._ui.getDebouncedButtonState()
        for i, b in enumerate(debounced_buttons):
            if not b: continue
            if i == 0: self._engine.clear()  # Hitting button 0 resets
            else:
                self._engine.addEmbedding(
                    emb, i)  # otherwise the button # is the class
        # Hitting exactly all 4 class buttons simultaneously quits the program.
        if sum(filter(
                lambda x: x,
                debounced_buttons[1:])) == 4 and not debounced_buttons[0]:
            self.clean_shutdown = True
            return True  # return True to shut down pipeline
        return self.visualize(classification, svg)
Пример #4
0
 def __init__(self, model_path, ui, KNN=3):
   TeachableMachine.__init__(self, model_path, ui)
   self._buffer = deque(maxlen = 4)
   self._engine = KNNEmbeddingEngine(model_path, KNN)
Пример #5
0
class TeachableMachineKNN_ByChun(TeachableMachine, Log):
  def __init__(self, model_path, ui, KNN=3, data_path='data'):
    TeachableMachine.__init__(self, model_path, ui)
    self._buffer = deque(maxlen = 4)
    self._engine = KNNEmbeddingEngine(model_path, KNN)
    
    self.cls_nums = KNN+1
    self.data_path = data_path
    self.trg_folder = []    # trg_folder = './data/{Class}'
    self.img_nums = [0, 0, 0, 0]    # img_nums = [ x, x, x, x], count each class's images
    self.check_dir()
    
    if sum(self.img_nums) != 0:
      t_reload = self.log_t('Reload Data', end=' ... ')
      self.reload_data()
      self.log_t('Done.', t_reload)
    
  
  def check_dir(self):
    
    t_check = self.log_t('Check Files', end=' ... ')
    for cls in range(1, self.cls_nums+1):  # Classes from 1 to 4
      self.trg_folder.append(os.path.join(self.data_path, str(cls)))
      
      # Check Directory is existed or not 
      if os.path.exists(self.trg_folder[cls-1]) is False:
        os.makedirs(self.trg_folder[cls-1])
        self.img_nums[cls-1] = 0
      else:
        self.img_nums[cls-1] = len(os.listdir(self.trg_folder[cls-1]))
    self.log_t('Done.', t_check)
    self.log_t(f'Files:{sum(self.img_nums)}')
    
  def clear_dir(self):
    shutil.rmtree(self.data_path) 
    self.check_dir()
    self.log_t('Clear')
    
  def reload_data(self):

    for cls in range(1, self.cls_nums+1):  # 1 ~ 4
      if self.img_nums[cls-1] != 0 :
        for idx in range(0, self.img_nums[cls-1]):
          img = Image.open(os.path.join(self.trg_folder[cls-1], f'{idx}.jpg'))
          emb = self._engine.DetectWithImage(img)
          self._buffer.append(self._engine.kNNEmbedding(emb))
          classification = Counter(self._buffer).most_common(1)[0][0]
          self._engine.addEmbedding(emb, cls)     
    
  def classify(self, img, infer=True, add_data=True):

    if infer:    
      # Classify current image and determine
      emb = self._engine.DetectWithImage(img)
      self._buffer.append(self._engine.kNNEmbedding(emb))
      classification = Counter(self._buffer).most_common(1)[0][0]
      # Interpret user button presses (if any)
      if add_data:
        debounced_buttons = self._ui.getDebouncedButtonState()
        for i, b in enumerate(debounced_buttons):
          if not b: continue
          if i == 0:
            self._engine.clear() # Hitting button 0 resets
            ### Modify by Chun : clear data folder
            self.clear_dir()   
          else :
            self._engine.addEmbedding(emb, i) # otherwise the button # is the class
            ### Modify by Chun : Save Image & Label
            save_path = os.path.join(self.trg_folder[i-1], f'{str(self.img_nums[i-1])}.jpg')
            img.save(save_path)
            self.img_nums[i-1] += 1
          
        # Hitting exactly all 4 class buttons simultaneously quits the program.
        if sum(filter(lambda x:x, debounced_buttons[1:])) == 4 and not debounced_buttons[0]:
          self.clean_shutdown = True
          return True # return True to shut down pipeline
      
      return self.get_results(classification)   ### Modify by Chun : log of results