Spaces:
Build error
Build error
Commit
·
64d2956
1
Parent(s):
98757fa
Initial implementation of MosqScope
Browse files
app.py
CHANGED
|
@@ -7,27 +7,19 @@ import streamlit as st
|
|
| 7 |
from PIL import Image
|
| 8 |
from huggingface_hub import hf_hub_download
|
| 9 |
|
| 10 |
-
# Configure Streamlit UI
|
| 11 |
-
st.title("Mosquito Detection from Camera Capture")
|
| 12 |
-
st.write("Take a picture to detect mosquito breeding sites using SSD.")
|
| 13 |
-
|
| 14 |
# Define dataset classes
|
| 15 |
classes = ['dengue-regions', 'wet_surface']
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
st.error(f"Error loading model: {str(e)}")
|
| 28 |
-
return None
|
| 29 |
-
|
| 30 |
-
model = load_model()
|
| 31 |
|
| 32 |
# Capture Image from Camera
|
| 33 |
captured_image = st.camera_input("Take a picture")
|
|
@@ -43,28 +35,21 @@ if captured_image is not None:
|
|
| 43 |
])
|
| 44 |
image_tensor = transform(image).unsqueeze(0)
|
| 45 |
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
cv2.putText(image_np, f"{label_name} {score:.2f}", (x_min, y_min - 5),
|
| 65 |
-
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2)
|
| 66 |
-
|
| 67 |
-
# Convert image back to RGB for Streamlit display
|
| 68 |
-
st.image(cv2.cvtColor(image_np, cv2.COLOR_BGR2RGB), caption="Detected Objects", use_column_width=True)
|
| 69 |
-
else:
|
| 70 |
-
st.warning("Model not loaded. Unable to process image.")
|
|
|
|
| 7 |
from PIL import Image
|
| 8 |
from huggingface_hub import hf_hub_download
|
| 9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
# Define dataset classes
|
| 11 |
classes = ['dengue-regions', 'wet_surface']
|
| 12 |
+
num_classes = len(classes) + 1 # Including background
|
| 13 |
+
|
| 14 |
+
# Load Model
|
| 15 |
+
st.title("Real-Time SSD Object Detection")
|
| 16 |
+
if 'model' not in st.session_state:
|
| 17 |
+
model_path = hf_hub_download(repo_id="DhominickJ/MosqScope", filename="mosquito_model.pth")
|
| 18 |
+
model = ssd300_vgg16(pretrained=True) # Multi-box Algorithm
|
| 19 |
+
model.load_state_dict(torch.load(model_path, map_location=torch.device("cpu")))
|
| 20 |
+
# model.load_state_dict(torch.load("./mosquito_model.pth", map_location=torch.device('cpu')))
|
| 21 |
+
model.eval()
|
| 22 |
+
st.session_state.model = model
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
# Capture Image from Camera
|
| 25 |
captured_image = st.camera_input("Take a picture")
|
|
|
|
| 35 |
])
|
| 36 |
image_tensor = transform(image).unsqueeze(0)
|
| 37 |
|
| 38 |
+
# Convert frame for model
|
| 39 |
+
image = Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
|
| 40 |
+
image_tensor = transform(image).unsqueeze(0)
|
| 41 |
+
|
| 42 |
+
with torch.no_grad():
|
| 43 |
+
output = st.session_state.model(image_tensor)[0]
|
| 44 |
+
|
| 45 |
+
# Draw detections
|
| 46 |
+
for box, label in zip(output["boxes"].cpu().numpy(), output["labels"].cpu().numpy()):
|
| 47 |
+
x_min, y_min, x_max, y_max = map(int, box)
|
| 48 |
+
cv2.rectangle(frame, (x_min, y_min), (x_max, y_max), (0, 255, 0), 2)
|
| 49 |
+
cv2.putText(frame, classes[label - 1], (x_min, y_min - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2)
|
| 50 |
+
|
| 51 |
+
# Display frame
|
| 52 |
+
stframe.image(frame, channels="BGR")
|
| 53 |
+
|
| 54 |
+
cap.release()
|
| 55 |
+
cv2.destroyAllWindows()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|