Spaces:
Sleeping
Sleeping
Upload 6 files
Browse files- .gitattributes +1 -0
- agent_integration.py +66 -0
- app.py +18 -18
- firedetection.py +34 -27
- flash_attn-2.7.3+cu11torch2.2cxx11abiFALSE-cp311-cp311-linux_x86_64.whl +3 -0
- requirements.txt +4 -1
.gitattributes
CHANGED
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
36 |
+
flash_attn-2.7.3+cu11torch2.2cxx11abiFALSE-cp311-cp311-linux_x86_64.whl filter=lfs diff=lfs merge=lfs -text
|
agent_integration.py
ADDED
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from typing import Optional, List
|
2 |
+
from langchain.agents import Tool, AgentType, initialize_agent
|
3 |
+
from langchain.llms.base import LLM
|
4 |
+
|
5 |
+
from firedetection import detect_fire_in_video
|
6 |
+
|
7 |
+
# Define a LangChain tool using a decorator or by manually creating a Tool object.
|
8 |
+
@Tool(
|
9 |
+
name="FireDetectionTool",
|
10 |
+
description="Detect fire in a given video using YOLO."
|
11 |
+
)
|
12 |
+
def detect_fire_in_video_tool(video_path: str) -> str:
|
13 |
+
"""
|
14 |
+
Takes a video path, calls 'detect_fire_in_video' function,
|
15 |
+
and returns a message indicating the output location.
|
16 |
+
"""
|
17 |
+
output_video_path = detect_fire_in_video(
|
18 |
+
input_video_path=video_path,
|
19 |
+
output_video_path="temp_outputs/result.avi",
|
20 |
+
model_path="/Users/bahakizil/Desktop/firedetection/firedetectionyolo/last.pt", # Adjust if your model file is named differently
|
21 |
+
device="cpu" # or 'cuda'/'mps' if available
|
22 |
+
)
|
23 |
+
return f"Fire detection completed. Output saved to: {output_video_path}"
|
24 |
+
|
25 |
+
|
26 |
+
class SimpleFakeLLM(LLM):
|
27 |
+
"""
|
28 |
+
A trivial/fake LLM for demonstration.
|
29 |
+
It always decides to call 'FireDetectionTool' regardless of the prompt.
|
30 |
+
This replaces the need for external APIs like OpenAI or HuggingFace,
|
31 |
+
while still demonstrating the agent + tool usage in LangChain.
|
32 |
+
"""
|
33 |
+
|
34 |
+
@property
|
35 |
+
def _llm_type(self) -> str:
|
36 |
+
return "fake-llm"
|
37 |
+
|
38 |
+
def _call(self, prompt: str, stop: Optional[List[str]] = None) -> str:
|
39 |
+
# Always produce a response suggesting the use of FireDetectionTool:
|
40 |
+
return "I should use FireDetectionTool to process the video."
|
41 |
+
|
42 |
+
def create_fire_detection_agent():
|
43 |
+
"""
|
44 |
+
Creates a LangChain agent with:
|
45 |
+
- A single tool: FireDetectionTool
|
46 |
+
- A trivial LLM (SimpleFakeLLM)
|
47 |
+
"""
|
48 |
+
llm = SimpleFakeLLM()
|
49 |
+
tools = [detect_fire_in_video_tool]
|
50 |
+
|
51 |
+
agent = initialize_agent(
|
52 |
+
tools=tools,
|
53 |
+
llm=llm,
|
54 |
+
agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
|
55 |
+
verbose=True
|
56 |
+
)
|
57 |
+
return agent
|
58 |
+
|
59 |
+
if __name__ == "__main__":
|
60 |
+
# Example usage:
|
61 |
+
agent = create_fire_detection_agent()
|
62 |
+
|
63 |
+
# Prompt the agent to detect fire in a given video (sample_video.mp4)
|
64 |
+
prompt = "Please detect fire in 'sample_video.mp4'."
|
65 |
+
response = agent.run(prompt)
|
66 |
+
print("\nAgent response:", response)
|
app.py
CHANGED
@@ -2,41 +2,41 @@ import gradio as gr
|
|
2 |
import os
|
3 |
from firedetection import detect_fire_in_video
|
4 |
|
|
|
5 |
TEMP_DIR = "temp_outputs"
|
6 |
os.makedirs(TEMP_DIR, exist_ok=True)
|
7 |
|
8 |
-
MODEL_PATH = "/Users/bahakizil/Desktop/firedetection/last.pt" # Fire detection model .pt dosyası (örnek)
|
9 |
-
DEVICE = "cpu" # İsterseniz 'cpu' veya 'cuda' vb. olarak değiştirebilirsiniz.
|
10 |
-
|
11 |
def fire_detection_interface(video_file):
|
12 |
-
|
13 |
-
|
|
|
|
|
|
|
|
|
|
|
14 |
return None
|
15 |
|
|
|
16 |
input_video_path = video_file
|
17 |
-
|
18 |
base_name = os.path.basename(input_video_path)
|
19 |
name_no_ext, ext = os.path.splitext(base_name)
|
20 |
-
|
|
|
21 |
|
22 |
processed_path = detect_fire_in_video(
|
23 |
input_video_path=input_video_path,
|
24 |
-
output_video_path=
|
25 |
-
model_path=
|
26 |
-
device=
|
27 |
)
|
28 |
-
|
29 |
return processed_path
|
30 |
|
31 |
-
|
32 |
demo = gr.Interface(
|
33 |
fn=fire_detection_interface,
|
34 |
-
inputs=gr.Video(label="Video
|
35 |
-
outputs=gr.Video(label="
|
36 |
-
title="Fire Detection",
|
37 |
-
description=
|
38 |
-
|
39 |
-
),
|
40 |
)
|
41 |
|
42 |
if __name__ == "__main__":
|
|
|
2 |
import os
|
3 |
from firedetection import detect_fire_in_video
|
4 |
|
5 |
+
# Temporary directory to store results
|
6 |
TEMP_DIR = "temp_outputs"
|
7 |
os.makedirs(TEMP_DIR, exist_ok=True)
|
8 |
|
|
|
|
|
|
|
9 |
def fire_detection_interface(video_file):
|
10 |
+
"""
|
11 |
+
Gradio interface function that:
|
12 |
+
1) Receives an uploaded video.
|
13 |
+
2) Calls detect_fire_in_video.
|
14 |
+
3) Returns the path of the processed video for display in Gradio.
|
15 |
+
"""
|
16 |
+
if video_file is None:
|
17 |
return None
|
18 |
|
19 |
+
# Extract filename
|
20 |
input_video_path = video_file
|
|
|
21 |
base_name = os.path.basename(input_video_path)
|
22 |
name_no_ext, ext = os.path.splitext(base_name)
|
23 |
+
|
24 |
+
output_path = os.path.join(TEMP_DIR, f"{name_no_ext}_fire{ext}")
|
25 |
|
26 |
processed_path = detect_fire_in_video(
|
27 |
input_video_path=input_video_path,
|
28 |
+
output_video_path=output_path,
|
29 |
+
model_path="yolov12n.pt",
|
30 |
+
device="cpu"
|
31 |
)
|
|
|
32 |
return processed_path
|
33 |
|
|
|
34 |
demo = gr.Interface(
|
35 |
fn=fire_detection_interface,
|
36 |
+
inputs=gr.Video(label="Upload a Video"),
|
37 |
+
outputs=gr.Video(label="Annotated Video"),
|
38 |
+
title="Fire Detection with YOLO",
|
39 |
+
description="This interface detects fire in an uploaded video using YOLO."
|
|
|
|
|
40 |
)
|
41 |
|
42 |
if __name__ == "__main__":
|
firedetection.py
CHANGED
@@ -1,3 +1,5 @@
|
|
|
|
|
|
1 |
from collections import defaultdict
|
2 |
import cv2
|
3 |
from ultralytics import YOLO
|
@@ -8,33 +10,38 @@ def detect_fire_in_video(
|
|
8 |
output_video_path: str,
|
9 |
model_path: str,
|
10 |
device: str = "cpu"
|
11 |
-
):
|
12 |
"""
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
:
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
|
|
|
|
|
|
21 |
"""
|
22 |
|
23 |
-
#
|
24 |
track_history = defaultdict(lambda: [])
|
25 |
|
26 |
-
# YOLO
|
27 |
model = YOLO(model_path, device=device)
|
28 |
|
29 |
-
#
|
30 |
cap = cv2.VideoCapture(input_video_path)
|
31 |
|
32 |
-
#
|
33 |
-
w, h, fps = (int(cap.get(
|
34 |
-
|
35 |
-
|
|
|
|
|
36 |
|
37 |
-
#
|
38 |
out = cv2.VideoWriter(
|
39 |
output_video_path,
|
40 |
cv2.VideoWriter_fourcc(*"MJPG"),
|
@@ -43,18 +50,18 @@ def detect_fire_in_video(
|
|
43 |
)
|
44 |
|
45 |
while True:
|
46 |
-
ret,
|
47 |
if not ret:
|
48 |
-
print("
|
49 |
break
|
50 |
|
51 |
-
#
|
52 |
-
annotator = Annotator(
|
53 |
|
54 |
-
#
|
55 |
-
results = model.track(
|
56 |
|
57 |
-
#
|
58 |
if results[0].boxes.id is not None and results[0].masks is not None:
|
59 |
masks = results[0].masks.xy
|
60 |
track_ids = results[0].boxes.id.int().cpu().tolist()
|
@@ -63,11 +70,11 @@ def detect_fire_in_video(
|
|
63 |
annotator.seg_bbox(
|
64 |
mask=mask,
|
65 |
mask_color=colors(int(track_id), True),
|
66 |
-
label=
|
67 |
)
|
68 |
|
69 |
-
#
|
70 |
-
out.write(
|
71 |
|
72 |
out.release()
|
73 |
cap.release()
|
|
|
1 |
+
|
2 |
+
|
3 |
from collections import defaultdict
|
4 |
import cv2
|
5 |
from ultralytics import YOLO
|
|
|
10 |
output_video_path: str,
|
11 |
model_path: str,
|
12 |
device: str = "cpu"
|
13 |
+
) -> str:
|
14 |
"""
|
15 |
+
Detects fire in the given video using a YOLO model.
|
16 |
+
It draws annotations on each frame and saves the output video.
|
17 |
+
|
18 |
+
Args:
|
19 |
+
input_video_path (str): Path to the input video file.
|
20 |
+
output_video_path (str): Path to save the annotated output video.
|
21 |
+
model_path (str): Path to the YOLO .pt file.
|
22 |
+
device (str): 'cpu', 'cuda', or 'mps' for processing.
|
23 |
+
|
24 |
+
Returns:
|
25 |
+
str: The path to the output annotated video.
|
26 |
"""
|
27 |
|
28 |
+
# Tracking history - optional usage
|
29 |
track_history = defaultdict(lambda: [])
|
30 |
|
31 |
+
# Load the YOLO model
|
32 |
model = YOLO(model_path, device=device)
|
33 |
|
34 |
+
# Open the video
|
35 |
cap = cv2.VideoCapture(input_video_path)
|
36 |
|
37 |
+
# Retrieve video properties
|
38 |
+
w, h, fps = (int(cap.get(prop)) for prop in [
|
39 |
+
cv2.CAP_PROP_FRAME_WIDTH,
|
40 |
+
cv2.CAP_PROP_FRAME_HEIGHT,
|
41 |
+
cv2.CAP_PROP_FPS
|
42 |
+
])
|
43 |
|
44 |
+
# Prepare output video writer
|
45 |
out = cv2.VideoWriter(
|
46 |
output_video_path,
|
47 |
cv2.VideoWriter_fourcc(*"MJPG"),
|
|
|
50 |
)
|
51 |
|
52 |
while True:
|
53 |
+
ret, frame = cap.read()
|
54 |
if not ret:
|
55 |
+
print("Reached end of video or no frame retrieved.")
|
56 |
break
|
57 |
|
58 |
+
# Create an annotator to draw on the frame
|
59 |
+
annotator = Annotator(frame, line_width=2)
|
60 |
|
61 |
+
# Perform object tracking
|
62 |
+
results = model.track(frame, persist=True)
|
63 |
|
64 |
+
# If there are boxes with IDs and masks, annotate them
|
65 |
if results[0].boxes.id is not None and results[0].masks is not None:
|
66 |
masks = results[0].masks.xy
|
67 |
track_ids = results[0].boxes.id.int().cpu().tolist()
|
|
|
70 |
annotator.seg_bbox(
|
71 |
mask=mask,
|
72 |
mask_color=colors(int(track_id), True),
|
73 |
+
label=f"ID:{track_id}"
|
74 |
)
|
75 |
|
76 |
+
# Write the annotated frame to output
|
77 |
+
out.write(frame)
|
78 |
|
79 |
out.release()
|
80 |
cap.release()
|
flash_attn-2.7.3+cu11torch2.2cxx11abiFALSE-cp311-cp311-linux_x86_64.whl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:9f28de4a263312786c4bc0444ecdf8397c24e46100fa24a9b92b036a62d1940d
|
3 |
+
size 193082618
|
requirements.txt
CHANGED
@@ -15,4 +15,7 @@ py-cpuinfo==9.0.0
|
|
15 |
huggingface-hub==0.23.2
|
16 |
safetensors==0.4.3
|
17 |
numpy==1.26.4
|
18 |
-
|
|
|
|
|
|
|
|
15 |
huggingface-hub==0.23.2
|
16 |
safetensors==0.4.3
|
17 |
numpy==1.26.4
|
18 |
+
ultralytics==8.0.20
|
19 |
+
opencv-python==4.7.0.72
|
20 |
+
langchain==0.0.102
|
21 |
+
gradio==3.23.0
|