Update app.py
Browse files
app.py
CHANGED
@@ -4,31 +4,31 @@ from roboflow import Roboflow
|
|
4 |
import tempfile
|
5 |
import os
|
6 |
|
7 |
-
#
|
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 |
-
#
|
15 |
rf = Roboflow(api_key=api_key)
|
16 |
project = rf.workspace(workspace).project(project_name)
|
17 |
model = project.version(model_version).model
|
18 |
|
19 |
-
#
|
20 |
def detect_objects(image):
|
21 |
-
#
|
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 |
-
#
|
27 |
predictions = model.predict(temp_file_path, confidence=60, overlap=80).json()
|
28 |
|
29 |
-
#
|
30 |
class_count = {}
|
31 |
-
total_count = 0 #
|
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 #
|
40 |
|
41 |
-
#
|
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 |
-
#
|
50 |
-
|
51 |
-
model.predict(temp_file_path, confidence=60, overlap=80).save(output_image_path)
|
52 |
|
53 |
-
#
|
54 |
os.remove(temp_file_path)
|
55 |
|
56 |
-
return
|
57 |
|
58 |
-
#
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
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 |
-
#
|
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()
|