Spaces:
Build error
Build error
File size: 1,862 Bytes
89b5017 53b904a b8c6c95 89b5017 5dff742 6b4e160 e6a1f8f 5dff742 6091386 1cc1ebc 6091386 89b5017 d6c49a8 89b5017 1cc1ebc 6091386 d6c49a8 5dff742 d6c49a8 6091386 b2dec6f d6c49a8 89b5017 b2dec6f 89b5017 d6c49a8 b2dec6f d6c49a8 1cc1ebc 6091386 |
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 |
import torch
import torchvision.transforms as transforms
from torchvision.models.detection.ssd import ssd300_vgg16
import numpy as np
import cv2
import streamlit as st
from PIL import Image
from huggingface_hub import hf_hub_download
# Define dataset classes
classes = ['dengue-regions', 'wet_surface']
num_classes = len(classes) + 1 # Including background
# Load Model
st.title("Real-Time SSD Object Detection")
if 'model' not in st.session_state:
model_path = hf_hub_download(repo_id="DhominickJ/MosqScope", filename="mosquito_model.pth")
model = ssd300_vgg16(pretrained=True) # Multi-box Algorithm
model.load_state_dict(torch.load(model_path, map_location=torch.device("cpu")))
model.eval()
st.session_state.model = model
# Open webcam
cap = cv2.VideoCapture(0)
stframe = st.empty()
transform = transforms.Compose([
transforms.Resize((300, 300)),
transforms.ToTensor()
])
captured_image = st.camera_input("Take a picture")
if captured_image is not None:
# Load image from Streamlit capture
image = Image.open(captured_image)
image = image.convert("RGB")
# Convert image to NumPy for OpenCV processing
image_np = np.array(image)
# Apply transformation for model input
image_tensor = transform(image).unsqueeze(0)
# Run inference
with torch.no_grad():
output = st.session_state.model(image_tensor)[0]
# Draw detections
for box, label in zip(output["boxes"].cpu().numpy(), output["labels"].cpu().numpy()):
x_min, y_min, x_max, y_max = map(int, box)
cv2.rectangle(image_np, (x_min, y_min), (x_max, y_max), (0, 255, 0), 2)
cv2.putText(image_np, classes[label - 1], (x_min, y_min - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2)
# Display frame in Streamlit
stframe.image(image_np, channels="RGB")
cap.release()
cv2.destroyAllWindows()
|