def __init__(self, in_queue, out_queue, options):
     threading.Thread.__init__(self)
     self.in_queue = in_queue
     self.out_queue = out_queue
     self.test_bodypart = None
     self.bodypart_knn_pos = cv2.KNearest()
     self.bodypart_knn_neg = cv2.KNearest()
     self.bodypart_trained_data_pos = None
     self.bodypart_vote = []
예제 #2
0
def get_matrix():
    chessboard_matrix = init_matrix()
    train_filename_list, train_label_list = train.find_picture('static/train/')
    train_file_list = train.preprocess_img(train_filename_list)

    test_filename_list, test_label_list = train.find_picture(
        'static/ClippedImg/')
    test_file_list = train.preprocess_img(test_filename_list)

    knn = cv2.KNearest()
    knn.train(train_file_list, train_label_list)
    ret, result, neighbours, dist = knn.find_nearest(test_file_list, k=3)
    for i in range(len(result)):
        #print int(result[i][0]),test_filename_list[i]
        if int(result[i][0]) == 1:
            position = re.search(r'\d{1,2}_\d{1,2}',
                                 str(test_filename_list[i])).group().split('_')
            xposition, yposition = int(position[0]), int(position[1])
            chessboard_matrix[xposition][yposition] = 1
        if int(result[i][0]) == 2:
            position = re.search(r'\d{1,2}_\d{1,2}',
                                 str(test_filename_list[i])).group().split('_')
            xposition, yposition = int(position[0]), int(position[1])
            chessboard_matrix[xposition][yposition] = 2
        # if str(test_filename_list[i]).find('2_2') != -1:
        #     print result[i],test_filename_list[i]
    return chessboard_matrix
예제 #3
0
def kNN(trainingData, trainingClasses, testData, testClasses):
    kNNClassifier = cv2.KNearest()
    kNNClassifier.train(trainingData, trainingClasses)
    ret, results, neighbours, dist = kNNClassifier.find_nearest(testData, 3)
    print results
    return calculateAccuracy(testClasses,
                             results), calcSensitivity(testClasses, results)
예제 #4
0
    def get_model_and_label_map(source_dir):
        responses = []
        label_map = []
        samples = numpy.empty((0, KNN_SQUARE_SIDE * KNN_SQUARE_SIDE),
                              numpy.float32)
        for label_idx, filename in enumerate(os.listdir(source_dir)):

            label = filename[:filename.index('.png')]
            label_map.append(label)
            responses.append(label_idx)

            image = cv2.imread(os.path.join(source_dir, filename), 0)

            suit_image_standard_size = cv2.resize(
                image, (KNN_SQUARE_SIDE, KNN_SQUARE_SIDE))
            sample = suit_image_standard_size.reshape(
                (1, KNN_SQUARE_SIDE * KNN_SQUARE_SIDE))
            samples = numpy.append(samples, sample, 0)

        responses = numpy.array(responses, numpy.float32)
        responses = responses.reshape((responses.size, 1))
        model = cv2.KNearest()
        model.train(samples, responses)

        return model, label_map
예제 #5
0
 def initKNN(self):
     print "Initialising letter classifier"
     self.kNN = cv2.KNearest()
     scriptdir = os.path.dirname(os.path.realpath(__file__))
     with np.load(scriptdir + '/knn.npz') as data:
         self.kNN.train(data['images'], data['letters'])
     print "Initialised letter classifier."
