AmirKaseb commited on
Commit
61a8794
·
verified ·
1 Parent(s): fb54041

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -12
app.py CHANGED
@@ -5,8 +5,13 @@ from PIL import Image
5
  import numpy as np
6
  import tempfile
7
 
 
8
  # YOLOv5 Model Loading (best.pt)
9
- model = torch.hub.load('ultralytics/yolov5', 'custom', path='best.pt') # Replace 'best.pt' with your model's path
 
 
 
 
10
 
11
  # Streamlit UI
12
  st.title('YOLOv5 Object Detection')
@@ -19,29 +24,33 @@ if upload_option == "Upload Image":
19
  uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "png", "jpeg"])
20
  if uploaded_file is not None:
21
  image = Image.open(uploaded_file)
22
- results = model(image) # Perform inference
23
- st.image(results.render()[0], caption='Detected Objects', use_column_width=True) # Display results
24
 
25
  # Real-Time Webcam Detection
26
  if upload_option == "Real-Time Webcam":
27
  run = st.checkbox('Run Webcam')
28
- FRAME_WINDOW = st.image([]) # Display window for webcam frames
29
 
30
  if run:
31
- cap = cv2.VideoCapture(0) # Open webcam (0 for default)
 
 
 
 
32
 
33
  while run:
34
- ret, frame = cap.read() # Capture frame
35
  if not ret:
36
- st.write("Error: Unable to capture frame")
37
- break
38
-
39
  # Convert to RGB and detect objects
40
  frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
41
  results = model(frame_rgb)
42
 
 
43
  annotated_frame = results.render()[0]
44
- FRAME_WINDOW.image(annotated_frame)
45
-
46
- cap.release() # Release webcam
47
 
 
 
5
  import numpy as np
6
  import tempfile
7
 
8
+
9
  # YOLOv5 Model Loading (best.pt)
10
+ @st.cache_resource # Cache the model for efficiency
11
+ def load_model():
12
+ return torch.hub.load('ultralytics/yolov5', 'custom', path='best.pt', force_reload=True)
13
+
14
+ model = load_model()
15
 
16
  # Streamlit UI
17
  st.title('YOLOv5 Object Detection')
 
24
  uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "png", "jpeg"])
25
  if uploaded_file is not None:
26
  image = Image.open(uploaded_file)
27
+ results = model(image)
28
+ st.image(results.render()[0], caption='Detected Objects', use_column_width=True)
29
 
30
  # Real-Time Webcam Detection
31
  if upload_option == "Real-Time Webcam":
32
  run = st.checkbox('Run Webcam')
33
+ FRAME_WINDOW = st.image([])
34
 
35
  if run:
36
+ try:
37
+ cap = cv2.VideoCapture(0)
38
+ except Exception as e:
39
+ st.error(f"Error accessing webcam: {e}")
40
+ st.stop()
41
 
42
  while run:
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
+ continue # Skip to the next iteration instead of stopping the loop entirely
47
+
48
  # Convert to RGB and detect objects
49
  frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
50
  results = model(frame_rgb)
51
 
52
+ # Render and display results
53
  annotated_frame = results.render()[0]
54
+ FRAME_WINDOW.image(annotated_frame, channels="BGR") # Display in BGR format
 
 
55
 
56
+ cap.release()