def get_save_path(self): """ Returns the current model root path.""" fileName = self.modelUuid path = utils.get_data_path() + fileName + "/" try: utils.create_dir_if_necessary(path) except: logging.exeption( "Could not create dir to save classifier in. Saving in {0} instead." .format(utils.get_data_path())) path = utils.get_data_path() return path
def learn_custom_marker(self): """ "Learns a custom rectangular marker object by calculating SIFT keypoints.""" markerImage = load_image(utils.get_path_via_file_ui(), 0, 1) # save image cv.imwrite(utils.get_data_path() + "segmentationMarkerImage.jpg", markerImage) sift = cv.SIFT() self.CUSTOM_MARKER = sift.detectAndCompute(markerImage, None) # contains keypoints and descriptors self.MARKER_SIZE = (utils.value_question("[Marker Size]", "What is the marker width in cm?", "f"), utils.value_question("[Marker Size]", "What is the marker height in cm?", "f")) # save marker (not saving keypoints as they are not pickable) markerFile = {"marker": self.CUSTOM_MARKER[1], "markerDimension": self.MARKER_SIZE} with open(utils.get_data_path() + "segmentationMarker", "wb") as f: pickle.dump(markerFile, f) raw_input("Learning complete. Press any key to continue")
def restore_custom_marker(self): """ Restores a custom marker from a file.""" path = utils.get_data_path() + "segmentationMarker" if utils.check_if_file_exists(path): with open(path, "r") as f: markerFile = pickle.load(f) self.MARKER_SIZE = markerFile["markerDimension"] # restore kps self.CUSTOM_MARKER_IMAGE = cv.imread(utils.get_data_path() + "segmentationMarkerImage.jpg", 0) sift = cv.SIFT() kp = sift.detect(self.CUSTOM_MARKER_IMAGE,None) self.CUSTOM_MARKER = (kp, markerFile["marker"]) print "restored custom marker"
def restore_classifier(self): # does the directory exist? path = utils.get_data_path() + self.name + "/" if not utils.check_if_dir_exists(path): return False # iterate over __classifiers and try to restore them using the name for classifierIndex in xrange(len(self.__classifiers)): # check classifier folder classifier = self.__classifiers[classifierIndex] classifierPath = utils.get_data_path() + classifier.name + "/" if not utils.check_if_dir_exists(classifierPath): return False self.__classifiers[classifierIndex] = classifier.load( classifierPath) print "* loaded {0} {1} including {2} SVMs.".format( str(classifier), classifier.name, len(self.__classifiers[classifierIndex].svms)) print "** Successfully loaded all classifiers.\n" return True
def get_model_dict(): """ Loads and returns the model dictionary.""" # load classifier dictionary path = utils.get_data_path() + "model_dictionary" # initialize if file doesn't exist modelDictionary = {} if utils.check_if_file_exists(path): with open(path, "r") as f: modelDictionary = pickle.load(f) return modelDictionary
def load(self): path = get_data_path() + "settings.dat" if not os.path.isfile(path): return self with open(path, "r") as f: loadedSettings = pickle.load(f) # update module values # G - General Settings.G_TEST_DATA_PATH = loadedSettings.G_TEST_DATA_PATH Settings.G_EVALUATION_DETAIL_HIGH = loadedSettings.G_EVALUATION_DETAIL_HIGH Settings.G_DETAILED_CONSOLE_OUTPUT = loadedSettings.G_DETAILED_CONSOLE_OUTPUT Settings.G_MAIL_FROM = loadedSettings.G_MAIL_FROM Settings.G_MAIL_TO = loadedSettings.G_MAIL_TO Settings.G_MAIL_SERVER = loadedSettings.G_MAIL_SERVER Settings.G_MAIL_USER = loadedSettings.G_MAIL_USER Settings.G_MAIL_PASSWD = loadedSettings.G_MAIL_PASSWD
def load(self, path=utils.get_data_path(), fileName=None): if fileName is None: fileName = "knn_" + self.className path += fileName + ".xml" self.__model.load(path)
def save(self, path=utils.get_data_path(), fileName=None): if fileName is None: fileName = "knn_" + self.className path += fileName + ".xml"
def save(self, path=utils.get_data_path(), fileName=None): if fileName is None: fileName = "svm_" + self.className path += fileName + ".xml" self.__model.save(path)
def save(self, path=utils.get_data_path(), fileName=None): print "No pickle support. Use sklearn save method."
def load(self, path=utils.get_data_path(), fileName=None): """ Method to load a svm.""" raise NotImplementedError("abstract class")
def save(self): path = get_data_path() + "settings.dat" with open(path, "wb") as f: pickle.dump(self, f)
class HighscoreService(object): """Deprecated High score class.""" HIGHSCORES_STANDARD_PATH = utils.get_data_path() + "Highscores.dat" def __init__(self): self.highscores = [] def insert_score(self, score, description): """ Inserts a score and a description and resorts the resulting list of scores.""" self.highscores.append((score, description)) self.__sort_scores() # calculate position position = 0 for i in range(len(self.highscores)): s, _ = self.highscores[i] if s == score: position = i + 1 self.save() return position def get_position(self, position): """ Returns the score for a position.""" return self.highscores[position - 1] def print_surrounding_highscores(self, position): """ Prints the surrounding scores around a position. Takes two scores before and two after position. """ # prevent overflow. Positions are naturally measured from 1 to n (not from 0 to n-1) betterPosition = max(1, position - 2) worsePosition = min(len(self.highscores), position + 2) higherScores = self.highscores[betterPosition - 1:position - 1] lowerScores = self.highscores[position:worsePosition] own_score, own_description = self.highscores[position - 1] for score, description in higherScores: print "\t\t[{0}]\t{1}".format(str(round(score, 3)), description) print "**\t{0}.\t[{1}]\t{2} **".format(position, str(round(own_score, 3)), own_description) for score, description in lowerScores: print "\t\t[{0}]\t{1}".format(str(round(score, 3)), description) def print_highscore(self): """ Prints the complete high score list to the console.""" for i in range(len(self.highscores)): score, description = self.highscores[i] print "\t{0}.\t[{1}]\t{2}".format(i + 1, str(round(score, 3)), description) def __sort_scores(self): self.highscores = sorted(self.highscores, key=operator.itemgetter(0), reverse=True) def load(self): if not os.path.isfile(HighscoreService.HIGHSCORES_STANDARD_PATH): self.highscores = [] return self with open(HighscoreService.HIGHSCORES_STANDARD_PATH, "r") as f: self = pickle.load(f) return self def save(self): with open(HighscoreService.HIGHSCORES_STANDARD_PATH, "wb") as f: pickle.dump(self, f)
def save_model_dict(modelDictionary): """ Saves the model dictionary to file.""" with open(utils.get_data_path() + "model_dictionary", "wb") as f: pickle.dump(modelDictionary, f)