Update app.py
Browse files
app.py
CHANGED
|
@@ -1,60 +1,64 @@
|
|
| 1 |
# install (if needed)
|
| 2 |
-
#
|
| 3 |
|
| 4 |
import tensorflow as tf
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
import gradio as gr
|
| 6 |
from PIL import Image
|
| 7 |
import numpy as np
|
|
|
|
| 8 |
|
| 9 |
# Load your model (handle any custom layers if needed)
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
|
| 19 |
# Replace with your classes
|
| 20 |
class_names = ["arrowheads", "axehammerstones", "scrapers"]
|
| 21 |
|
| 22 |
-
IMG_SIZE = (224, 224) # change this to your model's expected size
|
| 23 |
-
|
| 24 |
from tensorflow.keras.applications.efficientnet import preprocess_input
|
| 25 |
|
| 26 |
|
| 27 |
def predict(img):
|
| 28 |
-
"""Predict function for Gradio
|
| 29 |
|
| 30 |
img = img.convert("RGB").resize(IMG_SIZE)
|
| 31 |
x = np.array(img)
|
| 32 |
x = preprocess_input(x)
|
| 33 |
x = np.expand_dims(x, 0)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
-
preds = model.predict(x)[0] # probabilities
|
| 36 |
|
| 37 |
-
# ✅ find highest prediction
|
| 38 |
-
max_idx = int(np.argmax(preds))
|
| 39 |
|
| 40 |
-
# ✅ zero out all other classes
|
| 41 |
-
output = {}
|
| 42 |
-
for i in range(len(preds)):
|
| 43 |
-
if i == max_idx:
|
| 44 |
-
output[class_names[i]] = float(preds[i])
|
| 45 |
-
else:
|
| 46 |
-
output[class_names[i]] = 0.0
|
| 47 |
|
| 48 |
-
return output
|
| 49 |
|
| 50 |
|
| 51 |
# Gradio interface
|
| 52 |
iface = gr.Interface(
|
| 53 |
-
fn=predict,
|
| 54 |
inputs=gr.Image(type="pil"),
|
| 55 |
outputs=gr.Label(num_top_classes=len(class_names)),
|
| 56 |
title="Stone Artifact Classifier",
|
| 57 |
-
description="Upload an image of an artifact and the model will classify it."
|
| 58 |
)
|
| 59 |
|
| 60 |
-
iface.launch()
|
|
|
|
| 1 |
# install (if needed)
|
| 2 |
+
#pip install tensorflow
|
| 3 |
|
| 4 |
import tensorflow as tf
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
# load SavedModel dir or .h5
|
| 8 |
+
#model = tf.keras.models.load_model("stone_artifact_classifier.h5") # use compile=False if custom objects
|
| 9 |
+
# or: model = tf.keras.models.load_model("my_model.h5", compile=False)
|
| 10 |
+
|
| 11 |
import gradio as gr
|
| 12 |
from PIL import Image
|
| 13 |
import numpy as np
|
| 14 |
+
import tensorflow as tf
|
| 15 |
|
| 16 |
# Load your model (handle any custom layers if needed)
|
| 17 |
+
# If you have custom layers like RandomHeight, include them in custom_objects:
|
| 18 |
+
model = tf.keras.models.load_model("stone_artifact_classifier.h5", compile=False, custom_objects={"RandomHeight": tf.keras.layers.RandomHeight, "RandomWidth": tf.keras.layers.RandomWidth})
|
| 19 |
+
#model = tf.keras.models.load_model("stone_artifact_classifier.h5", compile=False)
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
|
| 25 |
|
| 26 |
# Replace with your classes
|
| 27 |
class_names = ["arrowheads", "axehammerstones", "scrapers"]
|
| 28 |
|
|
|
|
|
|
|
| 29 |
from tensorflow.keras.applications.efficientnet import preprocess_input
|
| 30 |
|
| 31 |
|
| 32 |
def predict(img):
|
| 33 |
+
"""Predict function for Gradio"""
|
| 34 |
|
| 35 |
img = img.convert("RGB").resize(IMG_SIZE)
|
| 36 |
x = np.array(img)
|
| 37 |
x = preprocess_input(x)
|
| 38 |
x = np.expand_dims(x, 0)
|
| 39 |
+
preds = model.predict(x)[0]
|
| 40 |
+
# top = np.argmax(preds)
|
| 41 |
+
return {class_names[i]: float(preds[i]) for i in range(len(preds))}
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
|
| 50 |
|
|
|
|
| 51 |
|
|
|
|
|
|
|
| 52 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
|
|
|
| 54 |
|
| 55 |
|
| 56 |
# Gradio interface
|
| 57 |
iface = gr.Interface(
|
|
|
|
| 58 |
inputs=gr.Image(type="pil"),
|
| 59 |
outputs=gr.Label(num_top_classes=len(class_names)),
|
| 60 |
title="Stone Artifact Classifier",
|
| 61 |
+
description="Upload an image of an artifact and the model will classify it."
|
| 62 |
)
|
| 63 |
|
| 64 |
+
iface.launch()
|