Spaces:
Sleeping
Sleeping
import streamlit as st | |
import cv2 | |
import io | |
import numpy as np | |
from inference_sdk import InferenceHTTPClient | |
# Fungsi untuk mendeteksi penyakit daun padi | |
def detect_leaf_diseases(image_bytes): | |
# Konversi BytesIO ke numpy array | |
image_array = cv2.imdecode(np.frombuffer(image_bytes, np.uint8), cv2.IMREAD_COLOR) | |
# Inisialisasi klien InferenceHTTPClient dengan API URL dan API key | |
CLIENT = InferenceHTTPClient( | |
api_url="https://detect.roboflow.com", | |
api_key="YOUR_API_KEY" | |
) | |
# Lakukan inferensi menggunakan model deteksi penyakit daun padi | |
result = CLIENT.infer(image_array, model_id="your_model_id") | |
# Dapatkan prediksi dari hasil inferensi | |
predictions = result.predictions | |
# Iterasi melalui prediksi dan tampilkan label dan kotak batas | |
for prediction in predictions: | |
label = prediction.label | |
box = prediction.box | |
confidence = prediction.confidence | |
# Tampilkan label dan kotak batas | |
cv2.rectangle(image_array, (box[0], box[1]), (box[2], box[3]), (0, 255, 0), 2) | |
cv2.putText(image_array, label, (box[0], box[1] - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2) | |
return image_array | |
# Judul halaman | |
st.title("Deteksi Penyakit Daun Padi") | |
# Deskripsi singkat | |
st.write("Unggah gambar daun padi untuk mendeteksi penyakit.") | |
# Input file gambar | |
uploaded_file = st.file_uploader("Pilih gambar daun padi:", type=["jpg", "jpeg", "png"]) | |
if uploaded_file is not None: | |
# Baca gambar dari file yang diunggah | |
image_bytes = uploaded_file.read() | |
if image_bytes is not None: | |
# Deteksi penyakit daun padi | |
detected_image = detect_leaf_diseases(image_bytes) | |
# Konversi numpy array ke BytesIO untuk ditampilkan di Streamlit | |
detected_image_bytes = cv2.imencode(".jpg", detected_image)[1].tobytes() | |
detected_image_bytes_io = io.BytesIO(detected_image_bytes) | |
# Tampilkan gambar asli dan gambar dengan deteksi | |
st.image(uploaded_file, caption="Gambar Asli", use_column_width=True) | |
st.image(detected_image_bytes_io, caption="Gambar dengan Deteksi Penyakit", use_column_width=True) | |
else: | |
st.error("Gagal membaca gambar. Pastikan file yang diunggah adalah gambar yang valid.") | |
else: | |
st.warning("Silakan unggah gambar daun padi untuk mendeteksi penyakit.") |