{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Overwriting app.py\n" ] } ], "source": [ "%%writefile app.py\n", "import streamlit as st\n", "import tensorflow as tf\n", "import cv2\n", "from PIL import Image, ImageOps\n", "import numpy as np\n", "import matplotlib.pyplot as plt\n", "import numpy as np\n", "import os\n", "import PIL\n", "import tensorflow as tf\n", "\n", "from tensorflow import keras\n", "from tensorflow.keras import layers\n", "from tensorflow.keras.models import Sequential\n", "\n", "st.title(\"Cat-Dog Classification\")\n", "st.header(\"Please input an image to be classified:\")\n", "st.text(\"Created by Saksham Gulati\")\n", "\n", "@st.cache(allow_output_mutation=True)\n", "\n", "def teachable_machine_classification(img, weights_file):\n", " # Load the model\n", " model = keras.models.load_model(weights_file)\n", "\n", " # Create the array of the right shape to feed into the keras model\n", " data = np.ndarray(shape=(1, 200, 200, 3), dtype=np.float32)\n", " image = img\n", " #image sizing\n", " size = (200, 200)\n", " image = ImageOps.fit(image, size, Image.ANTIALIAS)\n", "\n", " #turn the image into a numpy array\n", " image_array = np.asarray(image)\n", " # Normalize the image\n", " normalized_image_array = (image_array.astype(np.float32) / 255)\n", "\n", " # Load the image into the array\n", " data[0] = normalized_image_array\n", "\n", " # run the inference\n", " prediction_percentage = model.predict(data)\n", " prediction=prediction_percentage.round()\n", " \n", " return prediction,prediction_percentage\n", "\n", "\n", "uploaded_file = st.file_uploader(\"Choose an Cat or Dog Image...\", type=\"jpg\")\n", "\n", "if uploaded_file is not None:\n", " image = Image.open(uploaded_file)\n", " st.image(image, caption='Uploaded file', use_column_width=True)\n", " st.write(\"\")\n", " st.write(\"Classifying...\")\n", " label,perc = teachable_machine_classification(image, 'catdog.h5')\n", " if label == 1:\n", " st.write(\"Its a Dog, confidence level:\",perc)\n", " else:\n", " st.write(\"Its a Cat, confidence level:\",1-perc)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.12" } }, "nbformat": 4, "nbformat_minor": 2 }