{"metadata":{"colab":{"provenance":[]},"kernelspec":{"name":"python3","display_name":"Python 3","language":"python"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import os\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport cv2 \nimport tensorflow as tf\n\n\nmnist = tf.keras.datasets.mnist\n\n\n(x_train, y_train), (x_test, y_test)= mnist.load_data()\n\nx_train = tf.keras.utils.normalize(x_train, axis=1)\nx_test = tf.keras.utils.normalize(x_test, axis=1)\n\nmodel = tf.keras.models.Sequential()\n\nmodel.add(tf.keras.layers.Flatten(input_shape=(28, 28)))\nmodel.add(tf.keras.layers.Dense(128, activation='relu'))\nmodel.add(tf.keras.layers.Dense(128, activation='relu'))\nmodel.add(tf.keras.layers.Dense(10, activation='softmax'))\nmodel.compile(optimizer='adam', loss='sparse_categorical_crossentropy',metrics=['accuracy'])\n\nmodel.fit(x_train, y_train,)\n\nmodel.save('handwritten.model')","metadata":{"id":"myo-s2HfTT5g","outputId":"1c144ca4-84f9-4174-bb43-9857e778ebae","execution":{"iopub.status.busy":"2023-09-10T04:40:23.365441Z","iopub.execute_input":"2023-09-10T04:40:23.365845Z","iopub.status.idle":"2023-09-10T04:40:45.976582Z","shell.execute_reply.started":"2023-09-10T04:40:23.365811Z","shell.execute_reply":"2023-09-10T04:40:45.975337Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = tf.keras.models.load_model('handwritten.model')\n\nloss, accuracy = model.evaluate(x_test, y_test)\nprint(loss)\nprint(accuracy)","metadata":{"id":"-Dx8dmOvTsoT","outputId":"1016eb54-7f58-48c8-928f-708430dd0ed4","execution":{"iopub.status.busy":"2023-09-10T04:40:45.979681Z","iopub.execute_input":"2023-09-10T04:40:45.980196Z","iopub.status.idle":"2023-09-10T04:40:47.292179Z","shell.execute_reply.started":"2023-09-10T04:40:45.980138Z","shell.execute_reply":"2023-09-10T04:40:47.290840Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_number = 1\nwhile os.path.isfile(f\"Digits/digit{image_number}.png\"):\n  try:\n    img = cv2.imread(f\"Digits/digit{image_number}.png\")[:,:,0]\n    img = np.invert(np.array([img]))\n    prediction = model.predict(img)\n    print(f\"This digit is probably a {np.argmax(prediction)}\")\n    plt.imshow(img[0], cmap=plt.cm.binary)\n    plt.show()\n  except:\n    print(\"Error!\")\n  finally:\n    image_number += 1","metadata":{"id":"bsnMKqNDbCfR","outputId":"885122ac-0b04-4916-bb89-a03028c7e991","execution":{"iopub.status.busy":"2023-09-10T04:40:47.293349Z","iopub.execute_input":"2023-09-10T04:40:47.293680Z","iopub.status.idle":"2023-09-10T04:40:47.301184Z","shell.execute_reply.started":"2023-09-10T04:40:47.293653Z","shell.execute_reply":"2023-09-10T04:40:47.299954Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nimage_number = 1\nwhile os.path.isfile(f\"Digits/digit{image_number}.png\"):\n  try:\n    img = cv2.imread(f\"Digits/digit{image_number}.png\")[:,:,0]\n    img = np.invert(np.array([img]))\n    prediction = model.predict(img)\n    print(f\"This digit is probably a {np.argmax(prediction)}\")\n    plt.imshow(img[0], cmap=plt.cm.binary)\n    plt.show()\n  except:\n    print(\"Error!\")\n  finally:\n    image_number += 1","metadata":{"id":"Bk0L9n0sfQTr","outputId":"854408ee-6455-42ba-85a9-dac15e8540bf","execution":{"iopub.status.busy":"2023-09-10T04:40:47.303796Z","iopub.execute_input":"2023-09-10T04:40:47.304846Z","iopub.status.idle":"2023-09-10T04:40:47.315961Z","shell.execute_reply.started":"2023-09-10T04:40:47.304806Z","shell.execute_reply":"2023-09-10T04:40:47.315020Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"id":"ONJ04c3uhc1G"},"execution_count":null,"outputs":[]}],"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}}