Commit d920310e authored by SCtom's avatar SCtom

ส่งภาพ ทำส่วน machine learning

parent 4a3b7390
...@@ -62,14 +62,14 @@ class Board(QWidget): ...@@ -62,14 +62,14 @@ class Board(QWidget):
cnt = 0 cnt = 0
maxf = 0 maxf = 0
for file in os.listdir(label): for file in os.listdir(label):
if file.endswith('.bmp'): if file.endswith('.png'):
cnt += 1 cnt += 1
tmpN = int(file[:-4:]) tmpN = int(file[:-4:])
maxf = max(maxf,tmpN) maxf = max(maxf,tmpN)
visibleImage.save('tmp.bmp') visibleImage.save('tmp.png')
pixmap = QPixmap('tmp.bmp').scaled(8,8) pixmap = QPixmap('tmp.png').scaled(64,64)
pixmap.save(label+'/'+str(max(cnt,maxf)+1)+'.bmp') pixmap.save(label+'/'+str(max(cnt,maxf)+1)+'.png')
QFile.remove('tmp.bmp') QFile.remove('tmp.png')
def paintEvent(self, event): def paintEvent(self, event):
......
...@@ -5,6 +5,16 @@ from PyQt5.QtWidgets import (QAction, QApplication, QWidget,QGridLayout) ...@@ -5,6 +5,16 @@ from PyQt5.QtWidgets import (QAction, QApplication, QWidget,QGridLayout)
#from trainWidget import TrainWidget #from trainWidget import TrainWidget
#from testWidget import TestWidget #from testWidget import TestWidget
from ubuwidgets import (Button, ComboWidget, Board) from ubuwidgets import (Button, ComboWidget, Board)
import os
import matplotlib.pyplot as plt
# Import datasets, classifiers and performance metrics
from sklearn import datasets, svm, metrics
from PIL import Image
import numpy as np
import math
from sklearn.metrics import accuracy_score
from sklearn.externals import joblib
class MainWindow(QWidget): class MainWindow(QWidget):
def __init__(self,parent=None): def __init__(self,parent=None):
...@@ -26,9 +36,55 @@ class MainWindow(QWidget): ...@@ -26,9 +36,55 @@ class MainWindow(QWidget):
def on_clear(self): def on_clear(self):
self.scribbleArea.clearImage() self.scribbleArea.clearImage()
def data_prep(self):
print('preporcessing..')
def on_train(self): def on_train(self):
self.data_prep()
print('training..') print('training..')
list_of_label = os.listdir('Image/')
digit_data = list()
digit_label = list()
for label in list_of_label:
label_path = 'Image/'+str(label)+'/'
list_of_data = os.listdir(label_path)
#print(list_of_data)
for data in list_of_data:
im = Image.open(label_path+'/'+data)
arr_im = np.array(im)
list_im = str(arr_im.tolist())
width, height = im.size
gen_data = np.zeros(width*height)
count = 0
for i in range(len(arr_im)):
for j in range(len(arr_im[i])):
#ทำ Normalize ด้วยการ (R+G+B) หาร 765 มาจาก (255+255+255)
gen_data[count] = math.floor(((np.sum(arr_im[i][j][:3])/765)))
count+=1
digit_data.append( gen_data )
digit_label.append( label )
digit_data = np.array(digit_data) #ข้อมูล
digit_label = np.array(digit_label) # label ของข้อมูล
# Create a classifier: a support vector classifier
classifier = svm.SVC(gamma=0.001, C=.1)
# We learn the digits on the first half of the digits
classifier.fit(digit_data, digit_label)
#save model
joblib.dump(classifier, 'svmModel.pkl')
expected = digit_label
predicted = classifier.predict(digit_data)
print("Classification report for classifier %s:\n%s\n"
% (classifier, metrics.classification_report(expected, predicted)))
print("Confusion matrix:\n%s" % metrics.confusion_matrix(expected, predicted))
print("\naccuracy = ",accuracy_score(digit_label, predicted))
def on_save(self): def on_save(self):
print('on_save') print('on_save')
...@@ -37,6 +93,21 @@ class MainWindow(QWidget): ...@@ -37,6 +93,21 @@ class MainWindow(QWidget):
def on_test(self): def on_test(self):
print("testing...") print("testing...")
clf = joblib.load('svmModel.pkl')
print('preporcessing..')
i = Image.open('test/t.png')
iar = np.array(i)
test_data = np.zeros(64*64)
len(iar[0])
count = 0
for i in range(len(iar)):
for j in range(len(iar[i])):
test_data[count] = math.floor(((np.sum(iar[i][j][:3])/765)))
#print(test_data[count])
count+=1
predicted = clf.predict([test_data])
print(predicted)
if __name__ == '__main__': if __name__ == '__main__':
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment