Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,442 +1,445 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
import
|
| 4 |
-
import
|
| 5 |
-
|
| 6 |
-
import
|
| 7 |
-
import
|
| 8 |
-
from
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
import
|
| 14 |
-
import
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
</
|
| 28 |
-
<p class="description-text">
|
| 29 |
-
|
| 30 |
-
</p>
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
<div
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
<
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
<div
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
<
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
<div
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
</
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
</
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
<div
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
<div
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
<li>
|
| 102 |
-
<li>Reduce
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
</
|
| 107 |
-
</div>
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
<div
|
| 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 |
-
</div>
|
| 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 |
-
with gr.TabItem("
|
| 187 |
-
gr.
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
"""
|
| 191 |
-
# Model
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
#
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
#
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
#
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
#
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
"""
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
nn.Conv2d(
|
| 261 |
-
nn.BatchNorm2d(out_channels),
|
| 262 |
-
nn.ReLU(inplace=True)
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
|
| 311 |
-
|
| 312 |
-
|
| 313 |
-
|
| 314 |
-
|
| 315 |
-
self.
|
| 316 |
-
|
| 317 |
-
|
| 318 |
-
self.
|
| 319 |
-
|
| 320 |
-
self.
|
| 321 |
-
|
| 322 |
-
|
| 323 |
-
self.
|
| 324 |
-
|
| 325 |
-
|
| 326 |
-
self.
|
| 327 |
-
self.
|
| 328 |
-
|
| 329 |
-
|
| 330 |
-
|
| 331 |
-
|
| 332 |
-
|
| 333 |
-
|
| 334 |
-
|
| 335 |
-
|
| 336 |
-
|
| 337 |
-
|
| 338 |
-
|
| 339 |
-
x = self.
|
| 340 |
-
|
| 341 |
-
|
| 342 |
-
|
| 343 |
-
|
| 344 |
-
|
| 345 |
-
|
| 346 |
-
|
| 347 |
-
|
| 348 |
-
|
| 349 |
-
|
| 350 |
-
|
| 351 |
-
|
| 352 |
-
|
| 353 |
-
|
| 354 |
-
|
| 355 |
-
|
| 356 |
-
|
| 357 |
-
|
| 358 |
-
|
| 359 |
-
|
| 360 |
-
|
| 361 |
-
print("
|
| 362 |
-
|
| 363 |
-
|
| 364 |
-
|
| 365 |
-
|
| 366 |
-
|
| 367 |
-
|
| 368 |
-
|
| 369 |
-
|
| 370 |
-
|
| 371 |
-
|
| 372 |
-
|
| 373 |
-
|
| 374 |
-
|
| 375 |
-
|
| 376 |
-
|
| 377 |
-
|
| 378 |
-
|
| 379 |
-
|
| 380 |
-
|
| 381 |
-
#
|
| 382 |
-
|
| 383 |
-
|
| 384 |
-
|
| 385 |
-
|
| 386 |
-
|
| 387 |
-
|
| 388 |
-
|
| 389 |
-
|
| 390 |
-
|
| 391 |
-
|
| 392 |
-
|
| 393 |
-
|
| 394 |
-
|
| 395 |
-
axes[
|
| 396 |
-
|
| 397 |
-
|
| 398 |
-
|
| 399 |
-
|
| 400 |
-
|
| 401 |
-
|
| 402 |
-
|
| 403 |
-
|
| 404 |
-
|
| 405 |
-
|
| 406 |
-
|
| 407 |
-
|
| 408 |
-
|
| 409 |
-
|
| 410 |
-
|
| 411 |
-
|
| 412 |
-
|
| 413 |
-
|
| 414 |
-
|
| 415 |
-
|
| 416 |
-
|
| 417 |
-
|
| 418 |
-
|
| 419 |
-
|
| 420 |
-
|
| 421 |
-
|
| 422 |
-
|
| 423 |
-
|
| 424 |
-
|
| 425 |
-
|
| 426 |
-
|
| 427 |
-
|
| 428 |
-
|
| 429 |
-
|
| 430 |
-
|
| 431 |
-
|
| 432 |
-
|
| 433 |
-
|
| 434 |
-
|
| 435 |
-
|
| 436 |
-
|
| 437 |
-
|
| 438 |
-
|
| 439 |
-
|
| 440 |
-
|
| 441 |
-
|
| 442 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
os.system("pip install torch")
|
| 2 |
+
|
| 3 |
+
import gradio as gr
|
| 4 |
+
import os
|
| 5 |
+
import numpy as np
|
| 6 |
+
import torch
|
| 7 |
+
import torch.nn as nn
|
| 8 |
+
from torchvision import transforms
|
| 9 |
+
import requests
|
| 10 |
+
import os
|
| 11 |
+
from PIL import Image
|
| 12 |
+
from collections import OrderedDict
|
| 13 |
+
from torchvision import models
|
| 14 |
+
import torch.nn.functional as F
|
| 15 |
+
import matplotlib.pyplot as plt
|
| 16 |
+
import cv2
|
| 17 |
+
import io
|
| 18 |
+
# Import CSS and URL File
|
| 19 |
+
css_file_path = os.path.join(os.path.dirname(__file__), "ui.css")
|
| 20 |
+
with open(css_file_path,"r") as f:
|
| 21 |
+
custom_css = f.read()
|
| 22 |
+
# HTML Design
|
| 23 |
+
html_welcome_page = """
|
| 24 |
+
<div class="container">
|
| 25 |
+
<div class="inner-container">
|
| 26 |
+
<h1 class="title-text">Welcome to RemoveWeed Weed Detection System</h1>
|
| 27 |
+
<img src="https://i.ibb.co/fY1nk315/image-2.png" alt="RemoveWeed Logo" class="logo-container"/>
|
| 28 |
+
<p class="description-text">
|
| 29 |
+
Project Aim: This system is designed to optimize rice planting schedules with broad-leaved weed detection using machine learning.
|
| 30 |
+
</p>
|
| 31 |
+
<p class="description-text">
|
| 32 |
+
Designed by: Whitney Lim Wan Yee (TP068221)
|
| 33 |
+
</p>
|
| 34 |
+
</div>
|
| 35 |
+
</div>
|
| 36 |
+
"""
|
| 37 |
+
html_system_page ="""
|
| 38 |
+
<div class="container">
|
| 39 |
+
<img src="https://i.ibb.co/KxMMTmxG/Screenshot-2025-03-28-224907.png" alt="RemoveWeed Logo" class="logo-container-system"/>
|
| 40 |
+
<h1 class="system-page-title">RemoveWeed System Overview</h1>
|
| 41 |
+
<p class="system-page-description">
|
| 42 |
+
This system is designed to help farmers detect broad-leaved weeds in rice fields using machine learning techniques.
|
| 43 |
+
The aim is to optimize rice planting schedules and improve crop yield.
|
| 44 |
+
</p>
|
| 45 |
+
</div>
|
| 46 |
+
"""
|
| 47 |
+
html_project_description = """
|
| 48 |
+
<div class="project-container">
|
| 49 |
+
<h1 class="project-title">- 🌿 About Project 🌿 -</h1>
|
| 50 |
+
|
| 51 |
+
<div class="upper-content">
|
| 52 |
+
<div class="left-upper-column">
|
| 53 |
+
<div class="chart">
|
| 54 |
+
<img src="https://i.ibb.co/j9Ch3xnC/1312103.png" alt="Agricultural consumption of herbicides worldwide from 1990 to 2022" class="chart-image">
|
| 55 |
+
<p class="chart-caption">Resource: Statista (2024) - Agricultural consumption of herbicides worldwide from 1990 to 2022 (in 1,000 metric tons)</p>
|
| 56 |
+
</div>
|
| 57 |
+
</div>
|
| 58 |
+
<div class="right-upper-column">
|
| 59 |
+
<div class="herbicide-description">
|
| 60 |
+
<h2 class="herbicide-title">Herbicide Use Soars: A Shocking Yearly Increase!</h2>
|
| 61 |
+
<p class="herbicide-text">
|
| 62 |
+
Statista (2024) revealed that global herbicide consumption has reached <span class="bold-red">1.94 million</span> metric tons. To control dock weed in farming fields, the application of herbicides can cause <span class="bold-red">delays</span> in rice planting schedules ranging from <span class="bold-red">7 to 30 days</span>.
|
| 63 |
+
</p>
|
| 64 |
+
</div>
|
| 65 |
+
</div>
|
| 66 |
+
</div>
|
| 67 |
+
|
| 68 |
+
<div class="middle-content">
|
| 69 |
+
<div class="left-middle-column">
|
| 70 |
+
<div class="objective-description">
|
| 71 |
+
<h2 class="objective-title">Why Choose RemoveWeed?</h2>
|
| 72 |
+
<p class="objective-text">
|
| 73 |
+
RemoveWeed is a system designed to detect broad-leaved dock weed in paddy fields. It uses object detection like <span class="bold-red">Single Shot Detection (SSD)</span> model, along with instance segmentation models like <span class="bold-red">U-Net</span> and <span class="bold-red">Fully Convolutional Neural Network (FCNN</span>, to predict the presence of dock weed.
|
| 74 |
+
</p>
|
| 75 |
+
</div>
|
| 76 |
+
</div>
|
| 77 |
+
<div class="right-middle-column">
|
| 78 |
+
<div class="carousel-wrapper">
|
| 79 |
+
<div class="carousel-container">
|
| 80 |
+
<p class="carousel-title">Broad-leaved Dock Weed in Paddy Field</p>
|
| 81 |
+
<div class="carousel">
|
| 82 |
+
<div class="image-one"></div>
|
| 83 |
+
<div class="image-two"></div>
|
| 84 |
+
<div class="image-three"></div>
|
| 85 |
+
</div>
|
| 86 |
+
</div>
|
| 87 |
+
</div>
|
| 88 |
+
</div>
|
| 89 |
+
</div>
|
| 90 |
+
|
| 91 |
+
<div class="bottom-content">
|
| 92 |
+
<div class="left-bottom-column">
|
| 93 |
+
<div class="Proceed-To-Detection">
|
| 94 |
+
<img src="https://i.ibb.co/Txb9LFf5/agriculture-tan.jpg" alt="Model Training" class="model-image">
|
| 95 |
+
</div>
|
| 96 |
+
</div>
|
| 97 |
+
<div class="right-bottom-column">
|
| 98 |
+
<div class="benefits-description">
|
| 99 |
+
<h2 class="benefits-title">Potential Benefits</h2>
|
| 100 |
+
<ul class="benefits-list">
|
| 101 |
+
<li>Cost Savings 💰</li>
|
| 102 |
+
<li>Reduce Labor and Manual Monitoring Cost 💹</li>
|
| 103 |
+
<li>Increase Profitability by Rice Planting Scheduling Advice 📈</li>
|
| 104 |
+
<li>Provide Sustainable Practices in Agriculture 🧑🌾</li>
|
| 105 |
+
<li>Reduce Herbicide Pollution ☢️</li>
|
| 106 |
+
</ul>
|
| 107 |
+
</div>
|
| 108 |
+
</div>
|
| 109 |
+
</div>
|
| 110 |
+
</div>
|
| 111 |
+
"""
|
| 112 |
+
html_author_review_page = """
|
| 113 |
+
<div class="author-section">
|
| 114 |
+
<h1 class="author-title">- Project Owner Introduction -</h1>
|
| 115 |
+
|
| 116 |
+
<div class="author-content">
|
| 117 |
+
<div class="author-image-container">
|
| 118 |
+
<img src="https://i.ibb.co/4RZW1Pq4/Wanyu.jpg" alt="Whitney Lim Wan Yee" class="author-image">
|
| 119 |
+
</div>
|
| 120 |
+
|
| 121 |
+
<div class="author-bio">
|
| 122 |
+
<p class="author-text">
|
| 123 |
+
Whitney Lim Wan Yee is a student at Asia Pacific University (APU), pursuing Year 3 Computer Science specialization in Data Analytics. She is passionate about machine learning and its applications in agriculture.
|
| 124 |
+
</p>
|
| 125 |
+
|
| 126 |
+
<div class="social-links">
|
| 127 |
+
<a href="https://www.linkedin.com/in/whitneylimwanyee/" target="_blank" class="social-link">
|
| 128 |
+
<img src="https://images.rawpixel.com/image_png_800/czNmcy1wcml2YXRlL3Jhd3BpeGVsX2ltYWdlcy93ZWJzaXRlX2NvbnRlbnQvbHIvdjk4Mi1kMy0xMC5wbmc.png" alt="LinkedIn" class="social-icon">
|
| 129 |
+
<span>LinkedIn Profile</span>
|
| 130 |
+
</a>
|
| 131 |
+
|
| 132 |
+
<a href="https://www.kaggle.com/whitneylimwanyee" target="_blank" class="social-link">
|
| 133 |
+
<img src="https://cdn4.iconfinder.com/data/icons/logos-and-brands/512/189_Kaggle_logo_logos-512.png" alt="Kaggle" class="social-icon">
|
| 134 |
+
<span>Kaggle Profile</span>
|
| 135 |
+
</a>
|
| 136 |
+
|
| 137 |
+
<button onclick="window.location.href='mailto:whitneylim0719@gmail.com'" class="social-link">
|
| 138 |
+
<img src="https://static.vecteezy.com/system/resources/previews/016/716/465/non_2x/gmail-icon-free-png.png" alt="Email" class="social-icon">
|
| 139 |
+
<span>Email Me</span>
|
| 140 |
+
</button>
|
| 141 |
+
<a href="https://drive.google.com/file/d/1SvbvzLpFQJjzX6_VPGS3NddzK0ksXE8r/view" target="_blank" class="social-link">
|
| 142 |
+
<img src="https://cdn-icons-png.flaticon.com/512/8347/8347432.png" alt="Kaggle" class="social-icon">
|
| 143 |
+
<span>My Resume</span>
|
| 144 |
+
</a>
|
| 145 |
+
</div>
|
| 146 |
+
</div>
|
| 147 |
+
</div>
|
| 148 |
+
</div>
|
| 149 |
+
"""
|
| 150 |
+
|
| 151 |
+
|
| 152 |
+
js_func = """
|
| 153 |
+
function refresh() {
|
| 154 |
+
const url = new URL(window.location);
|
| 155 |
+
|
| 156 |
+
if (url.searchParams.get('__theme') !== 'light') {
|
| 157 |
+
url.searchParams.set('__theme', 'light');
|
| 158 |
+
window.location.href = url.href;
|
| 159 |
+
}
|
| 160 |
+
}
|
| 161 |
+
"""
|
| 162 |
+
def choose_model(choice):
|
| 163 |
+
if choice == "Instance Segmentation Model (U-Net)":
|
| 164 |
+
return "You have selected U-Net"
|
| 165 |
+
else:
|
| 166 |
+
return "Invalid selection"
|
| 167 |
+
# Gradio Interface
|
| 168 |
+
def gradio_interface(selected_model, uploaded_image):
|
| 169 |
+
# This will call the predict function and display the results
|
| 170 |
+
return predict(selected_model, uploaded_image)
|
| 171 |
+
|
| 172 |
+
with gr.Blocks(css=custom_css,js=js_func) as demo:
|
| 173 |
+
# State to track current page
|
| 174 |
+
page = gr.State(value="welcome")
|
| 175 |
+
|
| 176 |
+
# Welcome page container
|
| 177 |
+
with gr.Group(visible=True, elem_classes="gradio-container") as welcome_page:
|
| 178 |
+
gr.HTML(html_welcome_page) # Insert HTML structure
|
| 179 |
+
start_trial_button = gr.Button("Start Trial", variant="primary", elem_classes="trial-button")
|
| 180 |
+
|
| 181 |
+
# System description page container (initially hidden)
|
| 182 |
+
with gr.Group(visible=False) as system_page:
|
| 183 |
+
gr.HTML(html_system_page)
|
| 184 |
+
tabs = gr.Tabs()
|
| 185 |
+
with tabs:
|
| 186 |
+
with gr.TabItem("Project Description"):
|
| 187 |
+
tab_state = gr.State(value=0)
|
| 188 |
+
gr.HTML(html_project_description)
|
| 189 |
+
with gr.TabItem("Model Playground"):
|
| 190 |
+
gr.Markdown("""
|
| 191 |
+
### Model Playground:
|
| 192 |
+
This section allows users to interact with the model and test its capabilities.
|
| 193 |
+
""")
|
| 194 |
+
# Model selection radio buttons
|
| 195 |
+
radio = gr.Radio(choices=["Instance Segmentation Model (U-Net)"], label="Select Model", elem_classes="model-selection")
|
| 196 |
+
|
| 197 |
+
# Output for model selection result
|
| 198 |
+
output = gr.Textbox(label="Model Selection Result", elem_classes="model-selection-output")
|
| 199 |
+
|
| 200 |
+
# Trigger the choose_model function when a model is selected
|
| 201 |
+
radio.change(fn=choose_model, inputs=radio, outputs=output)
|
| 202 |
+
|
| 203 |
+
# Image input and upload button
|
| 204 |
+
img_input = gr.Image(type="numpy", label="Upload Image", elem_classes="image-input")
|
| 205 |
+
upload_image_button = gr.Button("Start Prediction", variant="primary", elem_classes="upload-button")
|
| 206 |
+
|
| 207 |
+
# Predicted output
|
| 208 |
+
img_output = gr.Image(label="Predicted Image", elem_classes="image-output")
|
| 209 |
+
|
| 210 |
+
# Predict and show output when image is uploaded
|
| 211 |
+
upload_image_button.click(fn=gradio_interface, inputs=[radio, img_input], outputs=img_output)
|
| 212 |
+
|
| 213 |
+
|
| 214 |
+
with gr.TabItem("Open Source API Link"):
|
| 215 |
+
gr.Markdown("""
|
| 216 |
+
### Open Source API Link:
|
| 217 |
+
This section provides access to the open-source API for the weed detection model.
|
| 218 |
+
""")
|
| 219 |
+
gr.Markdown("### API Documentation:")
|
| 220 |
+
with gr.TabItem("Contact and Review"):
|
| 221 |
+
gr.HTML(html_author_review_page)
|
| 222 |
+
back_button = gr.Button("Back", variant="secondary",elem_classes="back-button")
|
| 223 |
+
|
| 224 |
+
|
| 225 |
+
# Navigation functions
|
| 226 |
+
def go_to_system_page():
|
| 227 |
+
print("Going to system page")
|
| 228 |
+
return gr.update(visible=False), gr.update(visible=True)
|
| 229 |
+
|
| 230 |
+
def go_to_welcome_page():
|
| 231 |
+
print("Going to welcome page")
|
| 232 |
+
return gr.update(visible=True), gr.update(visible=False)
|
| 233 |
+
|
| 234 |
+
def process_image(uploaded_image):
|
| 235 |
+
# If the image is passed as a numpy array, convert it to a PIL image
|
| 236 |
+
if isinstance(uploaded_image, np.ndarray):
|
| 237 |
+
image = Image.fromarray(uploaded_image)
|
| 238 |
+
elif isinstance(uploaded_image, Image.Image):
|
| 239 |
+
image = uploaded_image
|
| 240 |
+
else:
|
| 241 |
+
raise ValueError("Uploaded image must be either a numpy array or a PIL Image.")
|
| 242 |
+
|
| 243 |
+
# Define the necessary transformations
|
| 244 |
+
transform = transforms.Compose([
|
| 245 |
+
# transforms.Resize((256, 256)), # Resize according to your model's input size
|
| 246 |
+
transforms.ToTensor(),
|
| 247 |
+
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
|
| 248 |
+
])
|
| 249 |
+
|
| 250 |
+
# Apply transformations and add batch dimension
|
| 251 |
+
image = transform(image).unsqueeze(0)
|
| 252 |
+
|
| 253 |
+
return image
|
| 254 |
+
|
| 255 |
+
|
| 256 |
+
class DoubleConv(nn.Module):
|
| 257 |
+
def __init__(self, in_channels, out_channels):
|
| 258 |
+
super().__init__()
|
| 259 |
+
self.double_conv = nn.Sequential(
|
| 260 |
+
nn.Conv2d(in_channels, out_channels, kernel_size=3, padding=1),
|
| 261 |
+
nn.BatchNorm2d(out_channels),
|
| 262 |
+
nn.ReLU(inplace=True),
|
| 263 |
+
nn.Conv2d(out_channels, out_channels, kernel_size=3, padding=1),
|
| 264 |
+
nn.BatchNorm2d(out_channels),
|
| 265 |
+
nn.ReLU(inplace=True)
|
| 266 |
+
)
|
| 267 |
+
|
| 268 |
+
def forward(self, x):
|
| 269 |
+
return self.double_conv(x)
|
| 270 |
+
|
| 271 |
+
class Down(nn.Module):
|
| 272 |
+
def __init__(self, in_channels, out_channels):
|
| 273 |
+
super().__init__()
|
| 274 |
+
self.maxpool_conv = nn.Sequential(
|
| 275 |
+
nn.MaxPool2d(2),
|
| 276 |
+
DoubleConv(in_channels, out_channels)
|
| 277 |
+
)
|
| 278 |
+
|
| 279 |
+
def forward(self, x):
|
| 280 |
+
return self.maxpool_conv(x)
|
| 281 |
+
|
| 282 |
+
class Up(nn.Module):
|
| 283 |
+
def __init__(self, in_channels, out_channels, bilinear=True):
|
| 284 |
+
super().__init__()
|
| 285 |
+
if bilinear:
|
| 286 |
+
self.up = nn.Upsample(scale_factor=2, mode='bilinear', align_corners=True)
|
| 287 |
+
else:
|
| 288 |
+
self.up = nn.ConvTranspose2d(in_channels // 2, in_channels // 2, kernel_size=2, stride=2)
|
| 289 |
+
|
| 290 |
+
self.conv = DoubleConv(in_channels, out_channels)
|
| 291 |
+
|
| 292 |
+
def forward(self, x1, x2):
|
| 293 |
+
x1 = self.up(x1)
|
| 294 |
+
# Resize x1 to match x2
|
| 295 |
+
diffY = x2.size()[2] - x1.size()[2]
|
| 296 |
+
diffX = x2.size()[3] - x1.size()[3]
|
| 297 |
+
x1 = F.pad(x1, [diffX // 2, diffX - diffX // 2,
|
| 298 |
+
diffY // 2, diffY - diffY // 2])
|
| 299 |
+
x = torch.cat([x2, x1], dim=1)
|
| 300 |
+
return self.conv(x)
|
| 301 |
+
|
| 302 |
+
class OutConv(nn.Module):
|
| 303 |
+
def __init__(self, in_channels, out_channels):
|
| 304 |
+
super().__init__()
|
| 305 |
+
self.conv = nn.Conv2d(in_channels, out_channels, kernel_size=1)
|
| 306 |
+
|
| 307 |
+
def forward(self, x):
|
| 308 |
+
return self.conv(x)
|
| 309 |
+
|
| 310 |
+
class UNet(nn.Module):
|
| 311 |
+
def __init__(self, n_channels=3, n_classes=1, bilinear=True):
|
| 312 |
+
super().__init__()
|
| 313 |
+
self.n_channels = n_channels
|
| 314 |
+
self.n_classes = n_classes
|
| 315 |
+
self.bilinear = bilinear
|
| 316 |
+
|
| 317 |
+
# Encoder
|
| 318 |
+
self.inc = DoubleConv(n_channels, 64)
|
| 319 |
+
self.down1 = Down(64, 128)
|
| 320 |
+
self.down2 = Down(128, 256)
|
| 321 |
+
self.down3 = Down(256, 512)
|
| 322 |
+
factor = 2 if bilinear else 1
|
| 323 |
+
self.down4 = Down(512, 1024 // factor)
|
| 324 |
+
|
| 325 |
+
# Decoder
|
| 326 |
+
self.up1 = Up(1024, 512 // factor, bilinear)
|
| 327 |
+
self.up2 = Up(512, 256 // factor, bilinear)
|
| 328 |
+
self.up3 = Up(256, 128 // factor, bilinear)
|
| 329 |
+
self.up4 = Up(128, 64, bilinear)
|
| 330 |
+
self.outc = OutConv(64, n_classes)
|
| 331 |
+
|
| 332 |
+
def forward(self, x):
|
| 333 |
+
x1 = self.inc(x)
|
| 334 |
+
x2 = self.down1(x1)
|
| 335 |
+
x3 = self.down2(x2)
|
| 336 |
+
x4 = self.down3(x3)
|
| 337 |
+
x5 = self.down4(x4)
|
| 338 |
+
|
| 339 |
+
x = self.up1(x5, x4)
|
| 340 |
+
x = self.up2(x, x3)
|
| 341 |
+
x = self.up3(x, x2)
|
| 342 |
+
x = self.up4(x, x1)
|
| 343 |
+
logits = self.outc(x)
|
| 344 |
+
return torch.sigmoid(logits)
|
| 345 |
+
def init_weights(self):
|
| 346 |
+
# Initialize with Kaiming initialization
|
| 347 |
+
def init_fn(m):
|
| 348 |
+
if isinstance(m, nn.Conv2d) or isinstance(m, nn.ConvTranspose2d):
|
| 349 |
+
nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu')
|
| 350 |
+
|
| 351 |
+
self.apply(init_fn)
|
| 352 |
+
|
| 353 |
+
def load_UNet_model(model_path):
|
| 354 |
+
print(f"Loading model from {model_path}")
|
| 355 |
+
model = torch.load(model_path, weights_only=False, map_location=torch.device('cpu')) # Load the model (entire model saved with torch.save)
|
| 356 |
+
model.eval() # Set the model to evaluation mode
|
| 357 |
+
return model
|
| 358 |
+
|
| 359 |
+
def predict(selected_model, uploaded_image):
|
| 360 |
+
if selected_model == "Instance Segmentation Model (U-Net)":
|
| 361 |
+
print("Predicting using U-Net")
|
| 362 |
+
model_path = "UNet_Model.pth" # Path to your trained model
|
| 363 |
+
else:
|
| 364 |
+
print("Invalid model selected")
|
| 365 |
+
return None
|
| 366 |
+
|
| 367 |
+
# Visualize predictions (call visualize_predictions)
|
| 368 |
+
return visualize_predictions(uploaded_image, model_path)
|
| 369 |
+
# Visualization function for contours and IoU
|
| 370 |
+
|
| 371 |
+
def visualize_predictions(uploaded_image, model_path="UNet.pth"):
|
| 372 |
+
model = load_UNet_model(model_path)
|
| 373 |
+
image = process_image(uploaded_image)
|
| 374 |
+
|
| 375 |
+
# Make prediction
|
| 376 |
+
with torch.no_grad():
|
| 377 |
+
output = model(image)
|
| 378 |
+
binary_pred = (output > 0.5).float().cpu().numpy() # Prediction as a binary mask
|
| 379 |
+
pred_prob = output.squeeze().cpu().numpy() # Prediction probabilities (for heatmap)
|
| 380 |
+
|
| 381 |
+
# Visualization part (assumes ground truth is available)
|
| 382 |
+
fig, axes = plt.subplots(1, 4, figsize=(16, 4))
|
| 383 |
+
|
| 384 |
+
# Original image
|
| 385 |
+
img = np.array(uploaded_image) / 255.0 # Normalize the image to [0, 1]
|
| 386 |
+
axes[0].imshow(img)
|
| 387 |
+
axes[0].set_title('Original Image')
|
| 388 |
+
axes[0].axis('off')
|
| 389 |
+
|
| 390 |
+
# Ground truth (this is just an example, you should provide the actual mask)
|
| 391 |
+
# For the sake of demonstration, we use a dummy mask
|
| 392 |
+
ground_truth = np.zeros_like(binary_pred[0, 0])
|
| 393 |
+
axes[1].imshow(ground_truth, cmap='gray')
|
| 394 |
+
axes[1].set_title('Ground Truth')
|
| 395 |
+
axes[1].axis('off')
|
| 396 |
+
|
| 397 |
+
# Prediction Probability
|
| 398 |
+
axes[2].imshow(pred_prob, cmap='jet', vmin=0, vmax=1)
|
| 399 |
+
axes[2].set_title('Prediction Probability')
|
| 400 |
+
axes[2].axis('off')
|
| 401 |
+
|
| 402 |
+
# Calculate IoU (Intersection over Union)
|
| 403 |
+
intersection = np.logical_and(binary_pred[0, 0] > 0.5, ground_truth > 0.5).sum()
|
| 404 |
+
union = np.logical_or(binary_pred[0, 0] > 0.5, ground_truth > 0.5).sum()
|
| 405 |
+
iou = intersection / union if union > 0 else 0
|
| 406 |
+
axes[3].imshow(img)
|
| 407 |
+
contours, _ = cv2.findContours(binary_pred[0, 0].astype(np.uint8), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
|
| 408 |
+
contour_img = np.zeros_like(binary_pred[0, 0])
|
| 409 |
+
cv2.drawContours(contour_img, contours, -1, 1, 2)
|
| 410 |
+
|
| 411 |
+
# Add the contour overlay with IoU text
|
| 412 |
+
axes[3].imshow(contour_img, cmap='Reds', alpha=0.5)
|
| 413 |
+
axes[3].set_title(f'Prediction Contour')
|
| 414 |
+
axes[3].axis('off')
|
| 415 |
+
|
| 416 |
+
plt.tight_layout()
|
| 417 |
+
|
| 418 |
+
# Save the figure to a BytesIO object and return it as an image
|
| 419 |
+
buf = io.BytesIO()
|
| 420 |
+
plt.savefig(buf, format='png')
|
| 421 |
+
buf.seek(0)
|
| 422 |
+
img = Image.open(buf)
|
| 423 |
+
return img
|
| 424 |
+
|
| 425 |
+
# Connect buttons to navigation functions
|
| 426 |
+
start_trial_button.click(
|
| 427 |
+
fn=go_to_system_page,
|
| 428 |
+
inputs=None, # Pass the current page state
|
| 429 |
+
outputs=[welcome_page, system_page]
|
| 430 |
+
)
|
| 431 |
+
|
| 432 |
+
back_button.click(
|
| 433 |
+
fn=go_to_welcome_page,
|
| 434 |
+
inputs=None, # Pass the current page state
|
| 435 |
+
outputs=[welcome_page, system_page]
|
| 436 |
+
)
|
| 437 |
+
upload_image_button.click(
|
| 438 |
+
fn=predict,
|
| 439 |
+
inputs=[radio, img_input],
|
| 440 |
+
outputs=img_output
|
| 441 |
+
)
|
| 442 |
+
|
| 443 |
+
|
| 444 |
+
|
| 445 |
+
demo.launch(share=True)
|