APIMONSTER commited on
Commit
ddca105
·
verified ·
1 Parent(s): f364e13

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +134 -134
app.py CHANGED
@@ -1,134 +1,134 @@
1
- import gradio as gr
2
- import cv2
3
- import time
4
- from ultralytics import YOLO
5
- import numpy as np
6
-
7
- # Model yükle
8
- model = YOLO("runs/detect/train2/weights/best.pt")
9
- device = 0 # GPU cihazı (0 = ilk GPU)
10
-
11
- def infer_fire(image, conf_thresh):
12
- # RGB→BGR çevir, infer
13
- img_bgr = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
14
- start = time.time()
15
- res = model.predict(
16
- source=img_bgr,
17
- device=device,
18
- imgsz=640,
19
- conf=conf_thresh
20
- )[0]
21
- elapsed = (time.time() - start) * 1000 # ms cinsinden
22
-
23
- annotated = img_bgr.copy()
24
- h, w = annotated.shape[:2]
25
- boxes_np = (
26
- res.boxes.data.cpu().numpy()
27
- if hasattr(res.boxes, "data")
28
- else np.empty((0, 6))
29
- )
30
-
31
- for x1, y1, x2, y2, conf, cls in boxes_np:
32
- x1, y1, x2, y2 = map(int, (x1, y1, x2, y2))
33
- # 1) Box'u resim sınırları içinde tut
34
- x1 = max(0, x1); y1 = max(0, y1)
35
- x2 = min(w-1, x2); y2 = min(h-1, y2)
36
-
37
- label = f"{model.names[int(cls)]} {conf:.2f}"
38
-
39
- # 2) Kalın box çiz
40
- cv2.rectangle(annotated, (x1, y1), (x2, y2), (0, 255, 0), 4)
41
-
42
- # 3) Yazı boyutunu al
43
- (text_w, text_h), baseline = cv2.getTextSize(
44
- label, cv2.FONT_HERSHEY_SIMPLEX, 0.7, 3
45
- )
46
-
47
- # 4) Etiket pozisyonunu ayarla
48
- text_x = x1
49
- text_y = y1 - 5
50
- # Üst sınırı aşmasın
51
- if text_y - text_h - baseline < 0:
52
- text_y = y1 + text_h + 5
53
- # Sağ sınırı aşmasın
54
- if text_x + text_w > w:
55
- text_x = w - text_w - 5
56
-
57
- # 5) Dolgu arkaplanlı dikdörtgen
58
- cv2.rectangle(
59
- annotated,
60
- (text_x, text_y - text_h - baseline),
61
- (text_x + text_w, text_y + baseline),
62
- (0, 255, 0),
63
- cv2.FILLED
64
- )
65
-
66
- # 6) Siyah renkli yazı
67
- cv2.putText(
68
- annotated,
69
- label,
70
- (text_x, text_y),
71
- cv2.FONT_HERSHEY_SIMPLEX,
72
- 0.7,
73
- (0, 0, 0),
74
- 3
75
- )
76
-
77
- out_img = cv2.cvtColor(annotated, cv2.COLOR_BGR2RGB)
78
- return out_img, f"Inference time: {elapsed:.1f} ms"
79
-
80
- # Gradio arayüzü
81
- examples = [
82
- ["datasets/test/images/WEB10432.jpg", 0.25],
83
- ["datasets/test/images/WEB11791.jpg", 0.25],
84
- ["datasets/test/images/WEB11706.jpg", 0.25],
85
- ]
86
-
87
- with gr.Blocks() as demo:
88
- gr.Markdown("## 🔥 Wildfire Smoke & Fire Detector")
89
- gr.Markdown(
90
- "Upload an image below, adjust the confidence threshold, "
91
- "and the model will highlight any smoke or fire regions."
92
- )
93
-
94
- with gr.Row():
95
- with gr.Column(scale=1, min_width=200):
96
- input_img = gr.Image(type="numpy", label="Input Image")
97
- conf_slider = gr.Slider(0.0, 1.0, 0.25, 0.01, label="Confidence Threshold")
98
- run_btn = gr.Button("Detect 🔍", variant="primary")
99
- with gr.Column(scale=5, min_width=1000):
100
- output_img = gr.Image(
101
- type="numpy",
102
- label="Annotated Output",
103
- height=800,
104
- width=1000
105
- )
106
- time_txt = gr.Textbox(label="Performance", interactive=False)
107
-
108
- run_btn.click(
109
- fn=infer_fire,
110
- inputs=[input_img, conf_slider],
111
- outputs=[output_img, time_txt]
112
- )
113
-
114
- gr.Examples(
115
- examples=examples,
116
- inputs=[input_img, conf_slider],
117
- outputs=[output_img, time_txt],
118
- fn=infer_fire,
119
- cache_examples=False
120
- )
121
-
122
- gr.Markdown(
123
- "---\n"
124
- "Model trained on a custom wildfire dataset using YOLOv8. "
125
- "Adjust the threshold to trade off between false positives and false negatives."
126
- )
127
-
128
- if __name__ == "__main__":
129
- demo.launch()
130
- # server_name="0.0.0.0",
131
- # server_port=7861,
132
- # share=False,
133
- # inbrowser=True
134
-
 
