{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "image 1/1 g:\\MAGANG\\ai elang yolov12\\WhatsApp Image 2025-02-27 at 19.23.35_58190fb4.jpg: 640x384 (no detections), 35.4ms\n",
      "Speed: 2.9ms preprocess, 35.4ms inference, 1.8ms postprocess per image at shape (1, 3, 640, 384)\n",
      "Tidak ada prediksi dengan confidence di atas 1 untuk gambar WhatsApp Image 2025-02-27 at 19.23.35_58190fb4.jpg\n"
     ]
    }
   ],
   "source": [
    "from ultralytics import YOLO\n",
    "import matplotlib.pyplot as plt\n",
    "from PIL import Image\n",
    "import os\n",
    "\n",
    "# Load the pretrained YOLO model\n",
    "model = YOLO(r\"visibility baru.pt\")  # Ganti dengan model yang Anda gunakan\n",
    "\n",
    "# Path ke gambar\n",
    "image_path = r\"WhatsApp Image 2025-02-27 at 19.23.35_58190fb4.jpg\"  # Ganti dengan nama gambar Anda\n",
    "\n",
    "# Cek apakah gambar ada\n",
    "if not os.path.exists(image_path):\n",
    "    raise FileNotFoundError(f\"Gambar tidak ditemukan: {image_path}\")\n",
    "\n",
    "# Jalankan inferensi pada gambar\n",
    "result = model(image_path)\n",
    "\n",
    "# Threshold confidence\n",
    "confidence_threshold = 1  # Threshold confidence\n",
    "\n",
    "# Filter deteksi berdasarkan confidence\n",
    "boxes = result[0].boxes.cpu().numpy()  # Ambil bounding boxes\n",
    "filtered_boxes = [box for box in boxes if box.conf > confidence_threshold]  # Filter confidence\n",
    "\n",
    "# Jika tidak ada kotak dengan confidence di atas threshold\n",
    "if not filtered_boxes:\n",
    "    print(f\"Tidak ada prediksi dengan confidence di atas {confidence_threshold} untuk gambar {image_path}\")\n",
    "else:\n",
    "    # Buat ulang gambar hasil prediksi dengan kotak yang difilter\n",
    "    result[0].boxes = filtered_boxes\n",
    "    pred_img = result[0].plot()  # Menghasilkan array gambar dengan prediksi\n",
    "\n",
    "    # Simpan hasil prediksi ke disk (dengan format asli BGR)\n",
    "    output_folder = \"G:/MAGANG/ai elang yolov12/results\"\n",
    "    os.makedirs(output_folder, exist_ok=True)\n",
    "    output_path = os.path.join(output_folder, \"result_single_image.jpg\")\n",
    "    Image.fromarray(pred_img).save(output_path)\n",
    "\n",
    "    # Konversi BGR ke RGB untuk ditampilkan di matplotlib\n",
    "    pred_img_rgb = pred_img[..., ::-1]\n",
    "\n",
    "    # Tampilkan hasil di notebook menggunakan matplotlib\n",
    "    plt.figure(figsize=(10, 10))\n",
    "    plt.imshow(pred_img_rgb)\n",
    "    plt.axis(\"off\")\n",
    "    plt.title(\"Hasil Prediksi - Gambar Tunggal\")\n",
    "    plt.show()\n",
    "\n",
    "    print(f\"Hasil disimpan di: {output_path}\")\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "yolov11",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
