예제 #1
0
def evaluate_model():
    global model_fname
    loader = FileLoader(
        dir_to_walk=r'C:\Users\Ivo Ribeiro\Documents\open-cv\datasets\captures'
    )
    loader.load_files()
    x = []
    y = []
    x_test = []
    y_test = []

    for row in loader.files:
        for fname in row['imgs_per_class']:
            img = cv2.imread(fname)
            descr = getDescriptor(img)
            x.append(descr)
            y.append(hot_encode_vect(len(loader.files), row['index']))
    for row in loader.files_test:
        for fname in row['imgs_per_class']:
            img = cv2.imread(fname)
            descr = getDescriptor(img)
            x_test.append(descr)
            y_test.append(hot_encode_vect(len(loader.files_test),
                                          row['index']))
    x = np.array(x)
    y = np.array(y)
    x_test = np.array(x_test)
    y_test = np.array(y_test)

    print('descriptors loaded.')
    print(loader.class_names)
    ann = MyAnn(input_layer_size=x.shape[1],
                hidden_nodes_size=[x.shape[1] // 4],
                output_layer_size=3,
                epochs=1000,
                ann_fname=model_fname)
    ann.fit(x=x, y=y)

    print('finished train or load.')
    print('evaluating')

    corrects = 0
    for i in np.arange(x_test.shape[0]):
        x = x_test[i]
        y = np.argmax(y_test[i])
        p, stats = ann.predict(x)
        p = int(p)
        y = int(y)
        stats = np.squeeze(stats)
        print('%s predicted as %s with %.2f' %
              (loader.class_names[y], loader.class_names[p], stats[p]))
        print(stats)
        if p == y:
            corrects += 1
    print('acc %.2f%s' % ((corrects / x_test.shape[0]) * 100, '%'))
예제 #2
0
def realtime_teste():
    global model_fname
    ann = MyAnn(input_layer_size=256,
                hidden_nodes_size=[256 // 4],
                output_layer_size=3,
                epochs=1000,
                ann_fname=model_fname)
    class_names = ['fermento', 'leite_caixa', 'leite_lata']
    capture = cv2.VideoCapture(0)
    success, frame = capture.read()
    center_pt = (frame.shape[1] // 2, frame.shape[0] // 2)
    x_center, y_center = center_pt
    curr_rect = None
    i = 0
    for curr_rect in middleRects(frame.shape,
                                 center_x=x_center,
                                 center_y=y_center):
        if i == 2:
            break
        i += 1

    while (success):
        frame_cpy = frame.copy()
        cv2.circle(frame_cpy, (x_center, y_center), 10, (0, 255, 255), 1)
        k = cv2.waitKey(5)
        if k == ord('q'):
            break
        success, frame = capture.read()
        x, y, w, h = curr_rect
        cv2.rectangle(frame_cpy, (x, y), (x + w, y + h), (0, 255, 0), 2)
        roi = frame[y:y + h, x:x + w]
        descr = getDescriptor(roi)
        _, stats = ann.predict(descr)
        stats = np.squeeze(stats)

        txt = ('label %s with %.2f' % (class_names[0], stats[0]))
        cv2.putText(frame_cpy, txt, (25, 25), cv2.FONT_HERSHEY_COMPLEX, 1,
                    (255, 0, 0), 1)

        txt = ('label %s with %.2f' % (class_names[1], stats[1]))
        cv2.putText(frame_cpy, txt, (25, 50), cv2.FONT_HERSHEY_COMPLEX, 1,
                    (0, 255, 0), 1)

        txt = ('label %s with %.2f' % (class_names[2], stats[2]))
        cv2.putText(frame_cpy, txt, (25, 75), cv2.FONT_HERSHEY_COMPLEX, 1,
                    (0, 0, 255), 1)

        txt = 'soma %.2f' % (np.sum(stats))
        cv2.putText(frame_cpy, txt, (25, 100), cv2.FONT_HERSHEY_COMPLEX, 1,
                    (0, 255, 255), 1)

        cv2.imshow('', frame_cpy)
    capture.release()
    cv2.destroyAllWindows()
예제 #3
0
 def train_knn(self):
      responses = []
      descriptors = []
      for row in self.files: 
          dirs = row['imgs_per_class']
          idx = row['index']
          for file_name in dirs:
             descriptor = getDescriptor( cv2.imread(file_name,cv2.IMREAD_GRAYSCALE),self.shape)
             descriptors.append(descriptor)
             responses.append(idx)
      print('training')
      descriptors = np.squeeze(descriptors)
      self.train(
                     np.array(descriptors,dtype=np.float32),
                     np.array(responses,dtype=np.float32)
                     )
      print('trained')
예제 #4
0
def chart_data(): 
    loader = FileLoader(r'C:\Users\Ivo Ribeiro\Documents\open-cv\datasets\captures')
    loader.load_files()
    seed = 11
    tsne = TSNE(n_components=2,random_state=seed)
    fig = plt.figure()
    ax = fig.add_subplot(111)
    colors = ['b','g','r'] #3 classes
    all_features = []
    all_labels = []
    all_class_names = []

    for row in loader.files:
        imgs_fnames = row['imgs_per_class']
        features = []
        labels = []
        class_names = []
        for f_name in imgs_fnames:
            img = cv2.imread(f_name)
            descr = getDescriptor(img,expected_shape=(128,64)) 
            features.append(descr)
            labels.append(row['index'])
            class_names.append(row['class_name'])
        row['features'] = np.array(features)       
        all_features.extend(features)
        all_labels.extend(labels)
        all_class_names.extend(class_names)
    all_features = np.array(all_features)
    all_labels = np.array(all_labels)
  
    stds = np.std(all_features,axis=0)  
    all_features = all_features[:,stds > 0.0003]
    
    print(all_features.shape)
    print(all_labels.shape)

    for label,label_name in zip(np.unique(all_labels),np.unique(all_class_names)):
      features = all_features[ all_labels==label ,:]
      transformed = tsne.fit_transform(features)
      ax.scatter(x=transformed[:,0],y=transformed[:,1],c=colors[label],label=label_name)
    plt.title('captures dataset')
    ax.legend(loc='best')
    plt.show()
예제 #5
0
def show_std(): 
  knn = Knn()
  knn.run()
  if knn.loaded: 
    knn.load_files()
  descriptors = []
  for row in knn.files:
     imgs = row['imgs_per_class']
     for fname in imgs: 
       descriptors.append(  getDescriptor( cv2.imread(fname , cv2.IMREAD_GRAYSCALE) ),knn.shape  )
  descriptors = np.array(descriptors)
  stds = np.std(descriptors,axis=0)
  stds = stds * 100
  stds = np.int8(stds)
  min_bin = np.min(stds)
  max_bin = np.max(stds)
  bins = (max_bin-min_bin)
  hist = np.histogram(stds,bins=bins)
  hist = hist[0]
  plt.figure(0)
  plt.title('std (> better)')
  ranges = np.linspace(min_bin,max_bin,bins)
  plt.bar(ranges,hist)
  plt.show()
예제 #6
0
 def processAndPredict(self,sample , k = 6):
     descriptor = getDescriptor(sample,self.shape)  
     return self.knn.findNearest(np.array([descriptor],dtype=np.float32), k)