muhammadsalmanalfaridzi commited on
Commit
7e0a954
·
verified ·
1 Parent(s): 1c8f85a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -30
app.py CHANGED
@@ -4,31 +4,31 @@ from roboflow import Roboflow
4
  import tempfile
5
  import os
6
 
7
- # Load environment variables from .env file
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
- # Initialize Roboflow using the loaded environment variables
15
  rf = Roboflow(api_key=api_key)
16
  project = rf.workspace(workspace).project(project_name)
17
  model = project.version(model_version).model
18
 
19
- # Function to handle image input and output
20
  def detect_objects(image):
21
- # Save the uploaded image as a temporary file
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
25
 
26
- # Perform prediction on the image
27
  predictions = model.predict(temp_file_path, confidence=60, overlap=80).json()
28
 
29
- # Count the number of objects per class
30
  class_count = {}
31
- total_count = 0 # Store the total number of objects
32
 
33
  for prediction in predictions['predictions']:
34
  class_name = prediction['class']
@@ -36,38 +36,31 @@ def detect_objects(image):
36
  class_count[class_name] += 1
37
  else:
38
  class_count[class_name] = 1
39
- total_count += 1 # Increment total object count for each prediction
40
 
41
- # Prepare the result text
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
- # Save the image with predictions
50
- output_image_path = "/tmp/prediction.jpg"
51
- model.predict(temp_file_path, confidence=60, overlap=80).save(output_image_path)
52
 
53
- # Remove the temporary file after prediction
54
  os.remove(temp_file_path)
55
 
56
- return output_image_path, result_text
57
 
58
- # Create the Gradio interface
59
- with gr.Blocks() as iface:
60
- with gr.Row():
61
- image_input = gr.Image(type="pil", label="Input Image")
62
- with gr.Row():
63
- image_output = gr.Image(label="Detect Object")
64
- with gr.Row():
65
- text_output = gr.Textbox(label="Counting Object")
66
- gr.Interface(
67
- fn=detect_objects,
68
- inputs=image_input,
69
- outputs=[image_output, text_output],
70
- )
71
 
72
- # Launch the interface
73
- iface.launch()
 
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
25
 
26
+ # Lakukan prediksi pada gambar
27
  predictions = model.predict(temp_file_path, confidence=60, overlap=80).json()
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']
 
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()