muhammadsalmanalfaridzi commited on
Commit
c66a6f5
·
verified ·
1 Parent(s): 6dbde7c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -6
app.py CHANGED
@@ -4,6 +4,7 @@ from roboflow import Roboflow
4
  import tempfile
5
  import os
6
  import requests
 
7
 
8
  # Muat variabel lingkungan dari file .env
9
  load_dotenv()
@@ -25,15 +26,22 @@ def detect_objects(image):
25
  temp_file_path = temp_file.name
26
 
27
  try:
28
- # Lakukan prediksi pada gambar
29
- predictions = model.predict(temp_file_path, confidence=60, overlap=80).json()
 
 
 
 
 
 
 
30
 
31
  # Menghitung jumlah objek per kelas
32
  class_count = {}
33
  total_count = 0 # Menyimpan total jumlah objek
34
 
35
- for prediction in predictions['predictions']:
36
- class_name = prediction['class']
37
  class_count[class_name] = class_count.get(class_name, 0) + 1
38
  total_count += 1 # Tambah jumlah objek untuk setiap prediksi
39
 
@@ -45,8 +53,9 @@ def detect_objects(image):
45
 
46
  # Menyimpan gambar dengan prediksi
47
  output_image_path = "/tmp/prediction.jpg"
48
- model.predict(temp_file_path, confidence=60, overlap=80).save(output_image_path)
49
-
 
50
  except requests.exceptions.HTTPError as http_err:
51
  # Menangani kesalahan HTTP
52
  result_text = f"HTTP error occurred: {http_err}"
 
4
  import tempfile
5
  import os
6
  import requests
7
+ from sahi.predict import get_sliced_prediction # SAHI slicing inference
8
 
9
  # Muat variabel lingkungan dari file .env
10
  load_dotenv()
 
26
  temp_file_path = temp_file.name
27
 
28
  try:
29
+ # Perform sliced inference with SAHI
30
+ result = get_sliced_prediction(
31
+ temp_file_path,
32
+ model,
33
+ slice_height=256, # Adjust as needed
34
+ slice_width=256, # Adjust as needed
35
+ overlap_height_ratio=0.2, # Adjust as needed
36
+ overlap_width_ratio=0.2 # Adjust as needed
37
+ )
38
 
39
  # Menghitung jumlah objek per kelas
40
  class_count = {}
41
  total_count = 0 # Menyimpan total jumlah objek
42
 
43
+ for prediction in result.object_prediction_list:
44
+ class_name = prediction.class_id # or prediction.class_name if available
45
  class_count[class_name] = class_count.get(class_name, 0) + 1
46
  total_count += 1 # Tambah jumlah objek untuk setiap prediksi
47
 
 
53
 
54
  # Menyimpan gambar dengan prediksi
55
  output_image_path = "/tmp/prediction.jpg"
56
+ result.export_visuals(export_dir="/tmp/") # Export visuals for display
57
+ output_image_path = "/tmp/prediction_visual.png" # Assuming the visual output is saved here
58
+
59
  except requests.exceptions.HTTPError as http_err:
60
  # Menangani kesalahan HTTP
61
  result_text = f"HTTP error occurred: {http_err}"