Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import cv2
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import numpy as np
|
| 5 |
+
from ultralytics import YOLO
|
| 6 |
+
|
| 7 |
+
# Load YOLO models
|
| 8 |
+
model_sawit = YOLO("best_yolov8.pt") # Ganti dengan path model YOLOv8 untuk pohon sawit
|
| 9 |
+
model_apel = YOLO("best_apple.pt") # Ganti dengan path model YOLOv8 untuk apel
|
| 10 |
+
|
| 11 |
+
# Sidebar menu
|
| 12 |
+
menu = st.sidebar.selectbox("Pilih Aplikasi", ["Deteksi Pohon Sawit", "Deteksi Warna Apel"])
|
| 13 |
+
|
| 14 |
+
if menu == "Deteksi Pohon Sawit":
|
| 15 |
+
st.title("Deteksi Pohon Sawit dengan YOLOv8")
|
| 16 |
+
confidence_threshold = st.slider("Confidence Threshold", min_value=0.0, max_value=1.0, value=0.5, step=0.05)
|
| 17 |
+
|
| 18 |
+
uploaded_file = st.file_uploader("Upload gambar", type=["jpg", "jpeg", "png"])
|
| 19 |
+
|
| 20 |
+
if uploaded_file is not None:
|
| 21 |
+
# Baca gambar
|
| 22 |
+
image = Image.open(uploaded_file)
|
| 23 |
+
img_array = np.array(image)
|
| 24 |
+
|
| 25 |
+
# Jalankan deteksi dengan pengaturan confidence threshold
|
| 26 |
+
results = model_sawit(img_array, conf=confidence_threshold)
|
| 27 |
+
|
| 28 |
+
# Tambahkan bounding box dan nomor urut dengan background warna
|
| 29 |
+
for i, box in enumerate(results[0].boxes):
|
| 30 |
+
x1, y1, x2, y2 = map(int, box.xyxy[0])
|
| 31 |
+
# Gambar bounding box
|
| 32 |
+
cv2.rectangle(img_array, (x1, y1), (x2, y2), (0, 0, 255), 2) # Gunakan warna merah
|
| 33 |
+
|
| 34 |
+
# Tambahkan nomor urut dengan background
|
| 35 |
+
text = f"{i+1}"
|
| 36 |
+
font = cv2.FONT_HERSHEY_SIMPLEX
|
| 37 |
+
font_scale = 0.9
|
| 38 |
+
thickness = 2
|
| 39 |
+
|
| 40 |
+
# Ukuran teks dan lokasi
|
| 41 |
+
(text_width, text_height), baseline = cv2.getTextSize(text, font, font_scale, thickness)
|
| 42 |
+
text_x, text_y = x1, y1 - 10 # Posisi teks di atas kotak
|
| 43 |
+
rect_x1, rect_y1 = text_x, text_y - text_height - 5
|
| 44 |
+
rect_x2, rect_y2 = text_x + text_width + 10, text_y + baseline - 5
|
| 45 |
+
|
| 46 |
+
# Gambar background untuk teks
|
| 47 |
+
cv2.rectangle(img_array, (rect_x1, rect_y1), (rect_x2, rect_y2), (0, 0, 255), -1) # Background merah
|
| 48 |
+
cv2.putText(img_array, text, (text_x, text_y), font, font_scale, (255, 255, 255), thickness) # Teks putih
|
| 49 |
+
|
| 50 |
+
# Tampilkan hasil
|
| 51 |
+
st.image(img_array, caption=f"Total objek terdeteksi: {len(results[0].boxes)}", use_column_width=True)
|
| 52 |
+
|
| 53 |
+
elif menu == "Deteksi Warna Apel":
|
| 54 |
+
st.title("Deteksi Warna Apel dengan YOLOv8")
|
| 55 |
+
st.subheader("Unggah gambar apel untuk mendeteksi dan menampilkan hasil crop sesuai warna")
|
| 56 |
+
|
| 57 |
+
# Upload gambar
|
| 58 |
+
uploaded_file = st.file_uploader("Unggah gambar", type=["jpg", "jpeg", "png"])
|
| 59 |
+
|
| 60 |
+
if uploaded_file is not None:
|
| 61 |
+
# Baca gambar dari upload
|
| 62 |
+
file_bytes = np.asarray(bytearray(uploaded_file.read()), dtype=np.uint8)
|
| 63 |
+
image = cv2.imdecode(file_bytes, cv2.IMREAD_COLOR)
|
| 64 |
+
|
| 65 |
+
# Deteksi objek menggunakan YOLOv8
|
| 66 |
+
results = model_apel(image)[0] # Ambil hasil prediksi pertama
|
| 67 |
+
|
| 68 |
+
# Tampilkan gambar asli
|
| 69 |
+
st.image(cv2.cvtColor(image, cv2.COLOR_BGR2RGB), caption="Gambar Asli", use_column_width=True)
|
| 70 |
+
|
| 71 |
+
st.write("### Hasil Crop:")
|
| 72 |
+
for i, box in enumerate(results.boxes):
|
| 73 |
+
cls = int(box.cls) # Indeks kelas
|
| 74 |
+
confidence = box.conf.item() # Tingkat kepercayaan
|
| 75 |
+
class_name = model_apel.names[cls] # Nama kelas (e.g., yellow, green, red)
|
| 76 |
+
|
| 77 |
+
# Bounding box koordinat
|
| 78 |
+
x1, y1, x2, y2 = map(int, box.xyxy[0].tolist())
|
| 79 |
+
cropped_image = image[y1:y2, x1:x2]
|
| 80 |
+
|
| 81 |
+
# Konversi ke RGB untuk ditampilkan di Streamlit
|
| 82 |
+
cropped_image_rgb = cv2.cvtColor(cropped_image, cv2.COLOR_BGR2RGB)
|
| 83 |
+
|
| 84 |
+
# Tampilkan hasil crop
|
| 85 |
+
st.image(cropped_image_rgb, caption=f"{class_name} ({confidence:.2f})", use_column_width=False)
|