|
import gradio as gr |
|
from roboflow import Roboflow |
|
import tempfile |
|
import os |
|
|
|
|
|
rf = Roboflow(api_key="Otg64Ra6wNOgDyjuhMYU") |
|
project = rf.workspace("alat-pelindung-diri").project("nescafe-4base") |
|
model = project.version(16).model |
|
|
|
|
|
def detect_objects(image): |
|
|
|
with tempfile.NamedTemporaryFile(delete=False, suffix=".jpg") as temp_file: |
|
image.save(temp_file, format="JPEG") |
|
temp_file_path = temp_file.name |
|
|
|
|
|
predictions = model.predict(temp_file_path, confidence=50, overlap=30).json() |
|
|
|
|
|
class_count = {} |
|
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 |
|
|
|
|
|
result_text = "Jumlah objek per kelas:\n" |
|
for class_name, count in class_count.items(): |
|
result_text += f"{class_name}: {count} objek\n" |
|
|
|
|
|
output_image = model.predict(temp_file_path, confidence=50, overlap=30).save("/tmp/prediction.jpg") |
|
|
|
|
|
os.remove(temp_file_path) |
|
|
|
return "/tmp/prediction.jpg", result_text |
|
|
|
|
|
iface = gr.Interface( |
|
fn=detect_objects, |
|
inputs=gr.Image(type="pil"), |
|
outputs=[gr.Image(), gr.Textbox()], |
|
live=True |
|
) |
|
|
|
|
|
iface.launch() |
|
|