|
import gradio as gr |
|
from roboflow import Roboflow |
|
import tempfile |
|
import os |
|
from sahi.slicing import slice_image |
|
import numpy as np |
|
import cv2 |
|
from PIL import Image, ImageDraw |
|
|
|
|
|
rf = Roboflow(api_key="Otg64Ra6wNOgDyjuhMYU") |
|
project = rf.workspace("alat-pelindung-diri").project("nescafe-4base") |
|
model = project.version(16).model |
|
|
|
def apply_nms(predictions, iou_threshold=0.5): |
|
boxes = [] |
|
scores = [] |
|
classes = [] |
|
|
|
|
|
for prediction in predictions: |
|
|
|
x = prediction['x'] |
|
y = prediction['y'] |
|
width = prediction['width'] |
|
height = prediction['height'] |
|
box = [x, y, width, height] |
|
|
|
boxes.append(box) |
|
scores.append(prediction['confidence']) |
|
classes.append(prediction['class']) |
|
|
|
boxes = np.array(boxes) |
|
scores = np.array(scores) |
|
classes = np.array(classes) |
|
|
|
|
|
indices = cv2.dnn.NMSBoxes(boxes.tolist(), scores.tolist(), score_threshold=0.25, nms_threshold=iou_threshold) |
|
|
|
print(f"Predictions before NMS: {predictions}") |
|
print(f"Indices after NMS: {indices}") |
|
|
|
|
|
if indices is None or len(indices) == 0: |
|
print("No valid indices returned from NMS.") |
|
return [] |
|
|
|
|
|
indices = indices.flatten() |
|
|
|
nms_predictions = [] |
|
|
|
for i in indices: |
|
nms_predictions.append({ |
|
'class': classes[i], |
|
'bbox': boxes[i], |
|
'confidence': scores[i] |
|
}) |
|
|
|
return nms_predictions |
|
|
|
|
|
def detect_objects(image): |
|
|
|
with tempfile.NamedTemporaryFile(delete=False, suffix=".jpg") as temp_file: |
|
image.save(temp_file, format="JPEG") |
|
temp_file_path = temp_file.name |
|
|
|
|
|
slice_image_result = slice_image( |
|
image=temp_file_path, |
|
output_file_name="sliced_image", |
|
output_dir="/tmp/sliced/", |
|
slice_height=256, |
|
slice_width=256, |
|
overlap_height_ratio=0.1, |
|
overlap_width_ratio=0.1 |
|
) |
|
|
|
|
|
print(f"Slice result: {slice_image_result}") |
|
|
|
|
|
try: |
|
sliced_image_paths = slice_image_result.sliced_image_paths |
|
print(f"Sliced image paths: {sliced_image_paths}") |
|
except AttributeError: |
|
print("Failed to access sliced_image_paths attribute.") |
|
sliced_image_paths = [] |
|
|
|
|
|
print("Predicting on the whole image (without slicing)...") |
|
whole_image_predictions = model.predict(image_path=temp_file_path).json() |
|
print(f"Whole image predictions: {whole_image_predictions}") |
|
|
|
|
|
if whole_image_predictions['predictions']: |
|
print("Using predictions from the whole image.") |
|
all_predictions = whole_image_predictions['predictions'] |
|
else: |
|
print("No predictions found for the whole image. Predicting on slices...") |
|
|
|
all_predictions = [] |
|
|
|
for sliced_image_path in sliced_image_paths: |
|
if isinstance(sliced_image_path, str): |
|
predictions = model.predict(image_path=sliced_image_path).json() |
|
all_predictions.extend(predictions['predictions']) |
|
else: |
|
print(f"Skipping invalid image path: {sliced_image_path}") |
|
|
|
|
|
postprocessed_predictions = apply_nms(all_predictions, iou_threshold=0.5) |
|
|
|
|
|
img = cv2.imread(temp_file_path) |
|
for prediction in postprocessed_predictions: |
|
class_name = prediction['class'] |
|
bbox = prediction['bbox'] |
|
confidence = prediction['confidence'] |
|
|
|
|
|
x, y, w, h = map(int, bbox) |
|
|
|
|
|
color = (0, 255, 0) |
|
thickness = 2 |
|
cv2.rectangle(img, (x, y), (x + w, y + h), color, thickness) |
|
|
|
label = f"{class_name}: {confidence:.2f}" |
|
cv2.putText(img, label, (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, thickness) |
|
|
|
|
|
img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) |
|
annotated_image = Image.fromarray(img_rgb) |
|
|
|
|
|
output_image_path = "/tmp/prediction.jpg" |
|
annotated_image.save(output_image_path) |
|
|
|
|
|
class_count = {} |
|
for detection in postprocessed_predictions: |
|
class_name = detection['class'] |
|
if class_name in class_count: |
|
class_count[class_name] += 1 |
|
else: |
|
class_count[class_name] = 1 |
|
|
|
|
|
result_text = "Jumlah objek per kelas:\n" |
|
for class_name, count in class_count.items(): |
|
result_text += f"{class_name}: {count} objek\n" |
|
|
|
|
|
os.remove(temp_file_path) |
|
|
|
return output_image_path, result_text |
|
|
|
|
|
iface = gr.Interface( |
|
fn=detect_objects, |
|
inputs=gr.Image(type="pil"), |
|
outputs=[gr.Image(), gr.Textbox()], |
|
live=True |
|
) |
|
|
|
|
|
iface.launch() |
|
|