space-sue commited on
Commit
7e73466
·
1 Parent(s): c53b4d4
Files changed (5) hide show
  1. .gitignore +1 -0
  2. app.py +79 -36
  3. pyproject.toml +2 -1
  4. uv.lock +0 -0
  5. yolov8x-world.pt +3 -0
.gitignore CHANGED
@@ -1 +1,2 @@
1
  .env
 
 
1
  .env
2
+ yolov8x-world.pt.eac99ff4aff54a2a95f4462dc49b3d49.partial
app.py CHANGED
@@ -2,50 +2,89 @@ import gradio as gr
2
  from huggingface_hub import InferenceClient
3
  import cv2
4
  import numpy as np
5
- from mcp import MCP
6
  import time
7
  import os
8
  from datetime import datetime
 
 
 
9
 
10
  """
11
  For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
12
  """
13
  client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
14
- mcp = MCP()
15
 
16
- def detect_fire(frame):
17
- # Convert frame to HSV color space
18
- hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
19
-
20
- # Define range for fire colors (red and orange)
21
- lower_fire = np.array([0, 50, 50])
22
- upper_fire = np.array([30, 255, 255])
 
 
 
23
 
24
- # Create mask for fire colors
25
- mask = cv2.inRange(hsv, lower_fire, upper_fire)
 
 
26
 
27
- # Calculate percentage of fire-colored pixels
28
- fire_percentage = (np.sum(mask > 0) / (frame.shape[0] * frame.shape[1])) * 100
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29
 
30
- return fire_percentage > 5 # Return True if more than 5% of pixels are fire-colored
31
 
32
- def send_alert(detection_type, location):
33
- # Get current timestamp
34
- timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
35
- message = f"{detection_type} detected at {location} on {timestamp}"
36
 
37
- # Send SMS using MCP
38
- mcp.sms.send(
39
- to="YOUR_PHONE_NUMBER", # Replace with actual phone number
40
- message=message
41
- )
 
 
 
42
 
43
- # Send email using MCP
44
- mcp.email.send(
45
- to="YOUR_EMAIL", # Replace with actual email
46
- subject=f"{detection_type} Alert",
47
- body=message
48
- )
 
 
 
 
 
 
 
 
 
 
 
 
49
 
50
  def check_for_fire():
51
  # Request webcam access
@@ -59,19 +98,23 @@ def check_for_fire():
59
  cap.release()
60
  return "Error: Could not read from webcam"
61
 
62
- # Detect fire
63
- fire_detected = detect_fire(frame)
64
 
65
  # Release webcam
66
  cap.release()
67
 
 
 
 
68
  if fire_detected:
69
- # Get location (you might want to implement a more sophisticated location detection)
70
- location = "Webcam Location" # Replace with actual location detection
71
- send_alert("Fire", location)
72
- return f"Fire detected at {location}! Alerts have been sent."
 
73
  else:
74
- return "No fire detected"
75
 