1
+ import gradio as gr
2
+ import cv2
3
+ import time
4
+ from ultralytics import YOLO
5
+ import numpy as np
6
+
7
+ # Model yükle
8
+ model = YOLO("best.pt")
9
+ device = 0 # GPU cihazı (0 = ilk GPU)
10
+
11
+ def infer_fire(image, conf_thresh):
12
+ # RGB→BGR çevir, infer
13
+ img_bgr = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
14
+ start = time.time()
15
+ res = model.predict(
16
+ source=img_bgr,
17
+ device=device,
18
+ imgsz=640,
19
+ conf=conf_thresh
20
+ )[0]
21
+ elapsed = (time.time() - start) * 1000 # ms cinsinden
22
+
23
+ annotated = img_bgr.copy()
24
+ h, w = annotated.shape[:2]
25
+ boxes_np = (
26
+ res.boxes.data.cpu().numpy()
27
+ if hasattr(res.boxes, "data")
28
+ else np.empty((0, 6))
29
+ )
30
+
31
+ for x1, y1, x2, y2, conf, cls in boxes_np:
32
+ x1, y1, x2, y2 = map(int, (x1, y1, x2, y2))
33
+ # 1) Box'u resim sınırları içinde tut
34
+ x1 = max(0, x1); y1 = max(0, y1)
35
+ x2 = min(w-1, x2); y2 = min(h-1, y2)
36
+
37
+ label = f"{model.names[int(cls)]} {conf:.2f}"
38
+
39
+ # 2) Kalın box çiz
40
+ cv2.rectangle(annotated, (x1, y1), (x2, y2), (0, 255, 0), 4)
41
+
42
+ # 3) Yazı boyutunu al
43
+ (text_w, text_h), baseline = cv2.getTextSize(
44
+ label, cv2.FONT_HERSHEY_SIMPLEX, 0.7, 3
45
+ )
46
+
47
+ # 4) Etiket pozisyonunu ayarla
48
+ text_x = x1
49
+ text_y = y1 - 5
50
+ # Üst sınırı aşmasın
51
+ if text_y - text_h - baseline < 0:
52
+ text_y = y1 + text_h + 5
53
+ # Sağ sınırı aşmasın
54
+ if text_x + text_w > w:
55
+ text_x = w - text_w - 5
56
+
57
+ # 5) Dolgu arkaplanlı dikdörtgen
58
+ cv2.rectangle(
59
+ annotated,
60
+ (text_x, text_y - text_h - baseline),
61
+ (text_x + text_w, text_y + baseline),
62
+ (0, 255, 0),
63
+ cv2.FILLED
64
+ )
65
+
66
+ # 6) Siyah renkli yazı
67
+ cv2.putText(
68
+ annotated,
69
+ label,
70
+ (text_x, text_y),
71
+ cv2.FONT_HERSHEY_SIMPLEX,
72
+ 0.7,
73
+ (0, 0, 0),
74
+ 3
75
+ )
76
+
77
+ out_img = cv2.cvtColor(annotated, cv2.COLOR_BGR2RGB)
78
+ return out_img, f"Inference time: {elapsed:.1f} ms"
79
+
80
+ # Gradio arayüzü
81
+ examples = [
82
+ ["datasets/test/images/WEB10432.jpg", 0.25],
83
+ ["datasets/test/images/WEB11791.jpg", 0.25],
84
+ ["datasets/test/images/WEB11706.jpg", 0.25],
85
+ ]
86
+
87
+ with gr.Blocks() as demo:
88
+ gr.Markdown("## 🔥 Wildfire Smoke & Fire Detector")
89
+ gr.Markdown(
90
+ "Upload an image below, adjust the confidence threshold, "
91
+ "and the model will highlight any smoke or fire regions."
92
+ )
93
+
94
+ with gr.Row():
95
+ with gr.Column(scale=1, min_width=200):
96
+ input_img = gr.Image(type="numpy", label="Input Image")
97
+ conf_slider = gr.Slider(0.0, 1.0, 0.25, 0.01, label="Confidence Threshold")
98
+ run_btn = gr.Button("Detect 🔍", variant="primary")
99
+ with gr.Column(scale=5, min_width=1000):
100
+ output_img = gr.Image(
101
+ type="numpy",
102
+ label="Annotated Output",
103
+ height=800,
104
+ width=1000
105
+ )
106
+ time_txt = gr.Textbox(label="Performance", interactive=False)
107
+
108
+ run_btn.click(
109
+ fn=infer_fire,
110
+ inputs=[input_img, conf_slider],
111
+ outputs=[output_img, time_txt]
112
+ )
113
+
114
+ gr.Examples(
115
+ examples=examples,
116
+ inputs=[input_img, conf_slider],
117
+ outputs=[output_img, time_txt],
118
+ fn=infer_fire,
119
+ cache_examples=False
120
+ )
121
+
122
+ gr.Markdown(
123
+ "---\n"
124
+ "Model trained on a custom wildfire dataset using YOLOv8. "
125
+ "Adjust the threshold to trade off between false positives and false negatives."
126
+ )
127
+
128
+ if __name__ == "__main__":
129
+ demo.launch()
130
+ # server_name="0.0.0.0",
131
+ # server_port=7861,
132
+ # share=False,
133
+ # inbrowser=True
134
+