updated
Browse files- .gitignore +1 -0
- app.py +79 -36
- pyproject.toml +2 -1
- uv.lock +0 -0
- 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 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
-
#
|
| 25 |
-
|
|
|
|
|
|
|
| 26 |
|
| 27 |
-
#
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
-
return
|
| 31 |
|
| 32 |
-
def
|
| 33 |
-
#
|
| 34 |
-
|
| 35 |
-
message = f"{detection_type} detected at {location} on {timestamp}"
|
| 36 |
|
| 37 |
-
#
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 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 =
|
| 64 |
|
| 65 |
# Release webcam
|
| 66 |
cap.release()
|
| 67 |
|
|
|
|
|
|
|
|
|
|
| 68 |
if fire_detected:
|
| 69 |
-
# Get
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
|
|
|
| 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.
|
| 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
|