AmirKaseb commited on
Commit
d91b0f6
·
verified ·
1 Parent(s): 6526cf1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -25
app.py CHANGED
@@ -5,11 +5,7 @@ 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
-
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
- # Convert PIL image to numpy array
26
- image_np = np.array(image)
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
- try:
39
- cap = cv2.VideoCapture(0)
40
- except Exception as e:
41
- st.error(f"Error accessing webcam: {e}")
42
  st.stop()
43
 
44
  while run:
45
- ret, frame = cap.read()
46
  if not ret:
47
- st.warning("Error: Unable to capture frame. Please check your webcam settings.") # More informative message
48
- break # Exit the loop if frame capture fails
49
 
50
  # Convert to RGB and detect objects
51
  frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
52
- # Convert numpy array to PyTorch tensor
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, channels="RGB") # Display in RGB format
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