Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| import cv2 | |
| from PIL import Image | |
| import numpy as np | |
| from ultralytics import YOLO | |
| # Load YOLO models | |
| model_sawit = YOLO("best_yolov8.pt") # Ganti dengan path model YOLOv8 untuk pohon sawit | |
| model_apel = YOLO("best_apple.pt") # Ganti dengan path model YOLOv8 untuk apel | |
| # Sidebar menu | |
| menu = st.sidebar.selectbox("Pilih Aplikasi", ["Deteksi Pohon Sawit", "Deteksi Warna Apel"]) | |
| if menu == "Deteksi Pohon Sawit": | |
| st.title("Deteksi Pohon Sawit dengan YOLOv8") | |
| confidence_threshold = st.slider("Confidence Threshold", min_value=0.0, max_value=1.0, value=0.5, step=0.05) | |
| uploaded_file = st.file_uploader("Upload gambar", type=["jpg", "jpeg", "png"]) | |
| if uploaded_file is not None: | |
| # Baca gambar | |
| image = Image.open(uploaded_file) | |
| img_array = np.array(image) | |
| # Jalankan deteksi dengan pengaturan confidence threshold | |
| results = model_sawit(img_array, conf=confidence_threshold) | |
| # Tambahkan bounding box dan nomor urut dengan background warna | |
| for i, box in enumerate(results[0].boxes): | |
| x1, y1, x2, y2 = map(int, box.xyxy[0]) | |
| # Gambar bounding box | |
| cv2.rectangle(img_array, (x1, y1), (x2, y2), (0, 0, 255), 2) # Gunakan warna merah | |
| # Tambahkan nomor urut dengan background | |
| text = f"{i+1}" | |
| font = cv2.FONT_HERSHEY_SIMPLEX | |
| font_scale = 0.9 | |
| thickness = 2 | |
| # Ukuran teks dan lokasi | |
| (text_width, text_height), baseline = cv2.getTextSize(text, font, font_scale, thickness) | |
| text_x, text_y = x1, y1 - 10 # Posisi teks di atas kotak | |
| rect_x1, rect_y1 = text_x, text_y - text_height - 5 | |
| rect_x2, rect_y2 = text_x + text_width + 10, text_y + baseline - 5 | |
| # Gambar background untuk teks | |
| cv2.rectangle(img_array, (rect_x1, rect_y1), (rect_x2, rect_y2), (0, 0, 255), -1) # Background merah | |
| cv2.putText(img_array, text, (text_x, text_y), font, font_scale, (255, 255, 255), thickness) # Teks putih | |
| # Tampilkan hasil | |
| st.image(img_array, caption=f"Total objek terdeteksi: {len(results[0].boxes)}", use_column_width=True) | |
| elif menu == "Deteksi Warna Apel": | |
| st.title("Deteksi Warna Apel dengan YOLOv8") | |
| st.subheader("Unggah gambar apel untuk mendeteksi dan menampilkan hasil crop sesuai warna") | |
| # Upload gambar | |
| uploaded_file = st.file_uploader("Unggah gambar", type=["jpg", "jpeg", "png"]) | |
| if uploaded_file is not None: | |
| # Baca gambar dari upload | |
| file_bytes = np.asarray(bytearray(uploaded_file.read()), dtype=np.uint8) | |
| image = cv2.imdecode(file_bytes, cv2.IMREAD_COLOR) | |
| # Deteksi objek menggunakan YOLOv8 | |
| results = model_apel(image)[0] # Ambil hasil prediksi pertama | |
| # Tampilkan gambar asli | |
| st.image(cv2.cvtColor(image, cv2.COLOR_BGR2RGB), caption="Gambar Asli", use_column_width=True) | |
| st.write("### Hasil Crop:") | |
| for i, box in enumerate(results.boxes): | |
| cls = int(box.cls) # Indeks kelas | |
| confidence = box.conf.item() # Tingkat kepercayaan | |
| class_name = model_apel.names[cls] # Nama kelas (e.g., yellow, green, red) | |
| # Bounding box koordinat | |
| x1, y1, x2, y2 = map(int, box.xyxy[0].tolist()) | |
| cropped_image = image[y1:y2, x1:x2] | |
| # Konversi ke RGB untuk ditampilkan di Streamlit | |
| cropped_image_rgb = cv2.cvtColor(cropped_image, cv2.COLOR_BGR2RGB) | |
| # Tampilkan hasil crop | |
| st.image(cropped_image_rgb, caption=f"{class_name} ({confidence:.2f})", use_column_width=False) | |