Spaces:
Build error
Build error
Commit
•
32f60c0
1
Parent(s):
5c43264
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from keras.models import load_model
|
3 |
+
from keras.preprocessing.image import ImageDataGenerator
|
4 |
+
import numpy as np
|
5 |
+
from tensorflow.keras.utils import img_to_array
|
6 |
+
from tensorflow.keras.applications.resnet50 import preprocess_input
|
7 |
+
from PIL import Image
|
8 |
+
|
9 |
+
# model path
|
10 |
+
cnn_model = load_model('./model/cnn_model.h5')
|
11 |
+
resnet_model = load_model('./model/resnet_model.h5')
|
12 |
+
|
13 |
+
import json
|
14 |
+
|
15 |
+
with open('data/class_dict.json', 'r') as json_file:
|
16 |
+
class_dict = json.load(json_file)
|
17 |
+
|
18 |
+
# get class names
|
19 |
+
class_names = [class_dict[i] for i in sorted(class_dict.keys())]
|
20 |
+
|
21 |
+
|
22 |
+
def classify_insect(model_name, img):
|
23 |
+
img = img.resize((150, 150))
|
24 |
+
img_array = img_to_array(img)
|
25 |
+
img_array = np.expand_dims(img_array, axis=0)
|
26 |
+
img_preprocessed = preprocess_input(img_array)
|
27 |
+
|
28 |
+
# Select the model based on the dropdown choice
|
29 |
+
if model_name == "CNN Model":
|
30 |
+
model = cnn_model
|
31 |
+
elif model_name == "Transfer Learning ResNet":
|
32 |
+
model = resnet_model
|
33 |
+
|
34 |
+
# Make a prediction
|
35 |
+
prediction = model.predict(img_preprocessed)
|
36 |
+
return {class_name: float(score) for class_name, score in zip(class_names, prediction[0])}
|
37 |
+
|
38 |
+
iface = gr.Interface(
|
39 |
+
fn=classify_insect,
|
40 |
+
inputs=[
|
41 |
+
gr.Dropdown(choices=["CNN Model", "Transfer Learning ResNet"], label="Select Model"),
|
42 |
+
gr.Image(shape=(150,150))
|
43 |
+
],
|
44 |
+
outputs=gr.Label(num_top_classes=3)
|
45 |
+
)
|
46 |
+
|
47 |
+
iface.launch(share=True)
|