File size: 6,056 Bytes
6bc77b4
 
 
 
 
604d721
6bc77b4
 
604d721
 
cc0ab26
 
604d721
cc0ab26
604d721
cc0ab26
 
604d721
cc0ab26
 
 
604d721
cc0ab26
 
 
 
0aa7a04
cc0ab26
 
0aa7a04
cc0ab26
 
0aa7a04
cc0ab26
 
0aa7a04
cc0ab26
 
 
 
 
 
604d721
cc0ab26
 
 
 
 
 
 
 
 
 
 
 
 
0aa7a04
 
f5a6f5b
 
604d721
f5a6f5b
 
604d721
f5a6f5b
 
604d721
f5a6f5b
 
 
 
 
 
604d721
f5a6f5b
 
604d721
f5a6f5b
 
 
 
 
 
604d721
f5a6f5b
 
604d721
f5a6f5b
 
604d721
f5a6f5b
 
 
604d721
f5a6f5b
 
46df183
b0b72e4
468bc78
 
b0b72e4
f5a6f5b
 
 
 
 
 
 
46df183
f5a6f5b
 
b0b72e4
f5a6f5b
 
46df183
f5a6f5b
 
 
46df183
f5a6f5b
 
 
 
 
46df183
f5a6f5b
 
c1f7bbb
2dcc0c7
46df183
f5a6f5b
 
 
 
 
 
 
46df183
f5a6f5b
 
46df183
f5a6f5b
 
46df183
f5a6f5b
 
 
 
 
b0b72e4
f5a6f5b
 
46df183
6bc77b4
421a39a
 
 
 
 
6bc77b4
 
8551175
 
421a39a
 
 
8551175
 
6bc77b4
8551175
 
 
 
6bc77b4
8551175
 
6bc77b4
8551175
 
6bc77b4
8551175
 
421a39a
8551175
 
 
 
 
421a39a
6bc77b4
46df183
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# import streamlit as st
# from ultralytics import YOLO
# import cv2
# import numpy as np
# from PIL import Image

# model = YOLO("best.pt") 
# model.predict(source=0,imgsize=640,conf=0.6,show=True)


# # Load the YOLO model (replace with your model path)
# model = YOLO("best.pt")  # Use your YOLO model file here

# st.title("Fire Detection in Forest")

# # Sidebar for input options
# input_option = st.sidebar.selectbox("Select Input Method", ["Upload Image", "Use Webcam", "Upload Video"])

# if input_option == "Upload Image":
#     # Upload Image
#     uploaded_file = st.file_uploader("Choose an Image", type=["jpg", "jpeg", "png"])

#     if uploaded_file is not None:
#         img = Image.open(uploaded_file)
#         st.image(img, caption='User Image')
#         st.write("Classifying...")

#         # Convert image to numpy array
#         img_np = np.array(img)

#         # Make predictions
#         results = model.predict(source=img_np, conf=0.5)

#         # Variable to check if fire is detected
#         fire_detected = False

#         # Draw bounding boxes on the image
#         for result in results:
#             boxes = result.boxes.xyxy
#             for box in boxes:
#                 x1, y1, x2, y2 = box[:4].astype(int)
#                 img_np = cv2.rectangle(img_np, (x1, y1), (x2, y2), (0, 255, 0), 2)

#                 # Check if the detected class is "fire" (adjust based on your model's class mapping)
#                 class_id = int(box[5])  # Assuming class ID is at the 6th position
#                 if class_id == 0:  # Replace 0 with the actual class ID for fire if different
#                     fire_detected = True

#         # Show the resulting image
#         st.image(img_np, caption='Detected Fire', use_column_width=True)

#         # Display message based on fire detection
#         if fire_detected:
#             st.success("🔥 Fire Detected!")
#         else:
#             st.warning("No Fire Detected.")


# elif input_option == "Use Webcam":
#     st.write("Starting webcam for live detection...")
    
#     # Start video capture
#     camera = cv2.VideoCapture(0)  # 0 is the default camera

#     # Create a placeholder for the video feed
#     video_placeholder = st.empty()

#     # Main loop for live detection
#     while True:
#         ret, frame = camera.read()
#         if not ret:
#             st.write("Failed to capture image")
#             break

#         # Make predictions
#         results = model.predict(source=frame, conf=0.5)

#         # Draw bounding boxes on the frame
#         for result in results:
#             boxes = result.boxes.xyxy
#             for box in boxes:
#                 x1, y1, x2, y2 = box[:4].astype(int)
#                 frame = cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 2)

#         # Convert frame to RGB
#         rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)

#         # Display the frame in the Streamlit app
#         video_placeholder.image(rgb_frame, channels="RGB", use_column_width=True)

#         # Break loop on user command
#         if st.button("Stop Detection"):
#             break

#     # Release the camera
#     camera.release()





# elif input_option == "Upload Video":
#     uploaded_video = st.file_uploader("Choose a video", type=["mp4", "avi", "mov", "mkv"])
#     if uploaded_video is not None:
#         # Save the uploaded video temporarily
#         temp_video_path = "temp_video.mp4"
#         with open(temp_video_path, "wb") as f:
#             f.write(uploaded_video.read())

#         # Display the uploaded video
#         st.video(temp_video_path)

#         # Open the video file
#         video_capture = cv2.VideoCapture(temp_video_path)

#         # Create a placeholder for video frame processing
#         video_frame_placeholder = st.empty()
#         fire_detected = False

#         # Loop through video frames
#         while video_capture.isOpened():
#             ret, frame = video_capture.read()
#             if not ret:
#                 break

#             # Make predictions using your fire detection model
#             results = model.predict(source=frame, conf=0.5)
            
                

#             # Draw bounding boxes on the frame if fire is detected
#             for result in results:
#                 boxes = result.boxes.xyxy
#                 for box in boxes:
#                     x1, y1, x2, y2 = box[:4].astype(int)
#                     frame = cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 2)
#                     fire_detected = True  # Set fire_detected flag if a bounding box is found

#             # Convert the frame to RGB format
#             rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)

#             # Display the processed frame
#             video_frame_placeholder.image(rgb_frame, channels="RGB", use_column_width=True)

#         # Display detection result
#         if fire_detected:
#             st.write("Fire detected in the video.")
#         else:
#             st.write("No fire detected in the video.")

#         # Release the video capture
#         video_capture.release()




import streamlit as st
from ultralytics import YOLO
import numpy as np
from PIL import Image

# Load the YOLO model (use the path to your 'best.pt' file)
model = YOLO("best.pt")

st.title("Fire Detection in Forest")

# Upload Image
uploaded_file = st.file_uploader("Upload an Image", type=["jpg", "jpeg", "png"])

if uploaded_file is not None:
    # Open the uploaded image
    img = Image.open(uploaded_file)
    st.image(img, caption="Uploaded Image", use_column_width=True)

    # Convert image to a numpy array
    img_np = np.array(img)

    # Make predictions
    results = model.predict(source=img_np, imgsz=640, conf=0.5)

    # Check if fire is detected
    fire_detected = any("fire" in results.names[int(cls)] for cls in results[0].boxes.cls)

    # Display results
    if fire_detected:
        st.success("🔥 Fire Detected!")
    else:
        st.warning("No Fire Detected.")