def train(self, readFromFile=True, path="./training3/"): """ train the image processor :return: True if the training succeeds, false otherwise. """ self.knn = ColorKnnClassifier() self.ratioKnns = [] for i in range(7): self.ratioKnns.append(LetterKnnClassifier()) if readFromFile: f = open(path, 'r') for l in f: l = l.split(",") x = [float(i) for i in l[1:]] self.ratioKnns[cc.getColorFromId(int(l[0]))].train.append((int(l[0]), x)) f.close() f = open("color_data.csv", 'r') for l in f: l = l.split(',') x = [float(i) for i in l[1:]] self.knn.train.append((int(l[0]), x)) f.close() else: fs = listdir(path) for f in fs: s = f.split(".") if len(s) < 2: continue s = f.split("_") if len(s) < 2: continue c1 = const.getConstantFromString(s[0]) c2 = cc.getColorFromId(c1) if c2 is not None: self.ratioKnns[c2].loadTrainingImage(path + f, c1) print "LetterClass. " + str(c2) + ":" + str( cc.getColorFromNumber(c2)) + " trained: " + f + " as " + str(const.getStringFromNumber(c1)) self.knn.trainModel() for r in self.ratioKnns: r.trainModel() print "Completed training" print "------------------"
def run(self): r = self.processor.knn.testFromImage(self.sec, show=False, seed=7) print int(r[0]), cc.getColorFromNumber(r[0]), v = self.processor.ratioKnns[int(r[0])].testFromImage(self.sec, show=False, seed=5) x = const.getStringFromNumber(v[0]) print x, self.row, self.col self.processor.lineLocks[self.row].acquire() self.processor.output[self.row][self.col] = x self.processor.lineLocks[self.row].release()
def train(self, readFromFile=True, path="./training3/"): """ train the image processor :return: True if the training succeeds, false otherwise. """ self.knn = ColorKnnClassifier() self.ratioKnns = [] for i in range(7): self.ratioKnns.append(LetterKnnClassifier()) if readFromFile: f = open(path, 'r') for l in f: l = l.split(",") x = [float(i) for i in l[1:]] self.ratioKnns[cc.getColorFromId(int(l[0]))].train.append( (int(l[0]), x)) f.close() f = open("color_data.csv", 'r') for l in f: l = l.split(',') x = [float(i) for i in l[1:]] self.knn.train.append((int(l[0]), x)) f.close() else: fs = listdir(path) for f in fs: s = f.split(".") if len(s) < 2: continue s = f.split("_") if len(s) < 2: continue c1 = const.getConstantFromString(s[0]) c2 = cc.getColorFromId(c1) if c2 is not None: self.ratioKnns[c2].loadTrainingImage(path + f, c1) print "LetterClass. " + str(c2) + ":" + str( cc.getColorFromNumber( c2)) + " trained: " + f + " as " + str( const.getStringFromNumber(c1)) self.knn.trainModel() for r in self.ratioKnns: r.trainModel() print "Completed training" print "------------------"