Madhuritha commited on
Commit
f8835e9
·
1 Parent(s): bc16603

Add Gradio app

Browse files
Files changed (1) hide show
  1. app.py +42 -0
app.py ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ import gradio as gr
3
+ import joblib
4
+
5
+ from tensorflow.keras.models import load_model
6
+ import numpy as np
7
+ from PIL import Image, ImageDraw
8
+
9
+ # Load your saved CNN or ANN model
10
+ model = load_model('CNN_model.h5', compile=False)
11
+ model.compile(optimizer='adam', loss='mean_squared_error')
12
+
13
+ def predict_and_draw_box(img):
14
+ try:
15
+ # Convert to grayscale & resize
16
+ img_gray = img.convert('L').resize((128, 128))
17
+ img_array = np.array(img_gray).astype("float32") / 255.0
18
+ img_array = img_array.reshape(1, 128, 128, 1)
19
+
20
+ # Predict bounding box
21
+ pred = model.predict(img_array)[0]
22
+ x, y, w, h = np.clip(pred, 0, 128)
23
+
24
+ # Draw box on the image
25
+ draw_img = img_gray.convert("RGB")
26
+ draw = ImageDraw.Draw(draw_img)
27
+ draw.rectangle([x, y, x + w, y + h], outline="red", width=3)
28
+
29
+ return draw_img
30
+
31
+ except Exception as e:
32
+ return f"Error: {str(e)}"
33
+
34
+ import gradio as gr
35
+
36
+ gr.Interface(
37
+ fn=predict_and_draw_box,
38
+ inputs=gr.Image(type="pil", label="Upload Image"),
39
+ outputs=gr.Image(type="pil", label="Predicted Bounding Box"),
40
+ title="Object Localization (CNN Model)",
41
+ description="Upload a grayscale image (128x128 or any size). The model will predict and draw a red bounding box."
42
+ ).launch(share=True)