File size: 2,038 Bytes
ac831c4 23645fb 30a0ff7 ac831c4 94f96f8 ac831c4 998a552 ac831c4 998a552 ac831c4 b72722a e9f12fe ac831c4 998a552 b72722a ac831c4 94f96f8 ac831c4 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
import gradio as gr
from roboflow import Roboflow
import tempfile
import os
# Inisialisasi Roboflow
rf = Roboflow(api_key="Otg64Ra6wNOgDyjuhMYU")
project = rf.workspace("alat-pelindung-diri").project("nescafe-4base")
model = project.version(46).model
# Fungsi untuk menangani input dan output gambar
def detect_objects(image):
# Menyimpan 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
# Lakukan prediksi pada gambar
predictions = model.predict(temp_file_path, confidence=60, overlap=80).json()
# Menghitung jumlah objek per kelas
class_count = {}
total_count = 0 # Menyimpan total jumlah objek
for prediction in predictions['predictions']:
class_name = prediction['class']
if class_name in class_count:
class_count[class_name] += 1
else:
class_count[class_name] = 1
total_count += 1 # Tambah jumlah objek untuk setiap prediksi
# Menyusun output berupa string hasil perhitungan
result_text = "Product Nestle \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 = model.predict(temp_file_path, confidence=60, overlap=80).save("/tmp/prediction.jpg")
# Hapus file sementara setelah prediksi
os.remove(temp_file_path)
return "/tmp/prediction.jpg", result_text
# Membuat antarmuka Gradio
iface = gr.Interface(
fn=detect_objects, # Fungsi yang dipanggil saat gambar diupload
inputs=gr.Image(type="pil"), # Input berupa gambar
outputs=[gr.Image(), gr.Textbox()], # Output gambar dan teks
live=True # Menampilkan hasil secara langsung
)
# Menjalankan antarmuka
iface.launch()
|