Spaces:
Sleeping
Sleeping
app.py
Browse files- app.py +45 -0
- model1.h5 +3 -0
- requirements.txt +2 -0
app.py
ADDED
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from tensorflow.keras.models import load_model
|
3 |
+
from PIL import Image
|
4 |
+
import numpy as np
|
5 |
+
import matplotlib.pyplot as plt
|
6 |
+
from io import BytesIO
|
7 |
+
|
8 |
+
# Load the trained model
|
9 |
+
model = load_model('model1.h5') # Make sure 'model1.h5' is the correct path to your model
|
10 |
+
|
11 |
+
# Prediction function for the Gradio app
|
12 |
+
def predict_and_visualize(img):
|
13 |
+
# Store the original image size
|
14 |
+
original_size = img.size
|
15 |
+
|
16 |
+
# Convert the input image to the target size expected by the model
|
17 |
+
img_resized = img.resize((256, 256))
|
18 |
+
img_array = np.array(img_resized) / 255.0 # Normalize the image
|
19 |
+
img_array = np.expand_dims(img_array, axis=0) # Add batch dimension
|
20 |
+
|
21 |
+
# Make a prediction
|
22 |
+
prediction = model.predict(img_array)
|
23 |
+
|
24 |
+
# Assuming the model outputs a single-channel image, normalize to 0-255 range for display
|
25 |
+
predicted_mask = (prediction[0, :, :, 0] * 255).astype(np.uint8)
|
26 |
+
|
27 |
+
# Convert the prediction to a PIL image
|
28 |
+
prediction_image = Image.fromarray(predicted_mask, mode='L') # 'L' mode is for grayscale
|
29 |
+
|
30 |
+
# Resize the predicted image back to the original image size
|
31 |
+
prediction_image = prediction_image.resize(original_size, Image.NEAREST)
|
32 |
+
|
33 |
+
return prediction_image
|
34 |
+
|
35 |
+
# Create the Gradio interface
|
36 |
+
iface = gr.Interface(
|
37 |
+
fn=predict_and_visualize,
|
38 |
+
inputs=gr.Image(type="pil"), # We expect a PIL Image
|
39 |
+
outputs=gr.Image(type="pil"), # We will return a PIL Image
|
40 |
+
title="MilitarEye: Military Camouflage Detection",
|
41 |
+
description="Upload an image to see the model's predicted probable camouflage mask position."
|
42 |
+
)
|
43 |
+
|
44 |
+
# Launch the Gradio app
|
45 |
+
iface.launch()
|
model1.h5
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:77918eb1f7dabb2e27182ee2c02f00f097756308dcbdf3514b4ff3c35302aeba
|
3 |
+
size 84387568
|
requirements.txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
tensorflow
|
2 |
+
gradio
|