superuser-aisensum commited on
Commit
6407b4a
·
verified ·
1 Parent(s): 13c0b84

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -18
app.py CHANGED
@@ -32,35 +32,43 @@ def detect_objects(image):
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()
 
32
 
33
  for prediction in predictions['predictions']:
34
  class_name = prediction['class']
35
+ class_count[class_name] = class_count.get(class_name, 0) + 1
 
 
 
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
+ result_text += f"\nTotal Product Nestle: {total_count}"
 
43
 
44
  # Menyimpan gambar dengan prediksi
45
+ output_image_path = "/tmp/prediction.jpg"
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 output_image_path, result_text
52
 
53
+ # Membuat antarmuka Gradio dengan tata letak fleksibel
54
+ with gr.Blocks() as iface:
55
+ with gr.Row():
56
+ with gr.Column():
57
+ input_image = gr.Image(type="pil", label="Input Image")
58
+ with gr.Column():
59
+ output_image = gr.Image(label="Detect Object")
60
+ with gr.Column():
61
+ output_text = gr.Textbox(label="Counting Object")
62
+
63
+ # Tombol untuk memproses input
64
+ detect_button = gr.Button("Detect")
65
+
66
+ # Hubungkan tombol dengan fungsi deteksi
67
+ detect_button.click(
68
+ fn=detect_objects,
69
+ inputs=input_image,
70
+ outputs=[output_image, output_text]
71
+ )
72
 
73
  # Menjalankan antarmuka
74
+ iface.launch()