muhammadsalmanalfaridzi commited on
Commit
3e8a5f8
·
verified ·
1 Parent(s): 3d78ecd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -32
app.py CHANGED
@@ -2,54 +2,62 @@ import gradio as gr
2
  from roboflow import Roboflow
3
  import tempfile
4
  import os
5
- import cv2
6
- import supervision as sv
7
- import numpy as np
8
 
9
- # Inisialisasi Roboflow
10
  rf = Roboflow(api_key="Otg64Ra6wNOgDyjuhMYU")
11
  project = rf.workspace("alat-pelindung-diri").project("nescafe-4base")
12
  model = project.version(16).model
13
 
14
- # Fungsi untuk deteksi objek dengan supervision InferenceSlicer
15
  def detect_objects(image):
16
- # Simpan gambar sementara
17
  with tempfile.NamedTemporaryFile(delete=False, suffix=".jpg") as temp_file:
18
  image.save(temp_file, format="JPEG")
19
  temp_file_path = temp_file.name
20
 
21
- # Membaca gambar dengan OpenCV
22
- img = cv2.imread(temp_file_path)
 
 
 
 
 
 
 
 
23
 
24
- # Callback function untuk model prediksi
25
- def callback(image_slice: np.ndarray) -> sv.Detections:
26
- # Lakukan inferensi pada setiap potongan gambar
27
- predictions = model.predict(image_slice, confidence=50, overlap=30).json()
28
- return sv.Detections.from_inference(predictions)
 
 
 
 
 
 
29
 
30
- # Menggunakan InferenceSlicer
31
- slicer = sv.InferenceSlicer(callback=callback, overlap_wh=(0.2, 0.2)) # Replacing overlap_ratio_wh
 
 
 
 
32
 
33
- # Proses gambar dengan slicer
34
- detections = slicer(img)
35
 
36
- # Filter deteksi yang tumpang tindih (gunakan NMM atau tanpa filter)
37
- overlap_filter = sv.OverlapFilter(strategy=sv.OverlapFilter.NON_MAX_MERGE, iou_threshold=0.5)
38
- filtered_detections = overlap_filter.filter(detections)
39
-
40
- # Annotasi gambar dengan deteksi
41
- annotated_image = sv.BoxAnnotator().annotate(scene=img.copy(), detections=filtered_detections)
42
-
43
- # Simpan gambar dengan prediksi
44
- output_path = "/tmp/prediction.jpg"
45
- cv2.imwrite(output_path, annotated_image)
46
-
47
- # Hapus file sementara
48
- os.remove(temp_file_path)
49
 
50
  # Menghitung jumlah objek per kelas
51
  class_count = {}
52
- for detection in filtered_detections:
53
  class_name = detection.class_name
54
  if class_name in class_count:
55
  class_count[class_name] += 1
@@ -61,7 +69,10 @@ def detect_objects(image):
61
  for class_name, count in class_count.items():
62
  result_text += f"{class_name}: {count} objek\n"
63
 
64
- return output_path, result_text
 
 
 
65
 
66
  # Membuat antarmuka Gradio
67
  iface = gr.Interface(
 
2
  from roboflow import Roboflow
3
  import tempfile
4
  import os
5
+ from sahi.slicing import slice_image
6
+ from sahi import get_sliced_prediction
7
+ from sahi.postprocess import postprocess_predictions
8
 
9
+ # Inisialisasi Roboflow (for model path)
10
  rf = Roboflow(api_key="Otg64Ra6wNOgDyjuhMYU")
11
  project = rf.workspace("alat-pelindung-diri").project("nescafe-4base")
12
  model = project.version(16).model
13
 
14
+ # Fungsi untuk deteksi objek menggunakan SAHI
15
  def detect_objects(image):
16
+ # Menyimpan gambar sementara
17
  with tempfile.NamedTemporaryFile(delete=False, suffix=".jpg") as temp_file:
18
  image.save(temp_file, format="JPEG")
19
  temp_file_path = temp_file.name
20
 
21
+ # Slice gambar menjadi potongan-potongan kecil
22
+ slice_image_result = slice_image(
23
+ image=temp_file_path,
24
+ output_file_name="sliced_image",
25
+ output_dir="/tmp/sliced/",
26
+ slice_height=256,
27
+ slice_width=256,
28
+ overlap_height_ratio=0.1,
29
+ overlap_width_ratio=0.1
30
+ )
31
 
32
+ # Jalankan prediksi pada setiap potongan gambar
33
+ predictions = get_sliced_prediction(
34
+ image_path=temp_file_path,
35
+ model_path="path_to_your_model",
36
+ model_type="yolov5", # Adjust based on your model
37
+ slice_height=256,
38
+ slice_width=256,
39
+ overlap_height_ratio=0.1,
40
+ overlap_width_ratio=0.1,
41
+ model_confidence_threshold=0.25
42
+ )
43
 
44
+ # Postprocess dan gabungkan hasil prediksi
45
+ postprocessed_predictions = postprocess_predictions(
46
+ predictions=predictions,
47
+ postprocess_type='NMS',
48
+ iou_threshold=0.5
49
+ )
50
 
51
+ # Annotate gambar dengan hasil prediksi
52
+ annotated_image = model.annotate_image_with_predictions(temp_file_path, postprocessed_predictions)
53
 
54
+ # Simpan gambar hasil annotasi
55
+ output_image_path = "/tmp/prediction.jpg"
56
+ annotated_image.save(output_image_path)
 
 
 
 
 
 
 
 
 
 
57
 
58
  # Menghitung jumlah objek per kelas
59
  class_count = {}
60
+ for detection in postprocessed_predictions:
61
  class_name = detection.class_name
62
  if class_name in class_count:
63
  class_count[class_name] += 1
 
69
  for class_name, count in class_count.items():
70
  result_text += f"{class_name}: {count} objek\n"
71
 
72
+ # Hapus file sementara
73
+ os.remove(temp_file_path)
74
+
75
+ return output_image_path, result_text
76
 
77
  # Membuat antarmuka Gradio
78
  iface = gr.Interface(