ArchiMathur commited on
Commit
4b5e784
·
verified ·
1 Parent(s): 1790886

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +74 -31
app.py CHANGED
@@ -57,42 +57,85 @@
57
 
58
  # # Launch the Gradio app
59
  # iface.launch()
60
- import gradio as gr
61
- import PIL.Image as Image
62
- from ultralytics import YOLO
63
 
64
- # Load the YOLOv8 model
65
- model = YOLO("best.pt")
66
 
67
- def predict_image(img, conf_threshold, iou_threshold):
68
- """Predicts objects in an image using a YOLOv8 model with adjustable confidence and IOU thresholds."""
69
- results = model.predict(
70
- source=img,
71
- conf=conf_threshold,
72
- iou=iou_threshold,
73
- show_labels=True,
74
- show_conf=True,
75
- imgsz=640,
76
- )
77
 
78
- for r in results:
79
- im_array = r.plot()
80
- im = Image.fromarray(im_array[..., ::-1])
81
 
82
- return im
 
 
 
 
 
 
 
 
 
 
 
 
 
83
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
84
  iface = gr.Interface(
85
- fn=predict_image,
86
- inputs=[
87
- gr.Image(source="webcam", type="pil", label="Capture Image"),
88
- gr.Slider(minimum=0, maximum=1, value=0.25, label="Confidence threshold"),
89
- gr.Slider(minimum=0, maximum=1, value=0.45, label="IoU threshold"),
90
- ],
91
- outputs=gr.Image(type="pil", label="Result"),
92
- live=True, # Enables real-time processing
93
- title="Ultralytics Gradio",
94
- description="Capture images from your webcam for real-time inference using the Ultralytics YOLOv8n model.",
95
  )
96
 
97
- if __name__ == "__main__":
98
- iface.launch()
 
57
 
58
  # # Launch the Gradio app
59
  # iface.launch()
60
+ # import gradio as gr
61
+ # import PIL.Image as Image
62
+ # from ultralytics import YOLO
63
 
64
+ # # Load the YOLOv8 model
65
+ # model = YOLO("best.pt")
66
 
67
+ # def predict_image(img, conf_threshold, iou_threshold):
68
+ # """Predicts objects in an image using a YOLOv8 model with adjustable confidence and IOU thresholds."""
69
+ # results = model.predict(
70
+ # source=img,
71
+ # conf=conf_threshold,
72
+ # iou=iou_threshold,
73
+ # show_labels=True,
74
+ # show_conf=True,
75
+ # imgsz=640,
76
+ # )
77
 
78
+ # for r in results:
79
+ # im_array = r.plot()
80
+ # im = Image.fromarray(im_array[..., ::-1])
81
 
82
+ # return im
83
+
84
+ # iface = gr.Interface(
85
+ # fn=predict_image,
86
+ # inputs=[
87
+ # gr.Image(source="webcam", type="pil", label="Capture Image"),
88
+ # gr.Slider(minimum=0, maximum=1, value=0.25, label="Confidence threshold"),
89
+ # gr.Slider(minimum=0, maximum=1, value=0.45, label="IoU threshold"),
90
+ # ],
91
+ # outputs=gr.Image(type="pil", label="Result"),
92
+ # live=True, # Enables real-time processing
93
+ # title="Ultralytics Gradio",
94
+ # description="Capture images from your webcam for real-time inference using the Ultralytics YOLOv8n model.",
95
+ # )
96
 
97
+ # if __name__ == "__main__":
98
+ # iface.launch()
99
+
100
+ import cv2
101
+ import gradio as gr
102
+ from ultralytics import YOLO
103
+
104
+ # Load the YOLO model (update the path to your fire detection model weights)
105
+ model = YOLO('path/to/your/best.pt') # Replace 'path/to/your/best.pt' with the actual path to your model file
106
+
107
+ def detect_fire(frame):
108
+ # Convert the frame to RGB format (YOLO expects this format)
109
+ img = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
110
+
111
+ # Perform fire detection using the YOLO model
112
+ results = model(img)
113
+
114
+ # Draw bounding boxes and labels on the detected fire areas
115
+ for bbox in results[0].boxes:
116
+ xyxy = bbox.xyxy[0] # Bounding box coordinates
117
+ conf = bbox.conf[0] # Confidence score
118
+ cls = int(bbox.cls[0]) # Class ID
119
+ label = model.names[cls] # Class name
120
+
121
+ if label == "fire": # Make sure this matches the label in your trained model
122
+ # Draw a rectangle around the detected fire
123
+ cv2.rectangle(img, (int(xyxy[0]), int(xyxy[1])), (int(xyxy[2]), int(xyxy[3])), (255, 0, 0), 2)
124
+ # Put the label text above the rectangle
125
+ cv2.putText(img, f"{label} {conf:.2f}", (int(xyxy[0]), int(xyxy[1]) - 10),
126
+ cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 0, 0), 2)
127
+
128
+ # Convert the image back to BGR format for display
129
+ return cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
130
+
131
+ # Create a Gradio interface for fire detection
132
  iface = gr.Interface(
133
+ fn=detect_fire,
134
+ inputs=gr.Image(source="webcam", tool="editor", streaming=True), # Use webcam as the input source
135
+ outputs="image",
136
+ title="Fire Detection using YOLO",
137
+ description="This application detects fire in real-time using a YOLO model."
 
 
 
 
 
138
  )
139
 
140
+ # Launch the Gradio app
141
+ iface.launch()