if __name__ == '__main__': symbols = [] #creating desc for each file with label for (dirpath,dirnames,filenames) in os.walk(path): for dirname in sorted(dirnames): print(dirname) symbols.append(dirname) for(direcpath,direcnames,files) in os.walk(path+"/"+dirname): file_list = list(files) #random.shuffle(file_list) for file in file_list[:BATCH_SIZE]: actual_path=path+"/"+dirname+"/"+file frame = cv2.imread(actual_path) des = func(frame) if des is None: # Eliminates empty items. continue img_descs.append(des) y.append(label) label=label+1 #finding indexes of test train and validate y=np.array(y) training_idxs, test_idxs, val_idxs = train_test_val_split_idxs(len(img_descs), 0.4, 0.0) #creating histogram using kmeans minibatch cluster model X, cluster_model = cluster_features(img_descs, training_idxs, MiniBatchKMeans(n_clusters=150)) #splitting data into test, train, validate using the indexes X_train, X_test, X_val, y_train, y_test, y_val = perform_data_split(X, y, training_idxs, test_idxs, val_idxs)
print("mlp started") clf.fit(X_train, y_train) y_pred = clf.predict(X_test) calc_accuracy("MLP classifier", y_test, y_pred) #creating desc for each file with label for (dirpath, dirnames, filenames) in os.walk(path): for dirname in dirnames: print(dirname) print(path + "/" + dirname) for (direcpath, direcnames, files) in os.walk(path + "/" + dirname): for file in files: actual_path = path + "/" + dirname + "/" + file print(actual_path) des = func(actual_path) if des != None: img_descs.append(des) y.append(label) label = label + 1 #finding indexes of test train and validate y = np.array(y) training_idxs, test_idxs, val_idxs = train_test_val_split_idxs( len(img_descs), 0.4, 0.0) #creating histogram using kmeans minibatch cluster model X, cluster_model = cluster_features(img_descs, training_idxs, MiniBatchKMeans(n_clusters=150)) #splitting data into test, train, validate using the indexes
img = cv2.flip(img, 1) cv2.imshow("original", img) axis_length = min(img.shape[0], img.shape[1]) diff = abs(img.shape[1] - img.shape[0]) if img.shape[0] < img.shape[1]: new_img = img[:, diff // 2:img.shape[1] - diff // 2] else: new_img = img[diff // 2:img.shape[0] - diff // 2, :] resized_img = cv2.resize(new_img, (200, 200)) image_data = cv2.imencode('.jpg', resized_img)[1].tostring() with open('input.jpg', 'wb') as image_file: image_file.write(image_data) img_des = func(resized_img) try: X, cluster_model = cluster_features([img_des], range(1), MiniBatchKMeans(n_clusters=150)) #print(cluster_model) y_pred = clf.predict(X) print("\n\nPredicted symbol:") print(symbols[int(y_pred)]) except ValueError: print("less features") if cv2.waitKey(2500) == ord('q'): break # Following line should... <-- This should work fine now