Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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)
|