muhammadsalmanalfaridzi commited on
Commit
2dc359d
·
verified ·
1 Parent(s): 31d24e2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -13
app.py CHANGED
@@ -1,17 +1,19 @@
1
  import gradio as gr
2
- from dotenv import load_dotenv
3
- from roboflow import Roboflow
4
- import tempfile
5
  import os
 
 
 
6
 
7
  # Muat variabel lingkungan dari file .env
8
  load_dotenv()
 
 
9
  api_key = os.getenv("ROBOFLOW_API_KEY")
10
  workspace = os.getenv("ROBOFLOW_WORKSPACE")
11
  project_name = os.getenv("ROBOFLOW_PROJECT")
12
  model_version = int(os.getenv("ROBOFLOW_MODEL_VERSION"))
13
 
14
- # Inisialisasi Roboflow menggunakan data yang diambil dari secrets
15
  rf = Roboflow(api_key=api_key)
16
  project = rf.workspace(workspace).project(project_name)
17
  model = project.version(model_version).model
@@ -39,12 +41,11 @@ def detect_objects(image):
39
  total_count += 1 # Tambah jumlah objek untuk setiap prediksi
40
 
41
  # Menyusun output berupa string hasil perhitungan
42
- result_text = "Product Nestle \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")
@@ -54,13 +55,14 @@ def detect_objects(image):
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()
 
1
  import gradio as gr
 
 
 
2
  import os
3
+ import tempfile
4
+ from roboflow import Roboflow
5
+ from dotenv import load_dotenv
6
 
7
  # Muat variabel lingkungan dari file .env
8
  load_dotenv()
9
+
10
+ # Ambil nilai dari environment variables
11
  api_key = os.getenv("ROBOFLOW_API_KEY")
12
  workspace = os.getenv("ROBOFLOW_WORKSPACE")
13
  project_name = os.getenv("ROBOFLOW_PROJECT")
14
  model_version = int(os.getenv("ROBOFLOW_MODEL_VERSION"))
15
 
16
+ # Inisialisasi Roboflow menggunakan data yang diambil dari .env
17
  rf = Roboflow(api_key=api_key)
18
  project = rf.workspace(workspace).project(project_name)
19
  model = project.version(model_version).model
 
41
  total_count += 1 # Tambah jumlah objek untuk setiap prediksi
42
 
43
  # Menyusun output berupa string hasil perhitungan
44
+ result_text = "Product Nestle\n\n" # Tambahkan baris kosong setelah judul
 
45
  for class_name, count in class_count.items():
46
  result_text += f"{class_name}: {count} \n"
47
 
48
+ result_text += f"\nTotal Product Nestle: {total_count}" # Tambahkan baris kosong antara kategori dan total
49
 
50
  # Menyimpan gambar dengan prediksi
51
  output_image = model.predict(temp_file_path, confidence=60, overlap=80).save("/tmp/prediction.jpg")
 
55
 
56
  return "/tmp/prediction.jpg", result_text
57
 
58
+ # Membuat antarmuka Gradio dengan label yang telah diganti
59
  iface = gr.Interface(
60
+ fn=detect_objects,
61
+ inputs=gr.Image(type="pil", label="Input Image"),
62
+ outputs=[gr.Image(label="Detect Object"), gr.Textbox(label="Counting Object")],
63
+ live=True,
64
+ layout="horizontal"
65
  )
66
 
67
  # Menjalankan antarmuka
68
+ iface.launch()