ArchiMathur commited on
Commit
604d721
·
verified ·
1 Parent(s): 6de8922

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +78 -0
main.py CHANGED
@@ -0,0 +1,78 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from ultralytics import YOLO
3
+ import cv2
4
+ import numpy as np
5
+ from PIL import Image
6
+
7
+ # Load the YOLO model (replace with your model path)
8
+ model = YOLO("D:\\streamlit\\my_streamlit_app\\fire\\best.pt")
9
+ # Use your YOLO model file here
10
+
11
+ st.title("Fire Detection in Forest")
12
+
13
+ # Sidebar for input options
14
+ input_option = st.sidebar.selectbox("Select Input Method", ["Upload Image", "Use Webcam"])
15
+
16
+ if input_option == "Upload Image":
17
+ # Upload Image
18
+ uploaded_file = st.file_uploader("Choose an Image", type=["jpg", "jpeg", "png"])
19
+
20
+ if uploaded_file is not None:
21
+ img = Image.open(uploaded_file)
22
+ st.image(img, caption='User Image')
23
+ st.write("Classifying...")
24
+
25
+ # Convert image to numpy array
26
+ img_np = np.array(img)
27
+
28
+ # Make predictions
29
+ results = model.predict(source=img_np, conf=0.5)
30
+
31
+ # Draw bounding boxes on the image
32
+ for result in results:
33
+ boxes = result.boxes.xyxy
34
+ for box in boxes:
35
+ x1, y1, x2, y2 = box[:4].astype(int)
36
+ img_np = cv2.rectangle(img_np, (x1, y1), (x2, y2), (0, 255, 0), 2)
37
+
38
+ # Show the resulting image
39
+ st.image(img_np, caption='Detected Fire', use_column_width=True)
40
+
41
+ elif input_option == "Use Webcam":
42
+ st.write("Starting webcam for live detection...")
43
+
44
+ # Start video capture
45
+ camera = cv2.VideoCapture(0) # 0 is the default camera
46
+
47
+ # Create a placeholder for the video feed
48
+ video_placeholder = st.empty()
49
+
50
+ # Main loop for live detection
51
+ while True:
52
+ ret, frame = camera.read()
53
+ if not ret:
54
+ st.write("Failed to capture image")
55
+ break
56
+
57
+ # Make predictions
58
+ results = model.predict(source=frame, conf=0.5)
59
+
60
+ # Draw bounding boxes on the frame
61
+ for result in results:
62
+ boxes = result.boxes.xyxy
63
+ for box in boxes:
64
+ x1, y1, x2, y2 = box[:4].astype(int)
65
+ frame = cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 2)
66
+
67
+ # Convert frame to RGB
68
+ rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
69
+
70
+ # Display the frame in the Streamlit app
71
+ video_placeholder.image(rgb_frame, channels="RGB", use_column_width=True)
72
+
73
+ # Break loop on user command
74
+ if st.button("Stop Detection"):
75
+ break
76
+
77
+ # Release the camera
78
+ camera.release()