sdqp commited on
Commit
4537938
·
verified ·
1 Parent(s): 1a2af9c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -51
app.py CHANGED
@@ -1,51 +1,59 @@
1
- import gradio as gr
2
- import pathlib
3
- import random
4
- import tensorflow as tf
5
- from PIL import Image
6
- from timeit import default_timer as timer
7
- from keras.models import load_model
8
-
9
- # Load the model
10
- model = load_model('MyResNet101Model_final.keras')
11
-
12
- # Define label mappings
13
- label2id = {'Hispa': 0, 'Bercak Coklat': 1, 'Blast Daun': 2, 'Sehat': 3}
14
- class_names = list(label2id.keys())
15
-
16
- def predict(img):
17
- start = timer()
18
-
19
- img = img.resize((224, 224))
20
- img_aug = tf.keras.layers.Rescaling(1., input_shape=(224, 224, 3))
21
- output = tf.expand_dims(img_aug(img), 0)
22
- pred_prob = tf.nn.softmax(model.predict(output))
23
- pred_dict = {class_names[i]:float(tf.squeeze(pred_prob)[i]) for i in range(len(class_names))}
24
-
25
- pred_time = round(timer() - start, 5)
26
-
27
- return pred_dict, pred_time
28
-
29
- # Get example images
30
- example_paths = list(pathlib.Path('examples').glob("*/*.jpg"))
31
- example_list = [[str(filepath)] for filepath in random.sample(example_paths, k=4)]
32
-
33
- # Set up the Gradio interface
34
- title = 'Klasifikasi Penyakit Daun Padi'
35
- description = 'Upload gambar daun padi untuk mengklasifikasikan penyakitnya.'
36
-
37
- demo = gr.Interface(
38
- fn=predict,
39
- inputs=gr.Image(type='pil'),
40
- outputs=[
41
- gr.Label(num_top_classes=4, label='Prediksi'),
42
- gr.Number(label="Waktu prediksi (detik)")
43
- ],
44
- description=description,
45
- title=title,
46
- allow_flagging='never',
47
- examples=example_list
48
- )
49
-
50
- if __name__ == "__main__":
51
- demo.launch(debug=True)
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import pathlib
3
+ import random
4
+ import tensorflow as tf
5
+ from PIL import Image
6
+ from timeit import default_timer as timer
7
+ from keras.models import load_model
8
+ from tensorflow.keras.preprocessing.image import ImageDataGenerator
9
+
10
+ # Load the model
11
+ model = load_model('MyResNet101Model_final.keras')
12
+
13
+ # Define label mappings based on your dataset
14
+ label2id = {'Hispa': 0, 'Bercak Coklat': 1, 'Blast Daun': 2, 'Sehat': 3}
15
+ class_names = list(label2id.keys())
16
+
17
+ # Function to predict an image
18
+ def predict(img):
19
+ start = timer()
20
+
21
+ img = img.resize((224, 224)) # Ensure size matches the model's expected input
22
+ img_array = tf.keras.preprocessing.image.img_to_array(img)
23
+ img_array = tf.expand_dims(img_array, 0) # Create batch dimension
24
+
25
+ # Normalize image
26
+ img_array /= 255.0
27
+
28
+ # Prediction
29
+ predictions = model.predict(img_array)
30
+ pred_prob = tf.nn.softmax(predictions[0])
31
+ pred_dict = {class_names[i]: float(pred_prob[i]) for i in range(len(class_names))}
32
+
33
+ pred_time = round(timer() - start, 5)
34
+
35
+ return pred_dict, pred_time
36
+
37
+ # Example images for demonstration
38
+ example_paths = list(pathlib.Path('examples').glob("*/*.jpg"))
39
+ example_list = [[str(filepath)] for filepath in random.sample(example_paths, k=4)]
40
+
41
+ # Set up the Gradio interface
42
+ title = 'Klasifikasi Penyakit Daun Padi'
43
+ description = 'Upload gambar daun padi untuk mengklasifikasikan penyakitnya.'
44
+
45
+ demo = gr.Interface(
46
+ fn=predict,
47
+ inputs=gr.Image(type='pil'),
48
+ outputs=[
49
+ gr.Label(num_top_classes=4, label='Prediksi'),
50
+ gr.Number(label="Waktu prediksi (detik)")
51
+ ],
52
+ description=description,
53
+ title=title,
54
+ allow_flagging='never',
55
+ examples=example_list
56
+ )
57
+
58
+ if __name__ == "__main__":
59
+ demo.launch(debug=True)