{ "cells": [ { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "ename": "ModuleNotFoundError", "evalue": "No module named 'google'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mModuleNotFoundError\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn[2], line 3\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mIPython\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mdisplay\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m display, HTML, Image\n\u001b[1;32m 2\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mbase64\u001b[39;00m\n\u001b[0;32m----> 3\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mgoogle\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mcolab\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m files\n\u001b[1;32m 4\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mos\u001b[39;00m\n\u001b[1;32m 6\u001b[0m \u001b[38;5;66;03m# Function to display HTML\u001b[39;00m\n", "\u001b[0;31mModuleNotFoundError\u001b[0m: No module named 'google'" ] } ], "source": [ "from IPython.display import display, HTML, Image\n", "import base64\n", "from google.colab import files\n", "import os\n", "\n", "# Function to display HTML\n", "def display_html(html):\n", " display(HTML(html))\n", "\n", "# HTML code for a webpage with image folder upload and label entry\n", "html_code = \"\"\"\n", "\n", "\n", "\n", " Object Detection Web App\n", "\n", "\n", "

Welcome to Object Detection Web App

\n", "

This is a simple web page created in Google Colab.

\n", "\n", "
\n", " \n", " \n", " \n", "
\n", "\n", "

Uploaded Images

\n", "
\n", "\n", " \n", "\n", "\n", "\"\"\"\n", "\n", "# Save HTML code to an HTML file\n", "html_file_path = '/content/object_detection_web_app.html'\n", "with open(html_file_path, 'w') as f:\n", " f.write(html_code)\n", "\n", "# Display a link to download the HTML file\n", "display_html(f'Download HTML File and Open Locally')\n", "\n", "# Image upload logic\n", "uploaded = files.upload()\n", "for label, images in uploaded.items():\n", " label_dir = os.path.join('/content/', label)\n", " os.makedirs(label_dir, exist_ok=True)\n", " for image_name, image_data in images.items():\n", " image_path = os.path.join(label_dir, image_name)\n", " with open(image_path, 'wb') as f:\n", " f.write(image_data)\n" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Collecting edgeimpulse\n", " Downloading edgeimpulse-1.0.8-py3-none-any.whl (44 kB)\n", " ---------------------------------------- 0.0/44.7 kB ? eta -:--:--\n", " --------- ------------------------------ 10.2/44.7 kB ? eta -:--:--\n", " ----------------- -------------------- 20.5/44.7 kB 217.9 kB/s eta 0:00:01\n", " -------------------------------------- 44.7/44.7 kB 313.3 kB/s eta 0:00:00\n", "Collecting edgeimpulse-api==1.40.11\n", " Downloading edgeimpulse_api-1.40.11-py3-none-any.whl (1.2 MB)\n", " ---------------------------------------- 0.0/1.2 MB ? eta -:--:--\n", " - -------------------------------------- 0.0/1.2 MB 991.0 kB/s eta 0:00:02\n", " -- ------------------------------------- 0.1/1.2 MB 812.7 kB/s eta 0:00:02\n", " ---- ----------------------------------- 0.1/1.2 MB 1.1 MB/s eta 0:00:02\n", " ----- ---------------------------------- 0.2/1.2 MB 1.2 MB/s eta 0:00:01\n", " --------- ------------------------------ 0.3/1.2 MB 1.3 MB/s eta 0:00:01\n", " ----------- ---------------------------- 0.4/1.2 MB 1.3 MB/s eta 0:00:01\n", " ----------------- ---------------------- 0.5/1.2 MB 1.7 MB/s eta 0:00:01\n", " ----------------------- ---------------- 0.7/1.2 MB 1.9 MB/s eta 0:00:01\n", " --------------------------- ------------ 0.8/1.2 MB 2.0 MB/s eta 0:00:01\n", " ---------------------------------- ----- 1.0/1.2 MB 2.3 MB/s eta 0:00:01\n", " ---------------------------------- ----- 1.0/1.2 MB 2.3 MB/s eta 0:00:01\n", " ----------------------------------- ---- 1.1/1.2 MB 1.9 MB/s eta 0:00:01\n", " ---------------------------------------- 1.2/1.2 MB 2.0 MB/s eta 0:00:00\n", "Requirement already satisfied: requests<3.0.0,>=2.23.0 in c:\\users\\admin\\appdata\\local\\programs\\python\\python310\\lib\\site-packages (from edgeimpulse) (2.31.0)\n", "Requirement already satisfied: python_dateutil<3.0.0,>=2.5.3 in c:\\users\\admin\\appdata\\local\\programs\\python\\python310\\lib\\site-packages (from edgeimpulse-api==1.40.11->edgeimpulse) (2.8.2)\n", "Collecting aenum<4.0.0,>=3.1.11\n", " Downloading aenum-3.1.15-py3-none-any.whl (137 kB)\n", " ---------------------------------------- 0.0/137.6 kB ? eta -:--:--\n", " -------------------------------------- 137.6/137.6 kB 4.1 MB/s eta 0:00:00\n", "Requirement already satisfied: urllib3<2.0.0,>=1.25.3 in c:\\users\\admin\\appdata\\local\\programs\\python\\python310\\lib\\site-packages (from edgeimpulse-api==1.40.11->edgeimpulse) (1.26.14)\n", "Collecting pydantic<2.0.0,>=1.10.2\n", " Downloading pydantic-1.10.13-cp310-cp310-win_amd64.whl (2.1 MB)\n", " ---------------------------------------- 0.0/2.1 MB ? eta -:--:--\n", " --- ------------------------------------ 0.2/2.1 MB 5.9 MB/s eta 0:00:01\n", " ------- -------------------------------- 0.4/2.1 MB 4.0 MB/s eta 0:00:01\n", " ----------- ---------------------------- 0.6/2.1 MB 4.8 MB/s eta 0:00:01\n", " -------------- ------------------------- 0.8/2.1 MB 4.1 MB/s eta 0:00:01\n", " ----------------- ---------------------- 0.9/2.1 MB 3.9 MB/s eta 0:00:01\n", " ------------------ --------------------- 1.0/2.1 MB 3.7 MB/s eta 0:00:01\n", " --------------------- ------------------ 1.1/2.1 MB 3.6 MB/s eta 0:00:01\n", " ----------------------- ---------------- 1.3/2.1 MB 3.3 MB/s eta 0:00:01\n", " ------------------------- -------------- 1.3/2.1 MB 3.3 MB/s eta 0:00:01\n", " ---------------------------- ----------- 1.5/2.1 MB 3.3 MB/s eta 0:00:01\n", " ------------------------------ --------- 1.6/2.1 MB 3.3 MB/s eta 0:00:01\n", " ------------------------------- -------- 1.7/2.1 MB 3.2 MB/s eta 0:00:01\n", " ---------------------------------- ----- 1.8/2.1 MB 3.1 MB/s eta 0:00:01\n", " -------------------------------------- - 2.0/2.1 MB 3.1 MB/s eta 0:00:01\n", " ---------------------------------------- 2.1/2.1 MB 3.1 MB/s eta 0:00:00\n", "Requirement already satisfied: idna<4,>=2.5 in c:\\users\\admin\\appdata\\local\\programs\\python\\python310\\lib\\site-packages (from requests<3.0.0,>=2.23.0->edgeimpulse) (3.4)\n", "Requirement already satisfied: certifi>=2017.4.17 in c:\\users\\admin\\appdata\\local\\programs\\python\\python310\\lib\\site-packages (from requests<3.0.0,>=2.23.0->edgeimpulse) (2022.12.7)\n", "Requirement already satisfied: charset-normalizer<4,>=2 in c:\\users\\admin\\appdata\\local\\programs\\python\\python310\\lib\\site-packages (from requests<3.0.0,>=2.23.0->edgeimpulse) (2.1.1)\n", "Requirement already satisfied: typing-extensions>=4.2.0 in c:\\users\\admin\\appdata\\local\\programs\\python\\python310\\lib\\site-packages (from pydantic<2.0.0,>=1.10.2->edgeimpulse-api==1.40.11->edgeimpulse) (4.4.0)\n", "Requirement already satisfied: six>=1.5 in c:\\users\\admin\\appdata\\local\\programs\\python\\python310\\lib\\site-packages (from python_dateutil<3.0.0,>=2.5.3->edgeimpulse-api==1.40.11->edgeimpulse) (1.16.0)\n", "Installing collected packages: aenum, pydantic, edgeimpulse-api, edgeimpulse\n", "Successfully installed aenum-3.1.15 edgeimpulse-1.0.8 edgeimpulse-api-1.40.11 pydantic-1.10.13\n", "Note: you may need to restart the kernel to use updated packages.\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "\n", "[notice] A new release of pip is available: 23.0 -> 23.3.2\n", "[notice] To update, run: python.exe -m pip install --upgrade pip\n" ] } ], "source": [ "pip install edgeimpulse\n" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Note: you may need to restart the kernel to use updated packages.\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "ERROR: Could not find a version that satisfies the requirement git (from versions: none)\n", "ERROR: No matching distribution found for git\n", "\n", "[notice] A new release of pip is available: 23.0 -> 23.3.2\n", "[notice] To update, run: python.exe -m pip install --upgrade pip\n" ] } ], "source": [ "pip install git" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Defaulting to user installation because normal site-packages is not writeable\n", "\u001b[31mERROR: Could not find a version that satisfies the requirement tensorflow.keras (from versions: none)\u001b[0m\n", "\u001b[31mERROR: No matching distribution found for tensorflow.keras\u001b[0m\n", "\u001b[33mWARNING: You are using pip version 21.2.4; however, version 23.3.2 is available.\n", "You should consider upgrading via the '/Library/Developer/CommandLineTools/usr/bin/python3 -m pip install --upgrade pip' command.\u001b[0m\n", "Note: you may need to restart the kernel to use updated packages.\n" ] } ], "source": [ "pip install " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Epoch 1/10\n", "1/1 [==============================] - 0s 398ms/step - loss: 0.6844 - accuracy: 0.6667 - val_loss: 0.7236 - val_accuracy: 0.5000\n", "Epoch 2/10\n", "1/1 [==============================] - 0s 24ms/step - loss: 0.6936 - accuracy: 0.5000 - val_loss: 0.7161 - val_accuracy: 0.5000\n", "Epoch 3/10\n", "1/1 [==============================] - 0s 24ms/step - loss: 0.6760 - accuracy: 0.6667 - val_loss: 0.7166 - val_accuracy: 0.5000\n", "Epoch 4/10\n", "1/1 [==============================] - 0s 22ms/step - loss: 0.6790 - accuracy: 0.6667 - val_loss: 0.7060 - val_accuracy: 0.5000\n", "Epoch 5/10\n", "1/1 [==============================] - 0s 22ms/step - loss: 0.6759 - accuracy: 0.8333 - val_loss: 0.6996 - val_accuracy: 0.5000\n", "Epoch 6/10\n", "1/1 [==============================] - 0s 23ms/step - loss: 0.6701 - accuracy: 0.6667 - val_loss: 0.6999 - val_accuracy: 0.5000\n", "Epoch 7/10\n", "1/1 [==============================] - 0s 22ms/step - loss: 0.6721 - accuracy: 0.6667 - val_loss: 0.6896 - val_accuracy: 0.5000\n", "Epoch 8/10\n", "1/1 [==============================] - 0s 22ms/step - loss: 0.6599 - accuracy: 0.6667 - val_loss: 0.6745 - val_accuracy: 1.0000\n", "Epoch 9/10\n", "1/1 [==============================] - 0s 24ms/step - loss: 0.6509 - accuracy: 0.8333 - val_loss: 0.6739 - val_accuracy: 0.5000\n", "Epoch 10/10\n", "1/1 [==============================] - 0s 24ms/step - loss: 0.6237 - accuracy: 0.8333 - val_loss: 0.6647 - val_accuracy: 0.5000\n", "Model trained with data augmentation and saved successfully.\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "/Users/varunsankar/Library/Python/3.9/lib/python/site-packages/keras/src/engine/training.py:3103: UserWarning: You are saving your model as an HDF5 file via `model.save()`. This file format is considered legacy. We recommend using instead the native Keras format, e.g. `model.save('my_model.keras')`.\n", " saving_api.save_model(\n" ] } ], "source": [ "\n", "from tensorflow.keras import layers, models, optimizers\n", "import cv2\n", "import numpy as np\n", "from sklearn.model_selection import train_test_split\n", "from sklearn.preprocessing import LabelEncoder\n", "from tensorflow.keras import layers, models, optimizers\n", "from tensorflow.keras.preprocessing.image import ImageDataGenerator\n", "import os\n", " \n", "# Function to load images and labels from folders\n", "def load_data(folder_path):\n", " images = []\n", " labels = []\n", " \n", " for label in os.listdir(folder_path):\n", " label_path = os.path.join(folder_path, label)\n", " if os.path.isdir(label_path):\n", " for filename in os.listdir(label_path):\n", " img_path = os.path.join(label_path, filename)\n", " img = cv2.imread(img_path)\n", " img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) # Convert to RGB\n", " images.append(img)\n", " labels.append(label)\n", " \n", " return np.array(images), np.array(labels)\n", "\n", "\n", "# Load data from folders\n", "data_path = \"/Users/varunsankar/webappml/data\"\n", "images, labels = load_data(data_path)\n", "\n", "# Encode labels\n", "label_encoder = LabelEncoder()\n", "encoded_labels = label_encoder.fit_transform(labels)\n", "\n", "# Split data into training and testing sets\n", "X_train, X_test, y_train, y_test = train_test_split(images, encoded_labels, test_size=0.2, random_state=42)\n", "\n", "# Normalize pixel values to be between 0 and 1\n", "X_train, X_test = X_train / 255.0, X_test / 255.0\n", "\n", "# Data Augmentation\n", "datagen = ImageDataGenerator(\n", " rotation_range=20,\n", " width_shift_range=0.2,\n", " height_shift_range=0.2,\n", " shear_range=0.2,\n", " zoom_range=0.2,\n", " horizontal_flip=True,\n", " fill_mode='nearest'\n", ")\n", "\n", "# Fit the ImageDataGenerator on the training data\n", "datagen.fit(X_train)\n", "\n", " \n", "model = models.Sequential()\n", "model.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 3)))\n", "model.add(layers.MaxPooling2D((2, 2)))\n", "model.add(layers.Conv2D(64, (3, 3), activation='relu'))\n", "model.add(layers.MaxPooling2D((2, 2)))\n", "model.add(layers.Conv2D(64, (3, 3), activation='relu'))\n", "model.add(layers.Flatten())\n", "model.add(layers.Dense(64, activation='relu'))\n", "model.add(layers.Dense(len(set(labels)), activation='softmax'))\n", "\n", "model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])\n", "\n", "# Train the model with augmented data\n", "model.fit(datagen.flow(X_train, y_train, batch_size=32), epochs=10, validation_data=(X_test, y_test))\n", "\n", "# Save the model\n", "model.save(\"01objcls.h5\")\n", "\n", "print(\"Model trained with data augmentation and saved successfully.\")\n", " " ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "WARNING:tensorflow:From c:\\Users\\Admin\\AppData\\Local\\Programs\\Python\\Python310\\lib\\site-packages\\keras\\src\\losses.py:2976: The name tf.losses.sparse_softmax_cross_entropy is deprecated. Please use tf.compat.v1.losses.sparse_softmax_cross_entropy instead.\n", "\n" ] } ], "source": [ "import cv2\n", "import numpy as np\n", "from sklearn.model_selection import train_test_split\n", "from sklearn.preprocessing import LabelEncoder\n", "from tensorflow.keras import layers, models, optimizers\n", "from tensorflow.keras.preprocessing.image import ImageDataGenerator" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Flask version: 3.0.0\n", "OpenCV version: 4.9.0\n", "scikit-learn version: 1.2.1\n", "NumPy version: 1.23.5\n", "TensorFlow version: 2.15.0\n", "Edge Impulse SDK version: 1.0.8\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "C:\\Users\\Admin\\AppData\\Local\\Temp\\ipykernel_16592\\2789360100.py:8: DeprecationWarning: The '__version__' attribute is deprecated and will be removed in Flask 3.1. Use feature detection or 'importlib.metadata.version(\"flask\")' instead.\n", " print(f'Flask version: {flask.__version__}')\n" ] } ], "source": [ "import flask\n", "import cv2\n", "import sklearn\n", "import numpy\n", "import tensorflow\n", "import edgeimpulse as ei\n", "\n", "print(f'Flask version: {flask.__version__}')\n", "print(f'OpenCV version: {cv2.__version__}')\n", "print(f'scikit-learn version: {sklearn.__version__}')\n", "print(f'NumPy version: {numpy.__version__}')\n", "print(f'TensorFlow version: {tensorflow.__version__}')\n", "print(f'Edge Impulse SDK version: {ei.__version__}')\n" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "from flask import Flask, render_template, request, jsonify\n", "import os\n", "import cv2\n", "import numpy as np\n", "from sklearn.model_selection import train_test_split\n", "from sklearn.preprocessing import LabelEncoder\n", "from tensorflow.keras import layers, models, optimizers\n", "from tensorflow.keras.preprocessing.image import ImageDataGenerator" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "WARNING:tensorflow:From c:\\Users\\Admin\\AppData\\Local\\Programs\\Python\\Python310\\lib\\site-packages\\keras\\src\\losses.py:2976: The name tf.losses.sparse_softmax_cross_entropy is deprecated. Please use tf.compat.v1.losses.sparse_softmax_cross_entropy instead.\n", "\n" ] } ], "source": [ "import os\n", "from werkzeug.utils import secure_filename\n", "import subprocess\n", "from flask import send_file\n", "from tensorflow import keras\n", "import edgeimpulse as ei\n", "import tensorflow as tf" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "ename": "NameError", "evalue": "name 'hs1' is not defined", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)", "Cell \u001b[1;32mIn[9], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m loaded_model \u001b[38;5;241m=\u001b[39m tf\u001b[38;5;241m.\u001b[39mkeras\u001b[38;5;241m.\u001b[39mmodels\u001b[38;5;241m.\u001b[39mload_model(\u001b[43mhs1\u001b[49m\u001b[38;5;241m.\u001b[39mh5)\n\u001b[0;32m 3\u001b[0m model_output_type \u001b[38;5;241m=\u001b[39m ei\u001b[38;5;241m.\u001b[39mmodel\u001b[38;5;241m.\u001b[39moutput_type\u001b[38;5;241m.\u001b[39mClassification(labels\u001b[38;5;241m=\u001b[39mlabels_list)\n\u001b[0;32m 5\u001b[0m \u001b[38;5;66;03m# Set model input type\u001b[39;00m\n", "\u001b[1;31mNameError\u001b[0m: name 'hs1' is not defined" ] } ], "source": [ "loaded_model = tf.keras.models.load_model(hs1.h5)\n", "\n", "model_output_type = ei.model.output_type.Classification(labels=labels_list)\n", "\n", " # Set model input type\n", "model_input_type = ei.model.input_type.OtherInput()\n", "\n", " # Estimate the RAM, ROM, and inference time for our model on the target hardware family\n", "try:\n", " profile = ei.model.profile(model=loaded_model, device=deploy_target)\n", " print(profile.summary())\n", "except Exception as e:\n", " print(f\"Could not profile: {e}\")\n", "\n", " # Create C++ library with trained model\n", "deploy_bytes = None\n", "try:\n", " deploy_bytes = ei.model.deploy(model=loaded_model, model_output_type=model_output_type,\n", " model_input_type=model_input_type, deploy_target=\"arduino\")\n", "except Exception as e:\n", " print(f\"Could not deploy: {e}\")\n", "\n", " # Write the downloaded raw bytes to a temporary file\n", "if deploy_bytes:\n", " temp_deploy_filename = deploy_filename\n", " with open(temp_deploy_filename, 'wb') as f:\n", " f.write(deploy_bytes.getvalue())\n", "\n" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "c3bd84c6323547fcbc3a934699437777", "version_major": 2, "version_minor": 0 }, "text/plain": [ "VBox(children=(HTML(value='