File size: 7,635 Bytes
c12a2ad
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "provenance": []
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "source": [
        "# Model Loading and Preparation"
      ],
      "metadata": {
        "id": "4O5JUlLfodka"
      }
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "mzcQ-zHRZT56"
      },
      "outputs": [],
      "source": [
        "import tensorflow as tf\n",
        "import numpy as np\n",
        "import json\n",
        "import requests\n",
        "from PIL import Image\n",
        "from io import BytesIO\n",
        "from huggingface_hub import snapshot_download\n",
        "\n",
        "!mkdir('model')\n",
        "# Download the entire model directory\n",
        "model_dir = snapshot_download(repo_id=\"eligapris/maize-diseases-detection\",\n",
        "    local_dir=\"model\")\n",
        "\n",
        "# Load the model\n",
        "model = tf.saved_model.load('model')\n",
        "\n"
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "# Image Download and Disease Prediction\n",
        "\n",
        "This section downloads an image of a maize leaf and uses the loaded model to predict any potential diseases or issues."
      ],
      "metadata": {
        "id": "4zcjmS_uoamd"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "\n",
        "# Now you can use the model for inference\n",
        "# Load and preprocess the image\n",
        "# url = 'https://plantvillage-production-new.s3.amazonaws.com/images/pics/000/062/234/original/5937333353_ea848b13e5_o.jpg'\n",
        "url = 'https://cropwatch.unl.edu/documents/Corn-southern-rust-F1.jpg'\n",
        "response = requests.get(url)\n",
        "\n",
        "img = Image.open(BytesIO(response.content))\n",
        "img = img.resize((300, 300 * img.size[1] // img.size[0]))\n",
        "img_array = np.array(img)[None]\n",
        "\n",
        "# Make prediction\n",
        "inp = tf.constant(img_array, dtype='float32')\n",
        "prediction = model(inp)[0].numpy()\n",
        "\n",
        "# Load class names\n",
        "with open('model/classes.json', 'r') as f:\n",
        "    class_names = json.load(f)\n",
        "\n",
        "# Get the predicted class\n",
        "predicted_class = list(class_names.keys())[prediction.argmax()]\n",
        "print(f\"Predicted class: {predicted_class}\")"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "GMGun011oZt6",
        "outputId": "fa8ec708-9d76-4951-ae07-5fd86bed1cb8"
      },
      "execution_count": 46,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Predicted class: Common_Rust\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "# Comprehensive Maize Disease Prediction and Insights\n",
        "\n",
        "This section provides detailed predictions for potential maize diseases identified in the input image. It offers further insights and information about the predicted diseases, including their characteristics, causes, and potential management strategies."
      ],
      "metadata": {
        "id": "wXjl2XdksEgc"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# Now you can use the model for inference\n",
        "# Load and preprocess the image\n",
        "url = 'https://cropwatch.unl.edu/image/969985-version%3D1.0%26t%3D1249303176000.jpg'\n",
        "response = requests.get(url)\n",
        "\n",
        "img = Image.open(BytesIO(response.content))\n",
        "img = img.resize((300, 300 * img.size[1] // img.size[0]))\n",
        "img_array = np.array(img)[None]\n",
        "\n",
        "# Make prediction\n",
        "inp = tf.constant(img_array, dtype='float32')\n",
        "prediction = model(inp)[0].numpy()\n",
        "\n",
        "# Load class names and details\n",
        "with open('model/classes_detailed.json', 'r') as f:\n",
        "    data = json.load(f)\n",
        "\n",
        "class_names = data['classes']\n",
        "class_details = data['details']\n",
        "\n",
        "# Get the predicted class\n",
        "predicted_class = list(class_names.keys())[prediction.argmax()]\n",
        "predicted_class_label = class_names[predicted_class]\n",
        "\n",
        "print(f\"Predicted class: {predicted_class} (Label: {predicted_class_label})\")\n",
        "\n",
        "# Print detailed information about the predicted class\n",
        "if predicted_class in class_details:\n",
        "    details = class_details[predicted_class]\n",
        "    print(\"\\nDetailed Information:\")\n",
        "    for key, value in details.items():\n",
        "        if isinstance(value, list):\n",
        "            print(f\"{key.capitalize()}:\")\n",
        "            for item in value:\n",
        "                print(f\"  - {item}\")\n",
        "        else:\n",
        "            print(f\"{key.capitalize()}: {value}\")\n",
        "\n",
        "# Print general notes\n",
        "print(\"\\nGeneral Notes:\")\n",
        "for note in data['general_notes']:\n",
        "    print(f\"- {note}\")"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "wrBohU41rUhl",
        "outputId": "72dac720-c3b8-4987-b672-5e3364882aa0"
      },
      "execution_count": 47,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Predicted class: Common_Rust (Label: 3)\n",
            "\n",
            "Detailed Information:\n",
            "Causative_agent: Puccinia sorghi\n",
            "Symptoms:\n",
            "  - Small, elongate, powdery pustules over both surfaces of the leaves\n",
            "  - Pustules are dark brown in early stages of infection\n",
            "  - Later, the epidermis is ruptured and the lesions turn black as the plant matures\n",
            "Environmental_conditions:\n",
            "  - Found worldwide in subtropical, temperate, and highland environments with high humidity\n",
            "Impact: Can reduce yield, especially if infection is severe before or during tasseling\n",
            "Notes:\n",
            "  - Most conspicuous when plants approach tasseling\n",
            "  - Alternate host (Oxalis spp.) may show light orange colored pustules\n",
            "\n",
            "General Notes:\n",
            "- Early detection and proper identification of these diseases are crucial for effective management.\n",
            "- Integrated pest management strategies, including resistant varieties, crop rotation, and timely fungicide applications, can help control these diseases.\n",
            "- Climate conditions, particularly humidity and temperature, play a significant role in the development and spread of these diseases.\n",
            "- Many diseases can have similar symptoms, so careful observation and sometimes laboratory analysis may be necessary for accurate diagnosis.\n",
            "- The severity of disease impact often depends on the timing of infection relative to the plant's growth stage.\n",
            "- Some pathogens can infect multiple parts of the plant, including leaves, stalks, and ears.\n"
          ]
        }
      ]
    }
  ]
}