Commit 66cc13b3 authored by SCtom's avatar SCtom

เขียนโค้ดสั้นลง

parent 79e90759
...@@ -142,6 +142,7 @@ class Board(QWidget): ...@@ -142,6 +142,7 @@ class Board(QWidget):
self.image = newImage self.image = newImage
def data_prep_test(self): def data_prep_test(self):
self.saveImageTest()
im = Image.open('Test/temp.png') im = Image.open('Test/temp.png')
iar = np.array(im) iar = np.array(im)
...@@ -155,7 +156,7 @@ class Board(QWidget): ...@@ -155,7 +156,7 @@ class Board(QWidget):
test_data[count] = math.floor(((np.sum(iar[i][j][:3]/765)))) test_data[count] = math.floor(((np.sum(iar[i][j][:3]/765))))
#print(test_data[count]) #print(test_data[count])
count+=1 count+=1
return test_data return [test_data]
def data_prep_train(self): def data_prep_train(self):
print('preporcessing..') print('preporcessing..')
...@@ -182,4 +183,12 @@ class Board(QWidget): ...@@ -182,4 +183,12 @@ class Board(QWidget):
digit_label.append( label ) digit_label.append( label )
digit_data = np.array(digit_data) #ข้อมูล digit_data = np.array(digit_data) #ข้อมูล
digit_label = np.array(digit_label) # label ของข้อมูล digit_label = np.array(digit_label) # label ของข้อมูล
return digit_data,digit_label return digit_data,digit_label
\ No newline at end of file
def evaluation(self, digit_data, digit_label, classifier):
predicted = classifier.predict(digit_data)
print("Classification report for classifier %s:\n%s\n"
% (classifier, metrics.classification_report(digit_label, predicted)))
print("Confusion matrix:\n%s" % metrics.confusion_matrix(digit_label, predicted))
print("\naccuracy = ",accuracy_score(digit_label, predicted))
\ No newline at end of file
...@@ -2,6 +2,10 @@ from PyQt5.QtCore import Qt ...@@ -2,6 +2,10 @@ from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import (QAction, QApplication, QWidget,QGridLayout) from PyQt5.QtWidgets import (QAction, QApplication, QWidget,QGridLayout)
from ubuwidgets import (Button, ComboWidget, Board) from ubuwidgets import (Button, ComboWidget, Board)
# Import datasets, classifiers and performance metrics
from sklearn import svm,
from sklearn.externals import joblib
class MainWindow(QWidget): class MainWindow(QWidget):
def __init__(self,parent=None): def __init__(self,parent=None):
super(MainWindow, self).__init__(parent) super(MainWindow, self).__init__(parent)
...@@ -10,6 +14,7 @@ class MainWindow(QWidget): ...@@ -10,6 +14,7 @@ class MainWindow(QWidget):
self.trainButton = Button(self, "Train", self.on_train) self.trainButton = Button(self, "Train", self.on_train)
self.saveCombo = ComboWidget(self, "Label: ", "Save", self.on_save) self.saveCombo = ComboWidget(self, "Label: ", "Save", self.on_save)
self.testCombo = ComboWidget(self, "Result: ", "Test", self.on_test, True) self.testCombo = ComboWidget(self, "Result: ", "Test", self.on_test, True)
grid = QGridLayout() grid = QGridLayout()
grid.addWidget(self.scribbleArea,1,0) grid.addWidget(self.scribbleArea,1,0)
grid.addWidget(self.clearButton,2,0) grid.addWidget(self.clearButton,2,0)
......
sessions/session-4/Test/temp.png

343 Bytes | W: | H:

sessions/session-4/Test/temp.png

406 Bytes | W: | H:

sessions/session-4/Test/temp.png
sessions/session-4/Test/temp.png
sessions/session-4/Test/temp.png
sessions/session-4/Test/temp.png
  • 2-up
  • Swipe
  • Onion skin
