Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,77 +1,32 @@
|
|
| 1 |
-
import
|
| 2 |
-
import
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
while cap.isOpened():
|
| 35 |
-
ret, frame = cap.read()
|
| 36 |
-
if not ret:
|
| 37 |
-
break
|
| 38 |
-
results = model(frame)
|
| 39 |
-
results.render()
|
| 40 |
-
annotated_frame = np.squeeze(results.render())
|
| 41 |
-
out.write(annotated_frame)
|
| 42 |
-
|
| 43 |
-
cap.release()
|
| 44 |
-
out.release()
|
| 45 |
-
|
| 46 |
-
# Streamlit Interface
|
| 47 |
-
st.title("YOLO Object Detection")
|
| 48 |
-
|
| 49 |
-
# Opsi untuk memilih gambar atau video
|
| 50 |
-
option = st.radio("Pilih jenis input:", ("Gambar", "Video"))
|
| 51 |
-
|
| 52 |
-
if option == "Gambar":
|
| 53 |
-
uploaded_image = st.file_uploader("Unggah gambar", type=["jpg", "jpeg", "png"])
|
| 54 |
-
if uploaded_image is not None:
|
| 55 |
-
image = Image.open(uploaded_image)
|
| 56 |
-
st.image(image, caption="Gambar asli", use_column_width=True)
|
| 57 |
-
|
| 58 |
-
# Deteksi objek
|
| 59 |
-
annotated_image = detect_image(np.array(image))
|
| 60 |
-
st.image(annotated_image, caption="Hasil deteksi", use_column_width=True)
|
| 61 |
-
|
| 62 |
-
elif option == "Video":
|
| 63 |
-
uploaded_video = st.file_uploader("Unggah video", type=["mp4", "avi", "mov"])
|
| 64 |
-
if uploaded_video is not None:
|
| 65 |
-
# Simpan video sementara
|
| 66 |
-
temp_video_path = tempfile.NamedTemporaryFile(delete=False).name
|
| 67 |
-
with open(temp_video_path, "wb") as f:
|
| 68 |
-
f.write(uploaded_video.read())
|
| 69 |
-
|
| 70 |
-
# Proses video
|
| 71 |
-
output_video_path = "output_video.mp4"
|
| 72 |
-
st.text("Sedang memproses video...")
|
| 73 |
-
detect_video(temp_video_path, output_video_path)
|
| 74 |
-
st.text("Proses selesai!")
|
| 75 |
-
|
| 76 |
-
# Tampilkan hasil video
|
| 77 |
-
st.video(output_video_path)
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from ultralytics import YOLO
|
| 3 |
+
|
| 4 |
+
# Muat model custom yang telah dilatih
|
| 5 |
+
model = YOLO("best.pt") # Pastikan file best.pt ada di direktori yang sama
|
| 6 |
+
|
| 7 |
+
# Fungsi untuk melakukan prediksi
|
| 8 |
+
def predict_image(img, conf_threshold, iou_threshold):
|
| 9 |
+
results = model.predict(
|
| 10 |
+
source=img,
|
| 11 |
+
conf=conf_threshold,
|
| 12 |
+
iou=iou_threshold,
|
| 13 |
+
show_labels=True,
|
| 14 |
+
show_conf=True,
|
| 15 |
+
)
|
| 16 |
+
return results[0].plot() if results else None
|
| 17 |
+
|
| 18 |
+
# Gradio Interface
|
| 19 |
+
iface = gr.Interface(
|
| 20 |
+
fn=predict_image,
|
| 21 |
+
inputs=[
|
| 22 |
+
gr.Image(type="pil", label="Upload Image"), # Input gambar
|
| 23 |
+
gr.Slider(minimum=0, maximum=1, value=0.25, label="Confidence threshold"), # Confidence threshold
|
| 24 |
+
gr.Slider(minimum=0, maximum=1, value=0.45, label="IoU threshold"), # IoU threshold
|
| 25 |
+
],
|
| 26 |
+
outputs=gr.Image(type="pil", label="Result"), # Output gambar dengan bounding box
|
| 27 |
+
title="Ultralytics Gradio YOLO11 - Custom Model", # Judul aplikasi
|
| 28 |
+
description="Upload images for custom YOLO11 object detection.", # Deskripsi aplikasi
|
| 29 |
+
)
|
| 30 |
+
|
| 31 |
+
# Luncurkan aplikasi
|
| 32 |
+
iface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|