recognizing but training must be improved

This commit is contained in:
shaaqu 2020-05-20 08:24:33 +02:00
parent ddb652119b
commit 4b8c560be9
8 changed files with 129 additions and 64 deletions

View File

@ -19,11 +19,14 @@
<select /> <select />
</component> </component>
<component name="ChangeListManager"> <component name="ChangeListManager">
<list default="true" id="828778c9-9d97-422f-a727-18ddbd059b85" name="Default Changelist" comment="train.py"> <list default="true" id="828778c9-9d97-422f-a727-18ddbd059b85" name="Default Changelist" comment="fun created">
<change afterPath="$PROJECT_DIR$/coder/gr_test.png" afterDir="false" /> <change afterPath="$PROJECT_DIR$/coder/rocognizer.py" afterDir="false" />
<change afterPath="$PROJECT_DIR$/coder/image.py" afterDir="false" /> <change afterPath="$PROJECT_DIR$/coder/test1.jpg" afterDir="false" />
<change afterPath="$PROJECT_DIR$/coder/test3.png" afterDir="false" />
<change afterPath="$PROJECT_DIR$/coder/test9.png" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" /> <change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/coder/test.jpg" beforeDir="false" afterPath="$PROJECT_DIR$/coder/test.jpg" afterDir="false" /> <change beforePath="$PROJECT_DIR$/coder/image.py" beforeDir="false" afterPath="$PROJECT_DIR$/coder/image.py" afterDir="false" />
<change beforePath="$PROJECT_DIR$/coder/train.py" beforeDir="false" afterPath="$PROJECT_DIR$/coder/train_nn.py" afterDir="false" />
</list> </list>
<option name="SHOW_DIALOG" value="false" /> <option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" /> <option name="HIGHLIGHT_CONFLICTS" value="true" />
@ -81,7 +84,7 @@
<recent name="C:\Users\Pawel Lukaszewicz\PycharmProjects\AL-2020\coder" /> <recent name="C:\Users\Pawel Lukaszewicz\PycharmProjects\AL-2020\coder" />
</key> </key>
</component> </component>
<component name="RunManager" selected="Python.train"> <component name="RunManager" selected="Python.load_image">
<configuration default="true" type="PythonConfigurationType" factoryName="Python"> <configuration default="true" type="PythonConfigurationType" factoryName="Python">
<module name="wozek" /> <module name="wozek" />
<option name="INTERPRETER_OPTIONS" value="" /> <option name="INTERPRETER_OPTIONS" value="" />
@ -158,7 +161,7 @@
<option name="ADD_CONTENT_ROOTS" value="true" /> <option name="ADD_CONTENT_ROOTS" value="true" />
<option name="ADD_SOURCE_ROOTS" value="true" /> <option name="ADD_SOURCE_ROOTS" value="true" />
<EXTENSION ID="PythonCoverageRunConfigurationExtension" runner="coverage.py" /> <EXTENSION ID="PythonCoverageRunConfigurationExtension" runner="coverage.py" />
<option name="SCRIPT_NAME" value="$PROJECT_DIR$/coder/image.py" /> <option name="SCRIPT_NAME" value="C:\Users\Pawel Lukaszewicz\PycharmProjects\AL-2020\coder\image.py" />
<option name="PARAMETERS" value="" /> <option name="PARAMETERS" value="" />
<option name="SHOW_COMMAND_LINE" value="true" /> <option name="SHOW_COMMAND_LINE" value="true" />
<option name="EMULATE_TERMINAL" value="false" /> <option name="EMULATE_TERMINAL" value="false" />
@ -189,7 +192,7 @@
<option name="INPUT_FILE" value="" /> <option name="INPUT_FILE" value="" />
<method v="2" /> <method v="2" />
</configuration> </configuration>
<configuration name="train" type="PythonConfigurationType" factoryName="Python" temporary="true" nameIsGenerated="true"> <configuration name="train_nn" type="PythonConfigurationType" factoryName="Python" temporary="true" nameIsGenerated="true">
<module name="wozek" /> <module name="wozek" />
<option name="INTERPRETER_OPTIONS" value="" /> <option name="INTERPRETER_OPTIONS" value="" />
<option name="PARENT_ENVS" value="true" /> <option name="PARENT_ENVS" value="true" />
@ -199,7 +202,7 @@
<option name="ADD_CONTENT_ROOTS" value="true" /> <option name="ADD_CONTENT_ROOTS" value="true" />
<option name="ADD_SOURCE_ROOTS" value="true" /> <option name="ADD_SOURCE_ROOTS" value="true" />
<EXTENSION ID="PythonCoverageRunConfigurationExtension" runner="coverage.py" /> <EXTENSION ID="PythonCoverageRunConfigurationExtension" runner="coverage.py" />
<option name="SCRIPT_NAME" value="C:\Users\Pawel Lukaszewicz\PycharmProjects\AL-2020\coder\train.py" /> <option name="SCRIPT_NAME" value="C:\Users\Pawel Lukaszewicz\PycharmProjects\AL-2020\coder\train_nn.py" />
<option name="PARAMETERS" value="" /> <option name="PARAMETERS" value="" />
<option name="SHOW_COMMAND_LINE" value="true" /> <option name="SHOW_COMMAND_LINE" value="true" />
<option name="EMULATE_TERMINAL" value="false" /> <option name="EMULATE_TERMINAL" value="false" />
@ -212,13 +215,13 @@
<item itemvalue="Python.board" /> <item itemvalue="Python.board" />
<item itemvalue="Python.main" /> <item itemvalue="Python.main" />
<item itemvalue="Python.feature_hashing" /> <item itemvalue="Python.feature_hashing" />
<item itemvalue="Python.train" /> <item itemvalue="Python.train_nn" />
<item itemvalue="Python.image" /> <item itemvalue="Python.load_image" />
</list> </list>
<recent_temporary> <recent_temporary>
<list> <list>
<item itemvalue="Python.train" /> <item itemvalue="Python.load_image" />
<item itemvalue="Python.image" /> <item itemvalue="Python.train_nn" />
<item itemvalue="Python.main" /> <item itemvalue="Python.main" />
<item itemvalue="Python.feature_hashing" /> <item itemvalue="Python.feature_hashing" />
<item itemvalue="Python.board" /> <item itemvalue="Python.board" />
@ -247,7 +250,7 @@
<workItem from="1589814601057" duration="4208000" /> <workItem from="1589814601057" duration="4208000" />
<workItem from="1589844260514" duration="777000" /> <workItem from="1589844260514" duration="777000" />
<workItem from="1589845066174" duration="177000" /> <workItem from="1589845066174" duration="177000" />
<workItem from="1589888090669" duration="16639000" /> <workItem from="1589888090669" duration="19744000" />
</task> </task>
<task id="LOCAL-00001" summary="create Shelf"> <task id="LOCAL-00001" summary="create Shelf">
<created>1589815443652</created> <created>1589815443652</created>
@ -277,7 +280,21 @@
<option name="project" value="LOCAL" /> <option name="project" value="LOCAL" />
<updated>1589918562733</updated> <updated>1589918562733</updated>
</task> </task>
<option name="localTasksCounter" value="5" /> <task id="LOCAL-00005" summary="img to 1d array">
<created>1589952733192</created>
<option name="number" value="00005" />
<option name="presentableId" value="LOCAL-00005" />
<option name="project" value="LOCAL" />
<updated>1589952733192</updated>
</task>
<task id="LOCAL-00006" summary="fun created">
<created>1589952979012</created>
<option name="number" value="00006" />
<option name="presentableId" value="LOCAL-00006" />
<option name="project" value="LOCAL" />
<updated>1589952979012</updated>
</task>
<option name="localTasksCounter" value="7" />
<servers /> <servers />
</component> </component>
<component name="TypeScriptGeneratedFilesManager"> <component name="TypeScriptGeneratedFilesManager">
@ -301,7 +318,9 @@
<MESSAGE value="create addShelf method in Field class" /> <MESSAGE value="create addShelf method in Field class" />
<MESSAGE value="changed CreateDataSweets to lowercase" /> <MESSAGE value="changed CreateDataSweets to lowercase" />
<MESSAGE value="train.py" /> <MESSAGE value="train.py" />
<option name="LAST_COMMIT_MESSAGE" value="train.py" /> <MESSAGE value="img to 1d array" />
<MESSAGE value="fun created" />
<option name="LAST_COMMIT_MESSAGE" value="fun created" />
</component> </component>
<component name="WindowStateProjectService"> <component name="WindowStateProjectService">
<state x="115" y="162" key="#com.intellij.refactoring.safeDelete.UnsafeUsagesDialog" timestamp="1589923610328"> <state x="115" y="162" key="#com.intellij.refactoring.safeDelete.UnsafeUsagesDialog" timestamp="1589923610328">

View File

@ -1,31 +1,63 @@
import numpy as np import numpy as np
from PIL import Image from PIL import Image
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
from sklearn import datasets
from sklearn.metrics import accuracy_score
from sklearn.neural_network import MLPClassifier
import cv2 import cv2
def image(): # training
img = cv2.cvtColor(cv2.imread('test.jpg'), cv2.COLOR_BGR2GRAY) # recznie napisane cyfry
img = cv2.GaussianBlur(img, (15, 15), 0) # poprawia jakosc digits = datasets.load_digits()
img = cv2.resize(img, (8, 8), interpolation=cv2.INTER_AREA)
print(type(img)) y = digits.target
print(img.shape) x = digits.images.reshape((len(digits.images), -1))
print(img)
plt.imshow(img, cmap='binary')
plt.show()
data = [] x_train = x[:1000000]
y_train = y[:1000000]
x_test = x[1000:]
y_test = y[1000:]
rows, cols = img.shape mlp = MLPClassifier(hidden_layer_sizes=(15,), activation='logistic', alpha=1e-4,
for i in range(rows): solver='sgd', tol=1e-4, random_state=1,
for j in range(cols): learning_rate_init=.1, verbose=True)
k = img[i, j]
if k > 200:
k = 0 # brak czarnego
else:
k = 1
data.append(k) mlp.fit(x_train, y_train)
predictions = mlp.predict(x_test)
print(accuracy_score(y_test, predictions))
# image
img = cv2.cvtColor(cv2.imread('test3.png'), cv2.COLOR_BGR2GRAY)
img = cv2.GaussianBlur(img, (5, 5), 0) # poprawia jakosc
img = cv2.resize(img, (8, 8), interpolation=cv2.INTER_AREA)
print(type(img))
print(img.shape)
print(img)
plt.imshow(img ,cmap='binary')
plt.show()
data = []
rows, cols = img.shape
for i in range(rows):
for j in range(cols):
k = img[i, j]
if k > 100:
k = 0 # brak czarnego
else:
k = 1
data.append(k)
data = np.asarray(data, dtype=np.float32)
print(data)
predictions = mlp.predict([data])
print("Liczba to:", predictions[0])
print(data)

13
coder/rocognizer.py Normal file
View File

@ -0,0 +1,13 @@
import matplotlib.pyplot as plt
import numpy as np
from numpy import asarray
from sklearn import datasets
from sklearn.neural_network import MLPClassifier
from sklearn.metrics import accuracy_score
from PIL import Image
import pygame
import functions
import sys
import time

BIN
coder/test1.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 17 KiB

BIN
coder/test3.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.9 KiB

BIN
coder/test9.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.7 KiB

View File

@ -1,29 +0,0 @@
import matplotlib.pyplot as plt
import numpy as np
from numpy import asarray
from sklearn import datasets
from sklearn.neural_network import MLPClassifier
from sklearn.metrics import accuracy_score
from PIL import Image
def train():
# recznie napisane cyfry
digits = datasets.load_digits()
y = digits.target
x = digits.images.reshape((len(digits.images), -1))
x_train = x[:1000000]
y_train = y[:1000000]
x_test = x[1000:]
y_test = y[1000:]
mlp = MLPClassifier(hidden_layer_sizes=(15,), activation='logistic', alpha=1e-4,
solver='sgd', tol=1e-4, random_state=1,
learning_rate_init=.1, verbose=True)
mlp.fit(x_train, y_train)
predictions = mlp.predict(x_test)
print(accuracy_score(y_test, predictions))

30
coder/train_nn.py Normal file
View File

@ -0,0 +1,30 @@
import matplotlib.pyplot as plt
import numpy as np
from numpy import asarray
import pygame
from sklearn import datasets
from sklearn.neural_network import MLPClassifier
from sklearn.metrics import accuracy_score
from PIL import Image
# recznie napisane cyfry
digits = datasets.load_digits()
y = digits.target
x = digits.images.reshape((len(digits.images), -1))
x_train = x[:1000000]
y_train = y[:1000000]
x_test = x[1000:]
y_test = y[1000:]
mlp = MLPClassifier(hidden_layer_sizes=(15,), activation='logistic', alpha=1e-4,
solver='sgd', tol=1e-4, random_state=1,
learning_rate_init=.1, verbose=True)
mlp.fit(x_train, y_train)
predictions = mlp.predict(x_test)
print(accuracy_score(y_test, predictions))
print(x_test[1])