...@@ -142,6 +142,7 @@ class Board(QWidget): ...@@ -142,6 +142,7 @@ class Board(QWidget):
self.image = newImage self.image = newImage
def data_prep_test(self): def data_prep_test(self):
self.saveImageTest()
im = Image.open('Test/temp.png') im = Image.open('Test/temp.png')
iar = np.array(im) iar = np.array(im)
...@@ -155,7 +156,7 @@ class Board(QWidget): ...@@ -155,7 +156,7 @@ class Board(QWidget):
test_data[count] = math.floor(((np.sum(iar[i][j][:3]/765)))) test_data[count] = math.floor(((np.sum(iar[i][j][:3]/765))))
#print(test_data[count]) #print(test_data[count])
count+=1 count+=1
return test_data return [test_data]
def data_prep_train(self): def data_prep_train(self):
print('preporcessing..') print('preporcessing..')
...@@ -182,4 +183,12 @@ class Board(QWidget): ...@@ -182,4 +183,12 @@ class Board(QWidget):
digit_label.append( label ) digit_label.append( label )
digit_data = np.array(digit_data) #ข้อมูล digit_data = np.array(digit_data) #ข้อมูล
digit_label = np.array(digit_label) # label ของข้อมูล digit_label = np.array(digit_label) # label ของข้อมูล
return digit_data,digit_label return digit_data,digit_label
\ No newline at end of file
def evaluation(self, digit_data, digit_label, classifier):
predicted = classifier.predict(digit_data)
print("Classification report for classifier %s:\n%s\n"
% (classifier, metrics.classification_report(digit_label, predicted)))
print("Confusion matrix:\n%s" % metrics.confusion_matrix(digit_label, predicted))
print("\naccuracy = ",accuracy_score(digit_label, predicted))
\ No newline at end of file
...@@ -7,8 +7,7 @@ from PyQt5.QtWidgets import (QAction, QApplication, QWidget,QGridLayout) ...@@ -7,8 +7,7 @@ from PyQt5.QtWidgets import (QAction, QApplication, QWidget,QGridLayout)
from ubuwidgets import (Button, ComboWidget, Board) from ubuwidgets import (Button, ComboWidget, Board)
# Import datasets, classifiers and performance metrics # Import datasets, classifiers and performance metrics
from sklearn import svm, metrics from sklearn import svm,
from sklearn.metrics import accuracy_score
from sklearn.externals import joblib from sklearn.externals import joblib
class MainWindow(QWidget): class MainWindow(QWidget):
...@@ -19,7 +18,7 @@ class MainWindow(QWidget): ...@@ -19,7 +18,7 @@ class MainWindow(QWidget):
self.trainButton = Button(self, "Train", self.on_train) self.trainButton = Button(self, "Train", self.on_train)
self.saveCombo = ComboWidget(self, "Label: ", "Save", self.on_save) self.saveCombo = ComboWidget(self, "Label: ", "Save", self.on_save)
self.testCombo = ComboWidget(self, "Result: ", "Test", self.on_test, True) self.testCombo = ComboWidget(self, "Result: ", "Test", self.on_test, True)
grid = QGridLayout() grid = QGridLayout()
grid.addWidget(self.scribbleArea,1,0) grid.addWidget(self.scribbleArea,1,0)
grid.addWidget(self.clearButton,2,0) grid.addWidget(self.clearButton,2,0)
...@@ -35,30 +34,36 @@ class MainWindow(QWidget): ...@@ -35,30 +34,36 @@ class MainWindow(QWidget):
def on_train(self): def on_train(self):
#เตรียมข้อมูล
digit_data, digit_label = self.scribbleArea.data_prep_train() digit_data, digit_label = self.scribbleArea.data_prep_train()
print('training..') print('training..')
# Create a classifier: a support vector classifier
# สร้าง classifier: a support vector classifier
classifier = svm.SVC(gamma=0.001) classifier = svm.SVC(gamma=0.001)
# We learn the digits on the first half of the digits # สอนให้คอมพิวเตอร์จดจำ
classifier.fit(digit_data, digit_label) classifier.fit(digit_data, digit_label)
#save model
#เซฟ model
joblib.dump(classifier, 'svmModel.pkl') joblib.dump(classifier, 'svmModel.pkl')
predicted = classifier.predict(digit_data) #วัดประสิทธิภาพของโมเดล
print("Classification report for classifier %s:\n%s\n" self.scribbleArea.evaluation(digit_data, digit_label, classifier)
% (classifier, metrics.classification_report(digit_label, predicted)))
print("Confusion matrix:\n%s" % metrics.confusion_matrix(digit_label, predicted))
print("\naccuracy = ",accuracy_score(digit_label, predicted))
def on_test(self): def on_test(self):
print("testing...") print("testing...")
#โหลดโมเดล
clf = joblib.load('svmModel.pkl') clf = joblib.load('svmModel.pkl')
self.scribbleArea.saveImageTest()
#เตรียมข้อมูลสำหรับทดสอบ
test_data = self.scribbleArea.data_prep_test() test_data = self.scribbleArea.data_prep_test()
predicted = clf.predict([test_data])
#ทำการทำนาย
predicted = clf.predict(test_data)
#แสดงผลการทำนาย
result = "ผลการทำนาย คือ "+predicted[0] result = "ผลการทำนาย คือ "+predicted[0]
self.testCombo.set_text(result) self.testCombo.set_text(result)
print("ผลการทำนาย คือ", predicted) print("ผลการทำนาย คือ", predicted)
...@@ -68,6 +73,8 @@ class MainWindow(QWidget): ...@@ -68,6 +73,8 @@ class MainWindow(QWidget):
self.scribbleArea.saveImage(self.saveCombo.text()) self.scribbleArea.saveImage(self.saveCombo.text())
self.scribbleArea.clearImage() self.scribbleArea.clearImage()
if __name__ == '__main__': if __name__ == '__main__':
import sys import sys
......
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