示例#1
0
    def predict_images(model):
        img_path = sys.argv[1]
        #img_path = input('image path: ')
        # img = openImage(img_path)
        # ret,img = cv2.threshold(img,130,255,cv2.THRESH_BINARY_INV)
        img = imp.load_image(img_path)
        img = ndimage.median_filter(img, 3)
        # imp.show_image(img)
        list_img = s._main(img)
        # imp.show_image_array(list_img)
        for i in range(len(list_img)):
            h, w = list_img[i].shape
            if h + w <= 5:
                continue
            img = list_img[i]
            imp.show_image_array(img)
            img = resize(img, 28, 28)
            img = np.array(img)
            #print(img)
            img = img / 255
            #print(img)
            img = np.reshape(img, (1, 28, 28, 1))
            result = model.predict_classes(img)
            #print('res: ',result)
            #print(labels)
            kio = le.inverse_transform(result)
            print(kio[0])
            with open('dataaa.csv') as csvfile:
                readCSV = csv.reader(csvfile, delimiter=',')
                filee = open("file.txt", "a")
                for row in readCSV:
                    if row[0] == kio[0]:
                        nee = row[1]
                        filee.write(nee)
                        print('UNICODE Value:', nee)
                        break

                filee.close()
示例#2
0
def main():
    sys.setrecursionlimit(9999)
    from sklearn.neighbors import KNeighborsClassifier

    #img_name = input('Input the image name to predict: ')
    img_name = sys.argv[1]
    img = openImage(img_name)
    #showImage(img)
    #gaussian filtering
    #img = cv2.blur(img,(5,5))
    ret, img = cv2.threshold(img, 130, 255, cv2.THRESH_BINARY)
    #showImage(img)
    img = ndimage.median_filter(img, 3)
    #print('background noise removed...')
    #showImage(img)

    trainingSet = []
    testSet = []
    split = 0.99
    loadDataset('dataset-1.csv', split, trainingSet, testSet)
    #print('Train set: '+repr(len(trainingSet)))
    #print('Test set: '+repr(len(testSet)))
    predictions = []
    k = 3
    #print('test: ',testSet)

    #.....preprocessing....

    #....line_segmentation.......
    lines = ls.segment(img)
    for u in range(len(lines)):
        #showImage(lines[u])
        #.....character segmentation.....
        list_img = s._main(lines[u])
        for i in range(len(list_img)):
            h, w = list_img[i].shape
            if h + w <= 5:
                continue
            list_img[i] = resize(list_img[i], 28, 28)
            #showImage(list_img[i])
            try:
                ret, list_img[i] = cv2.threshold(list_img[i], 130, 255,
                                                 cv2.THRESH_BINARY)
            except:
                print('')
            img = np.array(list_img[i])
            #showImage(img)
            array = np.array(img)

            array = np.reshape(array, (1, np.product(array.shape)))
            #showImage(list_img[i])

            #trainingSet = np.array(trainingSet)
            #print(trainingSet,'\n')

            knn = KNeighborsClassifier()

            trainingSet = np.array(trainingSet)

            length = len(trainingSet)
            #print('len: ',length)
            X = trainingSet[0:length, 0:784]
            Y = trainingSet[0:length, 784]

            #print('array: ',array)
            X = [[float(x) for x in rec] for rec in X]
            X = np.array(X)
            #print('array:',array)
            knn.fit(X, Y)
            #print('shape of X: ',X.shape)
            #print('shape of arr:',array.shape)
            print('predicted: ', knn.predict(array))
            g = knn.predict(array)
            print('Value:', g[0])

            with open('dataaa.csv') as csvfile:
                readCSV = csv.reader(csvfile, delimiter=',')
                filee = open("file.txt", "a")
                for row in readCSV:
                    if row[0] == g[0]:
                        nee = row[1]
                        filee.write(nee)
                        print('UNICODE Value:', nee)

                filee.close()

        webbrowser.open('http://127.0.0.1/FINALS/final.html')
示例#3
0
def main():
	sys.setrecursionlimit(99999)
	img_name = input('Input the image name containing training characters: ')
	img = openImage(img_name)
	showImage(img)
	
	#.....preprocessing....
	img = ndimage.median_filter(img, 3)
	#img = cv2.GaussianBlur(img,(5,5),0)
	#img = ske.skeleton(img)
	#print('after skeletonizing..')
	showImage(img)
	
	#.....segmentation.....
	list_img = s._main(img)
	
	label_name = input('Input the label of the segmented characters: ')
	csv_file_name1 = input('Dataset(without skeletonizing)PATH: ')
	csv_file_name2 = input('Dataset(with skeletonizing)PATH: ')
	#csv_file_name3 = input('Dataset(with zoning)PATH: ')
	
	#.....writing to csv.....
	csv_file_1 = open(csv_file_name1, 'a', newline = '')
	csv_file_2 = open(csv_file_name2, 'a', newline = '')
	#csv_file_3 = open(csv_file_name3, 'a', newline = '')
	with csv_file_1:
		with csv_file_2:
			writer1 = csv.writer(csv_file_1)
			writer2 = csv.writer(csv_file_2)
			for i in range(len(list_img)):
				try:
					w,h = list_img[i].shape
				except:
					continue
				if w+h <= 5:#to avoid very small segments
					continue
				#showImage(list_img[i])
				
				if True:
					try:
						list_img[i] = resize(list_img[i],28,28)
					except:
						continue
					#print('after resizing: ')
					#showImage(list_img[i])
					ret,skeleton = cv2.threshold(list_img[i],40,255,cv2.THRESH_BINARY_INV)
					#print('binarization: ')
					#showImage(list_img[i])
					skeleton = ske.skeleton(skeleton)
					print('after skeletonizing: ')
					showImage(skeleton)
					t = input('Want to store the feature values of this image? (Y / N)')
					if t not in ['Y','y']:
						continue
					#label_name = input('Input the label of the segmented characters: ')

					array1 = np.array(list_img[i])
					array1 = np.append(array1, [[label_name]])
					array1 = np.reshape(array1, (1,np.product(array1.shape)))
					
					array2 = np.array(skeleton)
					array2 = np.append(array2, [[label_name]])
					array2 = np.reshape(array2, (1,np.product(array2.shape)))
					
					writer1.writerows(array1)
					writer2.writerows(array2)
					print(((i+1)/len(list_img))*100, '% done...')