muhammadsalmanalfaridzi commited on
Commit
c5bbcf3
·
verified ·
1 Parent(s): 1ea7f62

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -2
app.py CHANGED
@@ -5,6 +5,7 @@ import os
5
  from sahi.slicing import slice_image
6
  import numpy as np
7
  import cv2
 
8
 
9
  # Inisialisasi Roboflow (for model path)
10
  rf = Roboflow(api_key="Otg64Ra6wNOgDyjuhMYU")
@@ -95,8 +96,27 @@ def detect_objects(image):
95
  # Aplikasikan NMS untuk menghapus duplikat deteksi
96
  postprocessed_predictions = apply_nms(all_predictions, iou_threshold=0.5)
97
 
98
- # Annotate gambar dengan hasil prediksi
99
- annotated_image = model.annotate_image_with_predictions(temp_file_path, postprocessed_predictions)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
100
 
101
  # Simpan gambar hasil annotasi
102
  output_image_path = "/tmp/prediction.jpg"
 
5
  from sahi.slicing import slice_image
6
  import numpy as np
7
  import cv2
8
+ from PIL import Image, ImageDraw
9
 
10
  # Inisialisasi Roboflow (for model path)
11
  rf = Roboflow(api_key="Otg64Ra6wNOgDyjuhMYU")
 
96
  # Aplikasikan NMS untuk menghapus duplikat deteksi
97
  postprocessed_predictions = apply_nms(all_predictions, iou_threshold=0.5)
98
 
99
+ # Annotate gambar dengan hasil prediksi menggunakan OpenCV
100
+ img = cv2.imread(temp_file_path)
101
+ for prediction in postprocessed_predictions:
102
+ class_name = prediction['class']
103
+ bbox = prediction['bbox']
104
+ confidence = prediction['confidence']
105
+
106
+ # Unpack the bounding box coordinates
107
+ x, y, w, h = map(int, bbox)
108
+
109
+ # Draw the bounding box and label on the image
110
+ color = (0, 255, 0) # Green color for the box
111
+ thickness = 2
112
+ cv2.rectangle(img, (x, y), (x + w, y + h), color, thickness)
113
+
114
+ label = f"{class_name}: {confidence:.2f}"
115
+ cv2.putText(img, label, (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, thickness)
116
+
117
+ # Convert the image from BGR to RGB for PIL compatibility
118
+ img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
119
+ annotated_image = Image.fromarray(img_rgb)
120
 
121
  # Simpan gambar hasil annotasi
122
  output_image_path = "/tmp/prediction.jpg"