jithin14 commited on
Commit
e76a638
·
verified ·
1 Parent(s): 83be1dd

Upload 7 files

Browse files
Files changed (7) hide show
  1. app.py +107 -0
  2. image4996.jpg +0 -0
  3. image4997.jpg +0 -0
  4. image4998.jpg +0 -0
  5. image4999.jpg +0 -0
  6. model.py +108 -0
  7. requirements.txt +8 -0
app.py ADDED
@@ -0,0 +1,107 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import numpy as np
3
+ from PIL import Image
4
+ import matplotlib.pyplot as plt
5
+ from model import BoundingBoxPredictor
6
+ import matplotlib.patches as patches
7
+ import os
8
+ import uuid
9
+
10
+ predictor = BoundingBoxPredictor()
11
+
12
+ current_dir = os.path.dirname(os.path.abspath(__file__))
13
+ cnn_path = os.path.join(current_dir, 'convolutional_nn.h5')
14
+ knn_path = os.path.join(current_dir, 'knn_model_tuned.pkl')
15
+
16
+ sample_images = [
17
+ os.path.join(current_dir, f"image{i}.jpg")
18
+ for i in range(4996, 5000)
19
+ ]
20
+
21
+ try:
22
+ predictor.load_models(cnn_path, knn_path)
23
+ print("Models loaded successfully!")
24
+ except Exception as e:
25
+ print(f"Error loading models: {str(e)}")
26
+ raise
27
+
28
+ def predict_and_draw_bbox(input_image, model_choice):
29
+ if input_image is None:
30
+ return None, "Please upload an image."
31
+
32
+ try:
33
+ bbox = predictor.predict(input_image, model_type=model_choice.lower())
34
+
35
+ img_array = np.array(input_image)
36
+
37
+ fig, ax = plt.subplots()
38
+ ax.imshow(img_array, cmap='gray')
39
+
40
+ rect = patches.Rectangle(
41
+ (bbox['x'], bbox['y']),
42
+ bbox['width'],
43
+ bbox['height'],
44
+ linewidth=2,
45
+ edgecolor='r',
46
+ facecolor='none'
47
+ )
48
+ ax.add_patch(rect)
49
+
50
+ plt.axis('off')
51
+
52
+ output_filename = f'output_{uuid.uuid4().hex[:8]}.png'
53
+ output_path = os.path.join(current_dir, output_filename)
54
+
55
+ plt.savefig(output_path, bbox_inches='tight', pad_inches=0)
56
+ plt.close()
57
+
58
+ return (
59
+ output_path,
60
+ f"Model Used: {model_choice}\n\nBounding Box Coordinates:\nX: {bbox['x']:.2f}\nY: {bbox['y']:.2f}\nWidth: {bbox['width']:.2f}\nHeight: {bbox['height']:.2f}"
61
+ )
62
+ except Exception as e:
63
+ return None, f"Error processing image: {str(e)}"
64
+
65
+ def select_sample(evt: gr.SelectData):
66
+ selected_image = Image.open(sample_images[evt.index])
67
+ return selected_image
68
+
69
+ with gr.Blocks() as iface:
70
+ gr.Markdown("# Bounding Box Detector")
71
+ gr.Markdown("""Upload an image to detect the bounding box. Choose between:
72
+ - CNN Model (Best performance, IoU: 0.65)
73
+ - KNN Model (Faster, IoU: 0.47)
74
+
75
+ The models work best with grayscale images of size 128x128.""")
76
+
77
+ with gr.Row():
78
+ with gr.Column():
79
+ input_image = gr.Image(type="pil", label="Input Image")
80
+ model_choice = gr.Radio(
81
+ choices=["CNN", "KNN"],
82
+ label="Choose Model",
83
+ value="CNN"
84
+ )
85
+ submit_btn = gr.Button("Detect Bounding Box")
86
+
87
+ with gr.Column():
88
+ output_image = gr.Image(type="filepath", label="Detected Bounding Box")
89
+ output_text = gr.Textbox(label="Coordinates")
90
+
91
+ gr.Markdown("### Sample Images (click to use)")
92
+ gallery = gr.Gallery(
93
+ value=sample_images,
94
+ label="Sample Images",
95
+ show_label=False,
96
+ columns=4,
97
+ height="auto"
98
+ ).select(select_sample, None, input_image)
99
+
100
+ submit_btn.click(
101
+ predict_and_draw_bbox,
102
+ inputs=[input_image, model_choice],
103
+ outputs=[output_image, output_text]
104
+ )
105
+
106
+ if __name__ == "__main__":
107
+ iface.launch()
image4996.jpg ADDED
image4997.jpg ADDED
image4998.jpg ADDED
image4999.jpg ADDED
model.py ADDED
@@ -0,0 +1,108 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import tensorflow as tf
2
+ import numpy as np
3
+ from PIL import Image
4
+ import joblib
5
+ import os
6
+ from tensorflow.keras.models import Sequential
7
+ from tensorflow.keras.layers import Conv2D, MaxPooling2D, Dense, Flatten
8
+
9
+ class BoundingBoxPredictor:
10
+ def __init__(self):
11
+ self.cnn_model = None
12
+ self.knn_model = None
13
+ self.input_shape = (128, 128)
14
+ # Updated normalization values based on actual dataset analysis
15
+ self.y_min = np.array([0., 0., 30., 30.]) # min values for x, y, width, height
16
+ self.y_max = np.array([97., 97., 49., 49.]) # max values for x, y, width, height
17
+
18
+ def create_cnn_model(self):
19
+ model = Sequential([
20
+ Conv2D(32, (3, 3), activation='relu', input_shape=(*self.input_shape, 1)),
21
+ MaxPooling2D((2, 2)),
22
+ Conv2D(64, (3, 3), activation='relu'),
23
+ MaxPooling2D((2, 2)),
24
+ Flatten(),
25
+ Dense(128, activation='relu'),
26
+ Dense(4, activation='sigmoid')
27
+ ])
28
+ model.compile(optimizer='adam', loss='mse', metrics=['mse'])
29
+ return model
30
+
31
+ def load_models(self, cnn_path, knn_path):
32
+ # Check if files exist
33
+ if not os.path.exists(cnn_path):
34
+ raise FileNotFoundError(f"CNN model file not found: {cnn_path}")
35
+ if not os.path.exists(knn_path):
36
+ raise FileNotFoundError(f"KNN model file not found: {knn_path}")
37
+
38
+ print(f"Loading CNN model from: {cnn_path}")
39
+ try:
40
+ self.cnn_model = tf.keras.models.load_model(cnn_path)
41
+ except Exception as e:
42
+ print(f"Error loading CNN model: {str(e)}")
43
+ print("Creating new CNN model...")
44
+ self.cnn_model = self.create_cnn_model()
45
+ try:
46
+ self.cnn_model.load_weights(cnn_path)
47
+ print("Successfully loaded CNN weights")
48
+ except:
49
+ print("Could not load CNN weights, using uninitialized model")
50
+
51
+ print("CNN model ready")
52
+
53
+ print(f"Loading KNN model from: {knn_path}")
54
+ self.knn_model = joblib.load(knn_path)
55
+ print("KNN model loaded successfully")
56
+
57
+ def preprocess_image(self, image):
58
+ # Convert to grayscale if needed
59
+ if image.mode != 'L':
60
+ image = image.convert('L')
61
+
62
+ # Resize image
63
+ image = image.resize(self.input_shape)
64
+
65
+ # Convert to numpy array and normalize
66
+ img_array = np.array(image)
67
+ img_array = img_array / 255.0
68
+
69
+ return img_array
70
+
71
+ def predict(self, image, model_type='cnn'):
72
+ # Check if models are loaded
73
+ if model_type == 'cnn' and self.cnn_model is None:
74
+ raise ValueError("CNN model not loaded. Please call load_models first.")
75
+ if model_type == 'knn' and self.knn_model is None:
76
+ raise ValueError("KNN model not loaded. Please call load_models first.")
77
+
78
+ # Preprocess the image
79
+ processed_image = self.preprocess_image(image)
80
+
81
+ try:
82
+ if model_type == 'cnn':
83
+ # Reshape for CNN input
84
+ img_array = processed_image.reshape(1, *self.input_shape, 1)
85
+ # Get normalized predictions (between 0 and 1)
86
+ prediction = self.cnn_model.predict(img_array)[0]
87
+ # Denormalize predictions to original scale
88
+ prediction = prediction * (self.y_max - self.y_min) + self.y_min
89
+ else: # KNN
90
+ # Flatten for KNN input
91
+ img_array = processed_image.flatten().reshape(1, -1)
92
+ # Get normalized predictions
93
+ prediction = self.knn_model.predict(img_array)[0]
94
+ # Denormalize predictions to original scale
95
+ prediction = prediction * (self.y_max - self.y_min) + self.y_min
96
+
97
+ # Ensure predictions are within valid ranges
98
+ prediction = np.clip(prediction, self.y_min, self.y_max)
99
+
100
+ return {
101
+ 'x': float(prediction[0]),
102
+ 'y': float(prediction[1]),
103
+ 'width': float(prediction[2]),
104
+ 'height': float(prediction[3])
105
+ }
106
+ except Exception as e:
107
+ print(f"Error during prediction with {model_type.upper()} model: {str(e)}")
108
+ raise
requirements.txt ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ tensorflow>=2.0.0
2
+ numpy
3
+ Pillow
4
+ gradio>=4.0.0
5
+ matplotlib
6
+ scikit-learn
7
+ joblib
8
+ huggingface-hub