File size: 20,435 Bytes
a50c70c 28e446a a50c70c 28e446a a50c70c 28e446a a50c70c 28e446a a50c70c |
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 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 |
import streamlit as st
import pandas as pd
import torch
import torchvision.transforms as transforms
from PIL import Image
import numpy as np
import torchvision.models as models
import torchvision
st.set_page_config(page_title="SmartVision AI - Intelligent Multi-Class Object Recognition System", layout="wide")
st.sidebar.title("π SmartVision AI")
st.sidebar.markdown("---")
st.title("π€ SmartVision AI - Intelligent Multi-Class Object Recognition System")
st.markdown("---")
page = st.sidebar.radio("Go to", ["π Home", "πΌοΈ Image Classification", "π¦ Object Detection", "π Model Performance", "πΈ Live Webcam Detection","βΉοΈ About"])
#------------------------------------------------Home Page----------------------------------------------------------------------------------------
if page == "π Home":
st.subheader("π Project Overview")
st.markdown("""
**SmartVision AI** is an intelligent computer vision system that performs real-time object detection
using a custom-trained **YOLO model**.
The system allows users to upload images and automatically identifies objects by drawing bounding boxes,
class labels, and confidence scores.
The goal of this project is to demonstrate an **end-to-end AI pipeline** β from model training
to optimized inference and visualization.
""")
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.")
st.markdown("---")
st.subheader("π Key Features")
st.markdown("""
β€ π **Accurate Object Detection** using a trained YOLO model
β€ π¦ **Bounding Boxes & Labels** on detected objects
β€ π **Confidence Scores** for every prediction
β€ π§ **Optional CNN-based verification**
β€ β‘ **Optimized CNNs** (VGG16, ResNet50, MobileNetV2, EfficientNet-B0)
""")
st.markdown("---")
st.subheader("π Instructions for Users")
st.text("""
β€ π Navigate to the Detection page
β€ π¦ Upload an image (JPG / PNG format)
β€ π Wait for the model to process the image
β€ π§ View the output image with bounding boxes and labels
β€ β‘ Check confidence scores for each detected object
""")
st.info("β οΈ For best results, use clear images with good lighting and visible objects.")
st.markdown("---")
st.subheader("πΌοΈ Sample Demo Images")
col1, col2 = st.columns(2)
with col1:
st.image("img.png", caption="YOLO Detection Example 1")
with col2:
st.image("img1.png", caption="YOLO Detection Example 2")
#----------------------------------------------------------------------------------------------------------------------------------
# we have already trained these models in the collab and using the state.dict(),after saving .here i am using the path of models
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']
NUM_CLASSES = len(Classes) # 25
import torch
import torch.nn as nn
import torchvision.models as models
# vgg16
@st.cache_resource
def load_custom_vgg16():
model = models.vgg16(pretrained=False)
model.classifier = nn.Sequential(
nn.Linear(25088, 1024),
nn.ReLU(inplace=True),
nn.Dropout(0.5),
nn.Linear(1024, 512),
nn.ReLU(inplace=True),
nn.Dropout(0.5),
nn.Linear(512, 25) # number of classes
)
model.load_state_dict(
torch.load(
"models/vgg16_smartvision.pth",
map_location=torch.device("cpu")
)
)
model.eval()
return model
# RestNet50
@st.cache_resource
def load_custom_restnet50():
model=models.resnet50(pretrained=False)
# CUSTOM CLASSIFICATION HEAD
model.fc = nn.Sequential(
nn.Linear(model.fc.in_features, 512),
nn.BatchNorm1d(512),
nn.ReLU(),
nn.Dropout(0.5),
nn.Linear(512, NUM_CLASSES)
)
model.load_state_dict(
torch.load(
"models/smartvision_resnet50.pth",
map_location=torch.device("cpu")
)
)
model.eval()
return model
# Mobilenet_v2
@st.cache_resource
def load_custom_mobilenetv2():
model=models.mobilenet_v2(pretrained=False)
# CUSTOM CLASSIFICATION HEAD
model.classifier = nn.Sequential(
nn.Linear(1280, 512),
nn.ReLU(),
nn.Dropout(0.4),
nn.Linear(512, NUM_CLASSES)
)
model.load_state_dict(
torch.load(
"models/mobilenetv2_smartvision.pth",
map_location=torch.device("cpu")
)
)
model.eval()
return model
# EffcientNetB0
@st.cache_resource
def load_custom_EffcientNet():
model=models.efficientnet_b0(pretrained=False)
# CUSTOM CLASSIFICATION HEAD
model.classifier = nn.Sequential(
nn.Dropout(0.4),
nn.Linear(1280, 512),
nn.ReLU(),
nn.Dropout(0.3),
nn.Linear(512, NUM_CLASSES)
)
model.load_state_dict(
torch.load(
"models/EfficientNetB0_smartvision.pth",
map_location=torch.device("cpu")
)
)
model.eval()
return model
# Image preprocessing
preprocess = transforms.Compose([
transforms.Resize((224, 224)),
transforms.ToTensor(),
transforms.Normalize(
mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225]
)
])
# ------------------------------------------------------------------------------------------------------------------------------------
if page == "πΌοΈ Image Classification":
st.subheader("πΌοΈ Image Classification (Custom Trained CNN Models)")
st.markdown("""
This page performs **single-object image classification** using multiple
**custom-trained CNN models**.
Predictions from each model are shown **side-by-side** for comparison.
""")
uploaded_file = st.file_uploader(
"π€ Upload an Image",
type=["jpg", "jpeg", "png"]
)
if uploaded_file:
image = Image.open(uploaded_file).convert("RGB")
st.markdown("### π· Uploaded Image")
st.image(image, width=300)
input_tensor = preprocess(image).unsqueeze(0)
# Load all models
models_dict = {
"π§ VGG16": load_custom_vgg16(),
"π§ ResNet50": load_custom_restnet50(),
"π§ MobileNetV2": load_custom_mobilenetv2(),
"π§ EfficientNet-B0": load_custom_EffcientNet()
}
st.markdown("---")
st.markdown("### π Model Predictions (Top-5)")
cols = st.columns(4)
for col, (model_name, model) in zip(cols, models_dict.items()):
with col:
st.markdown(f"#### {model_name}")
with torch.no_grad():
outputs = model(input_tensor)
probs = torch.nn.functional.softmax(outputs[0], dim=0)
top_probs, top_idxs = torch.topk(
probs, min(5, len(Classes))
)
for i in range(len(top_idxs)):
class_name = Classes[top_idxs[i].item()]
confidence = top_probs[i].item()
st.write(
f"**{i+1}. {class_name}** β {confidence*100:.2f}%"
)
st.progress(float(confidence))
else:
st.info("β¬οΈ Upload an image to classify.")
import cv2
import numpy as np
from ultralytics import YOLO
from PIL import Image
import streamlit as st
@st.cache_resource
def load_yolo_model():
return YOLO("best (1).pt") # path to my already trained model
yolo_model = load_yolo_model()
#-------------------------------------------------------------------------------------------------------------------------------------
if page == "π¦ Object Detection":
st.subheader("π― Object Detection using YOLO")
st.markdown("""
Upload an image to detect **multiple objects** using a custom-trained YOLO model.
Bounding boxes, class labels, and confidence scores will be displayed.
""")
st.markdown("---")
# Confidence threshold slider
conf_threshold = st.slider(
"π§ Confidence Threshold",
min_value=0.1,
max_value=1.0,
value=0.5,
step=0.05
)
uploaded_file = st.file_uploader(
"π€ Upload an Image (JPG / PNG)",
type=["jpg", "jpeg", "png"]
)
if uploaded_file:
image = Image.open(uploaded_file).convert("RGB")
img_array = np.array(image)
st.markdown("### π· Uploaded Image")
st.image(image, width=350)
st.markdown("---")
st.markdown("### π Detection Results")
# YOLO inference
results = yolo_model.predict(
source=img_array,
conf=conf_threshold,
save=False
)
annotated_img = img_array.copy()
detections_found = False
for r in results:
boxes = r.boxes
if boxes is not None:
for box in boxes:
detections_found = True
x1, y1, x2, y2 = map(int, box.xyxy[0])
conf = float(box.conf[0])
cls_id = int(box.cls[0])
label = yolo_model.names[cls_id]
# Draw bounding box
cv2.rectangle(
annotated_img,
(x1, y1),
(x2, y2),
(0, 255, 0),
2
)
# Label text
text = f"{label} {conf*100:.2f}%"
cv2.putText(
annotated_img,
text,
(x1, y1 - 10),
cv2.FONT_HERSHEY_SIMPLEX,
0.6,
(0, 255, 0),
2
)
if detections_found:
st.image(
annotated_img,
caption="YOLO Detection Output",
use_column_width=True
)
else:
st.warning("β οΈ No objects detected. Try lowering the confidence threshold.")
else:
st.info("β¬οΈ Upload an image to start object detection.")
# This dashboard compares multiple CNN architectures based on accuracy and inference speed.
# While deeper models like VGG16 perform well during training, lightweight models such as
# MobileNetV2 and EfficientNetB0 offer faster inference, making them suitable for real-time applications.
#----------------------------------Model Performance---------------------------------------------------------------
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
import streamlit as st
# ---------------- MODEL METRICS DATA ----------------
data = {
"Model": ["VGG16", "ResNet50", "MobileNetV2", "EfficientNetB0"],
"Train Accuracy": [0.877, 0.6815, 0.51, 0.5297],
"Val Accuracy": [0.6345, 0.6855, 0.54, 0.56],
"Test Accuracy": [0.633, 0.593, 0.579, 0.543],
"Speed": [8.9, 0.5, 13.0, 12.6] # higher = faster
}
df = pd.DataFrame(data)
# ---------------- PAGE 4: MODEL PERFORMANCE ----------------
if page == "π Model Performance":
st.subheader("π Model Performance Dashboard")
st.markdown("""
This section presents a **comparative analysis** of different CNN models used in SmartVision AI.
It highlights **training, validation, and test accuracy**, along with **relative inference speed**.
""")
st.markdown("---")
# ---------------- MODEL METRICS TABLE ----------------
st.markdown("### π Model Comparison Table")
st.dataframe(df, use_container_width=True)
st.markdown("---")
# ---------------- ACCURACY COMPARISON ----------------
st.markdown("### π Accuracy Comparison (Train / Validation / Test)")
acc_df = df.melt(
id_vars="Model",
value_vars=["Train Accuracy", "Val Accuracy", "Test Accuracy"],
var_name="Dataset",
value_name="Accuracy"
)
fig1, ax1 = plt.subplots()
sns.barplot(
data=acc_df,
x="Model",
y="Accuracy",
hue="Dataset",
ax=ax1
)
ax1.set_ylim(0, 1)
ax1.set_title("Accuracy Comparison Across Models")
ax1.set_ylabel("Accuracy")
ax1.set_xlabel("Model")
st.pyplot(fig1)
st.markdown("---")
# ---------------- INFERENCE SPEED COMPARISON ----------------
st.markdown("### β‘ Inference Speed Comparison")
fig2, ax2 = plt.subplots()
sns.barplot(
data=df,
x="Model",
y="Speed",
ax=ax2
)
ax2.set_title("Relative Inference Speed (Higher is Faster)")
ax2.set_ylabel("Speed Score")
ax2.set_xlabel("Model")
st.pyplot(fig2)
st.markdown("---")
# ---------------- PERFORMANCE INSIGHTS ----------------
st.markdown("### π§ Key Observations")
st.markdown("""
- **VGG16** shows strong training accuracy but noticeable generalization gap
- **ResNet50** provides better validation stability
- **MobileNetV2** and **EfficientNetB0** trade accuracy for faster inference
- Lightweight models are suitable for **real-time or edge deployment**
""")
#----------------------------------------------------Live Camera Detection----------------------------------------------------------------------------------
import cv2
import time
import numpy as np
import streamlit as st
from ultralytics import YOLO
#Loading the pretrained model from YOLO
@st.cache_resource
def load_pretrained_yolo():
return YOLO("yolov8n.pt") # pretrained model
yolo_model_live = load_pretrained_yolo()
if page == "πΈ Live Webcam Detection":
st.subheader("πΈ Live Camera Detection (Lightweight Mode)")
# 0.5 β show only detections above 50% if 0.1 then show only detections above 10%
conf_thres = st.slider("Confidence Threshold", 0.1, 1.0, 0.5, 0.05)
run = st.checkbox("βΆ Start Camera")
FRAME_WINDOW = st.image([])
fps_text = st.empty()
if run:
cap = cv2.VideoCapture(0)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)
# using this for faster optimization
frame_skip = 3 # π₯ process 1 frame out of 3
frame_count = 0
prev_time = time.time()
while run:
ret, frame = cap.read()
if not ret:
break
frame_count += 1
# Skip frames
if frame_count % frame_skip != 0:
continue
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
results = yolo_model_live.predict(
frame_rgb,
conf=conf_thres,
imgsz=416, # π₯ smaller image
verbose=False
)
annotated_frame = results[0].plot()
# FPS
curr_time = time.time()
fps = 1 / (curr_time - prev_time)
prev_time = curr_time
fps_text.markdown(f"β‘ FPS: {fps:.1f}")
FRAME_WINDOW.image(
annotated_frame,
channels="RGB",
use_column_width=True
)
time.sleep(0.03) # π₯ CPU cooldown
cap.release()
if page == "βΉοΈ About":
st.subheader("π About SmartVision AI")
st.markdown("---")
# ---------------- PROJECT OVERVIEW ----------------
st.markdown("## π§ Project Overview")
st.markdown("""
**SmartVision AI** is an end-to-end **computer vision system** designed to perform
**image classification**, **object detection**, and **real-time inference** using
state-of-the-art deep learning models.
The project demonstrates the complete AI lifecycle:
**dataset preparation β model training β optimized inference β deployment using Streamlit**.
""")
# ---------------- DATASET INFO ----------------
st.markdown("## π Dataset Information")
st.markdown("""
- **Image Classification Dataset**
- Domain-specific dataset with **25 object classes**
- Preprocessed and augmented for robustness
- Split into **Train / Validation / Test** sets
- **Object Detection Dataset**
- General object detection using **COCO dataset**
- 80 commonly occurring object classes
- Bounding-box annotated images
""")
# ---------------- MODEL ARCHITECTURES ----------------
st.markdown("## ποΈ Model Architectures Used")
st.markdown("""
### πΉ Image Classification Models
- **VGG16 (Custom Trained)**
- Modified fully connected layers
- High accuracy on domain-specific data
- **ResNet50**
- Residual connections for deeper learning
- Strong generalization capability
- **MobileNetV2**
- Lightweight architecture
- Optimized for speed and mobile devices
- **EfficientNet-B0**
- Balanced accuracy and efficiency
- Compound scaling technique
### πΉ Object Detection Model
- **YOLOv8 (Pretrained)**
- Real-time object detection
- Single-stage detector
- Optimized for speed and accuracy
""")
# ---------------- TECH STACK ----------------
st.markdown("## π οΈ Technical Stack")
st.markdown("""
**Programming Language**
- Python π
**Deep Learning & Vision**
- PyTorch
- Torchvision
- Ultralytics YOLOv8
- OpenCV
**Data Processing & Visualization**
- NumPy
- Pandas
- Matplotlib
- Seaborn
**Web & Deployment**
- Streamlit
- VS Code
- Git & GitHub
""")
# ---------------- OPTIMIZATION ----------------
st.markdown("## β‘ Performance Optimization Techniques")
st.markdown("""
- Model quantization (where applicable)
- Frame skipping for real-time inference
- Resolution scaling for faster detection
- CPU-optimized inference pipeline
- Streamlit resource caching
""")
# ---------------- DEVELOPER INFO ----------------
st.markdown("## π¨βπ» Developer Information")
st.markdown("""
**Developer:** Rahul Kumar
**Degree:** B.Tech in Information Technology
**Institution:** IIEST Shibpur
**Core Interests:**
- Computer Vision
- Deep Learning
- Full Stack Development
- AI Model Deployment
**Project Goal:**
To build scalable, efficient, and production-ready AI systems
with real-world deployment considerations.
""")
# ---------------------------------------FOOTER --------------------------------------------------------------------------------------
st.markdown("---")
st.info("π SmartVision AI β Bridging Deep Learning Research with Real-World Applications")
#-------------------Footer Part in sidebar----------------------------------------------------------------------------------------------
import streamlit as st
st.sidebar.markdown("---")
st.sidebar.markdown("### π SmartVision AI")
col1, col2, col3 = st.sidebar.columns(3)
with col1:
st.sidebar.markdown(
"[π GitHub](https://github.com/rahul-tech-kumar/SmartVision-AI---Intelligent-Multi-Class-Object-Recognition-System)",
unsafe_allow_html=True
)
with col2:
st.sidebar.markdown(
"[πΌ LinkedIn](https://www.linkedin.com/in/rahul-kumar-173546228/)",
unsafe_allow_html=True
)
with col3:
st.sidebar.markdown(
"[βοΈ Email](mailto:rahulkumar11062003@gmail.com)",
unsafe_allow_html=True
)
st.sidebar.markdown("---")
st.sidebar.markdown(
"""
<div style="text-align:center; font-size:12px; color:gray;">
π Built with Streamlit & PyTorch<br>
Β© 2025 SmartVision AI
</div>
""",
unsafe_allow_html=True
)
|