HvR commited on
Commit
9d0fb2c
·
verified ·
1 Parent(s): b74fa85

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +97 -0
app.py ADDED
@@ -0,0 +1,97 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ import gradio as gr
3
+ import numpy as np
4
+ import cv2
5
+ from PIL import Image
6
+ import tensorflow as tf
7
+ from tensorflow.keras.applications import MobileNetV2
8
+ from tensorflow.keras.applications.mobilenet_v2 import preprocess_input, decode_predictions
9
+ import json
10
+
11
+ # Load MobileNetV2 model
12
+ model = MobileNetV2(weights='imagenet')
13
+
14
+ def add_hsv_noise(image, hue_noise=0, saturation_noise=0, value_noise=0):
15
+ """Add HSV noise to an image"""
16
+ if image is None:
17
+ return None
18
+
19
+ # Convert PIL to numpy array
20
+ img_array = np.array(image)
21
+
22
+ # Convert RGB to HSV
23
+ hsv = cv2.cvtColor(img_array, cv2.COLOR_RGB2HSV).astype(np.float32)
24
+
25
+ # Add noise to each channel
26
+ hsv[:, :, 0] = np.clip(hsv[:, :, 0] + hue_noise, 0, 179) # Hue: 0-179
27
+ hsv[:, :, 1] = np.clip(hsv[:, :, 1] + saturation_noise, 0, 255) # Saturation: 0-255
28
+ hsv[:, :, 2] = np.clip(hsv[:, :, 2] + value_noise, 0, 255) # Value: 0-255
29
+
30
+ # Convert back to RGB
31
+ rgb = cv2.cvtColor(hsv.astype(np.uint8), cv2.COLOR_HSV2RGB)
32
+
33
+ return Image.fromarray(rgb)
34
+
35
+ def predict_image(image, top_n, hue_noise, saturation_noise, value_noise):
36
+ """Predict image classes with noise applied"""
37
+ if image is None:
38
+ return None, "Please upload an image first."
39
+
40
+ # Apply HSV noise
41
+ noisy_image = add_hsv_noise(image, hue_noise, saturation_noise, value_noise)
42
+
43
+ # Preprocess for MobileNet
44
+ img_resized = noisy_image.resize((224, 224))
45
+ img_array = np.array(img_resized)
46
+ img_array = np.expand_dims(img_array, axis=0)
47
+ img_array = preprocess_input(img_array)
48
+
49
+ # Make prediction
50
+ predictions = model.predict(img_array)
51
+ decoded_predictions = decode_predictions(predictions, top=top_n)[0]
52
+
53
+ # Format results
54
+ results = []
55
+ for i, (class_id, class_name, probability) in enumerate(decoded_predictions):
56
+ results.append(f"{i+1}. {class_name}: {probability:.4f} ({probability*100:.2f}%)")
57
+
58
+ results_text = "\n".join(results)
59
+
60
+ return noisy_image, results_text
61
+
62
+ # Create Gradio interface
63
+ with gr.Blocks(title="MobileNet HSV Noise Analysis") as demo:
64
+ gr.Markdown("# MobileNet Classification with HSV Noise")
65
+ gr.Markdown("Upload an image and adjust HSV noise sliders to see how it affects MobileNet predictions.")
66
+
67
+ with gr.Row():
68
+ with gr.Column():
69
+ # Input controls
70
+ input_image = gr.Image(type="pil", label="Upload Image")
71
+ top_n = gr.Slider(minimum=1, maximum=10, value=5, step=1, label="Top N Classes")
72
+
73
+ gr.Markdown("### HSV Noise Controls")
74
+ hue_noise = gr.Slider(minimum=-50, maximum=50, value=0, step=1, label="Hue Noise (-50 to 50)")
75
+ saturation_noise = gr.Slider(minimum=-100, maximum=100, value=0, step=5, label="Saturation Noise (-100 to 100)")
76
+ value_noise = gr.Slider(minimum=-100, maximum=100, value=0, step=5, label="Value/Brightness Noise (-100 to 100)")
77
+
78
+ with gr.Column():
79
+ # Output displays
80
+ output_image = gr.Image(label="Image with Noise Applied")
81
+ predictions_text = gr.Textbox(label="Top Predictions", lines=10, max_lines=15)
82
+
83
+ # Set up real-time updates
84
+ inputs = [input_image, top_n, hue_noise, saturation_noise, value_noise]
85
+ outputs = [output_image, predictions_text]
86
+
87
+ # Update predictions when any input changes
88
+ for input_component in inputs:
89
+ input_component.change(
90
+ fn=predict_image,
91
+ inputs=inputs,
92
+ outputs=outputs
93
+ )
94
+
95
+ # Launch the app
96
+ if __name__ == "__main__":
97
+ demo.launch(share=True, debug=True)