Spaces:
Runtime error
Runtime error
NORLIE JHON MALAGDAO
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -1,5 +1,3 @@
|
|
1 |
-
|
2 |
-
|
3 |
import gradio as gr
|
4 |
import matplotlib.pyplot as plt
|
5 |
import numpy as np
|
@@ -11,11 +9,17 @@ from tensorflow import keras
|
|
11 |
from tensorflow.keras import layers
|
12 |
from tensorflow.keras.models import Sequential
|
13 |
|
|
|
14 |
from PIL import Image
|
15 |
import gdown
|
16 |
import zipfile
|
|
|
17 |
import pathlib
|
18 |
|
|
|
|
|
|
|
|
|
19 |
# Define the Google Drive shareable link
|
20 |
gdrive_url = 'https://drive.google.com/file/d/1HjHYlQyRz5oWt8kehkt1TiOGRRlKFsv8/view?usp=drive_link'
|
21 |
|
@@ -55,33 +59,57 @@ for root, dirs, files in os.walk(extracted_path):
|
|
55 |
for f in files:
|
56 |
print(f"{subindent}{f}")
|
57 |
|
|
|
58 |
# Path to the dataset directory
|
59 |
data_dir = pathlib.Path('extracted_files/Pest_Dataset')
|
|
|
|
|
60 |
|
61 |
-
|
62 |
-
|
|
|
63 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
64 |
train_ds = tf.keras.preprocessing.image_dataset_from_directory(
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
|
73 |
val_ds = tf.keras.preprocessing.image_dataset_from_directory(
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
|
82 |
class_names = train_ds.class_names
|
83 |
print(class_names)
|
84 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
85 |
data_augmentation = keras.Sequential(
|
86 |
[
|
87 |
layers.RandomFlip("horizontal",
|
@@ -93,6 +121,7 @@ data_augmentation = keras.Sequential(
|
|
93 |
]
|
94 |
)
|
95 |
|
|
|
96 |
plt.figure(figsize=(10, 10))
|
97 |
for images, _ in train_ds.take(1):
|
98 |
for i in range(9):
|
@@ -118,10 +147,13 @@ model = Sequential([
|
|
118 |
layers.Dense(num_classes, name="outputs")
|
119 |
])
|
120 |
|
|
|
121 |
model.compile(optimizer='adam',
|
122 |
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
|
123 |
metrics=['accuracy'])
|
124 |
|
|
|
|
|
125 |
model.summary()
|
126 |
|
127 |
|
@@ -132,21 +164,39 @@ history = model.fit(
|
|
132 |
epochs=epochs
|
133 |
)
|
134 |
|
|
|
|
|
|
|
|
|
|
|
135 |
def predict_image(img):
|
136 |
img = np.array(img)
|
137 |
img_resized = tf.image.resize(img, (180, 180))
|
138 |
img_4d = tf.expand_dims(img_resized, axis=0)
|
139 |
prediction = model.predict(img_4d)[0]
|
140 |
-
|
141 |
-
|
142 |
|
143 |
image = gr.Image()
|
144 |
label = gr.Label(num_top_classes=12)
|
145 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
146 |
gr.Interface(
|
147 |
-
fn=predict_image,
|
148 |
-
inputs=image,
|
149 |
-
outputs=label,
|
150 |
-
title="Pest Classification",
|
151 |
-
description="
|
|
|
152 |
).launch(debug=True)
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
import matplotlib.pyplot as plt
|
3 |
import numpy as np
|
|
|
9 |
from tensorflow.keras import layers
|
10 |
from tensorflow.keras.models import Sequential
|
11 |
|
12 |
+
|
13 |
from PIL import Image
|
14 |
import gdown
|
15 |
import zipfile
|
16 |
+
|
17 |
import pathlib
|
18 |
|
19 |
+
|
20 |
+
|
21 |
+
|
22 |
+
|
23 |
# Define the Google Drive shareable link
|
24 |
gdrive_url = 'https://drive.google.com/file/d/1HjHYlQyRz5oWt8kehkt1TiOGRRlKFsv8/view?usp=drive_link'
|
25 |
|
|
|
59 |
for f in files:
|
60 |
print(f"{subindent}{f}")
|
61 |
|
62 |
+
import pathlib
|
63 |
# Path to the dataset directory
|
64 |
data_dir = pathlib.Path('extracted_files/Pest_Dataset')
|
65 |
+
data_dir = pathlib.Path(data_dir)
|
66 |
+
|
67 |
|
68 |
+
bees = list(data_dir.glob('bees/*'))
|
69 |
+
print(bees[0])
|
70 |
+
PIL.Image.open(str(bees[0]))
|
71 |
|
72 |
+
|
73 |
+
bees = list(data_dir.glob('bees/*'))
|
74 |
+
print(bees[0])
|
75 |
+
PIL.Image.open(str(bees[0]))
|
76 |
+
|
77 |
+
|
78 |
+
img_height,img_width=180,180
|
79 |
+
batch_size=32
|
80 |
train_ds = tf.keras.preprocessing.image_dataset_from_directory(
|
81 |
+
data_dir,
|
82 |
+
validation_split=0.2,
|
83 |
+
subset="training",
|
84 |
+
seed=123,
|
85 |
+
image_size=(img_height, img_width),
|
86 |
+
batch_size=batch_size)
|
87 |
+
|
88 |
|
89 |
val_ds = tf.keras.preprocessing.image_dataset_from_directory(
|
90 |
+
data_dir,
|
91 |
+
validation_split=0.2,
|
92 |
+
subset="validation",
|
93 |
+
seed=123,
|
94 |
+
image_size=(img_height, img_width),
|
95 |
+
batch_size=batch_size)
|
96 |
+
|
97 |
|
98 |
class_names = train_ds.class_names
|
99 |
print(class_names)
|
100 |
|
101 |
+
|
102 |
+
import matplotlib.pyplot as plt
|
103 |
+
|
104 |
+
plt.figure(figsize=(10, 10))
|
105 |
+
for images, labels in train_ds.take(1):
|
106 |
+
for i in range(9):
|
107 |
+
ax = plt.subplot(3, 3, i + 1)
|
108 |
+
plt.imshow(images[i].numpy().astype("uint8"))
|
109 |
+
plt.title(class_names[labels[i]])
|
110 |
+
plt.axis("off")
|
111 |
+
|
112 |
+
|
113 |
data_augmentation = keras.Sequential(
|
114 |
[
|
115 |
layers.RandomFlip("horizontal",
|
|
|
121 |
]
|
122 |
)
|
123 |
|
124 |
+
|
125 |
plt.figure(figsize=(10, 10))
|
126 |
for images, _ in train_ds.take(1):
|
127 |
for i in range(9):
|
|
|
147 |
layers.Dense(num_classes, name="outputs")
|
148 |
])
|
149 |
|
150 |
+
|
151 |
model.compile(optimizer='adam',
|
152 |
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
|
153 |
metrics=['accuracy'])
|
154 |
|
155 |
+
|
156 |
+
|
157 |
model.summary()
|
158 |
|
159 |
|
|
|
164 |
epochs=epochs
|
165 |
)
|
166 |
|
167 |
+
|
168 |
+
import gradio as gr
|
169 |
+
import numpy as np
|
170 |
+
import tensorflow as tf
|
171 |
+
|
172 |
def predict_image(img):
|
173 |
img = np.array(img)
|
174 |
img_resized = tf.image.resize(img, (180, 180))
|
175 |
img_4d = tf.expand_dims(img_resized, axis=0)
|
176 |
prediction = model.predict(img_4d)[0]
|
177 |
+
return {class_names[i]: float(prediction[i]) for i in range(len(class_names))}
|
178 |
+
|
179 |
|
180 |
image = gr.Image()
|
181 |
label = gr.Label(num_top_classes=12)
|
182 |
|
183 |
+
# Define custom CSS for background image
|
184 |
+
custom_css = """
|
185 |
+
body {
|
186 |
+
background-image: url('\extracted_files\Pest_Dataset\bees\bees (444).jpg');
|
187 |
+
background-size: cover;
|
188 |
+
background-repeat: no-repeat;
|
189 |
+
background-attachment: fixed;
|
190 |
+
color: white;
|
191 |
+
}
|
192 |
+
"""
|
193 |
+
|
194 |
gr.Interface(
|
195 |
+
fn=predict_image,
|
196 |
+
inputs=image,
|
197 |
+
outputs=label,
|
198 |
+
title="Welcome to Agricultural Pest Image Classification",
|
199 |
+
description="The image data set used was obtaied from Kaggle and has a collection of 12 different types of agricultral pests: Ants, Bees, Beetles, Caterpillars, Earthworms, Earwigs, Grasshoppers, Moths, Slugs, Snails, Wasps, and Weevils",
|
200 |
+
css=custom_css
|
201 |
).launch(debug=True)
|
202 |
+
|