Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# -*- coding: utf-8 -*-
|
2 |
+
"""app.ipynb
|
3 |
+
|
4 |
+
Automatically generated by Colaboratory.
|
5 |
+
|
6 |
+
Original file is located at
|
7 |
+
https://colab.research.google.com/drive/1WeNkl1pYnT0qeOTsUFooLFLJ1arRHC00
|
8 |
+
"""
|
9 |
+
|
10 |
+
# %pip install ultralytics -q
|
11 |
+
# %pip install gradio -q
|
12 |
+
|
13 |
+
import cv2
|
14 |
+
import os
|
15 |
+
import PIL.Image as Image
|
16 |
+
import gradio as gr
|
17 |
+
from huggingface_hub import hf_hub_download
|
18 |
+
from ultralytics import ASSETS, YOLO
|
19 |
+
|
20 |
+
# load trained model
|
21 |
+
model = YOLO("best.pt")
|
22 |
+
|
23 |
+
def predict_image(img, conf_threshold, iou_threshold):
|
24 |
+
results = model.predict(
|
25 |
+
source=img,
|
26 |
+
conf=conf_threshold,
|
27 |
+
iou=iou_threshold,
|
28 |
+
show_labels=True,
|
29 |
+
show_conf=True,
|
30 |
+
imgsz=640,
|
31 |
+
)
|
32 |
+
|
33 |
+
for r in results:
|
34 |
+
im_array = r.plot()
|
35 |
+
im = Image.fromarray(im_array[..., ::-1])
|
36 |
+
|
37 |
+
return im
|
38 |
+
|
39 |
+
|
40 |
+
iface = gr.Interface(
|
41 |
+
fn=predict_image,
|
42 |
+
inputs=[
|
43 |
+
gr.Image(type="pil", label="Upload Image"),
|
44 |
+
gr.Slider(minimum=0, maximum=1, value=0.25, label="Confidence threshold"),
|
45 |
+
gr.Slider(minimum=0, maximum=1, value=0.45, label="IoU threshold")
|
46 |
+
],
|
47 |
+
outputs=gr.Image(type="pil", label="Result"),
|
48 |
+
title="Fire Detecttion using YOLOv8n on Gradio",
|
49 |
+
description="Upload images for inference. The Ultralytics YOLOv8n trained model is used for inference.",
|
50 |
+
examples=[
|
51 |
+
[ASSETS / "bus.jpg", 0.25, 0.45],
|
52 |
+
[ASSETS / "zidane.jpg", 0.25, 0.45],
|
53 |
+
]
|
54 |
+
)
|
55 |
+
|
56 |
+
if __name__ == '__main__':
|
57 |
+
iface.launch()
|