Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,11 +3,9 @@ import torch
|
|
| 3 |
import cv2
|
| 4 |
from PIL import Image
|
| 5 |
import numpy as np
|
| 6 |
-
import tempfile
|
| 7 |
-
|
| 8 |
|
| 9 |
# YOLOv5 Model Loading (best.pt)
|
| 10 |
-
@st.
|
| 11 |
def load_model():
|
| 12 |
return torch.hub.load('ultralytics/yolov5', 'custom', path='best.pt', force_reload=True)
|
| 13 |
|
|
@@ -43,7 +41,7 @@ if upload_option == "Real-Time Webcam":
|
|
| 43 |
ret, frame = cap.read()
|
| 44 |
if not ret:
|
| 45 |
st.warning("Error: Unable to capture frame. Please check your webcam settings.") # More informative message
|
| 46 |
-
|
| 47 |
|
| 48 |
# Convert to RGB and detect objects
|
| 49 |
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
|
@@ -51,6 +49,6 @@ if upload_option == "Real-Time Webcam":
|
|
| 51 |
|
| 52 |
# Render and display results
|
| 53 |
annotated_frame = results.render()[0]
|
| 54 |
-
FRAME_WINDOW.image(annotated_frame, channels="
|
| 55 |
|
| 56 |
cap.release()
|
|
|
|
| 3 |
import cv2
|
| 4 |
from PIL import Image
|
| 5 |
import numpy as np
|
|
|
|
|
|
|
| 6 |
|
| 7 |
# YOLOv5 Model Loading (best.pt)
|
| 8 |
+
@st.cache(allow_output_mutation=True) # Cache the model for efficiency
|
| 9 |
def load_model():
|
| 10 |
return torch.hub.load('ultralytics/yolov5', 'custom', path='best.pt', force_reload=True)
|
| 11 |
|
|
|
|
| 41 |
ret, frame = cap.read()
|
| 42 |
if not ret:
|
| 43 |
st.warning("Error: Unable to capture frame. Please check your webcam settings.") # More informative message
|
| 44 |
+
break # Exit the loop if frame capture fails
|
| 45 |
|
| 46 |
# Convert to RGB and detect objects
|
| 47 |
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
|
|
|
| 49 |
|
| 50 |
# Render and display results
|
| 51 |
annotated_frame = results.render()[0]
|
| 52 |
+
FRAME_WINDOW.image(annotated_frame, channels="RGB") # Display in RGB format
|
| 53 |
|
| 54 |
cap.release()
|