I am creating a program that converts an Image(from UI) to MNIST array and then predict that digit. There is nothing wrong in model , I hope, as its predictions are most of the times accurate when testing MNIST image that was provided from data set. But the prediction is poor when I use my image and convert it to MNIST array
def imageprepare(argv):
im = PIL.Image.open(argv).convert('L')
width = float(im.size[0])
height = float(im.size[1])
newImage = PIL.Image.new('L', (28, 28), (255)) # creates white canvas of 28x28 pixels#28 28
if width > height: # check which dimension is bigger
# Width is bigger. Width becomes 20 pixels.
nheight = int(round((20.0 / width * height), 0)) # resize height according to ratio width
if (nheight == 0): # rare case but minimum is 1 pixel
nheight = 1
# resize and sharpen
img = im.resize((20, nheight), PIL.Image.ANTIALIAS).filter(ImageFilter.SHARPEN)
wtop = int(round(((28 - nheight) / 2), 0)) # calculate horizontal position
newImage.paste(img, (4, wtop)) # paste resized image on white canvas
else:
# Height is bigger. Heigth becomes 20 pixels.
nwidth = int(round((20.0 / height * width), 0)) # resize width according to ratio height
if (nwidth == 0): # rare case but minimum is 1 pixel
nwidth = 1
# resize and sharpen
img = im.resize((nwidth, 20), PIL.Image.ANTIALIAS).filter(ImageFilter.SHARPEN)
wleft = int(round(((28 - nwidth) / 2), 0)) # caculate vertical pozition
newImage.paste(img, (wleft, 4)) # paste resized image on white canvas
tv = list(newImage.getdata()) # get pixel values
# normalize pixels to 0 and 1. 0 is pure white, 1 is pure black.
tva = [(255 - x) * 1.0 / 255.0 for x in tv]
return tva
and here is the code that I have used for user interface (mouse as paint brush to enter a digit)(convertToPNG() is used for tranparent background)
def paint(event):
x1, y1 = (event.x - 1), (event.y - 1)
x2, y2 = (event.x + 1), (event.y + 1)
cv.create_oval(x1, y1, x2, y2, fill="black",width=15)
draw.line([x1, y1, x2, y2],fill="black",width=15)
def convertToPNG():
img = PIL.Image.open('./image.png')
img = img.convert("RGBA")
datas = img.getdata()
newData = []
for item in datas:
if item[0] == 255 and item[1] == 255 and item[2] == 255:
newData.append((255, 255, 255, 0))
else:
newData.append(item)
img.putdata(newData)
img.save("./image.png", "PNG")
root = Tk()
cv = Canvas(root, width=width, height=height, bg='white')
cv.pack()
image1 = PIL.Image.new("RGB", (width, height), white)
draw = ImageDraw.Draw(image1)
cv.pack(expand=YES, fill=BOTH)
cv.bind("<B1-Motion>", paint)
button=Button(text="SaveImage",command=save)
button.pack()
button2=Button(text="StartLearning",command=startLearning)
button2.pack()
root.mainloop()