diff --git a/.idea/workspace.xml b/.idea/workspace.xml
index 19317c7..4c5593f 100644
--- a/.idea/workspace.xml
+++ b/.idea/workspace.xml
@@ -19,16 +19,10 @@
-
-
-
-
+
-
-
-
@@ -88,7 +82,7 @@
-
+
@@ -137,9 +131,6 @@
-
-
-
@@ -227,9 +218,9 @@
+
-
@@ -272,7 +263,8 @@
-
+
+
1589815443652
@@ -365,7 +357,14 @@
1590538529471
-
+
+ 1590846768877
+
+
+
+ 1590846768877
+
+
@@ -398,7 +397,8 @@
-
+
+
@@ -425,14 +425,14 @@
-
+
-
-
+
+
-
+
diff --git a/coder/coder.py b/coder/coder.py
index 658fde4..3c18688 100644
--- a/coder/coder.py
+++ b/coder/coder.py
@@ -52,6 +52,7 @@ plt.show()
with torch.no_grad():
logps = model(img)
+print(logps)
ps = torch.exp(logps)
probab = list(ps.numpy()[0])
diff --git a/coder/digits_recognizer.py b/coder/digits_recognizer.py
index 9f2fe0d..c690cb8 100644
--- a/coder/digits_recognizer.py
+++ b/coder/digits_recognizer.py
@@ -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
diff --git a/coder/rocognizer.py b/coder/rocognizer.py
index 455c312..143dcbb 100644
--- a/coder/rocognizer.py
+++ b/coder/rocognizer.py
@@ -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()
\ No newline at end of file