[372cf9]: / BleedingImageClassification.ipynb

Download this file

183 lines (183 with data), 4.3 kB

{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "BleedingImageClassification.ipynb",
      "provenance": [],
      "collapsed_sections": []
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "cells": [
    {
      "cell_type": "code",
      "metadata": {
        "id": "tGZBwBkjWevk"
      },
      "source": [
        "!pip install tensorflow\n",
        "!pip install opencv-python\n",
        "!pip install numpy"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "L7taERIgXXH6"
      },
      "source": [
        "import tensorflow.keras\n",
        "import numpy as np\n",
        "import cv2\n",
        "import os\n",
        "np.set_printoptions(suppress=True)"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "87r5fH7FaDta",
        "outputId": "95ace021-e41f-4eeb-db35-ae25082e64fe"
      },
      "source": [
        "model = tensorflow.keras.models.load_model('keras_model.h5')"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "WARNING:tensorflow:No training configuration found in the save file, so the model was *not* compiled. Compile it manually.\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "0wGpjGSBa0Ga"
      },
      "source": [
        "data = np.ndarray(shape=(1, 224, 224, 3), dtype=np.float32)"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "YsDvM6oga0Rp"
      },
      "source": [
        "image = cv2.imread('/content/Capture.jpg')\n",
        "\n",
        "#resizing the image to be at least 224x224 and then cropping from the center\n",
        "size = (224, 224)\n",
        "\n",
        "image = cv2.resize(image, size, fx=0.5, fy=0.5, interpolation = cv2.INTER_AREA)\n",
        "#turn the image into a numpy array\n",
        "image_array = np.asarray(image)"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "oP537pQFa9Sq"
      },
      "source": [
        "normalized_image_array = (image_array.astype(np.float32) / 127.0) - 1"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "d6V-tVTha-dS"
      },
      "source": [
        "data[0] = normalized_image_array"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "LWzMOv2pbBPC",
        "outputId": "7f04c9f2-6785-4158-e0cc-2f1856a9a6bb"
      },
      "source": [
        "prediction = model.predict(data)\n",
        "print(prediction)"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "[[0.00362582 0.9963742 ]]\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "tuV4H65bZtT6",
        "outputId": "73aafe0a-ffb6-48cb-f099-d34e6d8cca7a"
      },
      "source": [
        "if prediction[0][0] > 0.7 and prediction[0][0] > prediction[0][1]:\n",
        "  print(\"Bleeding Brain\")\n",
        "else:\n",
        "  print(\"Non Bleeding Brain\")"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Non Bleeding Brain\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "b74k8FzlbL8h"
      },
      "source": [
        ""
      ],
      "execution_count": null,
      "outputs": []
    }
  ]
}