예제 #6
0
 def learn_from_pic(self, path_to_files, clear_all, to_save, to_train):
     # do you want to start from zero?
     # True: clear all
     # False -> just continue
     if (clear_all):
         self.samples = np.empty((0, self.roisize * self.roisize))
         self.responses = []
         self.model = 0
         self.model = cv2.KNearest()
     # make samples and responses
     cv2.namedWindow('norm', cv2.WINDOW_NORMAL)
     cv2.namedWindow('small', cv2.WINDOW_NORMAL)
     for img_path in path_to_files:
         imag = cv2.imread(img_path)
         binary = self.im_preprocess(imag)
         self.process_learning(imag, binary)
     cv2.destroyWindow('norm')
     cv2.destroyWindow('small')
     # ask for saving data
     # True -> save data
     # False -> nothing happens
     if (to_save):
         path_1 = self.path + 'samples_1.data'
         path_2 = self.path + 'responses_1.data'
         self.save_learned(path_1, path_2)
     # ask to train model on current datas
     # Y -> self.model.train(self.samples,self.responses)
     # N -> program ends
     if (to_train):
         responses = np.array(self.responses, np.float32)
         responses = responses.reshape((responses.size, 1))
         samples = np.array(self.samples, np.float32)
         self.model.train(samples, responses)
예제 #7
0
    def eval(self, knn_k=-1, Kpca=-1):
        if knn_k <= 0:
            knn_k = self.knn
        knn_k += 1  # exclude itself
        if Kpca < 0:
            Kpca = self.K

        responses = []
        for name, img, label in self.image_dictionary:
            responses.append(label)
        knn = cv2.KNearest()
        knn.train(self.weightTraining[0:Kpca, :].T.astype(np.float32),
                  np.asarray(responses, dtype=np.float32))
        # we have to discard the first predict result, since it has to be itself
        ret, results, neighbours2, dist = knn.find_nearest(
            self.weightTraining[0:Kpca, :].T.astype(np.float32), knn_k)
        neighbours = neighbours2[:, 1:]
        eval_data = []
        for idx, nb in enumerate(neighbours):
            neighbours_count = []
            for n in nb:
                neighbours_count.append(nb.tolist().count(n))
            vote = nb[neighbours_count.index(max(neighbours_count))]
            eval_data.append((vote, responses[idx]))
            #print ("predict:%s, neight: %s, label: %d" % (str(vote),str(nb), responses[idx]))
        return eval_data
예제 #8
0
def demo_digits():
    img = cv2.imread("digits.png")
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

    cells = [np.hsplit(row, 100) for row in np.vsplit(gray, 50)]

    x = np.array(cells)

    train = x[:, :50].reshape(-1, 400).astype(np.float32)
    test = x[:, 50:100].reshape(-1, 400).astype(np.float32)

    k = np.arange(10)
    train_labels = np.ndarray.flatten(np.repeat(k, 250)[:, np.newaxis])
    test_labels = train_labels.copy()

    # print(np.shape(train))
    # print(np.shape(train_labels))

    knn = cv2.KNearest()
    knn.train(train, train_labels)
    ret, result, neighbors, dist = knn.find_nearest(test, k=5)
    result = np.ndarray.flatten(result)

    # print result
    # print test_labels

    matches = (result == test_labels)

    correct = np.count_nonzero(matches)
    accuracy = correct * 100.0 / result.size

    print accuracy
예제 #9
0
def loadDatabase(dirName):
	print "Loading images from the object database..."
	files = os.listdir(dirName)
	sift = cv2.SIFT()
	Id = 0

	objKeyptDescr = [] #np.ndarray(shape=(0, 128), dtype=np.float32)
	"""Format: List of descriptors"""
	labelIds = [] #np.ndarray(shape=(0), dtype=np.int)
	"""Label ids for the descriptors"""
	global labels, noKeypoints

	for name in files:
		(shortname, extension) = os.path.splitext(name)
		if extension in ['.jpg', '.png', '.jpeg']:
			print "Loading ", shortname, "..."
			img = cv2.imread(os.path.join(dirName, name))#, cv2.IMREAD_GRAYSCALE)
			#cv2.imshow(shortname,img)
			kp, descrs = sift.detectAndCompute(img, None)
			for d in descrs:
				objKeyptDescr += [d]#np.append(objKeyptDescr, d)
				labelIds += [Id]#np.append(labelIds, Id)
			labels += [shortname]
			noKeypoints += [len(descrs)]
			Id += 1

	global classifier
	classifier = cv2.KNearest()
	classifier.train(np.array(objKeyptDescr),np.array(labelIds))
