ArchiMathur commited on
Commit
a80de8b
·
verified ·
1 Parent(s): 28fc212

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -45
app.py CHANGED
@@ -9,56 +9,32 @@ model = YOLO("best.pt") # Ensure the path to your model is correct
9
  # Set the title of the app
10
  st.title("Live Fire Detection App")
11
 
12
- # Create a placeholder for the video stream
13
- video_placeholder = st.empty()
 
14
 
15
- # Function to perform live fire detection
16
- def detect_fire():
17
- # Open a connection to the webcam (0 is the default camera)
18
- cap = cv2.VideoCapture(0)
19
 
20
- while True:
21
- ret, frame = cap.read()
22
- if not ret:
23
- st.error("Failed to capture video.")
24
- break
25
 
26
- # Convert the frame to RGB (as YOLO expects RGB input)
27
- rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
28
 
29
- # Make predictions on the current frame
30
- results = model.predict(source=rgb_frame, conf=0.5)
31
 
32
- # Draw bounding boxes on the frame
33
- fire_detected = False
34
- for result in results:
35
- boxes = result.boxes.xyxy # Bounding boxes
36
- for box in boxes:
37
- x1, y1, x2, y2 = box[:4].astype(int)
38
- rgb_frame = cv2.rectangle(rgb_frame, (x1, y1), (x2, y2), (0, 255, 0), 2)
39
 
40
- # Check if the detected class is "fire" (adjust based on your model's class mapping)
41
- class_id = int(box[5]) # Assuming class ID is at the 6th position
42
- if class_id == 0: # Replace 0 with the actual class ID for fire if different
43
- fire_detected = True
44
 
45
- # Display the frame in the Streamlit app
46
- video_placeholder.image(rgb_frame, channels="RGB", use_column_width=True)
 
47
 
48
- # Display message based on fire detection
49
- if fire_detected:
50
- st.success("🔥 Fire Detected!")
51
- else:
52
- st.warning("No Fire Detected.")
53
-
54
- # Break the loop if the user presses 'q'
55
- if cv2.waitKey(1) & 0xFF == ord('q'):
56
- break
57
-
58
- # Release the webcam and close windows
59
- cap.release()
60
- cv2.destroyAllWindows()
61
-
62
- # Start the detection process
63
- if st.button("Start Live Detection"):
64
- detect_fire()
 
9
  # Set the title of the app
10
  st.title("Live Fire Detection App")
11
 
12
+ # Define color ranges for fire detection
13
+ lower_red = np.array([0, 100, 100])
14
+ upper_red = np.array([10, 255, 255])
15
 
16
+ # Create a video capture object
17
+ cap = cv2.VideoCapture(0) # 0 for default camera, change as needed
 
 
18
 
19
+ while True:
20
+ ret, frame = cap.read()
 
 
 
21
 
22
+ # Convert BGR to HSV color space
23
+ hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
24
 
25
+ # Create a mask for the fire color range
26
+ mask = cv2.inRange(hsv, lower_red, upper_red)
27
 
28
+ # Apply the mask to the original frame
29
+ res = cv2.bitwise_and(frame, frame, mask=mask)
 
 
 
 
 
30
 
31
+ # Display the resulting frame
32
+ cv2.imshow('Fire Detection', res)
 
 
33
 
34
+ # Exit if 'q' is pressed
35
+ if cv2.waitKey(1) & 0xFF == ord('q'):
36
+ break
37
 
38
+ # Release the capture and close windows
39
+ cap.release()
40
+ cv2.destroyAllWindows() 1