Spaces:
Sleeping
Sleeping
Commit
·
d22c3e6
1
Parent(s):
48b3c78
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from tensorflow.keras.preprocessing import image
|
3 |
+
from tensorflow.keras.models import load_model
|
4 |
+
import numpy as np
|
5 |
+
|
6 |
+
# Load the model with weights
|
7 |
+
model_path = "https://huggingface.co/spaces/shamimjony1000/shamimjony/blob/main/my_model.h5" # Replace with the correct path to your model
|
8 |
+
model = load_model(model_path)
|
9 |
+
|
10 |
+
# Define the image height and width
|
11 |
+
image_height = 224
|
12 |
+
image_width = 224 # Adjust to match the input size of your model
|
13 |
+
|
14 |
+
# Map the class indices to class names based on your dataset
|
15 |
+
class_names = {
|
16 |
+
0: "Class 0",
|
17 |
+
1: "Class 1",
|
18 |
+
2: "Class 2",
|
19 |
+
3: "Class 3",
|
20 |
+
4: "Class 4",
|
21 |
+
}
|
22 |
+
|
23 |
+
# Define the prediction function
|
24 |
+
def predict_image(img_array):
|
25 |
+
# Preprocess the input image
|
26 |
+
img_array = img_array.reshape((1, image_height, image_width, 3)) # Reshape to (1, 224, 224, 3)
|
27 |
+
#img_array = img_array.astype(np.float32) / 255.0 # Normalize pixel values
|
28 |
+
|
29 |
+
# Perform additional preprocessing if needed, e.g., center-cropping
|
30 |
+
|
31 |
+
# Make predictions using the loaded model
|
32 |
+
predictions = model.predict(img_array)
|
33 |
+
|
34 |
+
# Get the predicted class label
|
35 |
+
predicted_class_index = np.argmax(predictions)
|
36 |
+
predicted_class = class_names[predicted_class_index]
|
37 |
+
|
38 |
+
# Return the predicted class name for Gradio to display in the output
|
39 |
+
return predicted_class
|
40 |
+
|
41 |
+
# Create Gradio Interface
|
42 |
+
iface = gr.Interface(
|
43 |
+
fn=predict_image,
|
44 |
+
inputs="image", # Gradio will automatically create an image uploader
|
45 |
+
outputs="text", # Display the predicted class name as text
|
46 |
+
)
|
47 |
+
|
48 |
+
# Launch the Gradio Interface
|
49 |
+
iface.launch()
|