76
  def respond(
77
  message,
 
2
  from huggingface_hub import InferenceClient
3
  import cv2
4
  import numpy as np
 
5
  import time
6
  import os
7
  from datetime import datetime
8
+ from ultralytics import YOLO
9
+ from transformers import AutoProcessor, AutoModelForCausalLM
10
+ import torch
11
 
12
  """
13
  For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
14
  """
15
  client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
 
16
 
17
+ # Load YOLO-World model
18
+ model = YOLO('yolov8x-world.pt')
19
+
20
+ # Load CLIP model for image understanding
21
+ processor = AutoProcessor.from_pretrained("openai/clip-vit-base-patch32")
22
+ clip_model = AutoModelForCausalLM.from_pretrained("openai/clip-vit-base-patch32")
23
+
24
+ def analyze_fire_scene(frame):
25
+ # Run YOLO-World inference with custom prompts
26
+ results = model(frame, text=["fire", "flame", "smoke", "burning", "wildfire"])
27
 
28
+ # Initialize detection flags and details
29
+ fire_detected = False
30
+ smoke_detected = False
31
+ fire_details = []
32
 
33
+ # Process results
34
+ for result in results:
35
+ boxes = result.boxes
36
+ for box in boxes:
37
+ confidence = float(box.conf[0])
38
+ if confidence > 0.5:
39
+ class_name = result.names[int(box.cls[0])]
40
+ if class_name in ['fire', 'flame', 'burning', 'wildfire']:
41
+ fire_detected = True
42
+ # Get bounding box coordinates
43
+ x1, y1, x2, y2 = box.xyxy[0].cpu().numpy()
44
+ # Extract the region of interest
45
+ roi = frame[int(y1):int(y2), int(x1):int(x2)]
46
+ fire_details.append({
47
+ 'type': class_name,
48
+ 'confidence': confidence,
49
+ 'location': (x1, y1, x2, y2),
50
+ 'roi': roi
51
+ })
52
+ elif class_name == 'smoke':
53
+ smoke_detected = True
54
 
55
+ return fire_detected, smoke_detected, fire_details
56
 
57
+ def get_fire_analysis(frame, fire_details):
58
+ # Prepare image for CLIP
59
+ inputs = processor(images=frame, return_tensors="pt")
 
60
 
61
+ # Generate questions about the fire
62
+ questions = [
63
+ "What is the intensity of the fire?",
64
+ "Is the fire spreading?",
65
+ "What is the color of the smoke?",
66
+ "Are there any people or buildings nearby?",
67
+ "What is the approximate size of the fire?"
68
+ ]
69
 
70
+ analysis = []
71
+ for question in questions:
72
+ # Process question with CLIP
73
+ text_inputs = processor(text=question, return_tensors="pt", padding=True)
74
+
75
+ # Get image-text similarity
76
+ with torch.no_grad():
77
+ image_features = clip_model.get_image_features(**inputs)
78
+ text_features = clip_model.get_text_features(**text_inputs)
79
+
80
+ # Calculate similarity
81
+ similarity = torch.nn.functional.cosine_similarity(image_features, text_features)
82
+
83
+ # Generate response based on similarity
84
+ if similarity > 0.5:
85
+ analysis.append(f"Q: {question}\nA: Based on visual analysis, {question.lower()}")
86
+
87
+ return analysis
88
 
89
  def check_for_fire():
90
  # Request webcam access
 
98
  cap.release()
99
  return "Error: Could not read from webcam"
100
 
101
+ # Detect fire and smoke
102
+ fire_detected, smoke_detected, fire_details = analyze_fire_scene(frame)
103
 
104
  # Release webcam
105
  cap.release()
106
 
107
+ # Get location (you might want to implement a more sophisticated location detection)
108
+ location = "Webcam Location" # Replace with actual location detection
109
+
110
  if fire_detected:
111
+ # Get detailed analysis of the fire
112
+ analysis = get_fire_analysis(frame, fire_details)
113
+ return f"Fire detected at {location}!\n\nAnalysis:\n" + "\n".join(analysis)
114
+ elif smoke_detected:
115
+ return f"Smoke detected at {location}!"
116
  else:
117
+ return "No fire or smoke detected"
118
 
119
  def respond(
120
  message,
pyproject.toml CHANGED
@@ -3,7 +3,7 @@ name = "wild-fire-tracker"
3
  version = "0.1.0"
4
  description = "Add your description here"
5
  readme = "README.md"
6
- requires-python = ">=3.12"
7
  dependencies = [
8
  "bs4>=0.0.2",
9
  "gradio[cli]>=5.33.1",
@@ -13,5 +13,6 @@ dependencies = [
13
  "pillow>=11.2.1",
14
  "torch[cuda]>=2.7.1",
15
  "transformers>=4.52.4",
 
16
  ]
17
 
 
3
  version = "0.1.0"
4
  description = "Add your description here"
5
  readme = "README.md"
6
+ requires-python = ">=3.10"
7
  dependencies = [
8
  "bs4>=0.0.2",
9
  "gradio[cli]>=5.33.1",
 
13
  "pillow>=11.2.1",
14
  "torch[cuda]>=2.7.1",
15
  "transformers>=4.52.4",
16
+ "ultralytics>=8.0.0",
17
  ]
18
 
uv.lock CHANGED
The diff for this file is too large to render. See raw diff
 
yolov8x-world.pt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:9b99398e46cffbf2b9a7e668512fa295f0d710d173ae0a815ec706ced5d1099b
3
+ size 147961954