Spaces:
Runtime error
Runtime error
Kyle Dampier commited on
Commit ·
89e16b4
1
Parent(s): 60d926f
removed example UI from Notebook
Browse files231 unmodified lines232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359231 unmodified lines"print(\"Test accuracy:\", score[1])"]},{"cell_type": "code","execution_count": 7,"metadata": {},"outputs": [],"source": ["model.save(\"mnist.h5\")"]},{"cell_type": "markdown","metadata": {},"source": ["## Example GUI"]},{"cell_type": "code","execution_count": 11,"metadata": {},"outputs": [{"name": "stdout","output_type": "stream","text": ["4\n"]}],"source": ["from tkinter import *\n","from PIL import ImageGrab\n","import imageio\n","import tkinter.font as font\n","\n","class Paint(object):\n"," def __init__(self):\n"," self.root=Tk()\n"," self.root.title('Playing with numbers')\n"," # self.root.wm_iconbitmap('44143.ico')\n"," self.root.configure(background='light salmon')\n"," self.c = Canvas(self.root,bg='light cyan', height=330, width=400)\n"," self.label = Label(self.root, text='Draw any numer', font=20, bg='light salmon')\n"," self.label.grid(row=0, column=3)\n"," self.c.grid(row=1, columnspan=9)\n"," self.c.create_line(0,0,400,0,width=20,fill='midnight blue')\n"," self.c.create_line(0,0,0,330,width=20,fill='midnight blue')\n"," self.c.create_line(400,0,400,330,width=20,fill='midnight blue')\n"," self.c.create_line(0,330,400,330,width=20,fill='midnight blue')\n"," self.myfont = font.Font(size=20,weight='bold')\n"," self.predicting_button=Button(self.root,text='Predict', fg='white', bg='blue', height=2, width=6, font=self.myfont, command=lambda:self.classify(self.c))\n"," self.predicting_button.grid(row=2,column=1)\n"," self.clear=Button(self.root,text='Clear', fg='white', bg='orange', height=2, width=6, font=self.myfont, command=self.clear)\n"," self.clear.grid(row=2,column=5)\n"," self.prediction_text = Text(self.root, height=5, width=5)\n"," self.prediction_text.grid(row=4, column=3)\n"," self.label=Label(self.root, text=\"Predicted Number is\", fg=\"black\", font=30, bg='light salmon')\n","\n"," self.label.grid(row=3,column=3)\n"," self.model=model\n"," self.setup()\n"," self.root.mainloop()\n","\n","\n"," def setup(self):\n"," self.old_x=None\n"," self.old_y=None\n"," self.color='black'\n"," self.linewidth=15\n"," self.c.bind('<B1-Motion>', self.paint)\n"," self.c.bind('<ButtonRelease-1>', self.reset)\n","\n","\n"," def paint(self,event):\n"," paint_color=self.color\n"," if self.old_x and self.old_y:\n"," self.c.create_line(self.old_x,self.old_y,event.x,event.y,fill=paint_color,width=self.linewidth,capstyle=ROUND,\n"," smooth=TRUE,splinesteps=48)\n"," self.old_x=event.x\n"," self.old_y=event.y\n","\n","\n"," def clear(self):\n"," \"\"\"Clear drawing area\"\"\"\n"," self.c.delete(\"all\")\n","\n"," def reset(self, event):\n"," \"\"\"reset old_x and old_y if the left mouse button is released\"\"\"\n"," self.old_x, self.old_y = None, None\n","\n","\n"," def classify(self,widget):\n"," x=self.root.winfo_rootx()+widget.winfo_x()\n"," y=self.root.winfo_rooty()+widget.winfo_y()\n"," x1=widget.winfo_width()\n"," y1=widget.winfo_height()\n"," ImageGrab.grab().crop((x,y,x1,y1)).resize((28,28)).save('classify.png')\n"," img=imageio.imread('classify.png', as_gray=True, pilmode='P')\n"," img=np.array(img)\n"," img=np.reshape(img,(1,28,28,1))\n"," img[img==0] = 255\n"," img[img==225] = 0\n"," # Predict digit\n"," pred = self.model.predict([img])\n"," # Get index with highest probability\n"," pred = np.argmax(pred)\n"," print(pred)\n"," self.prediction_text.delete(\"1.0\", END)\n"," self.prediction_text.insert(END, pred)\n"," labelfont = ('times', 30, 'bold')\n"," self.prediction_text.config(font=labelfont)\n","\n","if __name__ == '__main__':\n"," Paint()"]},{"cell_type": "code","execution_count": null,"metadata": {},"outputs": [],"source": []}],"metadata": {231 unmodified lines232233234235236237238239240241242243244245246247248249250251252231 unmodified lines"print(\"Test accuracy:\", score[1])"]},{"cell_type": "markdown","metadata": {},"source": ["## Save Model (h5 format)"]},{"cell_type": "code","execution_count": 7,"metadata": {},"outputs": [],"source": ["model.save(\"mnist.h5\")"]}],"metadata": {
Week1.ipynb+4-111