Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,59 +1,98 @@
|
|
| 1 |
-
import cv2
|
| 2 |
-
import numpy as np
|
| 3 |
-
import gradio as gr
|
| 4 |
-
import torch
|
| 5 |
-
from ultralytics import YOLO
|
| 6 |
-
# Load the YOLO model (adjust the path to your model weights and config)
|
| 7 |
-
model = torch.hub.load('best.pt') # Change to your model path
|
| 8 |
|
| 9 |
-
def detect_fire(frame):
|
| 10 |
-
|
| 11 |
-
|
| 12 |
|
| 13 |
-
|
| 14 |
-
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
| 18 |
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
|
| 26 |
-
|
| 27 |
|
| 28 |
-
def webcam_demo():
|
| 29 |
-
|
| 30 |
-
|
| 31 |
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
|
| 37 |
-
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
-
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
-
|
| 44 |
-
|
|
|
|
| 45 |
|
| 46 |
-
|
| 47 |
-
cv2.destroyAllWindows()
|
| 48 |
|
| 49 |
-
# Create a Gradio interface
|
| 50 |
iface = gr.Interface(
|
| 51 |
-
fn=
|
| 52 |
-
inputs=[
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
)
|
| 57 |
|
| 58 |
-
|
| 59 |
-
iface.launch()
|
|
|
|
| 1 |
+
# import cv2
|
| 2 |
+
# import numpy as np
|
| 3 |
+
# import gradio as gr
|
| 4 |
+
# import torch
|
| 5 |
+
# from ultralytics import YOLO
|
| 6 |
+
# # Load the YOLO model (adjust the path to your model weights and config)
|
| 7 |
+
# model = torch.hub.load('best.pt') # Change to your model path
|
| 8 |
|
| 9 |
+
# def detect_fire(frame):
|
| 10 |
+
# # Convert the frame to RGB (YOLO models usually expect this format)
|
| 11 |
+
# img = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
| 12 |
|
| 13 |
+
# # Use the model to detect objects
|
| 14 |
+
# results = model(img)
|
| 15 |
|
| 16 |
+
# # Get the predictions
|
| 17 |
+
# predictions = results.pred[0] # Assuming a single image
|
| 18 |
|
| 19 |
+
# # Draw boxes on the detected fire objects
|
| 20 |
+
# for *xyxy, conf, cls in predictions:
|
| 21 |
+
# label = model.names[int(cls)]
|
| 22 |
+
# if label == "fire": # Adjust based on your class name for fire
|
| 23 |
+
# cv2.rectangle(img, (int(xyxy[0]), int(xyxy[1])), (int(xyxy[2]), int(xyxy[3])), (255, 0, 0), 2)
|
| 24 |
+
# cv2.putText(img, f"{label} {conf:.2f}", (int(xyxy[0]), int(xyxy[1]) - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 0, 0), 2)
|
| 25 |
|
| 26 |
+
# return img
|
| 27 |
|
| 28 |
+
# def webcam_demo():
|
| 29 |
+
# # Start video capture from webcam
|
| 30 |
+
# cap = cv2.VideoCapture(0)
|
| 31 |
|
| 32 |
+
# while True:
|
| 33 |
+
# ret, frame = cap.read()
|
| 34 |
+
# if not ret:
|
| 35 |
+
# break
|
| 36 |
|
| 37 |
+
# # Detect fire in the current frame
|
| 38 |
+
# frame_with_detections = detect_fire(frame)
|
| 39 |
+
|
| 40 |
+
# # Display the result
|
| 41 |
+
# cv2.imshow("Fire Detection", frame_with_detections)
|
| 42 |
+
|
| 43 |
+
# if cv2.waitKey(1) & 0xFF == ord('q'):
|
| 44 |
+
# break
|
| 45 |
+
|
| 46 |
+
# cap.release()
|
| 47 |
+
# cv2.destroyAllWindows()
|
| 48 |
+
|
| 49 |
+
# # Create a Gradio interface
|
| 50 |
+
# iface = gr.Interface(
|
| 51 |
+
# fn=webcam_demo,
|
| 52 |
+
# inputs=[],
|
| 53 |
+
# outputs="image",
|
| 54 |
+
# title="Fire Detection using Webcam",
|
| 55 |
+
# description="This application detects fire using a webcam feed."
|
| 56 |
+
# )
|
| 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()
|