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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -34
app.py CHANGED
@@ -6,51 +6,75 @@ import math
6
 
7
 
8
 
9
- # start webcam
10
- cap = cv2.VideoCapture(0)
11
- cap.set(3, 640)
12
- cap.set(4, 480)
 
 
 
 
13
 
14
  # Load the YOLO model
15
  model = YOLO("best.pt") # Ensure the path to your model is correct
16
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
 
18
- while True:
19
- success, img = cap.read()
20
- results = model(img, stream=True)
21
 
22
- # coordinates
23
- for r in results:
24
- boxes = r.boxes
25
 
26
- for box in boxes:
27
- # bounding box
28
- x1, y1, x2, y2 = box.xyxy[0]
29
- x1, y1, x2, y2 = int(x1), int(y1), int(x2), int(y2) # convert to int values
30
 
31
- # put box in cam
32
- cv2.rectangle(img, (x1, y1), (x2, y2), (255, 0, 255), 3)
 
33
 
34
- # confidence
35
- confidence = math.ceil((box.conf[0]*100))/100
36
- print("Confidence --->",confidence)
37
 
38
- # class name
39
- cls = int(box.cls[0])
40
- print("Class name -->", classNames[cls])
 
 
 
 
41
 
42
- # object details
43
- org = [x1, y1]
44
- font = cv2.FONT_HERSHEY_SIMPLEX
45
- fontScale = 1
46
- color = (255, 0, 0)
47
- thickness = 2
48
 
49
- cv2.putText(img, classNames[cls], org, font, fontScale, color, thickness)
 
50
 
51
- cv2.imshow('Webcam', img)
52
- if cv2.waitKey(1) == ord('q'):
53
- break
54
 
55
- cap.release()
56
- cv2.destroyAllWindows()
 
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()