Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,11 +5,7 @@ from PIL import Image
|
|
| 5 |
import numpy as np
|
| 6 |
|
| 7 |
# YOLOv5 Model Loading (best.pt)
|
| 8 |
-
|
| 9 |
-
def load_model():
|
| 10 |
-
return torch.hub.load('ultralytics/yolov5', 'custom', path='best.pt', force_reload=True)
|
| 11 |
-
|
| 12 |
-
model = load_model()
|
| 13 |
|
| 14 |
# Streamlit UI
|
| 15 |
st.title('YOLOv5 Object Detection')
|
|
@@ -22,39 +18,32 @@ if upload_option == "Upload Image":
|
|
| 22 |
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "png", "jpeg"])
|
| 23 |
if uploaded_file is not None:
|
| 24 |
image = Image.open(uploaded_file)
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
# Convert numpy array to PyTorch tensor
|
| 28 |
-
image_tensor = torch.from_numpy(image_np)
|
| 29 |
-
results = model(image_tensor)
|
| 30 |
-
st.image(results.render()[0], caption='Detected Objects', use_column_width=True)
|
| 31 |
|
| 32 |
# Real-Time Webcam Detection
|
| 33 |
if upload_option == "Real-Time Webcam":
|
| 34 |
run = st.checkbox('Run Webcam')
|
| 35 |
-
FRAME_WINDOW = st.image([])
|
| 36 |
|
| 37 |
if run:
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
st.
|
| 42 |
st.stop()
|
| 43 |
|
| 44 |
while run:
|
| 45 |
-
ret, frame = cap.read()
|
| 46 |
if not ret:
|
| 47 |
-
st.
|
| 48 |
-
break
|
| 49 |
|
| 50 |
# Convert to RGB and detect objects
|
| 51 |
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
| 52 |
-
|
| 53 |
-
frame_tensor = torch.from_numpy(frame_rgb)
|
| 54 |
-
results = model(frame_tensor)
|
| 55 |
|
| 56 |
-
# Render and display results
|
| 57 |
annotated_frame = results.render()[0]
|
| 58 |
-
FRAME_WINDOW.image(annotated_frame
|
| 59 |
|
| 60 |
-
cap.release()
|
|
|
|
| 5 |
import numpy as np
|
| 6 |
|
| 7 |
# YOLOv5 Model Loading (best.pt)
|
| 8 |
+
model = torch.hub.load('ultralytics/yolov5', 'custom', path='best.pt') # Replace 'best.pt' with your model's path
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
# Streamlit UI
|
| 11 |
st.title('YOLOv5 Object Detection')
|
|
|
|
| 18 |
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "png", "jpeg"])
|
| 19 |
if uploaded_file is not None:
|
| 20 |
image = Image.open(uploaded_file)
|
| 21 |
+
results = model(image) # Perform inference
|
| 22 |
+
st.image(results.render()[0], caption='Detected Objects', use_column_width=True) # Display results
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
# Real-Time Webcam Detection
|
| 25 |
if upload_option == "Real-Time Webcam":
|
| 26 |
run = st.checkbox('Run Webcam')
|
| 27 |
+
FRAME_WINDOW = st.image([]) # Display window for webcam frames
|
| 28 |
|
| 29 |
if run:
|
| 30 |
+
cap = cv2.VideoCapture(0) # Open webcam (0 for default)
|
| 31 |
+
|
| 32 |
+
if not cap.isOpened():
|
| 33 |
+
st.write("Error: Unable to open webcam")
|
| 34 |
st.stop()
|
| 35 |
|
| 36 |
while run:
|
| 37 |
+
ret, frame = cap.read() # Capture frame
|
| 38 |
if not ret:
|
| 39 |
+
st.write("Error: Unable to capture frame")
|
| 40 |
+
break
|
| 41 |
|
| 42 |
# Convert to RGB and detect objects
|
| 43 |
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
| 44 |
+
results = model(frame_rgb)
|
|
|
|
|
|
|
| 45 |
|
|
|
|
| 46 |
annotated_frame = results.render()[0]
|
| 47 |
+
FRAME_WINDOW.image(annotated_frame)
|
| 48 |
|
| 49 |
+
cap.release() # Release webcam
|