{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Example of load model for detections\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import time\n",
    "import os\n",
    "import argparse\n",
    "import json\n",
    "import cv2\n",
    "import sys\n",
    "sys.path += [os.path.abspath('keras-yolo3-master')]\n",
    "\n",
    "from utils.utils import get_yolo_boxes, makedirs\n",
    "from utils.bbox import draw_boxes\n",
    "from tensorflow.keras.models import load_model\n",
    "from tqdm import tqdm\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "#detection_orto(infer_model, orto_image, div = [5,5], net_h = net_h, net_w = net_w, anchors\n",
    " #              obj_thresh = obj_thresh, nms_thresh = nms_thresh)\n",
    "    \n",
    "\n",
    "    \n",
    "    \n",
    "def detection_orto (infer_model, orto_image, div, net_h, net_w, anchors, obj_thresh, nms_thresh):\n",
    "    \n",
    "    div_h, div_w = div\n",
    "    \n",
    "    new_shape = [int(orto_image.shape[0] / div_h), int(orto_image.shape[1] / div_w)]\n",
    "    \n",
    "    final_boxes = []\n",
    "    \n",
    "    for h in range(div_h):\n",
    "        for w in range(div_w):\n",
    "            \n",
    "            image = orto_image[new_shape[0]*h: new_shape[0]*(h + 1), new_shape[1]*w : new_shape[1]*(w + 1)]\n",
    "            boxes = get_yolo_boxes(infer_model, [image], net_h, net_w, anchors, obj_thresh, nms_thresh)[0]\n",
    "            \n",
    "            for box in boxes:\n",
    "                box.xmin += new_shape[1]*w\n",
    "                box.xmax += new_shape[1]*w\n",
    "                box.ymin += new_shape[0]*h\n",
    "                box.ymax += new_shape[0]*h\n",
    "                final_boxes.append(box)\n",
    "    return final_boxes\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## ORTO PHOTO"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(-0.5, 799.5, 399.5, -0.5)"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 618996 characters]\n",
      "text/plain": [
       "<Figure size 1152x1152 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 213340 characters]\n",
      "text/plain": [
       "<Figure size 1152x1152 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "image_path = 'odm_orthophoto.tif' \n",
    "\n",
    "image = cv2.imread(image_path)\n",
    "\n",
    "fig, ax = plt.subplots(figsize=(16, 16))\n",
    "ax.set_title('Original Image')\n",
    "plt.imshow(image, cmap='gray')\n",
    "ax.axis('off') \n",
    "\n",
    "sub_image = image[1200:1600, 800:1600]\n",
    "fig, ax = plt.subplots(figsize=(16, 16))\n",
    "ax.set_title('Path Image')\n",
    "plt.imshow(sub_image, cmap='gray')\n",
    "ax.axis('off') "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Load Trained Model Soiling Fault"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "WARNING:tensorflow:No training configuration found in save file: the model was *not* compiled. Compile it manually.\n"
     ]
    }
   ],
   "source": [
    "## Config of trained model, change this for use different trained model\n",
    "config_path  = 'config_full_yolo_fault_1_infer.json' \n",
    "\n",
    "with open(config_path) as config_buffer:\n",
    "        config = json.load(config_buffer)\n",
    "                \n",
    "\n",
    "###############################\n",
    "#####   Load the model   ######\n",
    "###############################\n",
    "os.environ['CUDA_VISIBLE_DEVICES'] = config['train']['gpus']\n",
    "infer_model = load_model(config['train']['saved_weights_name'])\n",
    "\n",
    "## Parameters of detection\n",
    "net_h, net_w = 416, 416 # a multiple of 32, the smaller the faster\n",
    "obj_thresh, nms_thresh = 0.5, 0.45\n",
    "\n",
    "\n",
    "#infer_model.summary()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Detection Soling Fault"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Elapsed time = 75.83608198165894\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "(-0.5, 3938.5, 2425.5, -0.5)"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 618996 characters]\n",
      "text/plain": [
       "<Figure size 1152x1152 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 619668 characters]\n",
      "text/plain": [
       "<Figure size 1152x1152 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "image_path = 'odm_orthophoto.tif' \n",
    "image = cv2.imread(image_path)\n",
    "\n",
    "## Show original image\n",
    "fig, ax = plt.subplots(figsize=(16, 16))\n",
    "ax.set_title('Original Image')\n",
    "plt.imshow(image, cmap='gray')\n",
    "ax.axis('off') \n",
    "\n",
    "start = time.time()\n",
    "## predict the bounding boxes\n",
    "#boxes = get_yolo_boxes(infer_model, [image], net_h, net_w, config['model']['anchors'], obj_thresh, nms_thresh)[0]\n",
    "boxes = detection_orto(infer_model, orto_image = image, div = [6,6], net_h = net_h, net_w = net_w, \n",
    "               anchors = config['model']['anchors'], obj_thresh = obj_thresh, nms_thresh = nms_thresh)\n",
    "print('Elapsed time = {}'.format(time.time() - start))\n",
    "## draw bounding boxes on the image using labels\n",
    "draw_boxes(image, boxes, config['model']['labels'], obj_thresh)\n",
    "\n",
    "\n",
    "## Show Detection Fault\n",
    "fig, ax = plt.subplots(figsize=(16, 16))\n",
    "ax.set_title('Detection Fault')\n",
    "plt.imshow(image, cmap='gray')\n",
    "ax.axis('off') \n",
    "#plt.imsave('Diode Fault_6.png', image)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Load Trained Model Diode Fault"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "WARNING:tensorflow:No training configuration found in save file: the model was *not* compiled. Compile it manually.\n"
     ]
    }
   ],
   "source": [
    "## Config of trained model, change this for use different trained model\n",
    "config_path  = 'config_full_yolo_fault_4_infer.json' \n",
    "\n",
    "with open(config_path) as config_buffer:\n",
    "        config = json.load(config_buffer)\n",
    "                \n",
    "\n",
    "###############################\n",
    "#####   Load the model   ######\n",
    "###############################\n",
    "os.environ['CUDA_VISIBLE_DEVICES'] = config['train']['gpus']\n",
    "infer_model = load_model(config['train']['saved_weights_name'])\n",
    "\n",
    "#infer_model.summary()\n",
    "\n",
    "## Parameters of detection\n",
    "net_h, net_w = 416, 416 # a multiple of 32, the smaller the faster\n",
    "obj_thresh, nms_thresh = 0.5, 0.45"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Detection Diode Fault"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Elapsed time = 97.27587413787842\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "(-0.5, 3938.5, 2425.5, -0.5)"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 618996 characters]\n",
      "text/plain": [
       "<Figure size 1152x1152 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 621024 characters]\n",
      "text/plain": [
       "<Figure size 1152x1152 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "image_path = 'odm_orthophoto.tif' \n",
    "\n",
    "\n",
    "image = cv2.imread(image_path)\n",
    "\n",
    "## Show original image\n",
    "fig, ax = plt.subplots(figsize=(16, 16))\n",
    "ax.set_title('Original Image')\n",
    "plt.imshow(image, cmap='gray')\n",
    "ax.axis('off') \n",
    "\n",
    "start = time.time()\n",
    "## predict the bounding boxes\n",
    "#boxes = get_yolo_boxes(infer_model, [image], net_h, net_w, config['model']['anchors'], obj_thresh, nms_thresh)[0]\n",
    "boxes = detection_orto(infer_model, orto_image = image, div = [6,6], net_h = net_h, net_w = net_w, \n",
    "               anchors = config['model']['anchors'], obj_thresh = obj_thresh, nms_thresh = nms_thresh)\n",
    "print('Elapsed time = {}'.format(time.time() - start))\n",
    "## draw bounding boxes on the image using labels\n",
    "draw_boxes(image, boxes, config['model']['labels'], obj_thresh)\n",
    "\n",
    "\n",
    "## Show Detection Fault\n",
    "fig, ax = plt.subplots(figsize=(16, 16))\n",
    "ax.set_title('Detection Fault')\n",
    "plt.imshow(image, cmap='gray')\n",
    "ax.axis('off') "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Load Trained Model Panel Disconnect"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "WARNING:tensorflow:No training configuration found in save file: the model was *not* compiled. Compile it manually.\n"
     ]
    }
   ],
   "source": [
    "## Config of trained model, change this for use different trained model\n",
    "config_path  = 'config_full_yolo_panel_infer.json' \n",
    "\n",
    "with open(config_path) as config_buffer:\n",
    "        config = json.load(config_buffer)\n",
    "                \n",
    "\n",
    "###############################\n",
    "#####   Load the model   ######\n",
    "###############################\n",
    "os.environ['CUDA_VISIBLE_DEVICES'] = config['train']['gpus']\n",
    "infer_model = load_model(config['train']['saved_weights_name'])\n",
    "\n",
    "#infer_model.summary()\n",
    "\n",
    "## Parameters of detection\n",
    "net_h, net_w = 416, 416 # a multiple of 32, the smaller the faster\n",
    "obj_thresh, nms_thresh = 0.5, 0.3\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Detection Panel Disconnect"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Elapsed time = 117.7064254283905\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "(-0.5, 3938.5, 2425.5, -0.5)"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 618996 characters]\n",
      "text/plain": [
       "<Figure size 1152x1152 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 722536 characters]\n",
      "text/plain": [
       "<Figure size 1152x1152 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "image_path = 'odm_orthophoto.tif' \n",
    "\n",
    "\n",
    "image = cv2.imread(image_path)\n",
    "\n",
    "## Show original image\n",
    "fig, ax = plt.subplots(figsize=(16, 16))\n",
    "ax.set_title('Original Image')\n",
    "plt.imshow(image, cmap='gray')\n",
    "ax.axis('off') \n",
    "\n",
    "start = time.time()\n",
    "## predict the bounding boxes\n",
    "#boxes = get_yolo_boxes(infer_model, [image], net_h, net_w, config['model']['anchors'], obj_thresh, nms_thresh)[0]\n",
    "boxes = detection_orto(infer_model, orto_image = image, div = [6,6], net_h = net_h, net_w = net_w, \n",
    "               anchors = config['model']['anchors'], obj_thresh = obj_thresh, nms_thresh = nms_thresh)\n",
    "print('Elapsed time = {}'.format(time.time() - start))\n",
    "## draw bounding boxes on the image using labels\n",
    "draw_boxes(image, boxes, config['model']['labels'], obj_thresh)\n",
    "\n",
    "\n",
    "## Show Detection Fault\n",
    "fig, ax = plt.subplots(figsize=(16, 16))\n",
    "ax.set_title('Detection Fault')\n",
    "plt.imshow(image, cmap='gray')\n",
    "ax.axis('off') "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "boxes = [[box for box in boxes_image if box.get_score() > obj_thresh] for boxes_image in boxes]\n",
    "\n",
    "    print('Elapsed time = {}'.format(time.time() - start))\n",
    "    times.append(time.time() - start)\n",
    "\n",
    "    boxes_disc = [disconnect(image, boxes_image, z_thresh = 1.8) for image, boxes_image in zip(images, boxes)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.imsave('Detect Panel_6.png', image)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "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.7.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