예제 #10
0
파일: 2.knn.py 프로젝트: 1oss1ess/gittest
def main():
    # Training set of 20 random (x, y) values, x,y is between 0 and 49
    train_data = np.random.randint(0, MAX_INT,
                                   (DATA_SIZE, 2)).astype(np.float32)
    # Labels for each value in the training set - 0 for red, 1 for blue
    labels = np.random.randint(0, 2, (DATA_SIZE, 1)).astype(np.float32)

    # Red data: take all training data with label 0
    red = train_data[labels.ravel() == 0]
    # Plot: x points, y points, s - size of points,
    # c - color, marker - the shape
    plt.scatter(red[:, 0], red[:, 1], s=100, c='r', marker='^')

    # Blue data: take all training data with label 1
    blue = train_data[labels.ravel() == 1]
    plt.scatter(blue[:, 0], blue[:, 1], s=100, c='b', marker='s')

    # Green data: A single random (x, y) for input data
    input_data = np.random.randint(0, MAX_INT, (1, 2)).astype(np.float32)
    plt.scatter(input_data[:, 0], input_data[:, 1], s=100, c='g', marker='o')

    knn = cv2.KNearest()
    knn.train(train_data, labels)
    # Find k=3 nearest neighbours and classify the given input data
    retval, results, neighbours, dists = knn.find_nearest(input_data, k=3)

    classified_label = get_human_label(results[0][0])
    print('The input point (%s, %s) is %s' %
          (input_data[0][0], input_data[0][1], classified_label))
    for i, neighbour in enumerate(neighbours[0].tolist()):
        print('Neightbour #%s is %s, distance to point: %s' %
              (i + 1, get_human_label(neighbour), dists[0][i]))

    plt.show()
    def __init__(self, keypoints, descriptors, referencePointYX, bounds, eye1, eye2):
        """
            takes keypoints and descriptors for an image, and sets up the
            internals of the class as though it's just been run.
            internals:
                knn classifier
                reference point

            the main array of descriptors is also set up, with:
                 descriptors
                 vector to virtual point
                 matchedLocation: (None if not matched)
                 weight
            as representation may change, setters and gettters should be used
        """
        self.reference = referencePointYX
        assert(len(descriptors) > 0)
        self.rowsize = len(descriptors) / len(keypoints)
        #this doesn't need to be self.saved here now, but I may want it later:
        keypoints, self.descriptors = self.cropToBounds(keypoints, descriptors, bounds, eye1, eye2)
        labels = np.arange(len(keypoints), dtype = np.float32)
        self.oldknn = cv2.KNearest()
        #train the KNN matcher
        self.oldknn.train(self.descriptors, labels)
        self.keypointdata = []
        for kp in keypoints:
            vector = (self.reference[0] - kp.pt[0], self.reference[1] - kp.pt[1])
            data = self.mykeypoint(vector = vector)
            self.keypointdata.append(data)
예제 #12
0
def test_scanner():
    #######   training part    ###############
    samples = np.loadtxt('generalsamples.data',np.float32)
    responses = np.loadtxt('generalresponses.data',np.float32)
    responses = responses.reshape((responses.size,1))

    model = cv2.KNearest()
    model.train(samples,responses)
    ############################# testing part  #########################
    test_image_path = image_dir+'digits-test-1.png'
    im = cv2.imread(test_image_path)
    out = np.zeros(im.shape,np.uint8)
    gray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
    thresh = cv2.adaptiveThreshold(gray,255,1,1,11,2)

    contours,hierarchy = cv2.findContours(thresh,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)

    for cnt in contours:
        if cv2.contourArea(cnt)>50:
            [x,y,w,h] = cv2.boundingRect(cnt)
            if  h>28:
                cv2.rectangle(im,(x,y),(x+w,y+h),(0,255,0),2)
                roi = thresh[y:y+h,x:x+w]
                roismall = cv2.resize(roi,(10,10))
                roismall = roismall.reshape((1,100))
                roismall = np.float32(roismall)
                retval, results, neigh_resp, dists = model.find_nearest(roismall, k = 1)
                string = str(int((results[0][0])))
                cv2.putText(out,string,(x,y+h),0,1,(0,255,0))

    cv2.imshow('im',im)
    cv2.imshow('out',out)
    cv2.waitKey(0)
