Spaces:
Build error
Build error
import gradio as gr | |
from dotenv import load_dotenv | |
from roboflow import Roboflow | |
import tempfile | |
import os | |
import requests | |
from sahi.predict import get_sliced_prediction # SAHI slicing inference | |
# Muat variabel lingkungan dari file .env | |
load_dotenv() | |
api_key = os.getenv("ROBOFLOW_API_KEY") | |
workspace = os.getenv("ROBOFLOW_WORKSPACE") | |
project_name = os.getenv("ROBOFLOW_PROJECT") | |
model_version = int(os.getenv("ROBOFLOW_MODEL_VERSION")) | |
# Inisialisasi Roboflow menggunakan data yang diambil dari secrets | |
rf = Roboflow(api_key=api_key) | |
project = rf.workspace(workspace).project(project_name) | |
model = project.version(model_version).model | |
# Fungsi untuk menangani input dan output gambar | |
def detect_objects(image): | |
# Simpan gambar yang diupload sebagai file sementara | |
with tempfile.NamedTemporaryFile(delete=False, suffix=".jpg") as temp_file: | |
image.save(temp_file, format="JPEG") | |
temp_file_path = temp_file.name | |
try: | |
# Perform sliced inference with SAHI | |
result = get_sliced_prediction( | |
temp_file_path, | |
model, | |
slice_height=256, # Adjust as needed | |
slice_width=256, # Adjust as needed | |
overlap_height_ratio=0.2, # Adjust as needed | |
overlap_width_ratio=0.2 # Adjust as needed | |
) | |
# Menghitung jumlah objek per kelas | |
class_count = {} | |
total_count = 0 # Menyimpan total jumlah objek | |
for prediction in result.object_prediction_list: | |
class_name = prediction.class_id # or prediction.class_name if available | |
class_count[class_name] = class_count.get(class_name, 0) + 1 | |
total_count += 1 # Tambah jumlah objek untuk setiap prediksi | |
# Menyusun output berupa string hasil perhitungan | |
result_text = "Product Nestle\n\n" | |
for class_name, count in class_count.items(): | |
result_text += f"{class_name}: {count}\n" | |
result_text += f"\nTotal Product Nestle: {total_count}" | |
# Menyimpan gambar dengan prediksi | |
output_image_path = "/tmp/prediction.jpg" | |
result.export_visuals(export_dir="/tmp/") # Export visuals for display | |
output_image_path = "/tmp/prediction_visual.png" # Assuming the visual output is saved here | |
except requests.exceptions.HTTPError as http_err: | |
# Menangani kesalahan HTTP | |
result_text = f"HTTP error occurred: {http_err}" | |
output_image_path = temp_file_path # Kembalikan gambar asli jika terjadi error | |
except Exception as err: | |
# Menangani kesalahan lain | |
result_text = f"An error occurred: {err}" | |
output_image_path = temp_file_path # Kembalikan gambar asli jika terjadi error | |
# Hapus file sementara setelah prediksi | |
os.remove(temp_file_path) | |
return output_image_path, result_text | |
# Membuat antarmuka Gradio dengan tata letak fleksibel | |
with gr.Blocks() as iface: | |
with gr.Row(): | |
with gr.Column(): | |
input_image = gr.Image(type="pil", label="Input Image") | |
with gr.Column(): | |
output_image = gr.Image(label="Detect Object") | |
with gr.Column(): | |
output_text = gr.Textbox(label="Counting Object") | |
# Tombol untuk memproses input | |
detect_button = gr.Button("Detect") | |
# Hubungkan tombol dengan fungsi deteksi | |
detect_button.click( | |
fn=detect_objects, | |
inputs=input_image, | |
outputs=[output_image, output_text] | |
) | |
# Menjalankan antarmuka | |
iface.launch() | |