Spaces:
Runtime error
Runtime error
sxs
Browse files
app.py
CHANGED
|
@@ -21,118 +21,7 @@ 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
|
| 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 |
-
|
| 53 |
-
# Sidebar navigation
|
| 54 |
-
st.sidebar.title("Navigation")
|
| 55 |
-
page = st.sidebar.radio("Go to", ["Home", "Edge Detection", "Segmentation", "Feature Extraction", "AI Classification"])
|
| 56 |
-
|
| 57 |
-
# Home Page
|
| 58 |
-
if page == "Home":
|
| 59 |
-
uploaded_file = st.file_uploader("Upload an image", type=["jpg", "png", "jpeg"])
|
| 60 |
-
if uploaded_file is not None:
|
| 61 |
-
image = io.imread(uploaded_file)
|
| 62 |
-
if image.shape[-1] == 4:
|
| 63 |
-
image = image[:, :, :3]
|
| 64 |
-
gray = rgb2gray(image)
|
| 65 |
-
st.image(image, caption="Uploaded Image", use_container_width=True)
|
| 66 |
-
st.session_state["gray"] = gray # Store for use in other pages
|
| 67 |
-
|
| 68 |
-
# Edge Detection Page
|
| 69 |
-
elif page == "Edge Detection":
|
| 70 |
-
st.title("Edge Detection")
|
| 71 |
-
gray = st.session_state.get("gray")
|
| 72 |
-
if gray is not None:
|
| 73 |
-
edge_method = st.selectbox("Select Edge Detection Method", ["Canny", "Sobel"])
|
| 74 |
-
edges = canny(gray) if edge_method == "Canny" else sobel(gray)
|
| 75 |
-
st.image(edges, caption=f"{edge_method} Edge Detection", use_container_width=True)
|
| 76 |
-
st.text_area("Explanation", explain_ai(f"Explain how {edge_method} edge detection works in computer vision."), height=300)
|
| 77 |
-
else:
|
| 78 |
-
st.warning("Please upload an image on the Home page.")
|
| 79 |
-
|
| 80 |
-
# Segmentation Page
|
| 81 |
-
elif page == "Segmentation":
|
| 82 |
-
st.title("Image Segmentation")
|
| 83 |
-
gray = st.session_state.get("gray")
|
| 84 |
-
if gray is not None:
|
| 85 |
-
seg_method = st.selectbox("Select Segmentation Method", ["Watershed", "Thresholding"])
|
| 86 |
-
if seg_method == "Watershed":
|
| 87 |
-
elevation_map = sobel(gray)
|
| 88 |
-
markers = np.zeros_like(gray)
|
| 89 |
-
markers[gray < 0.3] = 1
|
| 90 |
-
markers[gray > 0.7] = 2
|
| 91 |
-
segmented = watershed(elevation_map, markers.astype(np.int32))
|
| 92 |
-
else:
|
| 93 |
-
threshold_value = st.slider("Choose threshold value", 0, 255, 127)
|
| 94 |
-
segmented = (gray > (threshold_value / 255)).astype(np.uint8) * 255
|
| 95 |
-
st.image(segmented, caption=f"{seg_method} Segmentation", use_container_width=True)
|
| 96 |
-
st.text_area("Explanation", explain_ai(f"Explain how {seg_method} segmentation works in image processing."), height=300)
|
| 97 |
-
else:
|
| 98 |
-
st.warning("Please upload an image on the Home page.")
|
| 99 |
-
|
| 100 |
-
# Feature Extraction Page
|
| 101 |
-
elif page == "Feature Extraction":
|
| 102 |
-
st.title("HOG Feature Extraction")
|
| 103 |
-
gray = st.session_state.get("gray")
|
| 104 |
-
if gray is not None:
|
| 105 |
-
fd, hog_image = hog(gray, pixels_per_cell=(8, 8), cells_per_block=(2, 2), visualize=True)
|
| 106 |
-
st.image(hog_image, caption="HOG Features", use_container_width=True)
|
| 107 |
-
st.text_area("Explanation", explain_ai("Explain how Histogram of Oriented Gradients (HOG) feature extraction works."), height=300)
|
| 108 |
-
else:
|
| 109 |
-
st.warning("Please upload an image on the Home page.")
|
| 110 |
-
|
| 111 |
-
# AI Classification Page
|
| 112 |
-
elif page == "AI Classification":
|
| 113 |
-
st.title("AI Classification")
|
| 114 |
-
gray = st.session_state.get("gray")
|
| 115 |
-
if gray is not None:
|
| 116 |
-
model_choice = st.selectbox("Select AI Model", ["Random Forest", "Logistic Regression"])
|
| 117 |
-
flat_image = gray.flatten().reshape(-1, 1)
|
| 118 |
-
labels = (flat_image > 0.5).astype(int).flatten()
|
| 119 |
-
ai_model = RandomForestClassifier(n_jobs=1) if model_choice == "Random Forest" else LogisticRegression()
|
| 120 |
-
scaler = StandardScaler()
|
| 121 |
-
flat_image_scaled = scaler.fit_transform(flat_image)
|
| 122 |
-
ai_model.fit(flat_image_scaled, labels)
|
| 123 |
-
predictions = ai_model.predict(flat_image_scaled).reshape(gray.shape)
|
| 124 |
-
predictions = (predictions * 255).astype(np.uint8)
|
| 125 |
-
accuracy = accuracy_score(labels, ai_model.predict(flat_image_scaled))
|
| 126 |
-
st.image(predictions, caption=f"{model_choice} Pixel Classification", use_container_width=True)
|
| 127 |
-
st.text_area("Explanation", explain_ai(f"Explain how {model_choice} is used for image classification."), height=300)
|
| 128 |
-
st.write(f"### Accuracy: {accuracy:.2f}")
|
| 129 |
-
fig, ax = plt.subplots()
|
| 130 |
-
ax.bar(["Accuracy"], [accuracy], color='blue')
|
| 131 |
-
ax.set_ylim([0, 1])
|
| 132 |
-
st.pyplot(fig)
|
| 133 |
-
else:
|
| 134 |
-
st.warning("Please upload an image on the Home page.")
|
| 135 |
-
|
| 136 |
|
| 137 |
MODEL_ID = "gemini-1.5-flash" # Specify the model ID for Gemini
|
| 138 |
gen_model = genai.GenerativeModel(MODEL_ID) # Initialize the Gemini model
|
|
@@ -149,8 +38,8 @@ def explain_ai(prompt):
|
|
| 149 |
# App title
|
| 150 |
st.title("Imaize: Smart Image Analyzer with XAI")
|
| 151 |
|
| 152 |
-
#
|
| 153 |
-
|
| 154 |
|
| 155 |
# App Description
|
| 156 |
st.markdown("""
|
|
|
|
| 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
|
|
|
|
| 38 |
# App title
|
| 39 |
st.title("Imaize: Smart Image Analyzer with XAI")
|
| 40 |
|
| 41 |
+
# Image upload section
|
| 42 |
+
uploaded_file = st.file_uploader("Upload an image", type=["jpg", "png", "jpeg"]) # Allow user to upload an image file
|
| 43 |
|
| 44 |
# App Description
|
| 45 |
st.markdown("""
|