-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython5 script
More file actions
180 lines (145 loc) · 6.16 KB
/
Copy pathpython5 script
File metadata and controls
180 lines (145 loc) · 6.16 KB
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
import os
import cv2
import numpy as np
import math
from datetime import datetime
import tkinter as tk
from tkinter import ttk, scrolledtext
from PIL import Image, ImageTk
# ----------------------------
# Infection detection
# ----------------------------
def analyze_infection(frame):
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
lower_infected = np.array([10, 50, 50])
upper_infected = np.array([30, 255, 255])
mask_infected = cv2.inRange(hsv, lower_infected, upper_infected)
infected_pixels = np.sum(mask_infected > 0)
total_pixels = frame.shape[0] * frame.shape[1]
x = infected_pixels / total_pixels if total_pixels > 0 else 0
return x, mask_infected
# ----------------------------
# Van der Plank infection rate
# ----------------------------
def compute_vanderplank_rate(x1, t1, x2, t2):
if not (0 < x1 < 1 and 0 < x2 < 1):
return None
dt = (t2 - t1).total_seconds() / 86400.0
if dt <= 0:
return None
numerator = x2 * (1 - x1)
denominator = x1 * (1 - x2)
if denominator <= 0 or numerator <= 0:
return None
r = (1.0 / dt) * math.log(numerator / denominator)
return r
# ----------------------------
# Decision logic (based on infection rate only)
# ----------------------------
def should_spray(x_current, r, battery=100, rain_prob=0.0, wind_speed=2.0):
BATTERY_THRESHOLD = 30
MAX_WIND = 5.0
MAX_RAIN_PROB = 0.4
if battery < BATTERY_THRESHOLD:
return False, "⚡ Battery too low", "orange"
if rain_prob > MAX_RAIN_PROB or wind_speed > MAX_WIND:
return False, "🌧️ Unsafe weather", "orange"
if r is None:
return False, "❓ Unable to compute infection rate", "gray"
if r > 0.05:
return True, "🚨 Spray at FULL velocity", "red"
elif r > 0.01:
return True, "⚠️ Spray at MODERATE velocity", "orange"
else:
return False, "✅ No spray needed", "green"
# ----------------------------
# Tkinter GUI
# ----------------------------
class SprayApp:
def __init__(self, root, folder_path):
self.root = root
self.folder_path = folder_path
self.images = [f for f in os.listdir(folder_path) if f.lower().endswith((".jpg", ".png","pjpeg"))]
print("Dataset folder:", folder_path)
print("Images found:", self.images)
self.index = 0
self.root.title("🌿 Smart Spray Monitor")
self.root.geometry("1000x600")
self.root.configure(bg="#f0f4f7")
# Header
header_frame = tk.Frame(root, bg="#2c3e50")
header_frame.pack(fill="x")
# Load logo
logo_img = Image.open(os.path.expanduser("~/Downloads/lol.png")) # use correct path
logo_img = logo_img.resize((50, 50), Image.Resampling.LANCZOS)
logo_tk = ImageTk.PhotoImage(logo_img)
logo_label = tk.Label(header_frame, image=logo_tk, bg="#2c3e50")
logo_label.image = logo_tk # keep a reference
logo_label.pack(side="left", padx=10, pady=5)
header_text = tk.Label(header_frame, text="PESTIFY",
font=("Segoe UI", 20, "bold"),
bg="#2c3e50", fg="white", pady=10)
header_text.pack(side="left")
# Main Frame (split layout)
main_frame = tk.Frame(root, bg="#f0f4f7")
main_frame.pack(fill="both", expand=True, padx=10, pady=10)
# Left side (image)
self.image_label = tk.Label(main_frame, bg="white", relief="groove", bd=2)
self.image_label.pack(side="left", fill="both", expand=True, padx=10, pady=10)
# Right side (console + status)
right_frame = tk.Frame(main_frame, bg="#f0f4f7")
right_frame.pack(side="right", fill="y")
self.log_area = scrolledtext.ScrolledText(right_frame, width=40, height=25,
bg="#1e272e", fg="#d2dae2",
font=("Consolas", 10))
self.log_area.pack(pady=10)
self.status_label = tk.Label(right_frame, text="Status: Waiting...",
font=("Segoe UI", 14, "bold"),
bg="#f0f4f7", fg="black")
self.status_label.pack(pady=10)
# Control buttons
btn_frame = tk.Frame(right_frame, bg="#f0f4f7")
btn_frame.pack(pady=10)
ttk.Button(btn_frame, text="⬅ Prev", command=self.prev_image).grid(row=0, column=0, padx=5)
ttk.Button(btn_frame, text="Next ➡", command=self.next_image).grid(row=0, column=1, padx=5)
# Show first image
if self.images:
self.show_image()
def show_image(self):
img_path = os.path.join(self.folder_path, self.images[self.index])
frame = cv2.imread(img_path)
if frame is None:
return
# Analysis
x_now, mask = analyze_infection(frame)
x_prev = 0.05
t1 = datetime(2025, 9, 1)
t2 = datetime.now()
r = compute_vanderplank_rate(x_prev, t1, x_now, t2)
spray, reason, color = should_spray(x_now, r, battery=85, rain_prob=0.2, wind_speed=3.0)
# Resize + convert to display
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
img_pil = Image.fromarray(frame_rgb)
img_pil.thumbnail((600, 500))
img_tk = ImageTk.PhotoImage(img_pil)
self.image_label.config(image=img_tk)
self.image_label.image = img_tk
# Update log
self.log_area.insert(tk.END, f"\nAnalyzing: {self.images[self.index]}\n")
self.log_area.insert(tk.END, f"Infection proportion x = {x_now:.3f}\n")
self.log_area.insert(tk.END, f"Infection rate r = {r}\n")
self.log_area.insert(tk.END, f"Decision: {reason}\n")
self.log_area.see(tk.END)
# Status label
self.status_label.config(text=f"Status: {reason}", fg=color)
def next_image(self):
self.index = (self.index + 1) % len(self.images)
self.show_image()
def prev_image(self):
self.index = (self.index - 1) % len(self.images)
self.show_image()
if __name__ == "__main__":
dataset_folder = os.path.expanduser("~/PlantDataset")
root = tk.Tk()
app = SprayApp(root, dataset_folder)
root.mainloop()