ArchiMathur commited on
Commit
26d18d4
·
verified ·
1 Parent(s): d4e04eb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -66
app.py CHANGED
@@ -2,79 +2,26 @@ import streamlit as st
2
  import cv2
3
  import numpy as np
4
  from ultralytics import YOLO
5
- import math
6
 
7
 
8
-
9
-
10
-
11
-
12
-
13
-
14
- # Define fire color range (adjust based on your dataset)
15
- lower_red = np.array([0, 100, 100])
16
- upper_red = np.array([10, 255, 255])
17
-
18
  # Load the YOLO model
19
  model = YOLO("best.pt") # Ensure the path to your model is correct
20
 
21
- def detect_fire(frame):
22
- """Detects fire in an image using YOLOv8 and color thresholding.
23
-
24
- Args:
25
- frame: The image to be processed (OpenCV BGR format).
26
-
27
- Returns:
28
- bool: True if fire is detected, False otherwise.
29
- """
30
-
31
- # Convert image to RGB format for YOLOv8 compatibility
32
- image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
33
-
34
- # Make predictions using YOLOv8 (modify class ID if needed)
35
- results = model(image)
36
-
37
- # Check for fire detections or color thresholding
38
- fire_detected = False
39
- for detection in results.pandas().xyxy[0]: # Assuming single image inference
40
- class_id = int(detection['name']) # Get class ID (adjust based on your dataset)
41
- if (class_id == 0 or # Check for fire class (adjust for your model)
42
- cv2.inRange(cv2.cvtColor(frame, cv2.COLOR_BGR2HSV), lower_red, upper_red).any()): # Check for fire color range
43
- fire_detected = True
44
- break # Stop iterating after finding fire
45
-
46
- return fire_detected
47
-
48
- def main():
49
- """Captures video from webcam, detects fire, and displays results."""
50
-
51
- cap = cv2.VideoCapture(0) # 0 for default camera
52
-
53
- while True:
54
- ret, frame = cap.read()
55
-
56
- if not ret:
57
- print("Error: Could not read frame.")
58
- break
59
-
60
- # Detect fire
61
- fire_detected = detect_fire(frame)
62
 
63
- # Display results (modify text/color as needed)
64
- if fire_detected:
65
- cv2.putText(frame, "Fire Detected!", (10, 30), cv2.FONT_HERSHEY_SIMPLEX,
66
- 1, (0, 0, 255), 2, cv2.LINE_AA)
67
- else:
68
- cv2.putText(frame, "No Fire Detected", (10, 30), cv2.FONT_HERSHEY_SIMPLEX,
69
- 1, (0, 255, 0), 2, cv2.LINE_AA)
70
 
71
- cv2.imshow('Fire Detection', frame)
 
72
 
73
- if cv2.waitKey(1) & 0xFF == ord('q'):
74
- break
75
 
76
- cap.release()
77
- cv2.destroyAllWindows()
78
 
79
- if __name__ == '__main__':
80
- main()
 
2
  import cv2
3
  import numpy as np
4
  from ultralytics import YOLO
 
5
 
6
 
 
 
 
 
 
 
 
 
 
 
7
  # Load the YOLO model
8
  model = YOLO("best.pt") # Ensure the path to your model is correct
9
 
10
+ webcamera = cv2.VideoCapture(0)
11
+ # webcamera.set(cv2.CAP_PROP_FRAME_WIDTH, 1920)
12
+ # webcamera.set(cv2.CAP_PROP_FRAME_HEIGHT, 1080)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
 
14
+ while True:
15
+ success, frame = webcamera.read()
16
+
17
+ results = model.track(frame, classes=0, conf=0.8, imgsz=480)
18
+ cv2.putText(frame, f"Total: {len(results[0].boxes)}", (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2, cv2.LINE_AA)
19
+ cv2.imshow("Live Camera", results[0].plot())
 
20
 
21
+ if cv2.waitKey(1) == ord('q'):
22
+ break
23
 
24
+ webcamera.release()
25
+ cv2.destroyAllWindows()
26
 
 
 
27