Ci-Dave commited on
Commit
1b884b3
·
1 Parent(s): bf2932a
Files changed (1) hide show
  1. app.py +124 -1
app.py CHANGED
@@ -21,7 +21,130 @@ from sklearn.preprocessing import StandardScaler # Standardization of image dat
21
 
22
  # Load Gemini API key from Streamlit Secrets configuration
23
  api_key = st.secrets["gemini"]["api_key"] # Get API key from Streamlit secrets
24
- genai.configure(api_key=api_key) # Configure the Gemini API with the API key
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
 
26
  MODEL_ID = "gemini-1.5-flash" # Specify the model ID for Gemini
27
  gen_model = genai.GenerativeModel(MODEL_ID) # Initialize the Gemini model
 
21
 
22
  # Load Gemini API key from Streamlit Secrets configuration
23
  api_key = st.secrets["gemini"]["api_key"] # Get API key from Streamlit secrets
24
+ genai.configure(api_key=api_key) # Configure the Gemini API with the API keyimport streamlit as st
25
+ import numpy as np
26
+ import google.generativeai as genai
27
+ import matplotlib.pyplot as plt
28
+
29
+ from sklearn.ensemble import RandomForestClassifier
30
+ from sklearn.linear_model import LogisticRegression
31
+ from skimage.filters import sobel
32
+ from skimage.segmentation import watershed
33
+ from skimage.feature import canny, hog
34
+ from skimage.color import rgb2gray
35
+ from skimage import io
36
+ from sklearn.preprocessing import StandardScaler
37
+ from sklearn.metrics import accuracy_score
38
+
39
+ # Load Gemini API key
40
+ api_key = st.secrets["gemini"]["api_key"]
41
+ genai.configure(api_key=api_key)
42
+ MODEL_ID = "gemini-1.5-flash"
43
+ gen_model = genai.GenerativeModel(MODEL_ID)
44
+
45
+ def explain_ai(prompt):
46
+ try:
47
+ response = gen_model.generate_content(prompt)
48
+ return response.text
49
+ except Exception as e:
50
+ return f"Error: {str(e)}"
51
+
52
+ # Streamlit app with multiple pages
53
+ st.set_page_config(page_title="Imaize: Smart Image Analyzer with XAI")
54
+
55
+ # Sidebar navigation
56
+ st.sidebar.title("Navigation")
57
+ page = st.sidebar.radio("Go to", ["Home", "Edge Detection", "Segmentation", "Feature Extraction", "AI Classification"])
58
+
59
+ # Home Page
60
+ if page == "Home":
61
+ st.title("Imaize: Smart Image Analyzer with XAI")
62
+ st.markdown("""
63
+ **Welcome to Imaize!** This app allows you to analyze images using AI-powered techniques:
64
+ - **Edge Detection**
65
+ - **Image Segmentation**
66
+ - **Feature Extraction**
67
+ - **AI Classification**
68
+
69
+ Upload an image and explore how AI explains the techniques!
70
+ """)
71
+ uploaded_file = st.file_uploader("Upload an image", type=["jpg", "png", "jpeg"])
72
+ if uploaded_file is not None:
73
+ image = io.imread(uploaded_file)
74
+ if image.shape[-1] == 4:
75
+ image = image[:, :, :3]
76
+ gray = rgb2gray(image)
77
+ st.image(image, caption="Uploaded Image", use_container_width=True)
78
+ st.session_state["gray"] = gray # Store for use in other pages
79
+
80
+ # Edge Detection Page
81
+ elif page == "Edge Detection":
82
+ st.title("Edge Detection")
83
+ gray = st.session_state.get("gray")
84
+ if gray is not None:
85
+ edge_method = st.selectbox("Select Edge Detection Method", ["Canny", "Sobel"])
86
+ edges = canny(gray) if edge_method == "Canny" else sobel(gray)
87
+ st.image(edges, caption=f"{edge_method} Edge Detection", use_container_width=True)
88
+ st.text_area("Explanation", explain_ai(f"Explain how {edge_method} edge detection works in computer vision."), height=300)
89
+ else:
90
+ st.warning("Please upload an image on the Home page.")
91
+
92
+ # Segmentation Page
93
+ elif page == "Segmentation":
94
+ st.title("Image Segmentation")
95
+ gray = st.session_state.get("gray")
96
+ if gray is not None:
97
+ seg_method = st.selectbox("Select Segmentation Method", ["Watershed", "Thresholding"])
98
+ if seg_method == "Watershed":
99
+ elevation_map = sobel(gray)
100
+ markers = np.zeros_like(gray)
101
+ markers[gray < 0.3] = 1
102
+ markers[gray > 0.7] = 2
103
+ segmented = watershed(elevation_map, markers.astype(np.int32))
104
+ else:
105
+ threshold_value = st.slider("Choose threshold value", 0, 255, 127)
106
+ segmented = (gray > (threshold_value / 255)).astype(np.uint8) * 255
107
+ st.image(segmented, caption=f"{seg_method} Segmentation", use_container_width=True)
108
+ st.text_area("Explanation", explain_ai(f"Explain how {seg_method} segmentation works in image processing."), height=300)
109
+ else:
110
+ st.warning("Please upload an image on the Home page.")
111
+
112
+ # Feature Extraction Page
113
+ elif page == "Feature Extraction":
114
+ st.title("HOG Feature Extraction")
115
+ gray = st.session_state.get("gray")
116
+ if gray is not None:
117
+ fd, hog_image = hog(gray, pixels_per_cell=(8, 8), cells_per_block=(2, 2), visualize=True)
118
+ st.image(hog_image, caption="HOG Features", use_container_width=True)
119
+ st.text_area("Explanation", explain_ai("Explain how Histogram of Oriented Gradients (HOG) feature extraction works."), height=300)
120
+ else:
121
+ st.warning("Please upload an image on the Home page.")
122
+
123
+ # AI Classification Page
124
+ elif page == "AI Classification":
125
+ st.title("AI Classification")
126
+ gray = st.session_state.get("gray")
127
+ if gray is not None:
128
+ model_choice = st.selectbox("Select AI Model", ["Random Forest", "Logistic Regression"])
129
+ flat_image = gray.flatten().reshape(-1, 1)
130
+ labels = (flat_image > 0.5).astype(int).flatten()
131
+ ai_model = RandomForestClassifier(n_jobs=1) if model_choice == "Random Forest" else LogisticRegression()
132
+ scaler = StandardScaler()
133
+ flat_image_scaled = scaler.fit_transform(flat_image)
134
+ ai_model.fit(flat_image_scaled, labels)
135
+ predictions = ai_model.predict(flat_image_scaled).reshape(gray.shape)
136
+ predictions = (predictions * 255).astype(np.uint8)
137
+ accuracy = accuracy_score(labels, ai_model.predict(flat_image_scaled))
138
+ st.image(predictions, caption=f"{model_choice} Pixel Classification", use_container_width=True)
139
+ st.text_area("Explanation", explain_ai(f"Explain how {model_choice} is used for image classification."), height=300)
140
+ st.write(f"### Accuracy: {accuracy:.2f}")
141
+ fig, ax = plt.subplots()
142
+ ax.bar(["Accuracy"], [accuracy], color='blue')
143
+ ax.set_ylim([0, 1])
144
+ st.pyplot(fig)
145
+ else:
146
+ st.warning("Please upload an image on the Home page.")
147
+
148
 
149
  MODEL_ID = "gemini-1.5-flash" # Specify the model ID for Gemini
150
  gen_model = genai.GenerativeModel(MODEL_ID) # Initialize the Gemini model