Spaces:
Runtime error
Runtime error
Upload 4 files
Browse files- app.py +65 -0
- config.json +9 -0
- model.weights.h5 +3 -0
- requirements.txt +5 -0
app.py
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import tensorflow as tf
|
| 2 |
+
from tensorflow.keras import layers, models, regularizers
|
| 3 |
+
import json
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import numpy as np
|
| 6 |
+
import os
|
| 7 |
+
import gradio as gr
|
| 8 |
+
|
| 9 |
+
# ---- Load config if present ----
|
| 10 |
+
cfg = None
|
| 11 |
+
if os.path.exists("config.json"):
|
| 12 |
+
with open("config.json") as f:
|
| 13 |
+
cfg = json.load(f)
|
| 14 |
+
|
| 15 |
+
# ---- Class labels ----
|
| 16 |
+
class_names = ['battery', 'glass', 'metal', 'organic', 'paper', 'plastic']
|
| 17 |
+
|
| 18 |
+
# ---- Build model architecture ----
|
| 19 |
+
base_model = tf.keras.applications.EfficientNetB7(weights=None)
|
| 20 |
+
model = models.Sequential([
|
| 21 |
+
base_model,
|
| 22 |
+
layers.GlobalAveragePooling2D(),
|
| 23 |
+
layers.Dense(256, activation='relu', kernel_regularizer=regularizers.l2(0.001)),
|
| 24 |
+
layers.Dropout(0.3),
|
| 25 |
+
layers.Dense(128, activation='relu', kernel_regularizer=regularizers.l2(0.001)),
|
| 26 |
+
layers.Dropout(0.3),
|
| 27 |
+
layers.Dense(6, activation='softmax')
|
| 28 |
+
])
|
| 29 |
+
|
| 30 |
+
# ---- Load trained weights ----
|
| 31 |
+
model.load_weights("model.weights.h5")
|
| 32 |
+
|
| 33 |
+
# ---- Preprocess image ----
|
| 34 |
+
def preprocess(image: Image.Image):
|
| 35 |
+
image = image.resize((224, 224))
|
| 36 |
+
image = np.array(image) / 255.0
|
| 37 |
+
image = np.expand_dims(image, axis=0)
|
| 38 |
+
return image
|
| 39 |
+
|
| 40 |
+
# ---- Prediction function ----
|
| 41 |
+
def predict(image: Image.Image):
|
| 42 |
+
x = preprocess(image)
|
| 43 |
+
preds = model.predict(x)
|
| 44 |
+
class_idx = int(np.argmax(preds, axis=1)[0])
|
| 45 |
+
confidence = float(np.max(preds))
|
| 46 |
+
return {
|
| 47 |
+
"class": class_names[class_idx],
|
| 48 |
+
"confidence": confidence
|
| 49 |
+
}
|
| 50 |
+
|
| 51 |
+
# ---- Gradio interface ----
|
| 52 |
+
interface = gr.Interface(
|
| 53 |
+
fn=predict,
|
| 54 |
+
inputs=gr.Image(type="pil", label="Upload an image"),
|
| 55 |
+
outputs=[
|
| 56 |
+
gr.Label(num_top_classes=1, label="Predicted class"),
|
| 57 |
+
gr.Number(label="Confidence")
|
| 58 |
+
],
|
| 59 |
+
title="NeuRecAI - Material Classifier",
|
| 60 |
+
description="Upload an image to classify it as battery, glass, metal, organic, paper, or plastic."
|
| 61 |
+
)
|
| 62 |
+
|
| 63 |
+
# ---- Launch ----
|
| 64 |
+
if __name__ == "__main__":
|
| 65 |
+
interface.launch()
|
config.json
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"model_name": "NeuRecAI",
|
| 3 |
+
"framework": "tensorflow",
|
| 4 |
+
"architecture": "EfficientNetB7",
|
| 5 |
+
"num_classes": 6,
|
| 6 |
+
"class_names": ["battery", "glass", "metal", "organic", "paper", "plastic"],
|
| 7 |
+
"input_size": [224, 224],
|
| 8 |
+
"description": "A Keras-based EfficientNetB7 model for classifying recyclable materials such as battery, glass, metal, organic, paper, and plastic."
|
| 9 |
+
}
|
model.weights.h5
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:108bdbb008342b2921a89899327002a66d4e281b1cadab9b5e29fa92953b84b8
|
| 3 |
+
size 266726412
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
tensorflow
|
| 2 |
+
gradio
|
| 3 |
+
pillow
|
| 4 |
+
numpy
|
| 5 |
+
efficientnet
|