BrainAI-1 commited on
Commit
810f96b
·
verified ·
1 Parent(s): 3bcb96b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -13
app.py CHANGED
@@ -1,20 +1,38 @@
1
  import streamlit as st
2
  import cv2
3
  import numpy as np
4
- from PIL import Image
5
 
6
- st.set_page_config(page_title="Webcam App", page_icon="📷")
 
7
 
8
- st.title("📷 Streamlit Webcam App")
9
 
10
- # Use Streamlit's built-in camera input
11
- image_file = st.camera_input("Take a picture")
 
 
 
12
 
13
- if image_file:
14
- # Convert image to OpenCV format
15
- img = Image.open(image_file)
16
- img = np.array(img) # Convert to NumPy array
17
-
18
-
19
- # Show the captured image
20
- st.image(img, caption="Captured Image")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import streamlit as st
2
  import cv2
3
  import numpy as np
4
+ from ultralytics import YOLO
5
 
6
+ # Load YOLO model (Download "yolov8n.pt" first if not available)
7
+ model = YOLO("yolov8n.pt")
8
 
9
+ st.title("🎥 YOLO Object Detection Live Stream")
10
 
11
+ # Open Webcam
12
+ cap = cv2.VideoCapture(0)
13
+ if not cap.isOpened():
14
+ st.error("Error: Could not access the webcam.")
15
+ st.stop()
16
 
17
+ stframe = st.empty() # Streamlit placeholder for video frames
18
+
19
+ while True:
20
+ ret, frame = cap.read()
21
+ if not ret:
22
+ st.error("Error: Failed to capture frame.")
23
+ break
24
+
25
+ # Convert frame to RGB (Streamlit expects RGB format)
26
+ frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
27
+
28
+ # Run YOLO object detection
29
+ results = model(frame)
30
+ img_pot = result[0].plot()
31
+
32
+
33
+
34
+ # Show processed frame in Streamlit
35
+ stframe.image(img_pot, channels="RGB")
36
+
37
+ # Release webcam
38
+ cap.release()