forked from s444420/AL-2020
img is cropped and transformed to torch
This commit is contained in:
parent
a12cde0aa7
commit
29486c27df
@ -19,16 +19,10 @@
|
||||
<select />
|
||||
</component>
|
||||
<component name="ChangeListManager">
|
||||
<list default="true" id="828778c9-9d97-422f-a727-18ddbd059b85" name="Default Changelist" comment="add coder.py">
|
||||
<change afterPath="$PROJECT_DIR$/coder/12345.png" afterDir="false" />
|
||||
<change afterPath="$PROJECT_DIR$/coder/ll.png" afterDir="false" />
|
||||
<change afterPath="$PROJECT_DIR$/coder/testno.png" afterDir="false" />
|
||||
<list default="true" id="828778c9-9d97-422f-a727-18ddbd059b85" name="Default Changelist" comment="detecting digits">
|
||||
<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/digit_reco_model.pt" beforeDir="false" afterPath="$PROJECT_DIR$/coder/digit_reco_model.pt" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/coder/digit_reco_model2.pt" beforeDir="false" afterPath="$PROJECT_DIR$/coder/digit_reco_model2.pt" 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/image.py" beforeDir="false" afterPath="$PROJECT_DIR$/coder/image.py" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/coder/rocognizer.py" beforeDir="false" afterPath="$PROJECT_DIR$/coder/rocognizer.py" afterDir="false" />
|
||||
</list>
|
||||
<option name="SHOW_DIALOG" value="false" />
|
||||
@ -88,7 +82,7 @@
|
||||
<recent name="C:\Users\Pawel Lukaszewicz\PycharmProjects\AL-2020\coder\dataset" />
|
||||
</key>
|
||||
</component>
|
||||
<component name="RunManager" selected="Python.rocognizer">
|
||||
<component name="RunManager" selected="Python.digits_recognizer">
|
||||
<configuration default="true" type="PythonConfigurationType" factoryName="Python">
|
||||
<module name="wozek" />
|
||||
<option name="INTERPRETER_OPTIONS" value="" />
|
||||
@ -137,9 +131,6 @@
|
||||
<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" />
|
||||
@ -227,9 +218,9 @@
|
||||
</list>
|
||||
<recent_temporary>
|
||||
<list>
|
||||
<item itemvalue="Python.digits_recognizer" />
|
||||
<item itemvalue="Python.rocognizer" />
|
||||
<item itemvalue="Python.coder" />
|
||||
<item itemvalue="Python.digits_recognizer" />
|
||||
<item itemvalue="Python.z8" />
|
||||
<item itemvalue="Python.image" />
|
||||
</list>
|
||||
@ -272,7 +263,8 @@
|
||||
<workItem from="1590518246722" duration="12616000" />
|
||||
<workItem from="1590559069326" duration="13892000" />
|
||||
<workItem from="1590575699320" duration="792000" />
|
||||
<workItem from="1590766924835" duration="4315000" />
|
||||
<workItem from="1590766924835" duration="10060000" />
|
||||
<workItem from="1590929874289" duration="7728000" />
|
||||
</task>
|
||||
<task id="LOCAL-00001" summary="create Shelf">
|
||||
<created>1589815443652</created>
|
||||
@ -365,7 +357,14 @@
|
||||
<option name="project" value="LOCAL" />
|
||||
<updated>1590538529471</updated>
|
||||
</task>
|
||||
<option name="localTasksCounter" value="14" />
|
||||
<task id="LOCAL-00014" summary="detecting digits">
|
||||
<created>1590846768877</created>
|
||||
<option name="number" value="00014" />
|
||||
<option name="presentableId" value="LOCAL-00014" />
|
||||
<option name="project" value="LOCAL" />
|
||||
<updated>1590846768877</updated>
|
||||
</task>
|
||||
<option name="localTasksCounter" value="15" />
|
||||
<servers />
|
||||
</component>
|
||||
<component name="TypeScriptGeneratedFilesManager">
|
||||
@ -398,7 +397,8 @@
|
||||
<MESSAGE value="new dataset" />
|
||||
<MESSAGE value="going to pytorch on conda eve" />
|
||||
<MESSAGE value="add coder.py" />
|
||||
<option name="LAST_COMMIT_MESSAGE" value="add coder.py" />
|
||||
<MESSAGE value="detecting digits" />
|
||||
<option name="LAST_COMMIT_MESSAGE" value="detecting digits" />
|
||||
</component>
|
||||
<component name="WindowStateProjectService">
|
||||
<state x="115" y="162" key="#com.intellij.refactoring.safeDelete.UnsafeUsagesDialog" timestamp="1589923610328">
|
||||
@ -425,14 +425,14 @@
|
||||
<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="1590538563335">
|
||||
<state x="361" y="145" key="Vcs.Push.Dialog.v2" timestamp="1590846777672">
|
||||
<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="1590538563335" />
|
||||
<state x="54" y="145" width="672" height="678" key="search.everywhere.popup" timestamp="1589918982407">
|
||||
<state x="361" y="145" key="Vcs.Push.Dialog.v2/0.0.1536.824@0.0.1536.824" timestamp="1590846777672" />
|
||||
<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>
|
||||
<state x="54" y="145" width="672" height="678" key="search.everywhere.popup/0.0.1536.824@0.0.1536.824" timestamp="1589918982407" />
|
||||
<state x="54" y="145" width="672" height="678" key="search.everywhere.popup/0.0.1536.824@0.0.1536.824" timestamp="1590930912296" />
|
||||
</component>
|
||||
<component name="com.intellij.coverage.CoverageDataManagerImpl">
|
||||
<SUITE FILE_PATH="coverage/AL_2020$feature_hashing.coverage" NAME="feature_hashing Coverage Results" MODIFIED="1589823737949" SOURCE_PROVIDER="com.intellij.coverage.DefaultCoverageFileProvider" RUNNER="coverage.py" COVERAGE_BY_TEST_ENABLED="true" COVERAGE_TRACING_ENABLED="false" WORKING_DIRECTORY="$PROJECT_DIR$/Assiging" />
|
||||
|
@ -52,6 +52,7 @@ plt.show()
|
||||
|
||||
with torch.no_grad():
|
||||
logps = model(img)
|
||||
print(logps)
|
||||
|
||||
ps = torch.exp(logps)
|
||||
probab = list(ps.numpy()[0])
|
||||
|
@ -17,6 +17,7 @@ train_set = datasets.MNIST('PATH_TO_STORE_TRAINSET', download=True, train=True,
|
||||
val_set = datasets.MNIST('PATH_TO_STORE_TESTSET', download=True, train=False, transform=transform)
|
||||
train_loader = torch.utils.data.DataLoader(train_set, batch_size=64, shuffle=True)
|
||||
val_loader = torch.utils.data.DataLoader(val_set, batch_size=64, shuffle=True)
|
||||
print(train_set[0])
|
||||
|
||||
# building nn model
|
||||
input_size = 784 # = 28*28
|
||||
|
@ -4,10 +4,19 @@ import imutils
|
||||
import cv2
|
||||
import matplotlib.pyplot as plt
|
||||
import torch
|
||||
from matplotlib import cm
|
||||
from torch import nn
|
||||
from PIL import Image
|
||||
from skimage.feature import hog
|
||||
from torchvision.transforms import transforms
|
||||
|
||||
code = []
|
||||
path = "test1.jpg"
|
||||
|
||||
transform = transforms.Compose([transforms.ToTensor(),
|
||||
transforms.Normalize((0.5,), (0.5,)),
|
||||
])
|
||||
|
||||
img = cv2.imread(path)
|
||||
|
||||
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
|
||||
@ -19,18 +28,44 @@ ctrs, hier = cv2.findContours(im_th.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_
|
||||
|
||||
rects = [cv2.boundingRect(ctr) for ctr in ctrs]
|
||||
|
||||
# load nn model
|
||||
input_size = 784 # = 28*28
|
||||
hidden_sizes = [128, 128, 64]
|
||||
output_size = 10
|
||||
model = nn.Sequential(nn.Linear(input_size, hidden_sizes[0]),
|
||||
nn.ReLU(),
|
||||
nn.Linear(hidden_sizes[0], hidden_sizes[1]),
|
||||
nn.ReLU(),
|
||||
nn.Linear(hidden_sizes[1], hidden_sizes[2]),
|
||||
nn.ReLU(),
|
||||
nn.Linear(hidden_sizes[2], output_size),
|
||||
nn.LogSoftmax(dim=-1))
|
||||
model.load_state_dict(torch.load('digit_reco_model2.pt'))
|
||||
model.eval()
|
||||
|
||||
for rect in rects:
|
||||
# Draw the rectangles
|
||||
cv2.rectangle(img, (rect[0], rect[1]), (rect[0] + rect[2], rect[1] + rect[3]), (0, 255, 0), 3)
|
||||
# Make the rectangular region around the digit
|
||||
leng = int(rect[3] * 1.6)
|
||||
pt1 = int(rect[1] + rect[3] // 2 - leng // 2)
|
||||
pt2 = int(rect[0] + rect[2] // 2 - leng // 2)
|
||||
roi = im_th[pt1:pt1+leng, pt2:pt2+leng]
|
||||
# Crop image
|
||||
crop_img = img[rect[1]:rect[1] + rect[3], rect[0]:rect[0] + rect[2]]
|
||||
plt.imshow(crop_img)
|
||||
plt.show()
|
||||
|
||||
# Resize the image
|
||||
roi = cv2.resize(roi, (28, 28), interpolation=cv2.INTER_AREA)
|
||||
roi = cv2.dilate(roi, (3, 3))
|
||||
# Calculate the HOG features
|
||||
roi = cv2.resize(crop_img, (28, 28), interpolation=cv2.INTER_AREA)
|
||||
plt.imshow(roi)
|
||||
plt.show()
|
||||
im = Image.fromarray(roi)
|
||||
|
||||
im = transform(im)
|
||||
print(im)
|
||||
plt.imshow(im)
|
||||
plt.show()
|
||||
|
||||
with torch.no_grad():
|
||||
logps = model(im)
|
||||
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.waitKey()
|
Loading…
Reference in New Issue
Block a user