예제 #13
0
파일: Main.py 프로젝트: icguy/PySudoku
def get_knn():
    from glob import glob
    import os
    x = np.zeros((10, 224, 20, 20))

    for i in range(10):
        print i
        folder = "images/pdf2png/%d/" % i
        files = glob(folder + "*.png")
        for file in files:
            fname = os.path.basename(file)
            idx = int(fname[:4])
            img = cv2.imread(file, 0)
            x[i, idx, :, :] = img

    # Make it into a Numpy array. It size will be (50,100,20,20)
    # x = np.array(cells)

    # Now we prepare train_data and test_data.
    train = x[:, :112].reshape(-1, 400).astype(np.float32)  # Size = (1120,400)
    test = x[:, 112:224].reshape(-1, 400).astype(np.float32)  # Size = (1120,400)

    # Create labels for train and test data
    k = np.arange(10)
    train_labels = np.repeat(k, 112)[:, np.newaxis]
    test_labels = train_labels.copy()

    # Initiate kNN, train the data, then test it with test data for k=1
    knn = cv2.KNearest()
    knn.train(train, train_labels)
    return knn
예제 #14
0
def knn():

    total_samples = len(glob.glob("images/training_data/**/*"))

    samples = np.zeros((total_samples, FEATURE_LENGTH), dtype=np.float32)
    responses = []

    training_folders = glob.glob("images/training_data/*")
    i = 0
    for f in training_folders:
        piece_type = f[21:]
        for training_image in glob.glob(f + "/*"):
            img = cv2.imread(training_image)

            samples[i] = image_feature(img)
            i += 1
            responses.append(ord(piece_type))
    responses = np.array(responses, dtype=np.float32)
    responses = responses.reshape((responses.size, 1))

    test_samples = samples[-10:]
    test_responses = responses[-10:]

    samples = samples[:-10]
    responses = responses[:-10]

    knn = cv2.KNearest()
    knn.train(samples, responses)
    return knn
예제 #15
0
 def __init__(self, trainName, testName, save = 0):
     self.save = save
     self.trainName = trainName
     self.testName = testName
     self.trainData = np.empty(shape = [0, 3])
     self.testData = self.trainData.copy()
     self.knn = cv2.KNearest()
예제 #16
0
def demo_alphabet():
    data = np.loadtxt('letter-recognition.data',
                      dtype='float32',
                      delimiter=',',
                      converters={0: lambda ch: ord(ch) - ord('A')})

    train, test = np.vsplit(data, 2)

    responses, trainData = np.hsplit(train, [1])
    labels, testData = np.hsplit(test, [1])

    # print labels
    #
    # print np.shape(trainData)
    # print np.shape(testData)
    #
    # print np.shape(responses)
    # print np.shape(labels)

    knn = cv2.KNearest()
    knn.train(trainData, responses)
    ret, result, neighbors, dist = knn.find_nearest(testData, k=5)

    # result = np.ndarray.flatten(result)

    # print(result)

    correct = np.count_nonzero(result == labels)
    accuracy = correct * 100.0 / 10000
    print accuracy
예제 #17
0
    def learn_digits(self):

        train_digits = mnist.read("training")
        k_number = []
        k_label = []
        for i in range(5000):
            k = train_digits.next()
            k_label.append(k[0])
            k_number.append(k[1])
        y = np.array(list(k_label))
        x = np.array(list(k_number))
        print y[0]
        print x[0]

        # Now we prepare train_data and test_data.
        train = x[:5000].reshape(-1, 784).astype(np.float32)

        # Create labels for train and test data
        k = np.arange(10)
        train_labels = y[:5000].astype(np.int)

        # Initiate kNN, train the data, then test it with test data for k=1
        knn = cv2.KNearest()
        knn.train(train, train_labels)

        number = self.edit_image(self.snap())
        number = number.reshape(-1, 784).astype(np.float32)
        nparray = np.array(number)
        ret2, result2, neighbours2, dist2 = knn.find_nearest(nparray, k=5)
        print result2
