Spaces:
Build error
Build error
Upload 2 files
Browse files- 350epochs.pt +3 -0
- yolostreamlit.py +59 -0
350epochs.pt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:d74957f928358205257cad2d295ef9bbee66a4db9678765e9db5a3a567927ca2
|
3 |
+
size 22518553
|
yolostreamlit.py
ADDED
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import cv2
|
2 |
+
import streamlit as st
|
3 |
+
from PIL import Image
|
4 |
+
from ultralytics import YOLO
|
5 |
+
import tempfile
|
6 |
+
import os
|
7 |
+
import time
|
8 |
+
|
9 |
+
def main():
|
10 |
+
st.title("Gun Detection")
|
11 |
+
|
12 |
+
video_source_option = st.radio("Select Video Source:", ("Video File", "RTSP Stream", "Webcam"))
|
13 |
+
|
14 |
+
if video_source_option == "Video File":
|
15 |
+
video_file = st.file_uploader("Upload Video", type=["mp4", "avi"])
|
16 |
+
if video_file is not None:
|
17 |
+
# Create a temporary file to store the uploaded video
|
18 |
+
with tempfile.NamedTemporaryFile(delete=False) as temp_file:
|
19 |
+
temp_file.write(video_file.read())
|
20 |
+
temp_file_path = temp_file.name
|
21 |
+
detect_objects(temp_file_path)
|
22 |
+
# Remove the temporary file after processing
|
23 |
+
os.unlink(temp_file_path)
|
24 |
+
|
25 |
+
elif video_source_option == "RTSP Stream":
|
26 |
+
rtsp_link = st.text_input("Enter RTSP Link:")
|
27 |
+
if st.button("Start Detection"):
|
28 |
+
detect_objects(rtsp_link)
|
29 |
+
|
30 |
+
elif video_source_option == "Webcam":
|
31 |
+
detect_objects(0)
|
32 |
+
|
33 |
+
def detect_objects(video_source):
|
34 |
+
yolo_model = YOLO('300epochs.pt')
|
35 |
+
cap = cv2.VideoCapture(video_source)
|
36 |
+
placeholder = st.empty()
|
37 |
+
while cap.isOpened():
|
38 |
+
ret, frame = cap.read()
|
39 |
+
if not ret:
|
40 |
+
break
|
41 |
+
|
42 |
+
# Get predictions
|
43 |
+
results = yolo_model(frame)
|
44 |
+
|
45 |
+
# Draw bounding boxes and labels on the frame
|
46 |
+
annotated_frame = results[0].plot()
|
47 |
+
|
48 |
+
# Convert the annotated frame to RGB (Streamlit uses RGB)
|
49 |
+
annotated_frame_rgb = cv2.cvtColor(annotated_frame, cv2.COLOR_BGR2RGB)
|
50 |
+
|
51 |
+
# Convert the frame to Image
|
52 |
+
img_pil = Image.fromarray(annotated_frame_rgb)
|
53 |
+
|
54 |
+
# Display the frame
|
55 |
+
placeholder.image(img_pil, use_column_width=True)
|
56 |
+
time.sleep(0.1)
|
57 |
+
|
58 |
+
if __name__ == '__main__':
|
59 |
+
main()
|