Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,10 +1,9 @@
|
|
1 |
import gradio as gr
|
2 |
import tensorflow as tf
|
3 |
from tensorflow.keras.models import load_model
|
4 |
-
from tensorflow.keras.preprocessing.image import img_to_array
|
5 |
import numpy as np
|
6 |
import os
|
7 |
-
import requests
|
8 |
|
9 |
# Load your model and tokenizer
|
10 |
labels = {
|
@@ -20,7 +19,6 @@ hierarchical_models = {}
|
|
20 |
model_path = r"inceptionv3_class.h5"
|
21 |
hierarchical_models['class'] = load_model(model_path)
|
22 |
|
23 |
-
|
24 |
def load_and_preprocess_image(image, target_size=(299, 299)):
|
25 |
img_array = img_to_array(image)
|
26 |
img_array = np.expand_dims(img_array, axis=0)
|
@@ -35,22 +33,38 @@ def predict(image):
|
|
35 |
class_preds = hierarchical_models['class'].predict(image_array)
|
36 |
class_idx = np.argmax(class_preds)
|
37 |
class_label = labels['class'][class_idx]
|
38 |
-
|
|
|
39 |
# Predict species level
|
40 |
hierarchical_models[class_label] = load_model(f"inceptionv3_{class_label}.h5")
|
41 |
species_preds = hierarchical_models[class_label].predict(image_array)
|
42 |
species_idx = np.argmax(species_preds)
|
43 |
species_label = labels[class_label][species_idx]
|
44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
|
46 |
# Create Gradio interface
|
47 |
iface = gr.Interface(
|
48 |
fn=predict,
|
49 |
inputs=gr.Image(type="pil"),
|
50 |
-
outputs=[gr.Label(label="
|
|
|
51 |
title="Image Classification",
|
52 |
-
description="Upload an image to classify it into species and class level."
|
|
|
53 |
)
|
54 |
|
55 |
# Launch the interface
|
56 |
-
iface.launch()
|
|
|
1 |
import gradio as gr
|
2 |
import tensorflow as tf
|
3 |
from tensorflow.keras.models import load_model
|
4 |
+
from tensorflow.keras.preprocessing.image import img_to_array
|
5 |
import numpy as np
|
6 |
import os
|
|
|
7 |
|
8 |
# Load your model and tokenizer
|
9 |
labels = {
|
|
|
19 |
model_path = r"inceptionv3_class.h5"
|
20 |
hierarchical_models['class'] = load_model(model_path)
|
21 |
|
|
|
22 |
def load_and_preprocess_image(image, target_size=(299, 299)):
|
23 |
img_array = img_to_array(image)
|
24 |
img_array = np.expand_dims(img_array, axis=0)
|
|
|
33 |
class_preds = hierarchical_models['class'].predict(image_array)
|
34 |
class_idx = np.argmax(class_preds)
|
35 |
class_label = labels['class'][class_idx]
|
36 |
+
class_confidence = class_preds[0][class_idx]
|
37 |
+
|
38 |
# Predict species level
|
39 |
hierarchical_models[class_label] = load_model(f"inceptionv3_{class_label}.h5")
|
40 |
species_preds = hierarchical_models[class_label].predict(image_array)
|
41 |
species_idx = np.argmax(species_preds)
|
42 |
species_label = labels[class_label][species_idx]
|
43 |
+
species_confidence = species_preds[0][species_idx]
|
44 |
+
|
45 |
+
return {
|
46 |
+
"Class": f"{class_label} ({class_confidence*100:.2f}%)",
|
47 |
+
"Species": f"{species_label} ({species_confidence*100:.2f}%)"
|
48 |
+
}
|
49 |
+
|
50 |
+
# Sample images (you can add paths to images here)
|
51 |
+
# sample_images = [
|
52 |
+
# ("Sample Amphibia", "path/to/amphibia.jpg"),
|
53 |
+
# ("Sample Aves", "path/to/aves.jpg"),
|
54 |
+
# ("Sample Mammalia", "path/to/mammalia.jpg"),
|
55 |
+
# # Add more sample images as needed
|
56 |
+
# ]
|
57 |
|
58 |
# Create Gradio interface
|
59 |
iface = gr.Interface(
|
60 |
fn=predict,
|
61 |
inputs=gr.Image(type="pil"),
|
62 |
+
outputs=[gr.Label(label="Class Prediction"), gr.Label(label="Species Prediction")],
|
63 |
+
# examples=sample_images,
|
64 |
title="Image Classification",
|
65 |
+
description="Upload an image to classify it into species and class level.",
|
66 |
+
interpretation="default"
|
67 |
)
|
68 |
|
69 |
# Launch the interface
|
70 |
+
iface.launch()
|