File size: 6,280 Bytes
bd780cd
 
 
 
1252a19
 
 
 
 
 
 
 
 
 
bd780cd
 
1252a19
 
bd780cd
1252a19
 
bd780cd
 
 
 
1252a19
 
bd780cd
1252a19
bd780cd
 
 
 
 
1252a19
bd780cd
 
 
 
 
 
 
 
1252a19
bd780cd
 
1252a19
 
 
 
 
 
bd780cd
 
 
 
1252a19
bd780cd
 
 
1252a19
 
 
 
 
 
 
 
bd780cd
 
1252a19
 
 
bd780cd
1252a19
 
bd780cd
1252a19
bd780cd
1252a19
 
 
bd780cd
 
1252a19
 
 
bd780cd
1252a19
 
 
 
 
bd780cd
1252a19
 
bd780cd
1252a19
 
 
 
bd780cd
1252a19
bd780cd
1252a19
 
 
bd780cd
 
1252a19
 
 
 
 
 
 
bd780cd
1252a19
bd780cd
1252a19
 
 
bd780cd
 
1252a19
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bd780cd
1252a19
bd780cd
1252a19
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import streamlit as st  # Streamlit library to create the web interface
import numpy as np  # Library for numerical operations
import google.generativeai as genai  # Gemini API for AI-generated explanations

# Import necessary computer vision functions
from skimage.filters import sobel
from skimage.segmentation import watershed
from skimage.feature import canny, hog
from skimage.color import rgb2gray
from skimage import io
from sklearn.preprocessing import StandardScaler
from sklearn.ensemble import RandomForestClassifier
from sklearn.linear_model import LogisticRegression
import matplotlib.pyplot as plt  # For better visualization

# Load Gemini API key from Streamlit Secrets configuration
api_key = st.secrets["gemini"]["api_key"]
genai.configure(api_key=api_key)

MODEL_ID = "gemini-1.5-flash"
gen_model = genai.GenerativeModel(MODEL_ID)

def explain_ai(prompt):
    """Generate an explanation using Gemini API with error handling."""
    try:
        response = gen_model.generate_content(prompt)
        return response.text
    except Exception as e:
        return f"Error: {str(e)}"

# App title
st.title("Imaize: Smart Image Analyzer with XAI")

# Image upload section
uploaded_file = st.file_uploader("Upload an image", type=["jpg", "png", "jpeg"])

# App Description 
st.markdown("""
This app combines AI-powered image analysis techniques with an easy-to-use interface for explanation generation. 
It leverages advanced computer vision algorithms such as **edge detection**, **image segmentation**, and **feature extraction**. 
Additionally, the app provides **explanations** for each method used, powered by the Gemini API, to make the process more understandable.
""")

# Instructions for using the app
st.markdown("""
### How to Use the App:
1. **Upload an Image**: Click on the "Upload an image" button to upload an image (in JPG, PNG, or JPEG format).
2. **Edge Detection**: Choose between **Canny** or **Sobel** edge detection methods.
3. **Segmentation**: Select **Watershed** or **Thresholding** segmentation.
4. **Extract HOG Features**: Visualize the Histogram of Oriented Gradients (HOG) features.
5. **AI Classification**: Classify the image using Random Forest or Logistic Regression models.
6. **Read the Explanations**: For each technique, you'll find a detailed explanation generated by AI.
""")

# If an image is uploaded, proceed with the analysis
if uploaded_file is not None:
    image = io.imread(uploaded_file)
    if image.shape[-1] == 4:  # If the image has 4 channels (RGBA), remove the alpha channel
        image = image[:, :, :3]
    
    # Convert to grayscale
    gray = rgb2gray(image)

    # Normalize grayscale image to make it visible (if it's in float range 0-1)
    gray_normalized = (gray * 255).astype(np.uint8)  # Convert grayscale image to 8-bit format

    # Display uploaded image
    st.image(image, caption="Uploaded Image", use_container_width=True)

    # Edge Detection Section
    st.subheader("Edge Detection")
    edge_method = st.selectbox("Select Edge Detection Method", ["Canny", "Sobel"], key="edge")
    edges = canny(gray) if edge_method == "Canny" else sobel(gray)
    edges = (edges * 255).astype(np.uint8)  # Convert edge map to 8-bit image format

    col1, col2 = st.columns([1, 1])
    with col1:
        st.image(edges, caption=f"{edge_method} Edge Detection", use_container_width=True)
    with col2:
        st.write("### Explanation")
        explanation = explain_ai(f"Explain how {edge_method} edge detection works in computer vision.")
        st.text_area("Explanation", explanation, height=300)

    # Segmentation Section
    st.subheader("Segmentation")
    seg_method = st.selectbox("Select Segmentation Method", ["Watershed", "Thresholding"], key="seg")

    if seg_method == "Watershed":
        elevation_map = sobel(gray)
        markers = np.zeros_like(gray)
        markers[gray < 0.3] = 1
        markers[gray > 0.7] = 2
        segmented = watershed(elevation_map, markers.astype(np.int32))
    else:
        threshold_value = st.slider("Choose threshold value", 0, 255, 127)
        segmented = (gray > (threshold_value / 255)).astype(np.uint8) * 255
    
    # Normalize segmented result for better visibility
    segmented_normalized = (segmented * 255).astype(np.uint8)

    col1, col2 = st.columns([1, 1])
    with col1:
        st.image(segmented_normalized, caption=f"{seg_method} Segmentation", use_container_width=True)
    with col2:
        st.write("### Explanation")
        explanation = explain_ai(f"Explain how {seg_method} segmentation works in image processing.")
        st.text_area("Explanation", explanation, height=300)

    # HOG Feature Extraction Section
    st.subheader("HOG Feature Extraction")
    fd, hog_image = hog(gray, pixels_per_cell=(8, 8), cells_per_block=(2, 2), visualize=True)

    # Normalize HOG image for display
    hog_image = (hog_image * 255).astype(np.uint8)

    col1, col2 = st.columns([1, 1])
    with col1:
        st.image(hog_image, caption="HOG Features", use_container_width=True)
    with col2:
        st.write("### Explanation")
        explanation = explain_ai("Explain how Histogram of Oriented Gradients (HOG) feature extraction works.")
        st.text_area("Explanation", explanation, height=300)

    # AI Classification Section
    st.subheader("AI Classification")
    model_choice = st.selectbox("Select AI Model", ["Random Forest", "Logistic Regression"], key="model")

    flat_image = gray.flatten().reshape(-1, 1)
    labels = (flat_image > 0.5).astype(int).flatten()

    ai_model = RandomForestClassifier(n_jobs=1) if model_choice == "Random Forest" else LogisticRegression()
    scaler = StandardScaler()
    flat_image_scaled = scaler.fit_transform(flat_image)

    ai_model.fit(flat_image_scaled, labels)
    predictions = ai_model.predict(flat_image_scaled).reshape(gray.shape)
    predictions = (predictions * 255).astype(np.uint8)

    col1, col2 = st.columns([1, 1])
    with col1:
        st.image(predictions, caption=f"{model_choice} Pixel Classification", use_container_width=True)
    with col2:
        st.write("### Explanation")
        explanation = explain_ai(f"Explain how {model_choice} is used for image classification.")
        st.text_area("Explanation", explanation, height=300)