PY
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
# This is a work in progress image classifier - It uses training data of handwritten digits 0-9 (x4 for better accuracy) and attempts to identify the test digit.
# You can try it out on your own by copying this code into a folder and creating a sub-folder called 'Images', inside that sub-folder create two more folders, one titled 'Testing_Images' and the other titled 'Training_Images' and then replace those images with the ones below. Or you can replace the entire paths to both training and testing images.
# I've opted to use 28x28 pixel images. You can use any size images, but make sure they are the exact same size and saved in RGB. This code uses RGB values to perform Euclidean distance calculations.
from PIL import Image
def euc(a,b):
arr = []
if isinstance(a, list) and isinstance(b, list) or isinstance(a, tuple) and isinstance(b, tuple):
if len(a) == len(b):
for indx,val in enumerate(a):
arr.append( ((a[indx] - b[indx])**2) )
return math.sqrt(sum(arr))
else:
return 'Error: Lists must be of same lengths'
elif isinstance(a, int) and isinstance(b, int):
return abs(a-b)
else:
return 'Error: Must be either int or list objects'
#Test Image
test_image = Image.open('Images/Testing_Images/test0.jpg', 'r')
test_val = list(test_image.getdata())
test = [x for sets in test_val for x in sets]
Enter to Rename, Shift+Enter to Preview
OUTPUT
Run