muhammadsalmanalfaridzi commited on
Commit
ae7d8a2
·
verified ·
1 Parent(s): 92a3dd5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -20
app.py CHANGED
@@ -1,24 +1,16 @@
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
18
 
19
  # Fungsi untuk menangani input dan output gambar
20
  def detect_objects(image):
21
- # Simpan gambar yang diupload sebagai file sementara
22
  with tempfile.NamedTemporaryFile(delete=False, suffix=".jpg") as temp_file:
23
  image.save(temp_file, format="JPEG")
24
  temp_file_path = temp_file.name
@@ -28,24 +20,22 @@ def detect_objects(image):
28
 
29
  # Menghitung jumlah objek per kelas
30
  class_count = {}
31
- total_count = 0 # Menyimpan total jumlah objek
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
 
 
1
  import gradio as gr
 
2
  from roboflow import Roboflow
3
  import tempfile
4
  import os
5
 
6
+ # Inisialisasi Roboflow
7
+ rf = Roboflow(api_key="Otg64Ra6wNOgDyjuhMYU")
8
+ project = rf.workspace("alat-pelindung-diri").project("nescafe-4base")
9
+ model = project.version(46).model
 
 
 
 
 
 
 
10
 
11
  # Fungsi untuk menangani input dan output gambar
12
  def detect_objects(image):
13
+ # Menyimpan gambar yang diupload sebagai file sementara
14
  with tempfile.NamedTemporaryFile(delete=False, suffix=".jpg") as temp_file:
15
  image.save(temp_file, format="JPEG")
16
  temp_file_path = temp_file.name
 
20
 
21
  # Menghitung jumlah objek per kelas
22
  class_count = {}
23
+ total_count = 0
 
24
  for prediction in predictions['predictions']:
25
  class_name = prediction['class']
26
  if class_name in class_count:
27
  class_count[class_name] += 1
28
  else:
29
  class_count[class_name] = 1
30
+ total_count += 1
31
 
32
  # Menyusun output berupa string hasil perhitungan
33
  result_text = "Product Nestle\n\n"
 
34
  for class_name, count in class_count.items():
35
+ result_text += f"{class_name}: {count} objek\n"
36
+
37
  result_text += f"\nTotal Product Nestle: {total_count}"
38
+
39
  # Menyimpan gambar dengan prediksi
40
  output_image = model.predict(temp_file_path, confidence=60, overlap=80).save("/tmp/prediction.jpg")
41