GwFirman commited on
Commit
658d3b6
·
verified ·
1 Parent(s): 516d376

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -77
app.py CHANGED
@@ -1,77 +1,32 @@
1
- import streamlit as st
2
- import torch
3
- import cv2
4
- import tempfile
5
- import os
6
- from PIL import Image
7
- import numpy as np
8
-
9
- # Muat model YOLOv5 yang sudah dilatih
10
- @st.cache_resource
11
- def load_model():
12
- model = torch.hub.load('ultralytics/yolov11', 'custom', path='best.pt', force_reload=True)
13
- return model
14
-
15
- model = load_model()
16
-
17
- # Fungsi untuk deteksi pada gambar
18
- def detect_image(image):
19
- results = model(image)
20
- results.render() # Tambahkan bounding box ke gambar
21
- annotated_image = results.imgs[0] # Ambil gambar yang sudah dianotasi
22
- return annotated_image
23
-
24
- # Fungsi untuk deteksi pada video
25
- def detect_video(video_path, output_path):
26
- cap = cv2.VideoCapture(video_path)
27
- fourcc = cv2.VideoWriter_fourcc(*'mp4v')
28
- fps = int(cap.get(cv2.CAP_PROP_FPS))
29
- width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
30
- height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
31
-
32
- out = cv2.VideoWriter(output_path, fourcc, fps, (width, height))
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()