예제 #18
0
def createDigitsModel(fontfile, digitheight):
    font = ImageFont.truetype(fontfile, digitheight)
    samples = np.empty((0, digitheight * (digitheight / 2)))
    responses = []
    for n in range(10):
        pil_im = Image.new("RGB", (digitheight, digitheight * 2))
        ImageDraw.Draw(pil_im).text((0, 0), str(n), font=font)
        pil_im = pil_im.crop(pil_im.getbbox())
        pil_im = ImageOps.invert(pil_im)
        #pil_im.save(str(n) + ".png")

        # convert to cv image
        cv_image = cv2.cvtColor(np.array(pil_im), cv2.COLOR_RGBA2BGRA)
        gray = cv2.cvtColor(cv_image, cv2.COLOR_BGR2GRAY)
        blur = cv2.GaussianBlur(gray, (5, 5), 0)
        thresh = cv2.adaptiveThreshold(blur, 255, 1, 1, 11, 2)

        roi = cv2.resize(thresh, (digitheight, digitheight / 2))
        responses.append(n)
        sample = roi.reshape((1, digitheight * (digitheight / 2)))
        samples = np.append(samples, sample, 0)

    samples = np.array(samples, np.float32)
    responses = np.array(responses, np.float32)

    model = cv2.KNearest()
    model.train(samples, responses)
    return model
 def __init__(self, character_width=15, character_height=15, classifier_type="svm_linear"):
     self.character_width = character_width
     self.character_height = character_height
     self.classifier_type = classifier_type
     self.resize = 128
     
     if classifier_type == "knn":
         self.classifier = cv2.KNearest()
     else:
         self.classifier = cv2.SVM()
         
     if self.classifier_type == "svm_linear_hog":                
         winSize = (128,128)
         blockSize = (32,32)
         blockStride = (16,16)
         cellSize = (8,8)
         nbins = 9
         derivAperture = 1
         winSigma = 4.
         histogramNormType = 0
         L2HysThreshold = 2.0000000000000001e-01
         gammaCorrection = 0
         nlevels = 32
         self.hog = cv2.HOGDescriptor(winSize,blockSize,blockStride,cellSize,nbins,derivAperture,winSigma,
                                 histogramNormType,L2HysThreshold,gammaCorrection,nlevels)
예제 #20
0
    def clusterize_colors(self, img, imgName):
        """Reduce colors by clusterizing the colors using K-Nearest algorithm.
		"""
        if img is None:
            return

    # Set the color classes
        colors = np.array(
            [[0x00, 0x00, 0x00], [0xff, 0xff, 0xff], [0xff, 0x00, 0xff]],
            dtype=np.float32)
        classes = np.array([[0], [1], [2]], np.float32)

        # Predict with K-Nearest
        knn = cv2.KNearest()
        knn.train(colors, classes)
        img_flatten = np.reshape(np.ravel(img, 'C'), (-1, 3))
        retval, result, neighbors, dist = knn.find_nearest(
            img_flatten.astype(np.float32), 1)

        # Set new colors
        dst = colors[np.ravel(result, 'C').astype(np.uint8)]
        dst = dst.reshape(img.shape).astype(np.uint8)

        # Save image
        if self.debug:
            cv2.imwrite(imgName, dst)

        return dst
