File size: 3,122 Bytes
4f7e359
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
{
 "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
}