bahakizil commited on
Commit
11ce781
·
verified ·
1 Parent(s): 2b7dfab

Upload 6 files

Browse files
.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
- if not video_file:
 
 
 
 
 
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
- output_video_path = os.path.join(TEMP_DIR, f"{name_no_ext}_fire_detection{ext}")
 
21
 
22
  processed_path = detect_fire_in_video(
23
  input_video_path=input_video_path,
24
- output_video_path=output_video_path,
25
- model_path=MODEL_PATH,
26
- device=DEVICE
27
  )
28
-
29
  return processed_path
30
 
31
-
32
  demo = gr.Interface(
33
  fn=fire_detection_interface,
34
- inputs=gr.Video(label="Video Yükleyin"),
35
- outputs=gr.Video(label="İşlenmiş Video"),
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
- Belirtilen videoda YOLO modelini kullanarak yangın tespiti yapar ve
14
- etiketlenmiş çıktıyı output_video_path'e kaydeder.
15
-
16
- :param input_video_path: Girdi videosunun dosya yolu
17
- :param output_video_path: İşlenen (annotate edilmiş) videonun kaydedileceği dosya yolu
18
- :param model_path: YOLO model dosyasının (ör. .pt uzantılı) dosya yolu
19
- :param device: İşlem için kullanılacak donanım ('cpu', 'cuda', 'mps' vb.)
20
- :return: İşlenen video yolunu döndürür.
 
 
 
21
  """
22
 
23
- # Takip geçmişi için defaultdict oluştur
24
  track_history = defaultdict(lambda: [])
25
 
26
- # YOLO modelini yükle
27
  model = YOLO(model_path, device=device)
28
 
29
- # Video dosyasını
30
  cap = cv2.VideoCapture(input_video_path)
31
 
32
- # Video özelliklerini al
33
- w, h, fps = (int(cap.get(x)) for x in (cv2.CAP_PROP_FRAME_WIDTH,
34
- cv2.CAP_PROP_FRAME_HEIGHT,
35
- cv2.CAP_PROP_FPS))
 
 
36
 
37
- # Çıktı videosu kaydı için VideoWriter ayarla
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, im0 = cap.read()
47
  if not ret:
48
- print("Video bitti veya çerçeve okunamadı.")
49
  break
50
 
51
- # Annotator objesi ile çizim yapma
52
- annotator = Annotator(im0, line_width=2)
53
 
54
- # model.track ile takip (tracking) sonuçlarını al
55
- results = model.track(im0, persist=True)
56
 
57
- # Track ID'ler ve maskeler varsa işleme al
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=str(track_id)
67
  )
68
 
69
- # Annotate edilmiş frame'i kaydet
70
- out.write(im0)
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