Spaces:
Build error
Build error
Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from ultralytics import YOLO
|
| 2 |
+
from PIL import Image
|
| 3 |
+
import gradio as gr
|
| 4 |
+
from huggingface_hub import snapshot_download
|
| 5 |
+
import os
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
def load_model(repo_id):
|
| 9 |
+
download_dir = snapshot_download(repo_id)
|
| 10 |
+
print(download_dir)
|
| 11 |
+
path = os.path.join(download_dir, "best_int8_openvino_model")
|
| 12 |
+
print(path)
|
| 13 |
+
detection_model = YOLO(path, task='detect')
|
| 14 |
+
return detection_model
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
def predict(pilimg, confidence, iou):
|
| 18 |
+
source = pilimg
|
| 19 |
+
result = detection_model.predict(source, conf=confidence, iou=iou)
|
| 20 |
+
img_bgr = result[0].plot()
|
| 21 |
+
out_pilimg = Image.fromarray(img_bgr[..., ::-1]) # RGB-order PIL image
|
| 22 |
+
return out_pilimg
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
REPO_ID = "skngew/9053220B"
|
| 26 |
+
detection_model = load_model(REPO_ID)
|
| 27 |
+
|
| 28 |
+
# Student ID
|
| 29 |
+
student_id = "Student ID: 9053220B"
|
| 30 |
+
|
| 31 |
+
# Create the Gradio interface
|
| 32 |
+
def create_interface():
|
| 33 |
+
# Persistent state for default values
|
| 34 |
+
confidence_default = gr.State(0.5)
|
| 35 |
+
iou_default = gr.State(0.6)
|
| 36 |
+
|
| 37 |
+
interface = gr.Interface(
|
| 38 |
+
fn=predict,
|
| 39 |
+
inputs=[
|
| 40 |
+
gr.Image(type="pil", label="Input Image"),
|
| 41 |
+
gr.Slider(0, 1, value=confidence_default.value, label="Confidence Threshold"), # Default to 0.5
|
| 42 |
+
gr.Slider(0, 1, value=iou_default.value, label="IOU Threshold") # Default to 0.6
|
| 43 |
+
],
|
| 44 |
+
outputs=gr.Image(type="pil", label="Output Image"),
|
| 45 |
+
title="Object Detection with YOLOv8",
|
| 46 |
+
description=student_id,
|
| 47 |
+
live=False,
|
| 48 |
+
)
|
| 49 |
+
|
| 50 |
+
return interface
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
# Launch the Gradio app
|
| 54 |
+
app_interface = create_interface()
|
| 55 |
+
app_interface.launch(share=True)
|