Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
P
Python-Roadshow
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
workshops
Python-Roadshow
Commits
79e90759
Commit
79e90759
authored
Jan 19, 2018
by
SCtom
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
optimize code2
parent
a16ee9a8
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
57 additions
and
2 deletions
+57
-2
ubuwidgets.py
sessions/session-3/ubuwidgets.py
+57
-2
No files found.
sessions/session-3/ubuwidgets.py
View file @
79e90759
...
@@ -2,6 +2,17 @@ from PyQt5.QtCore import Qt, QDir, QFile, QPoint, QRect, QSize
...
@@ -2,6 +2,17 @@ from PyQt5.QtCore import Qt, QDir, QFile, QPoint, QRect, QSize
from
PyQt5.QtWidgets
import
QWidget
,
QPushButton
,
QLineEdit
,
QLabel
,
QGridLayout
from
PyQt5.QtWidgets
import
QWidget
,
QPushButton
,
QLineEdit
,
QLabel
,
QGridLayout
from
PyQt5.QtGui
import
QImage
,
QImageWriter
,
QPainter
,
QPen
,
qRgb
,
QPixmap
from
PyQt5.QtGui
import
QImage
,
QImageWriter
,
QPainter
,
QPen
,
qRgb
,
QPixmap
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
Button
(
QWidget
):
class
Button
(
QWidget
):
def
__init__
(
self
,
parent
=
None
,
label
=
"Button"
,
on_click
=
None
):
def
__init__
(
self
,
parent
=
None
,
label
=
"Button"
,
on_click
=
None
):
super
(
Button
,
self
)
.
__init__
(
parent
)
super
(
Button
,
self
)
.
__init__
(
parent
)
...
@@ -67,7 +78,7 @@ class Board(QWidget):
...
@@ -67,7 +78,7 @@ class Board(QWidget):
tmpN
=
int
(
file
[:
-
4
:])
tmpN
=
int
(
file
[:
-
4
:])
maxf
=
max
(
maxf
,
tmpN
)
maxf
=
max
(
maxf
,
tmpN
)
visibleImage
.
save
(
'tmp.png'
)
visibleImage
.
save
(
'tmp.png'
)
pixmap
=
QPixmap
(
'tmp.png'
)
.
scaled
(
16
,
16
)
pixmap
=
QPixmap
(
'tmp.png'
)
.
scaled
(
64
,
64
)
pixmap
.
save
(
label
+
'/'
+
str
(
max
(
cnt
,
maxf
)
+
1
)
+
'.png'
)
pixmap
.
save
(
label
+
'/'
+
str
(
max
(
cnt
,
maxf
)
+
1
)
+
'.png'
)
QFile
.
remove
(
'tmp.png'
)
QFile
.
remove
(
'tmp.png'
)
...
@@ -79,7 +90,7 @@ class Board(QWidget):
...
@@ -79,7 +90,7 @@ class Board(QWidget):
QDir
()
.
mkdir
(
'Test'
)
QDir
()
.
mkdir
(
'Test'
)
visibleImage
.
save
(
'tmp.png'
)
visibleImage
.
save
(
'tmp.png'
)
pixmap
=
QPixmap
(
'tmp.png'
)
.
scaled
(
16
,
16
)
pixmap
=
QPixmap
(
'tmp.png'
)
.
scaled
(
64
,
64
)
pixmap
.
save
(
'Test/temp.png'
)
pixmap
.
save
(
'Test/temp.png'
)
QFile
.
remove
(
'tmp.png'
)
QFile
.
remove
(
'tmp.png'
)
...
@@ -129,3 +140,46 @@ class Board(QWidget):
...
@@ -129,3 +140,46 @@ class Board(QWidget):
painter
=
QPainter
(
newImage
)
painter
=
QPainter
(
newImage
)
painter
.
drawImage
(
QPoint
(
0
,
0
),
image
)
painter
.
drawImage
(
QPoint
(
0
,
0
),
image
)
self
.
image
=
newImage
self
.
image
=
newImage
def
data_prep_test
(
self
):
im
=
Image
.
open
(
'Test/temp.png'
)
iar
=
np
.
array
(
im
)
width
,
height
=
im
.
size
test_data
=
np
.
zeros
(
width
*
height
)
test_data
=
np
.
zeros
(
64
*
64
)
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
return
test_data
def
data_prep_train
(
self
):
print
(
'preporcessing..'
)
folder_name
=
'Image/'
list_of_label
=
os
.
listdir
(
folder_name
)
digit_data
=
list
()
digit_label
=
list
()
for
label
in
list_of_label
:
label_path
=
folder_name
+
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
)
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 ของข้อมูล
return
digit_data
,
digit_label
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment