2
0
forked from s444420/AL-2020

new tests

This commit is contained in:
shaaqu 2020-06-01 00:21:32 +02:00
parent 29486c27df
commit db76915759
14 changed files with 57 additions and 62 deletions

View File

@ -20,10 +20,20 @@
</component>
<component name="ChangeListManager">
<list default="true" id="828778c9-9d97-422f-a727-18ddbd059b85" name="Default Changelist" comment="detecting digits">
<change afterPath="$PROJECT_DIR$/coder/517.png" afterDir="false" />
<change afterPath="$PROJECT_DIR$/coder/test1.png" afterDir="false" />
<change afterPath="$PROJECT_DIR$/coder/test2.png" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/coder/coder.py" beforeDir="false" afterPath="$PROJECT_DIR$/coder/coder.py" afterDir="false" />
<change beforePath="$PROJECT_DIR$/coder/digits_recognizer.py" beforeDir="false" afterPath="$PROJECT_DIR$/coder/digits_recognizer.py" afterDir="false" />
<change beforePath="$PROJECT_DIR$/coder/ll.png" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/coder/rocognizer.py" beforeDir="false" afterPath="$PROJECT_DIR$/coder/rocognizer.py" afterDir="false" />
<change beforePath="$PROJECT_DIR$/coder/test.jpg" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/coder/test1.jpg" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/coder/test3.png" beforeDir="false" afterPath="$PROJECT_DIR$/coder/test3.png" afterDir="false" />
<change beforePath="$PROJECT_DIR$/coder/test5.png" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/coder/test9.png" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/coder/testno.png" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/coder/train_nn.py" beforeDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
@ -72,7 +82,7 @@
<property name="RunOnceActivity.ShowReadmeOnStart" value="true" />
<property name="SHARE_PROJECT_CONFIGURATION_FILES" value="true" />
<property name="WebServerToolWindowFactoryState" value="false" />
<property name="last_opened_file_path" value="$PROJECT_DIR$/coder" />
<property name="last_opened_file_path" value="$USER_HOME$/Downloads/digitRecognition" />
<property name="restartRequiresConfirmation" value="false" />
<property name="settings.editor.selected.configurable" value="com.jetbrains.python.configuration.PyActiveSdkModuleConfigurable" />
</component>
@ -82,7 +92,7 @@
<recent name="C:\Users\Pawel Lukaszewicz\PycharmProjects\AL-2020\coder\dataset" />
</key>
</component>
<component name="RunManager" selected="Python.digits_recognizer">
<component name="RunManager" selected="Python.rocognizer">
<configuration default="true" type="PythonConfigurationType" factoryName="Python">
<module name="wozek" />
<option name="INTERPRETER_OPTIONS" value="" />
@ -131,6 +141,9 @@
<module name="wozek" />
<option name="INTERPRETER_OPTIONS" value="" />
<option name="PARENT_ENVS" value="true" />
<envs>
<env name="PYTHONUNBUFFERED" value="1" />
</envs>
<option name="SDK_HOME" value="" />
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$/coder" />
<option name="IS_MODULE_SDK" value="true" />
@ -218,8 +231,8 @@
</list>
<recent_temporary>
<list>
<item itemvalue="Python.digits_recognizer" />
<item itemvalue="Python.rocognizer" />
<item itemvalue="Python.digits_recognizer" />
<item itemvalue="Python.coder" />
<item itemvalue="Python.z8" />
<item itemvalue="Python.image" />
@ -265,6 +278,9 @@
<workItem from="1590575699320" duration="792000" />
<workItem from="1590766924835" duration="10060000" />
<workItem from="1590929874289" duration="7728000" />
<workItem from="1590938377522" duration="176000" />
<workItem from="1590944305590" duration="10043000" />
<workItem from="1590961284964" duration="2200000" />
</task>
<task id="LOCAL-00001" summary="create Shelf">
<created>1589815443652</created>
@ -364,7 +380,14 @@
<option name="project" value="LOCAL" />
<updated>1590846768877</updated>
</task>
<option name="localTasksCounter" value="15" />
<task id="LOCAL-00015" summary="img is cropped and transformed to torch">
<created>1590938465634</created>
<option name="number" value="00015" />
<option name="presentableId" value="LOCAL-00015" />
<option name="project" value="LOCAL" />
<updated>1590938465634</updated>
</task>
<option name="localTasksCounter" value="16" />
<servers />
</component>
<component name="TypeScriptGeneratedFilesManager">
@ -398,13 +421,18 @@
<MESSAGE value="going to pytorch on conda eve" />
<MESSAGE value="add coder.py" />
<MESSAGE value="detecting digits" />
<option name="LAST_COMMIT_MESSAGE" value="detecting digits" />
<MESSAGE value="img is cropped and transformed to torch" />
<option name="LAST_COMMIT_MESSAGE" value="img is cropped and transformed to torch" />
</component>
<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="1590956217731">
<screen x="0" y="0" width="1536" height="824" />
</state>
<state x="115" y="162" key="#com.intellij.refactoring.safeDelete.UnsafeUsagesDialog/0.0.1536.824@0.0.1536.824" timestamp="1589923610328" />
<state x="115" y="162" key="#com.intellij.refactoring.safeDelete.UnsafeUsagesDialog/0.0.1536.824@0.0.1536.824" timestamp="1590956217731" />
<state x="549" y="167" key="FileChooserDialogImpl" timestamp="1590962272315">
<screen x="0" y="0" width="1536" height="824" />
</state>
<state x="549" y="167" key="FileChooserDialogImpl/0.0.1536.824@0.0.1536.824" timestamp="1590962272315" />
<state width="1493" height="208" key="GridCell.Tab.0.bottom" timestamp="1589845242796">
<screen x="0" y="0" width="1536" height="824" />
</state>
@ -425,10 +453,10 @@
<screen x="0" y="0" width="1536" height="824" />
</state>
<state x="277" y="57" key="SettingsEditor/0.0.1536.824@0.0.1536.824" timestamp="1590443566792" />
<state x="361" y="145" key="Vcs.Push.Dialog.v2" timestamp="1590846777672">
<state x="361" y="145" key="Vcs.Push.Dialog.v2" timestamp="1590938471243">
<screen x="0" y="0" width="1536" height="824" />
</state>
<state x="361" y="145" key="Vcs.Push.Dialog.v2/0.0.1536.824@0.0.1536.824" timestamp="1590846777672" />
<state x="361" y="145" key="Vcs.Push.Dialog.v2/0.0.1536.824@0.0.1536.824" timestamp="1590938471243" />
<state x="54" y="145" width="672" height="678" key="search.everywhere.popup" timestamp="1590930912296">
<screen x="0" y="0" width="1536" height="824" />
</state>

BIN
coder/517.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

View File

@ -75,6 +75,9 @@ print("\nTraining Time (in minutes) =", (time() - time0) / 60)
images, labels = next(iter(val_loader))
img = images[0].view(1, 784)
print(type(img))
print(img.size())
with torch.no_grad():
logps = model(img)
ps = torch.exp(logps)

Binary file not shown.

Before

Width:  |  Height:  |  Size: 14 KiB

View File

@ -1,17 +1,19 @@
import numpy as np
import argparse
import imutils
import cv2
import matplotlib.pyplot as plt
import torch
from matplotlib import cm
from PIL.Image import Image
from torch import nn
from PIL import Image
from skimage.feature import hog
from torchvision.transforms import transforms
def white_bg_square(img):
"return a white-background-color image having the img in exact center"
size = (max(img.size),)*2
layer = Image.new('RGB', size, (255, 255, 255))
layer.paste(img, tuple(map(lambda x:(x[0]-x[1])/2, zip(size, img.size))))
return layer
code = []
path = "test1.jpg"
path = "test5.jpg"
transform = transforms.Compose([transforms.ToTensor(),
transforms.Normalize((0.5,), (0.5,)),
@ -45,27 +47,19 @@ model.eval()
for rect in rects:
# Crop image
crop_img = img[rect[1]:rect[1] + rect[3], rect[0]:rect[0] + rect[2]]
plt.imshow(crop_img)
plt.show()
crop_img = img[rect[1]:rect[1] + rect[3] + 10, rect[0]:rect[0] + rect[2] + 10, 0]
# Resize the image
roi = cv2.resize(crop_img, (28, 28), interpolation=cv2.INTER_AREA)
roi = cv2.resize(crop_img, (28, 28), interpolation=cv2.INTER_LINEAR)
roi = cv2.dilate(roi, (3, 3))
plt.imshow(roi)
plt.show()
im = Image.fromarray(roi)
im = transform(im)
print(im)
plt.imshow(im)
plt.show()
im = transform(roi)
im = im.view(1, 784)
with torch.no_grad():
logps = model(im)
logps = model(im.float())
ps = torch.exp(logps)
print(ps[0])
probab = list(ps.numpy()[0])
print("Predicted Digit =", probab.index(max(probab)))
cv2.imshow("Resulting Image with Rectangular ROIs", img)
cv2.imshow("Code", img)
cv2.waitKey()

Binary file not shown.

Before

Width:  |  Height:  |  Size: 7.7 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 17 KiB

BIN
coder/test1.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.3 KiB

BIN
coder/test2.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.9 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 7.3 KiB

After

Width:  |  Height:  |  Size: 6.9 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 7.6 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 8.7 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.7 KiB

View File

@ -1,30 +0,0 @@
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])