Spaces:
Sleeping
Sleeping
Upload 3 files
Browse files- app.py +40 -0
- best_model.h5 +3 -0
- requirements.txt +4 -0
app.py
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import numpy as np
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import tensorflow as tf
|
| 5 |
+
from tensorflow.keras.applications.resnet import preprocess_input
|
| 6 |
+
from tensorflow.keras.models import load_model
|
| 7 |
+
|
| 8 |
+
# Load the model
|
| 9 |
+
model = load_model("best_model.h5")
|
| 10 |
+
|
| 11 |
+
# Class names
|
| 12 |
+
class_names = ['Cloudy', 'Rain', 'Shine', 'Sunrise']
|
| 13 |
+
|
| 14 |
+
# Preprocessing function
|
| 15 |
+
def preprocess_image(img):
|
| 16 |
+
img = img.resize((224, 224))
|
| 17 |
+
img_array = np.array(img)
|
| 18 |
+
img_array = preprocess_input(img_array)
|
| 19 |
+
img_array = np.expand_dims(img_array, axis=0)
|
| 20 |
+
return img_array
|
| 21 |
+
|
| 22 |
+
# Prediction function
|
| 23 |
+
def classify_image(image):
|
| 24 |
+
processed_img = preprocess_image(image)
|
| 25 |
+
preds = model.predict(processed_img)[0]
|
| 26 |
+
predicted_class = class_names[np.argmax(preds)]
|
| 27 |
+
confidence = float(np.max(preds))
|
| 28 |
+
return {predicted_class: confidence}
|
| 29 |
+
|
| 30 |
+
# Gradio Interface
|
| 31 |
+
interface = gr.Interface(
|
| 32 |
+
fn=classify_image,
|
| 33 |
+
inputs=gr.Image(type="pil"),
|
| 34 |
+
outputs=gr.Label(num_top_classes=4),
|
| 35 |
+
title="Weather Image Classifier",
|
| 36 |
+
description="Upload an image of the weather and get the predicted category (Cloudy, Rain, Shine, Sunrise)"
|
| 37 |
+
)
|
| 38 |
+
|
| 39 |
+
if __name__ == "__main__":
|
| 40 |
+
interface.launch()
|
best_model.h5
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:1606bc87695d0909aac9d6c4c051a6a0879626f4c650e398378fa25846f29a03
|
| 3 |
+
size 223971152
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
tensorflow
|
| 3 |
+
numpy
|
| 4 |
+
pillow
|