예제 #21
0
파일: fonctions.py 프로젝트: Dogeek/TIPE
def recognize(thresh, AireContourMini, hmin, factor=1):
    digits = []
    #Entrainement de kNN à partir de deux fichiers
    samples = np.loadtxt('samples.data', np.float32)
    responses = np.loadtxt('responses.data', np.float32)
    responses = responses.reshape((responses.size, 1))
    model = cv2.KNearest()
    model.train(samples, responses)
    #Application
    contours, hierarchy = cv2.findContours(thresh, cv2.RETR_LIST,
                                           cv2.CHAIN_APPROX_SIMPLE)
    for cnt in contours:
        if cv2.contourArea(cnt) > AireContourMini:
            [x, y, w, h] = cv2.boundingRect(cnt)
            if h > hmin:
                roi = thresh[y:y + h, x:x + w]
                roismall = cv2.resize(roi, (10, 10))
                roismall = roismall.reshape((1, 100))
                roismall = np.float32(roismall)
                retval, results, neigh_resp, dists = model.find_nearest(
                    roismall, k=factor)
                string = str(int((results[0][0])))
                digits.append(string)
    cv2.waitKey(0)
    return digits
    pass
예제 #22
0
def icp(d1, d2, max_iterate=100):
    src = np.array([d1.T], copy=True).astype(np.float32)
    dst = np.array([d2.T], copy=True).astype(np.float32)

    knn = cv2.KNearest()
    responses = np.array(range(len(d2[0]))).astype(np.float32)
    knn.train(src[0], responses)

    Tr = np.array([[np.cos(0), -np.sin(0), 0], [np.sin(0),
                                                np.cos(0), 0], [0, 0, 1]])

    dst = cv2.transform(dst, Tr[0:2])
    max_dist = sys.maxint

    scale_x = np.max(d1[0]) - np.min(d1[0])
    scale_y = np.max(d1[1]) - np.min(d1[1])
    scale = max(scale_x, scale_y)

    for i in range(max_iterate):
        ret, results, neighbours, dist = knn.find_nearest(dst[0], 1)

        indeces = results.astype(np.int32).T
        indeces = del_miss(indeces, dist, max_dist)

        T = cv2.estimateRigidTransform(dst[0, indeces], src[0, indeces], True)

        max_dist = np.max(dist)
        dst = cv2.transform(dst, T)
        Tr = np.dot(np.vstack((T, [0, 0, 1])), Tr)

        if (is_converge(T, scale)):
            break

    return Tr[0:2]
예제 #23
0
 def __init__(self, k=1, debug=False):
     if get_opencv_version() == 3:
         self.knn = cv2.ml.KNearest_create()
     else:
         self.knn = cv2.KNearest()
     self.k = k
     self.debug = debug
예제 #24
0
    def create_model(self, train_folder):
        """
        Return the training set, its labels and the trained model
        :param train_folder: folder where to retrieve data
        :return: (train_set, train_labels, trained_model)
        """
        digits = []
        labels = []
        for n in range(1, 10):
            folder = train_folder + str(n)
            samples = [pic for pic in os.listdir(folder)
                       if os.path.isfile(os.path.join(folder, pic))]

            for sample in samples:
                image = cv2.imread(os.path.join(folder, sample))
                # Expecting black on white
                image = 255 - cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
                _, image = cv2.threshold(image, 0, 255,
                                         cv2.THRESH_BINARY + cv2.THRESH_OTSU)
                feat = self.feature(image)
                digits.append(feat)
                labels.append(n)

        digits = np.array(digits, np.float32)
        labels = np.array(labels, np.float32)
        if cv2.__version__[0] == '2':
            model = cv2.KNearest()
            model.train(digits, labels)
        else:
            model = cv2.ml.KNearest_create()
            model.train(digits, cv2.ml.ROW_SAMPLE, labels)
        return digits, labels, model
