-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogisticDigits.py
84 lines (63 loc) · 1.94 KB
/
logisticDigits.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#%matplotlib inline
from sklearn.datasets import load_digits
from sklearn.model_selection import train_test_split
import numpy as np
import matplotlib.pyplot as plt
digits_data = load_digits()
digits = digits_data.data
targets = digits_data.target
a_digit = np.split(digits[0], 8)
plt.title('digits[0]')
plt.imshow(a_digit, cmap='gray')
plt.show()
# SPLIT training data and test data
x_train, x_test, y_train, y_test = train_test_split(digits, targets, test_size=0.25)
print (x_train.shape, x_test.shape)
from sklearn.linear_model import LogisticRegression
logistic_reg = LogisticRegression()
logistic_reg.fit(x_train, y_train)
print(x_test[2].shape)
predict_data = x_test[2].reshape(1, -1)
print(predict_data)
plt.imshow(np.split(x_test[2], 8), cmap='gray')
plt.title('x_test[2]')
predicted=logistic_reg.predict(x_test[4].reshape(1, -1))
print(predicted)
print(predicted.shape)
plt.show()
predict_data = x_test[17].reshape(1, -1)
print(predict_data)
plt.imshow(np.split(x_test[17], 8), cmap='gray')
plt.title('x_test[17]')
predicted=logistic_reg.predict(x_test[17].reshape(1, -1))
print(predicted)
print(predicted.shape)
plt.show()
import cv2
# Read RGB image
fileName='digit_four_1.jpg'
img = cv2.imread(fileName)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
print(gray.shape)
print(gray)
cv2.imshow('image', img)
custom_pixels = list(gray)
corr_pixels = []
# convert pixel data to fit training data format (swap grey values)
for row in custom_pixels:
for x in row:
x = 255 - x
corr_pixels.append(x)
print(corr_pixels)
test_set = np.array(corr_pixels)
print(test_set)
a_digit = np.split(test_set, 8)
plt.title(fileName)
plt.imshow(a_digit, cmap='gray')
predicted=logistic_reg.predict(test_set.reshape(1, -1))
print(predicted)
print(predicted.shape)
plt.show()
cv2.imshow(fileName, gray)
# Destroying present windows on screen
cv2.destroyAllWindows()