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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
num_points = st.slider("Number of points in spiral", 1, 10000, 1100)
|
| 17 |
-
num_turns = st.slider("Number of turns in spiral", 1, 300, 31)
|
| 18 |
-
|
| 19 |
-
indices = np.linspace(0, 1, num_points)
|
| 20 |
-
theta = 2 * np.pi * num_turns * indices
|
| 21 |
-
radius = indices
|
| 22 |
-
|
| 23 |
-
x = radius * np.cos(theta)
|
| 24 |
-
y = radius * np.sin(theta)
|
| 25 |
-
|
| 26 |
-
df = pd.DataFrame({
|
| 27 |
-
"x": x,
|
| 28 |
-
"y": y,
|
| 29 |
-
"idx": indices,
|
| 30 |
-
"rand": np.random.randn(num_points),
|
| 31 |
-
})
|
| 32 |
-
|
| 33 |
-
st.altair_chart(alt.Chart(df, height=700, width=700)
|
| 34 |
-
.mark_point(filled=True)
|
| 35 |
-
.encode(
|
| 36 |
-
x=alt.X("x", axis=None),
|
| 37 |
-
y=alt.Y("y", axis=None),
|
| 38 |
-
color=alt.Color("idx", legend=None, scale=alt.Scale()),
|
| 39 |
-
size=alt.Size("rand", legend=None, scale=alt.Scale(range=[1, 150])),
|
| 40 |
-
))
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import pandas as pd
|
| 3 |
+
|
| 4 |
+
import torch
|
| 5 |
+
import torchvision.transforms as transforms
|
| 6 |
+
from PIL import Image
|
| 7 |
+
import numpy as np
|
| 8 |
+
import torchvision.models as models
|
| 9 |
+
import torchvision
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
st.set_page_config(page_title="SmartVision AI - Intelligent Multi-Class Object Recognition System", layout="wide")
|
| 13 |
+
st.sidebar.title("π SmartVision AI")
|
| 14 |
+
|
| 15 |
+
st.sidebar.markdown("---")
|
| 16 |
+
|
| 17 |
+
st.title("π€ SmartVision AI - Intelligent Multi-Class Object Recognition System")
|
| 18 |
+
st.markdown("---")
|
| 19 |
+
|
| 20 |
+
page = st.sidebar.radio("Go to", ["π Home", "πΌοΈ Image Classification", "π¦ Object Detection", "π Model Performance", "πΈ Live Webcam Detection","βΉοΈ About"])
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
#------------------------------------------------Home Page----------------------------------------------------------------------------------------
|
| 25 |
+
|
| 26 |
+
if page == "π Home":
|
| 27 |
+
st.subheader("π Project Overview")
|
| 28 |
+
st.markdown("""
|
| 29 |
+
**SmartVision AI** is an intelligent computer vision system that performs real-time object detection
|
| 30 |
+
using a custom-trained **YOLO model**.
|
| 31 |
+
The system allows users to upload images and automatically identifies objects by drawing bounding boxes,
|
| 32 |
+
class labels, and confidence scores.
|
| 33 |
+
|
| 34 |
+
The goal of this project is to demonstrate an **end-to-end AI pipeline** β from model training
|
| 35 |
+
to optimized inference and visualization.
|
| 36 |
+
""")
|
| 37 |
+
|
| 38 |
+
st.info("β¨ This project is designed to showcase practical skills in Deep Learning, Computer Vision, and Model Deployment, with a focus on performance optimization and clean output presentation.")
|
| 39 |
+
st.markdown("---")
|
| 40 |
+
st.subheader("π Key Features")
|
| 41 |
+
st.markdown("""
|
| 42 |
+
β€ π **Accurate Object Detection** using a trained YOLO model
|
| 43 |
+
β€ π¦ **Bounding Boxes & Labels** on detected objects
|
| 44 |
+
β€ π **Confidence Scores** for every prediction
|
| 45 |
+
β€ π§ **Optional CNN-based verification**
|
| 46 |
+
β€ β‘ **Optimized CNNs** (VGG16, ResNet50, MobileNetV2, EfficientNet-B0)
|
| 47 |
+
""")
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
st.markdown("---")
|
| 51 |
+
st.subheader("π Instructions for Users")
|
| 52 |
+
st.text("""
|
| 53 |
+
β€ π Navigate to the Detection page
|
| 54 |
+
β€ π¦ Upload an image (JPG / PNG format)
|
| 55 |
+
β€ π Wait for the model to process the image
|
| 56 |
+
β€ π§ View the output image with bounding boxes and labels
|
| 57 |
+
β€ β‘ Check confidence scores for each detected object
|
| 58 |
+
""")
|
| 59 |
+
st.info("β οΈ For best results, use clear images with good lighting and visible objects.")
|
| 60 |
+
|
| 61 |
+
st.markdown("---")
|
| 62 |
+
st.subheader("πΌοΈ Sample Demo Images")
|
| 63 |
+
col1, col2 = st.columns(2)
|
| 64 |
+
with col1:
|
| 65 |
+
st.image("img.png", caption="YOLO Detection Example 1")
|
| 66 |
+
with col2:
|
| 67 |
+
st.image("img1.png", caption="YOLO Detection Example 2")
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
|
| 71 |
+
#----------------------------------------------------------------------------------------------------------------------------------
|
| 72 |
+
# we have already trained these models in the collab and using the state.dict(),after saving .here i am using the path of models
|
| 73 |
+
|
| 74 |
+
Classes= ['airplane', 'banana', 'bear', 'bicycle', 'bird', 'bowl', 'bus', 'cake', 'car', 'cat', 'dog', 'elephant', 'horse', 'laptop', 'motorcycle', 'mouse', 'parking meter', 'person', 'potted plant', 'sheep', 'toilet', 'traffic light', 'truck', 'tv', 'wine glass']
|
| 75 |
+
NUM_CLASSES = len(Classes) # 25
|
| 76 |
+
|
| 77 |
+
import torch
|
| 78 |
+
import torch.nn as nn
|
| 79 |
+
import torchvision.models as models
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
# vgg16
|
| 83 |
+
@st.cache_resource
|
| 84 |
+
def load_custom_vgg16():
|
| 85 |
+
model = models.vgg16(pretrained=False)
|
| 86 |
+
|
| 87 |
+
model.classifier = nn.Sequential(
|
| 88 |
+
nn.Linear(25088, 1024),
|
| 89 |
+
nn.ReLU(inplace=True),
|
| 90 |
+
nn.Dropout(0.5),
|
| 91 |
+
|
| 92 |
+
nn.Linear(1024, 512),
|
| 93 |
+
nn.ReLU(inplace=True),
|
| 94 |
+
nn.Dropout(0.5),
|
| 95 |
+
|
| 96 |
+
nn.Linear(512, 25) # number of classes
|
| 97 |
+
)
|
| 98 |
+
|
| 99 |
+
model.load_state_dict(
|
| 100 |
+
torch.load(
|
| 101 |
+
"models/vgg16_smartvision.pth",
|
| 102 |
+
map_location=torch.device("cpu")
|
| 103 |
+
)
|
| 104 |
+
)
|
| 105 |
+
|
| 106 |
+
model.eval()
|
| 107 |
+
return model
|
| 108 |
+
|
| 109 |
+
# RestNet50
|
| 110 |
+
@st.cache_resource
|
| 111 |
+
def load_custom_restnet50():
|
| 112 |
+
model=models.resnet50(pretrained=False)
|
| 113 |
+
|
| 114 |
+
# CUSTOM CLASSIFICATION HEAD
|
| 115 |
+
model.fc = nn.Sequential(
|
| 116 |
+
nn.Linear(model.fc.in_features, 512),
|
| 117 |
+
nn.BatchNorm1d(512),
|
| 118 |
+
nn.ReLU(),
|
| 119 |
+
nn.Dropout(0.5),
|
| 120 |
+
nn.Linear(512, NUM_CLASSES)
|
| 121 |
+
)
|
| 122 |
+
|
| 123 |
+
model.load_state_dict(
|
| 124 |
+
torch.load(
|
| 125 |
+
"models/smartvision_resnet50.pth",
|
| 126 |
+
map_location=torch.device("cpu")
|
| 127 |
+
)
|
| 128 |
+
)
|
| 129 |
+
|
| 130 |
+
model.eval()
|
| 131 |
+
return model
|
| 132 |
+
|
| 133 |
+
# Mobilenet_v2
|
| 134 |
+
@st.cache_resource
|
| 135 |
+
def load_custom_mobilenetv2():
|
| 136 |
+
model=models.mobilenet_v2(pretrained=False)
|
| 137 |
+
|
| 138 |
+
# CUSTOM CLASSIFICATION HEAD
|
| 139 |
+
model.classifier = nn.Sequential(
|
| 140 |
+
nn.Linear(1280, 512),
|
| 141 |
+
nn.ReLU(),
|
| 142 |
+
nn.Dropout(0.4),
|
| 143 |
+
nn.Linear(512, NUM_CLASSES)
|
| 144 |
+
)
|
| 145 |
+
|
| 146 |
+
model.load_state_dict(
|
| 147 |
+
torch.load(
|
| 148 |
+
"models/mobilenetv2_smartvision.pth",
|
| 149 |
+
map_location=torch.device("cpu")
|
| 150 |
+
)
|
| 151 |
+
)
|
| 152 |
+
|
| 153 |
+
model.eval()
|
| 154 |
+
return model
|
| 155 |
+
|
| 156 |
+
|
| 157 |
+
# EffcientNetB0
|
| 158 |
+
@st.cache_resource
|
| 159 |
+
def load_custom_EffcientNet():
|
| 160 |
+
model=models.efficientnet_b0(pretrained=False)
|
| 161 |
+
|
| 162 |
+
# CUSTOM CLASSIFICATION HEAD
|
| 163 |
+
model.classifier = nn.Sequential(
|
| 164 |
+
nn.Dropout(0.4),
|
| 165 |
+
nn.Linear(1280, 512),
|
| 166 |
+
nn.ReLU(),
|
| 167 |
+
nn.Dropout(0.3),
|
| 168 |
+
nn.Linear(512, NUM_CLASSES)
|
| 169 |
+
)
|
| 170 |
+
|
| 171 |
+
model.load_state_dict(
|
| 172 |
+
torch.load(
|
| 173 |
+
"models/EfficientNetB0_smartvision.pth",
|
| 174 |
+
map_location=torch.device("cpu")
|
| 175 |
+
)
|
| 176 |
+
)
|
| 177 |
+
|
| 178 |
+
model.eval()
|
| 179 |
+
return model
|
| 180 |
+
|
| 181 |
+
# Image preprocessing
|
| 182 |
+
|
| 183 |
+
preprocess = transforms.Compose([
|
| 184 |
+
transforms.Resize((224, 224)),
|
| 185 |
+
transforms.ToTensor(),
|
| 186 |
+
transforms.Normalize(
|
| 187 |
+
mean=[0.485, 0.456, 0.406],
|
| 188 |
+
std=[0.229, 0.224, 0.225]
|
| 189 |
+
)
|
| 190 |
+
])
|
| 191 |
+
# ------------------------------------------------------------------------------------------------------------------------------------
|
| 192 |
+
|
| 193 |
+
if page == "πΌοΈ Image Classification":
|
| 194 |
+
st.subheader("πΌοΈ Image Classification (Custom Trained CNN Models)")
|
| 195 |
+
st.markdown("""
|
| 196 |
+
This page performs **single-object image classification** using multiple
|
| 197 |
+
**custom-trained CNN models**.
|
| 198 |
+
Predictions from each model are shown **side-by-side** for comparison.
|
| 199 |
+
""")
|
| 200 |
+
|
| 201 |
+
uploaded_file = st.file_uploader(
|
| 202 |
+
"π€ Upload an Image",
|
| 203 |
+
type=["jpg", "jpeg", "png"]
|
| 204 |
+
)
|
| 205 |
+
|
| 206 |
+
if uploaded_file:
|
| 207 |
+
image = Image.open(uploaded_file).convert("RGB")
|
| 208 |
+
|
| 209 |
+
st.markdown("### π· Uploaded Image")
|
| 210 |
+
st.image(image, width=300)
|
| 211 |
+
|
| 212 |
+
input_tensor = preprocess(image).unsqueeze(0)
|
| 213 |
+
|
| 214 |
+
# Load all models
|
| 215 |
+
models_dict = {
|
| 216 |
+
"π§ VGG16": load_custom_vgg16(),
|
| 217 |
+
"π§ ResNet50": load_custom_restnet50(),
|
| 218 |
+
"π§ MobileNetV2": load_custom_mobilenetv2(),
|
| 219 |
+
"π§ EfficientNet-B0": load_custom_EffcientNet()
|
| 220 |
+
}
|
| 221 |
+
|
| 222 |
+
st.markdown("---")
|
| 223 |
+
st.markdown("### π Model Predictions (Top-5)")
|
| 224 |
+
|
| 225 |
+
cols = st.columns(4)
|
| 226 |
+
|
| 227 |
+
for col, (model_name, model) in zip(cols, models_dict.items()):
|
| 228 |
+
with col:
|
| 229 |
+
st.markdown(f"#### {model_name}")
|
| 230 |
+
|
| 231 |
+
with torch.no_grad():
|
| 232 |
+
outputs = model(input_tensor)
|
| 233 |
+
probs = torch.nn.functional.softmax(outputs[0], dim=0)
|
| 234 |
+
|
| 235 |
+
top_probs, top_idxs = torch.topk(
|
| 236 |
+
probs, min(5, len(Classes))
|
| 237 |
+
)
|
| 238 |
+
|
| 239 |
+
for i in range(len(top_idxs)):
|
| 240 |
+
class_name = Classes[top_idxs[i].item()]
|
| 241 |
+
confidence = top_probs[i].item()
|
| 242 |
+
|
| 243 |
+
st.write(
|
| 244 |
+
f"**{i+1}. {class_name}** β {confidence*100:.2f}%"
|
| 245 |
+
)
|
| 246 |
+
st.progress(float(confidence))
|
| 247 |
+
|
| 248 |
+
else:
|
| 249 |
+
st.info("β¬οΈ Upload an image to classify.")
|
| 250 |
+
|
| 251 |
+
|
| 252 |
+
|
| 253 |
+
|
| 254 |
+
import cv2
|
| 255 |
import numpy as np
|
| 256 |
+
from ultralytics import YOLO
|
| 257 |
+
from PIL import Image
|
| 258 |
+
import streamlit as st
|
| 259 |
+
|
| 260 |
+
|
| 261 |
+
@st.cache_resource
|
| 262 |
+
def load_yolo_model():
|
| 263 |
+
return YOLO("best (1).pt") # path to my already trained model
|
| 264 |
+
|
| 265 |
+
yolo_model = load_yolo_model()
|
| 266 |
+
|
| 267 |
+
#-------------------------------------------------------------------------------------------------------------------------------------
|
| 268 |
+
|
| 269 |
+
if page == "π¦ Object Detection":
|
| 270 |
+
st.subheader("π― Object Detection using YOLO")
|
| 271 |
+
st.markdown("""
|
| 272 |
+
Upload an image to detect **multiple objects** using a custom-trained YOLO model.
|
| 273 |
+
Bounding boxes, class labels, and confidence scores will be displayed.
|
| 274 |
+
""")
|
| 275 |
+
|
| 276 |
+
st.markdown("---")
|
| 277 |
+
|
| 278 |
+
# Confidence threshold slider
|
| 279 |
+
conf_threshold = st.slider(
|
| 280 |
+
"π§ Confidence Threshold",
|
| 281 |
+
min_value=0.1,
|
| 282 |
+
max_value=1.0,
|
| 283 |
+
value=0.5,
|
| 284 |
+
step=0.05
|
| 285 |
+
)
|
| 286 |
+
|
| 287 |
+
uploaded_file = st.file_uploader(
|
| 288 |
+
"π€ Upload an Image (JPG / PNG)",
|
| 289 |
+
type=["jpg", "jpeg", "png"]
|
| 290 |
+
)
|
| 291 |
+
|
| 292 |
+
if uploaded_file:
|
| 293 |
+
image = Image.open(uploaded_file).convert("RGB")
|
| 294 |
+
img_array = np.array(image)
|
| 295 |
+
|
| 296 |
+
st.markdown("### π· Uploaded Image")
|
| 297 |
+
st.image(image, width=350)
|
| 298 |
+
|
| 299 |
+
st.markdown("---")
|
| 300 |
+
st.markdown("### π Detection Results")
|
| 301 |
+
|
| 302 |
+
# YOLO inference
|
| 303 |
+
results = yolo_model.predict(
|
| 304 |
+
source=img_array,
|
| 305 |
+
conf=conf_threshold,
|
| 306 |
+
save=False
|
| 307 |
+
)
|
| 308 |
+
|
| 309 |
+
annotated_img = img_array.copy()
|
| 310 |
+
|
| 311 |
+
detections_found = False
|
| 312 |
+
|
| 313 |
+
for r in results:
|
| 314 |
+
boxes = r.boxes
|
| 315 |
+
|
| 316 |
+
if boxes is not None:
|
| 317 |
+
for box in boxes:
|
| 318 |
+
detections_found = True
|
| 319 |
+
|
| 320 |
+
x1, y1, x2, y2 = map(int, box.xyxy[0])
|
| 321 |
+
conf = float(box.conf[0])
|
| 322 |
+
cls_id = int(box.cls[0])
|
| 323 |
+
label = yolo_model.names[cls_id]
|
| 324 |
+
|
| 325 |
+
# Draw bounding box
|
| 326 |
+
cv2.rectangle(
|
| 327 |
+
annotated_img,
|
| 328 |
+
(x1, y1),
|
| 329 |
+
(x2, y2),
|
| 330 |
+
(0, 255, 0),
|
| 331 |
+
2
|
| 332 |
+
)
|
| 333 |
+
|
| 334 |
+
# Label text
|
| 335 |
+
text = f"{label} {conf*100:.2f}%"
|
| 336 |
+
cv2.putText(
|
| 337 |
+
annotated_img,
|
| 338 |
+
text,
|
| 339 |
+
(x1, y1 - 10),
|
| 340 |
+
cv2.FONT_HERSHEY_SIMPLEX,
|
| 341 |
+
0.6,
|
| 342 |
+
(0, 255, 0),
|
| 343 |
+
2
|
| 344 |
+
)
|
| 345 |
+
|
| 346 |
+
if detections_found:
|
| 347 |
+
st.image(
|
| 348 |
+
annotated_img,
|
| 349 |
+
caption="YOLO Detection Output",
|
| 350 |
+
use_column_width=True
|
| 351 |
+
)
|
| 352 |
+
else:
|
| 353 |
+
st.warning("β οΈ No objects detected. Try lowering the confidence threshold.")
|
| 354 |
+
|
| 355 |
+
else:
|
| 356 |
+
st.info("β¬οΈ Upload an image to start object detection.")
|
| 357 |
+
|
| 358 |
+
|
| 359 |
+
|
| 360 |
+
|
| 361 |
+
# This dashboard compares multiple CNN architectures based on accuracy and inference speed.
|
| 362 |
+
# While deeper models like VGG16 perform well during training, lightweight models such as
|
| 363 |
+
# MobileNetV2 and EfficientNetB0 offer faster inference, making them suitable for real-time applications.
|
| 364 |
+
|
| 365 |
+
|
| 366 |
+
#----------------------------------Model Performance---------------------------------------------------------------
|
| 367 |
+
import matplotlib.pyplot as plt
|
| 368 |
import pandas as pd
|
| 369 |
+
import seaborn as sns
|
| 370 |
+
import streamlit as st
|
| 371 |
+
|
| 372 |
+
# ---------------- MODEL METRICS DATA ----------------
|
| 373 |
+
data = {
|
| 374 |
+
"Model": ["VGG16", "ResNet50", "MobileNetV2", "EfficientNetB0"],
|
| 375 |
+
"Train Accuracy": [0.877, 0.6815, 0.51, 0.5297],
|
| 376 |
+
"Val Accuracy": [0.6345, 0.6855, 0.54, 0.56],
|
| 377 |
+
"Test Accuracy": [0.633, 0.593, 0.579, 0.543],
|
| 378 |
+
"Speed": [8.9, 0.5, 13.0, 12.6] # higher = faster
|
| 379 |
+
}
|
| 380 |
+
|
| 381 |
+
df = pd.DataFrame(data)
|
| 382 |
+
|
| 383 |
+
# ---------------- PAGE 4: MODEL PERFORMANCE ----------------
|
| 384 |
+
if page == "π Model Performance":
|
| 385 |
+
st.subheader("π Model Performance Dashboard")
|
| 386 |
+
st.markdown("""
|
| 387 |
+
This section presents a **comparative analysis** of different CNN models used in SmartVision AI.
|
| 388 |
+
It highlights **training, validation, and test accuracy**, along with **relative inference speed**.
|
| 389 |
+
""")
|
| 390 |
+
|
| 391 |
+
st.markdown("---")
|
| 392 |
+
|
| 393 |
+
# ---------------- MODEL METRICS TABLE ----------------
|
| 394 |
+
st.markdown("### π Model Comparison Table")
|
| 395 |
+
st.dataframe(df, use_container_width=True)
|
| 396 |
+
|
| 397 |
+
st.markdown("---")
|
| 398 |
+
|
| 399 |
+
# ---------------- ACCURACY COMPARISON ----------------
|
| 400 |
+
st.markdown("### π Accuracy Comparison (Train / Validation / Test)")
|
| 401 |
+
|
| 402 |
+
acc_df = df.melt(
|
| 403 |
+
id_vars="Model",
|
| 404 |
+
value_vars=["Train Accuracy", "Val Accuracy", "Test Accuracy"],
|
| 405 |
+
var_name="Dataset",
|
| 406 |
+
value_name="Accuracy"
|
| 407 |
+
)
|
| 408 |
+
|
| 409 |
+
fig1, ax1 = plt.subplots()
|
| 410 |
+
sns.barplot(
|
| 411 |
+
data=acc_df,
|
| 412 |
+
x="Model",
|
| 413 |
+
y="Accuracy",
|
| 414 |
+
hue="Dataset",
|
| 415 |
+
ax=ax1
|
| 416 |
+
)
|
| 417 |
+
ax1.set_ylim(0, 1)
|
| 418 |
+
ax1.set_title("Accuracy Comparison Across Models")
|
| 419 |
+
ax1.set_ylabel("Accuracy")
|
| 420 |
+
ax1.set_xlabel("Model")
|
| 421 |
+
|
| 422 |
+
st.pyplot(fig1)
|
| 423 |
+
|
| 424 |
+
st.markdown("---")
|
| 425 |
+
|
| 426 |
+
# ---------------- INFERENCE SPEED COMPARISON ----------------
|
| 427 |
+
st.markdown("### β‘ Inference Speed Comparison")
|
| 428 |
+
|
| 429 |
+
fig2, ax2 = plt.subplots()
|
| 430 |
+
sns.barplot(
|
| 431 |
+
data=df,
|
| 432 |
+
x="Model",
|
| 433 |
+
y="Speed",
|
| 434 |
+
ax=ax2
|
| 435 |
+
)
|
| 436 |
+
ax2.set_title("Relative Inference Speed (Higher is Faster)")
|
| 437 |
+
ax2.set_ylabel("Speed Score")
|
| 438 |
+
ax2.set_xlabel("Model")
|
| 439 |
+
|
| 440 |
+
st.pyplot(fig2)
|
| 441 |
+
|
| 442 |
+
st.markdown("---")
|
| 443 |
+
|
| 444 |
+
# ---------------- PERFORMANCE INSIGHTS ----------------
|
| 445 |
+
st.markdown("### π§ Key Observations")
|
| 446 |
+
st.markdown("""
|
| 447 |
+
- **VGG16** shows strong training accuracy but noticeable generalization gap
|
| 448 |
+
- **ResNet50** provides better validation stability
|
| 449 |
+
- **MobileNetV2** and **EfficientNetB0** trade accuracy for faster inference
|
| 450 |
+
- Lightweight models are suitable for **real-time or edge deployment**
|
| 451 |
+
""")
|
| 452 |
+
|
| 453 |
+
|
| 454 |
+
|
| 455 |
+
#----------------------------------------------------Live Camera Detection----------------------------------------------------------------------------------
|
| 456 |
+
import cv2
|
| 457 |
+
import time
|
| 458 |
+
import numpy as np
|
| 459 |
import streamlit as st
|
| 460 |
+
from ultralytics import YOLO
|
| 461 |
+
|
| 462 |
+
#Loading the pretrained model from YOLO
|
| 463 |
+
|
| 464 |
+
@st.cache_resource
|
| 465 |
+
def load_pretrained_yolo():
|
| 466 |
+
return YOLO("yolov8n.pt") # pretrained model
|
| 467 |
+
|
| 468 |
+
yolo_model_live = load_pretrained_yolo()
|
| 469 |
+
|
| 470 |
+
|
| 471 |
+
if page == "πΈ Live Webcam Detection":
|
| 472 |
+
st.subheader("πΈ Live Camera Detection (Lightweight Mode)")
|
| 473 |
+
|
| 474 |
+
# 0.5 β show only detections above 50% if 0.1 then show only detections above 10%
|
| 475 |
+
conf_thres = st.slider("Confidence Threshold", 0.1, 1.0, 0.5, 0.05)
|
| 476 |
+
run = st.checkbox("βΆ Start Camera")
|
| 477 |
+
|
| 478 |
+
FRAME_WINDOW = st.image([])
|
| 479 |
+
fps_text = st.empty()
|
| 480 |
+
|
| 481 |
+
if run:
|
| 482 |
+
cap = cv2.VideoCapture(0)
|
| 483 |
+
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
|
| 484 |
+
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)
|
| 485 |
+
|
| 486 |
+
# using this for faster optimization
|
| 487 |
+
frame_skip = 3 # π₯ process 1 frame out of 3
|
| 488 |
+
frame_count = 0
|
| 489 |
+
prev_time = time.time()
|
| 490 |
+
|
| 491 |
+
while run:
|
| 492 |
+
ret, frame = cap.read()
|
| 493 |
+
if not ret:
|
| 494 |
+
break
|
| 495 |
+
|
| 496 |
+
frame_count += 1
|
| 497 |
+
|
| 498 |
+
# Skip frames
|
| 499 |
+
if frame_count % frame_skip != 0:
|
| 500 |
+
continue
|
| 501 |
+
|
| 502 |
+
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
| 503 |
+
|
| 504 |
+
results = yolo_model_live.predict(
|
| 505 |
+
frame_rgb,
|
| 506 |
+
conf=conf_thres,
|
| 507 |
+
imgsz=416, # π₯ smaller image
|
| 508 |
+
verbose=False
|
| 509 |
+
)
|
| 510 |
+
|
| 511 |
+
annotated_frame = results[0].plot()
|
| 512 |
+
|
| 513 |
+
# FPS
|
| 514 |
+
curr_time = time.time()
|
| 515 |
+
fps = 1 / (curr_time - prev_time)
|
| 516 |
+
prev_time = curr_time
|
| 517 |
+
|
| 518 |
+
fps_text.markdown(f"β‘ FPS: {fps:.1f}")
|
| 519 |
+
|
| 520 |
+
FRAME_WINDOW.image(
|
| 521 |
+
annotated_frame,
|
| 522 |
+
channels="RGB",
|
| 523 |
+
use_column_width=True
|
| 524 |
+
)
|
| 525 |
+
|
| 526 |
+
time.sleep(0.03) # π₯ CPU cooldown
|
| 527 |
+
|
| 528 |
+
cap.release()
|
| 529 |
+
|
| 530 |
+
|
| 531 |
+
|
| 532 |
+
if page == "βΉοΈ About":
|
| 533 |
+
st.subheader("π About SmartVision AI")
|
| 534 |
+
st.markdown("---")
|
| 535 |
+
|
| 536 |
+
# ---------------- PROJECT OVERVIEW ----------------
|
| 537 |
+
st.markdown("## π§ Project Overview")
|
| 538 |
+
st.markdown("""
|
| 539 |
+
**SmartVision AI** is an end-to-end **computer vision system** designed to perform
|
| 540 |
+
**image classification**, **object detection**, and **real-time inference** using
|
| 541 |
+
state-of-the-art deep learning models.
|
| 542 |
+
|
| 543 |
+
The project demonstrates the complete AI lifecycle:
|
| 544 |
+
**dataset preparation β model training β optimized inference β deployment using Streamlit**.
|
| 545 |
+
""")
|
| 546 |
+
|
| 547 |
+
# ---------------- DATASET INFO ----------------
|
| 548 |
+
st.markdown("## π Dataset Information")
|
| 549 |
+
st.markdown("""
|
| 550 |
+
- **Image Classification Dataset**
|
| 551 |
+
- Domain-specific dataset with **25 object classes**
|
| 552 |
+
- Preprocessed and augmented for robustness
|
| 553 |
+
- Split into **Train / Validation / Test** sets
|
| 554 |
+
|
| 555 |
+
- **Object Detection Dataset**
|
| 556 |
+
- General object detection using **COCO dataset**
|
| 557 |
+
- 80 commonly occurring object classes
|
| 558 |
+
- Bounding-box annotated images
|
| 559 |
+
""")
|
| 560 |
+
|
| 561 |
+
# ---------------- MODEL ARCHITECTURES ----------------
|
| 562 |
+
st.markdown("## ποΈ Model Architectures Used")
|
| 563 |
+
st.markdown("""
|
| 564 |
+
### πΉ Image Classification Models
|
| 565 |
+
- **VGG16 (Custom Trained)**
|
| 566 |
+
- Modified fully connected layers
|
| 567 |
+
- High accuracy on domain-specific data
|
| 568 |
+
|
| 569 |
+
- **ResNet50**
|
| 570 |
+
- Residual connections for deeper learning
|
| 571 |
+
- Strong generalization capability
|
| 572 |
+
|
| 573 |
+
- **MobileNetV2**
|
| 574 |
+
- Lightweight architecture
|
| 575 |
+
- Optimized for speed and mobile devices
|
| 576 |
+
|
| 577 |
+
- **EfficientNet-B0**
|
| 578 |
+
- Balanced accuracy and efficiency
|
| 579 |
+
- Compound scaling technique
|
| 580 |
+
|
| 581 |
+
### πΉ Object Detection Model
|
| 582 |
+
- **YOLOv8 (Pretrained)**
|
| 583 |
+
- Real-time object detection
|
| 584 |
+
- Single-stage detector
|
| 585 |
+
- Optimized for speed and accuracy
|
| 586 |
+
""")
|
| 587 |
+
|
| 588 |
+
# ---------------- TECH STACK ----------------
|
| 589 |
+
st.markdown("## π οΈ Technical Stack")
|
| 590 |
+
st.markdown("""
|
| 591 |
+
**Programming Language**
|
| 592 |
+
- Python π
|
| 593 |
+
|
| 594 |
+
**Deep Learning & Vision**
|
| 595 |
+
- PyTorch
|
| 596 |
+
- Torchvision
|
| 597 |
+
- Ultralytics YOLOv8
|
| 598 |
+
- OpenCV
|
| 599 |
+
|
| 600 |
+
**Data Processing & Visualization**
|
| 601 |
+
- NumPy
|
| 602 |
+
- Pandas
|
| 603 |
+
- Matplotlib
|
| 604 |
+
- Seaborn
|
| 605 |
+
|
| 606 |
+
**Web & Deployment**
|
| 607 |
+
- Streamlit
|
| 608 |
+
- VS Code
|
| 609 |
+
- Git & GitHub
|
| 610 |
+
""")
|
| 611 |
+
|
| 612 |
+
# ---------------- OPTIMIZATION ----------------
|
| 613 |
+
st.markdown("## β‘ Performance Optimization Techniques")
|
| 614 |
+
st.markdown("""
|
| 615 |
+
- Model quantization (where applicable)
|
| 616 |
+
- Frame skipping for real-time inference
|
| 617 |
+
- Resolution scaling for faster detection
|
| 618 |
+
- CPU-optimized inference pipeline
|
| 619 |
+
- Streamlit resource caching
|
| 620 |
+
""")
|
| 621 |
+
|
| 622 |
+
# ---------------- DEVELOPER INFO ----------------
|
| 623 |
+
st.markdown("## π¨βπ» Developer Information")
|
| 624 |
+
st.markdown("""
|
| 625 |
+
**Developer:** Rahul Kumar
|
| 626 |
+
**Degree:** B.Tech in Information Technology
|
| 627 |
+
**Institution:** IIEST Shibpur
|
| 628 |
+
|
| 629 |
+
**Core Interests:**
|
| 630 |
+
- Computer Vision
|
| 631 |
+
- Deep Learning
|
| 632 |
+
- Full Stack Development
|
| 633 |
+
- AI Model Deployment
|
| 634 |
+
|
| 635 |
+
**Project Goal:**
|
| 636 |
+
To build scalable, efficient, and production-ready AI systems
|
| 637 |
+
with real-world deployment considerations.
|
| 638 |
+
""")
|
| 639 |
+
|
| 640 |
+
# ---------------------------------------FOOTER --------------------------------------------------------------------------------------
|
| 641 |
+
st.markdown("---")
|
| 642 |
+
st.info("π SmartVision AI β Bridging Deep Learning Research with Real-World Applications")
|
| 643 |
+
|
| 644 |
+
|
| 645 |
+
#-------------------Footer Part in sidebar----------------------------------------------------------------------------------------------
|
| 646 |
+
|
| 647 |
+
import streamlit as st
|
| 648 |
+
|
| 649 |
+
st.sidebar.markdown("---")
|
| 650 |
+
|
| 651 |
+
st.sidebar.markdown("### π SmartVision AI")
|
| 652 |
+
|
| 653 |
+
col1, col2, col3 = st.sidebar.columns(3)
|
| 654 |
+
|
| 655 |
+
with col1:
|
| 656 |
+
st.sidebar.markdown(
|
| 657 |
+
"[π GitHub](https://github.com/rahul-tech-kumar/SmartVision-AI---Intelligent-Multi-Class-Object-Recognition-System)",
|
| 658 |
+
unsafe_allow_html=True
|
| 659 |
+
)
|
| 660 |
+
|
| 661 |
+
with col2:
|
| 662 |
+
st.sidebar.markdown(
|
| 663 |
+
"[πΌ LinkedIn](https://www.linkedin.com/in/rahul-kumar-173546228/)",
|
| 664 |
+
unsafe_allow_html=True
|
| 665 |
+
)
|
| 666 |
+
|
| 667 |
+
with col3:
|
| 668 |
+
st.sidebar.markdown(
|
| 669 |
+
"[βοΈ Email](mailto:rahulkumar11062003@gmail.com)",
|
| 670 |
+
unsafe_allow_html=True
|
| 671 |
+
)
|
| 672 |
+
|
| 673 |
+
st.sidebar.markdown("---")
|
| 674 |
|
| 675 |
+
st.sidebar.markdown(
|
| 676 |
+
"""
|
| 677 |
+
<div style="text-align:center; font-size:12px; color:gray;">
|
| 678 |
+
π Built with Streamlit & PyTorch<br>
|
| 679 |
+
Β© 2025 SmartVision AI
|
| 680 |
+
</div>
|
| 681 |
+
""",
|
| 682 |
+
unsafe_allow_html=True
|
| 683 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|