Spaces:
Sleeping
Sleeping
jamesthong
commited on
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import PIL.Image as Image
|
3 |
+
|
4 |
+
from ultralytics import ASSETS, YOLO
|
5 |
+
|
6 |
+
model = YOLO("yolov8n.pt")
|
7 |
+
|
8 |
+
|
9 |
+
def predict_image(img, conf_threshold, iou_threshold):
|
10 |
+
"""Predicts objects in an image using a YOLOv8 model with adjustable confidence and IOU thresholds."""
|
11 |
+
results = model.predict(
|
12 |
+
source=img,
|
13 |
+
conf=conf_threshold,
|
14 |
+
iou=iou_threshold,
|
15 |
+
show_labels=True,
|
16 |
+
show_conf=True,
|
17 |
+
imgsz=640,
|
18 |
+
)
|
19 |
+
|
20 |
+
for r in results:
|
21 |
+
im_array = r.plot()
|
22 |
+
im = Image.fromarray(im_array[..., ::-1])
|
23 |
+
|
24 |
+
return im
|
25 |
+
|
26 |
+
|
27 |
+
with gr.Blocks() as demo:
|
28 |
+
with gr.Row():
|
29 |
+
with gr.Column():
|
30 |
+
input_image = gr.Image(type="pil", label="Upload Image")
|
31 |
+
conf = gr.Slider(minimum=0, maximum=1, value=0.25, label="Confidence threshold")
|
32 |
+
iou = gr.Slider(minimum=0, maximum=1, value=0.45, label="IoU threshold")
|
33 |
+
with gr.Row():
|
34 |
+
reset = gr.ClearButton([input_image])
|
35 |
+
submit = gr.Button("Submit")
|
36 |
+
with gr.Column():
|
37 |
+
output_image = gr.Image(type="pil", label="Result")
|
38 |
+
submit.click(fn=predict_image, inputs=[input_image, conf,iou], outputs=[output_image])
|
39 |
+
examples = gr.Examples(([
|
40 |
+
['https://ultralytics.com/images/zidane.jpg', 0.25, 0.45],
|
41 |
+
['https://unsplash.com/photos/2pPw5Glro5I/download?ixid=M3wxMjA3fDB8MXxzZWFyY2h8Mnx8dXJsfGVufDB8fHx8MTcyMTgwNzkyMnww&force=true', 0.5, 0.3],
|
42 |
+
['https://unsplash.com/photos/5CUyfyde_io/download?ixid=M3wxMjA3fDB8MXxzZWFyY2h8OHx8dG9reW98ZW58MHx8fHwxNzIxODY4MzQzfDA&force=true', 0.3, 0.3]
|
43 |
+
]),[input_image,conf,iou]),
|
44 |
+
|
45 |
+
if __name__ == "__main__":
|
46 |
+
demo.launch()
|