Update app.py
Browse files
app.py
CHANGED
@@ -32,40 +32,35 @@ def detect_objects(image):
|
|
32 |
|
33 |
for prediction in predictions['predictions']:
|
34 |
class_name = prediction['class']
|
35 |
-
|
|
|
|
|
|
|
36 |
total_count += 1 # Tambah jumlah objek untuk setiap prediksi
|
37 |
|
38 |
# Menyusun output berupa string hasil perhitungan
|
39 |
result_text = "Product Nestle\n\n"
|
|
|
40 |
for class_name, count in class_count.items():
|
41 |
-
result_text += f"{class_name}: {count}\n"
|
42 |
-
|
|
|
43 |
|
44 |
# Menyimpan gambar dengan prediksi
|
45 |
-
|
46 |
-
model.predict(temp_file_path, confidence=60, overlap=80).save(output_image_path)
|
47 |
|
48 |
# Hapus file sementara setelah prediksi
|
49 |
os.remove(temp_file_path)
|
50 |
|
51 |
-
return
|
52 |
|
53 |
-
# Membuat antarmuka Gradio
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
with gr.Column():
|
61 |
-
output_text = gr.Textbox(label="Counting Object")
|
62 |
-
|
63 |
-
# Hubungkan input dengan fungsi deteksi
|
64 |
-
input_image.change(
|
65 |
-
fn=detect_objects,
|
66 |
-
inputs=input_image,
|
67 |
-
outputs=[output_image, output_text]
|
68 |
-
)
|
69 |
|
70 |
# Menjalankan antarmuka
|
71 |
-
iface.launch()
|
|
|
32 |
|
33 |
for prediction in predictions['predictions']:
|
34 |
class_name = prediction['class']
|
35 |
+
if class_name in class_count:
|
36 |
+
class_count[class_name] += 1
|
37 |
+
else:
|
38 |
+
class_count[class_name] = 1
|
39 |
total_count += 1 # Tambah jumlah objek untuk setiap prediksi
|
40 |
|
41 |
# Menyusun output berupa string hasil perhitungan
|
42 |
result_text = "Product Nestle\n\n"
|
43 |
+
|
44 |
for class_name, count in class_count.items():
|
45 |
+
result_text += f"{class_name}: {count} \n"
|
46 |
+
|
47 |
+
result_text += f"\nTotal Product Nestle: {total_count}"
|
48 |
|
49 |
# Menyimpan gambar dengan prediksi
|
50 |
+
output_image = model.predict(temp_file_path, confidence=60, overlap=80).save("/tmp/prediction.jpg")
|
|
|
51 |
|
52 |
# Hapus file sementara setelah prediksi
|
53 |
os.remove(temp_file_path)
|
54 |
|
55 |
+
return "/tmp/prediction.jpg", result_text
|
56 |
|
57 |
+
# Membuat antarmuka Gradio
|
58 |
+
iface = gr.Interface(
|
59 |
+
fn=detect_objects, # Fungsi yang dipanggil saat gambar diupload
|
60 |
+
inputs=gr.Image(type="pil"), # Input berupa gambar
|
61 |
+
outputs=[gr.Image(), gr.Textbox()], # Output gambar dan teks
|
62 |
+
live=True # Menampilkan hasil secara langsung
|
63 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
64 |
|
65 |
# Menjalankan antarmuka
|
66 |
+
iface.launch()
|