|
|
import streamlit as st
|
|
|
import cv2
|
|
|
import numpy as np
|
|
|
from src.preprocess import preprocess_image, camera_stream, overlay_heatmap
|
|
|
from src.gradcam import GradCAM
|
|
|
import torch
|
|
|
from PIL import Image
|
|
|
import requests
|
|
|
|
|
|
st.title("AutoVision: Real-Time Defect Detection")
|
|
|
|
|
|
|
|
|
@st.cache_resource
|
|
|
def load_models():
|
|
|
gradcam = GradCAM('../models/resnet18_anomaly.pth')
|
|
|
return gradcam
|
|
|
|
|
|
gradcam = load_models()
|
|
|
classes = ['normal', 'crazing', 'inclusion', 'pits', 'pitted_surface', 'rolled-in_scale', 'scratches']
|
|
|
|
|
|
|
|
|
frame_placeholder = st.empty()
|
|
|
prediction_placeholder = st.empty()
|
|
|
|
|
|
|
|
|
use_api = st.checkbox("Use FastAPI Backend for Inference")
|
|
|
|
|
|
for frame in camera_stream():
|
|
|
rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
|
|
|
|
|
|
|
|
input_data = preprocess_image(rgb_frame)
|
|
|
input_tensor = torch.from_numpy(input_data).float()
|
|
|
|
|
|
if use_api:
|
|
|
|
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
with torch.no_grad():
|
|
|
output = gradcam.model(input_tensor)
|
|
|
pred = output.argmax().item()
|
|
|
confidence = torch.softmax(output, dim=1).max().item()
|
|
|
|
|
|
|
|
|
heatmap = gradcam.generate(input_tensor, pred)
|
|
|
|
|
|
|
|
|
overlaid = overlay_heatmap(rgb_frame, heatmap)
|
|
|
|
|
|
frame_placeholder.image(overlaid, channels="RGB")
|
|
|
|
|
|
prediction_placeholder.markdown(f"**Prediction:** {classes[pred]} ({confidence:.2%})")
|
|
|
|
|
|
st.info("Press Ctrl+C to stop camera.") |