예제 #25
0
 def __init__(self,
              saved_model=None,
              train_folder=None,
              feature=_feature.__func__):
     """
     :param saved_model: optional saved train set and labels as .npz
     :param train_folder: optional custom train data to process
     :param feature: feature function - compatible with saved_model
     """
     self.feature = feature
     if train_folder is not None:
         self.train_set, self.train_labels, self.model = \
             self.create_model(train_folder)
     else:
         if cv2.__version__[0] == '2':
             self.model = cv2.KNearest()
         else:
             self.model = cv2.ml.KNearest_create()
         if saved_model is None:
             saved_model = TRAIN_DATA+'raw_pixel_data.npz'
         with np.load(saved_model) as data:
             self.train_set = data['train_set']
             self.train_labels = data['train_labels']
             if cv2.__version__[0] == '2':
                 self.model.train(self.train_set, self.train_labels)
             else:
                 self.model.train(self.train_set, cv2.ml.ROW_SAMPLE,
                                  self.train_labels)
예제 #26
0
파일: CVKNN.py 프로젝트: ssj018/maxent
def teamMain():
    DIR = '/home/archer/Documents/maxent/data/basketball/leaguerank/'
    teamIds = loadTeamIds(DIR + 'teamidshortname.csv')
    teamNames = [x[1] for x in loadMatrixFromFile(DIR + 'teamidshortname.csv')]
    countTotal = 0
    total = 0

    for team in teamIds:
        trainData = buildTrainingSets(DIR + team + '-train.csv')
        trainLabels = buildTrainingLabels(DIR + team + '-train.csv')
        testData = buildTestingSets(DIR + team + '-test.csv')
        testLabels = buildTestingLabels(DIR + team + '-test.csv')
        total = total + len(testLabels)

        knn = cv2.KNearest()
        knn.train(trainData, trainLabels)

        # Accuracy
        count = 0
        for i in range(len(testLabels)):
            ret, results, neighbours, dist = knn.find_nearest(
                np.array([testData[i]]), 11)
            if results[0][0] == testLabels[i][0]:
                count = count + 1

        countTotal = countTotal + count
        print 'INFO: Accuracy(', teamNames[teamIds.index(
            team)], ')', count / float(len(testLabels))
    print 'INFO: Total Accuracy: ', countTotal / float(total)
예제 #27
0
파일: CVKNN.py 프로젝트: ssj018/maxent
def seasonMain():
    DIR = '/home/archer/Documents/maxent/data/basketball/leaguerank/'
    seasons = loadSeasons(DIR + 'seasons-18-Nov-2014.txt')
    countTotal = 0
    total = 0

    for season in seasons:
        trainData = buildTrainingSets(DIR + season + '-train.csv')
        testData = buildTestingSets(DIR + season + '-test.csv')
        trainLabels = buildTestingLabels(DIR + season + '-train.csv')
        testLabels = buildTestingLabels(DIR + season + '-test.csv')
        total = total + len(testLabels)

        knn = cv2.KNearest()
        knn.train(trainData, trainLabels)

        # Accuracy
        count = 0
        for i in range(len(testLabels)):
            ret, results, neighbours, dist = knn.find_nearest(
                np.array([testData[i]]), 11)
            if results[0][0] == testLabels[i][0]:
                count = count + 1

        countTotal = countTotal + count
        print 'INFO: Accuracy(', season, ')', count / float(len(testLabels))

    print 'INFO: Total Accuracy: ', countTotal / float(total)
예제 #28
0
 def __init__(self):
     self.knn = cv2.KNearest()
     self.results = []
     self.a = 10.0  #KNN can't train on letters so a = 10 and m = 11
     self.m = 11.0
     self.mnist_labels = []
     self.mnist_data = []
     return
예제 #29
0
    def train_from_file(self, train_data_file):
        train = np.load(train_data_file + "/train_captchas_data_images.npy")
        train_labels = np.load(train_data_file +
                               "/train_captchas_data_labels.npy")
        knn = cv2.KNearest()
        knn.train(train, train_labels)

        return knn
예제 #30
0
 def __init__(self, className):
     """
     Note: Set Settings.K_NUMBER_OF_NEIGHBORS_K for the variable k before calling this constructor.
     """
     super(KNearestNeighbours, self).__init__(ModelType.KNearest, True)
     self.className = className
     self.__model = cv.KNearest()
     self.k = Settings.K_NUMBER_OF_NEIGHBORS_K