Spaces:
Sleeping
Sleeping
File size: 9,428 Bytes
75f48fa |
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 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 |
"""
π NAVADA 2.0 - Advanced AI Computer Vision Application (Lite Version)
Streamlit Version for Hugging Face Spaces Deployment
Enhanced Edition by Lee Akpareva | AI Consultant & Computer Vision Specialist
"""
import streamlit as st
import time
from datetime import datetime
import plotly.graph_objects as go
import plotly.express as px
from PIL import Image
import numpy as np
import os
# Configure Streamlit page (MUST be first!)
st.set_page_config(
page_title="π NAVADA 2.0 - AI Computer Vision",
page_icon="π",
layout="wide",
initial_sidebar_state="expanded"
)
# Backend imports - Lite version (no face recognition)
try:
from backend.yolo import detect_objects
from backend.openai_client import explain_detection
except ImportError as e:
st.error(f"β οΈ Import error: {e}")
st.error("π¦ Please install dependencies: pip install -r requirements.txt")
st.stop()
# Custom CSS for enhanced styling
st.markdown("""
<style>
.main-header {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
padding: 2rem;
border-radius: 10px;
color: white;
text-align: center;
margin-bottom: 2rem;
}
.feature-card {
background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
padding: 1.5rem;
border-radius: 10px;
color: white;
margin: 1rem 0;
}
.stats-card {
background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
padding: 1rem;
border-radius: 8px;
color: white;
text-align: center;
margin: 0.5rem;
}
</style>
""", unsafe_allow_html=True)
def create_detection_chart(detected_objects):
"""Create an interactive chart showing detection statistics"""
# Count object types
object_counts = {}
for obj in detected_objects:
object_counts[obj] = object_counts.get(obj, 0) + 1
if not object_counts:
# Create empty chart
fig = go.Figure()
fig.add_annotation(
text="No objects detected",
xref="paper", yref="paper",
x=0.5, y=0.5, showarrow=False,
font=dict(size=20, color="gray")
)
fig.update_layout(
height=300,
showlegend=False,
paper_bgcolor='rgba(0,0,0,0)',
plot_bgcolor='rgba(0,0,0,0)'
)
return fig
# Create bar chart
objects = list(object_counts.keys())
counts = list(object_counts.values())
fig = go.Figure(data=[
go.Bar(
x=objects,
y=counts,
marker_color='rgba(50, 171, 96, 0.6)',
marker_line_color='rgba(50, 171, 96, 1.0)',
marker_line_width=2,
text=counts,
textposition='auto'
)
])
fig.update_layout(
title="Detected Objects",
xaxis_title="Object Type",
yaxis_title="Count",
height=400,
showlegend=False,
paper_bgcolor='rgba(0,0,0,0)',
plot_bgcolor='rgba(0,0,0,0)'
)
return fig
def main():
# Main header
st.markdown("""
<div class="main-header">
<h1>π NAVADA 2.0 - Advanced AI Computer Vision</h1>
<p><strong>Lite Version - Object Detection & AI Analysis</strong></p>
<p>Built with YOLOv8 β’ OpenAI β’ Streamlit</p>
</div>
""", unsafe_allow_html=True)
# Sidebar
with st.sidebar:
st.markdown("### π― Detection Settings")
# Detection confidence threshold
confidence = st.slider(
"Detection Confidence",
min_value=0.1,
max_value=1.0,
value=0.5,
step=0.05,
help="Minimum confidence for object detection"
)
st.markdown("### π Features")
st.markdown("""
- π― **Object Detection**: YOLOv8 powered
- π€ **AI Explanations**: OpenAI integration
- π **Interactive Charts**: Real-time analytics
- π¨ **Visual Results**: Annotated images
""")
st.markdown("### βΉοΈ About")
st.markdown("""
This is the **Lite Version** optimized for Hugging Face Spaces.
**Created by:** Lee Akpareva
**AI Consultant & Computer Vision Specialist**
""")
# Main content
col1, col2 = st.columns([2, 1])
with col1:
st.markdown("### πΈ Upload Image for Analysis")
uploaded_file = st.file_uploader(
"Choose an image...",
type=['png', 'jpg', 'jpeg'],
help="Upload an image to detect objects and get AI analysis"
)
if uploaded_file is not None:
# Display uploaded image
image = Image.open(uploaded_file)
st.image(image, caption="Uploaded Image", use_column_width=True)
# Analysis button
if st.button("π Analyze Image", type="primary"):
with st.spinner("π Detecting objects..."):
# Perform object detection
results = detect_objects(image, confidence_threshold=confidence)
if results and len(results['detections']) > 0:
# Extract detected objects
detected_objects = [det['class'] for det in results['detections']]
# Display results
st.success(f"β
Detected {len(detected_objects)} objects!")
# Show annotated image
st.markdown("### π― Detection Results")
if 'annotated_image' in results:
st.image(results['annotated_image'], caption="Detected Objects", use_column_width=True)
# Show detection details
st.markdown("### π Detected Objects")
for i, detection in enumerate(results['detections']):
col_a, col_b, col_c = st.columns(3)
with col_a:
st.metric("Object", detection['class'])
with col_b:
st.metric("Confidence", f"{detection['confidence']:.2%}")
with col_c:
st.metric("Count", f"#{i+1}")
# AI Explanation
if os.getenv("OPENAI_API_KEY"):
st.markdown("### π€ AI Analysis")
with st.spinner("π§ Generating AI explanation..."):
try:
explanation = explain_detection(detected_objects)
st.markdown(f"**AI Insight:** {explanation}")
except Exception as e:
st.warning(f"AI analysis unavailable: {str(e)}")
else:
st.warning("π Add OPENAI_API_KEY in settings for AI explanations")
else:
st.warning("β No objects detected. Try adjusting the confidence threshold.")
with col2:
st.markdown("### π Detection Statistics")
# Sample chart (will be updated with real data)
sample_data = {
'Object': ['Person', 'Car', 'Dog', 'Cat'],
'Count': [3, 2, 1, 1]
}
fig = px.bar(
sample_data,
x='Object',
y='Count',
title="Sample Detection Results",
color='Count',
color_continuous_scale='Viridis'
)
fig.update_layout(height=300)
st.plotly_chart(fig, use_container_width=True)
# Feature highlights
st.markdown("### β¨ Key Features")
features = [
("π―", "Object Detection", "Advanced YOLOv8 model"),
("π€", "AI Analysis", "OpenAI explanations"),
("π", "Real-time Charts", "Interactive visualizations"),
("π", "Fast Processing", "Optimized for speed")
]
for icon, title, desc in features:
st.markdown(f"""
<div style="display: flex; align-items: center; margin: 1rem 0; padding: 0.5rem; background: #f0f2f6; border-radius: 5px;">
<div style="font-size: 1.5rem; margin-right: 1rem;">{icon}</div>
<div>
<strong>{title}</strong><br>
<small>{desc}</small>
</div>
</div>
""", unsafe_allow_html=True)
# Footer
st.markdown("---")
st.markdown("""
<div style="text-align: center; padding: 2rem; background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); border-radius: 10px; color: white; margin-top: 2rem;">
<h3>π Experience Advanced Computer Vision</h3>
<p><strong>β Built by Lee Akpareva | AI Consultant & Computer Vision Specialist β</strong></p>
<p>π <em>Powered by YOLOv8 β’ OpenAI β’ Streamlit</em></p>
</div>
""", unsafe_allow_html=True)
if __name__ == "__main__":
main() |