{
 "cells": [
  {
   "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",
    "from panel_disconnect import disconnect\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Define Image Path and output"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "input_path   = '100_Example/'\n",
    "output_path  = 'Result_Complete_Example/'\n",
    "makedirs(output_path)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Load Model (Panel Detector, Soiling, Diode, Cell Damage)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "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",
      "WARNING:tensorflow:No training configuration found in save file: the model was *not* compiled. Compile it manually.\n",
      "WARNING:tensorflow:No training configuration found in save file: the model was *not* compiled. Compile it manually.\n",
      "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_panel  = 'config_full_yolo_panel_infer.json' #Panel\n",
    "config_path_soiling  = 'config_full_yolo_fault_1_infer.json' #Soiling\n",
    "config_path_diode  = 'config_full_yolo_fault_4_infer.json'  #Diode\n",
    "config_path_cellD  = 'config_full_yolo_fault_2_infer.json'  #Cell Damage\n",
    "\n",
    "\n",
    "\n",
    "\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, nms_thresh_panel = 0.5, 0.45, 0.3\n",
    "\n",
    "####################################################\n",
    "#####   Load the model Panel Detector  #############\n",
    "####################################################\n",
    "with open(config_path_panel) as config_buffer:\n",
    "        config_panel = json.load(config_buffer)\n",
    "\n",
    "os.environ['CUDA_VISIBLE_DEVICES'] = config_panel['train']['gpus']\n",
    "infer_model_panel = load_model(config_panel['train']['saved_weights_name'])\n",
    "\n",
    "####################################################\n",
    "#####   Load the model Soiling  #############\n",
    "####################################################\n",
    "with open(config_path_soiling) as config_buffer:\n",
    "        config_soiling = json.load(config_buffer)\n",
    "\n",
    "infer_model_soiling = load_model(config_soiling['train']['saved_weights_name'])\n",
    "\n",
    "####################################################\n",
    "#####   Load the model Diode  #############\n",
    "####################################################\n",
    "with open(config_path_diode) as config_buffer:\n",
    "        config_diode = json.load(config_buffer)\n",
    "\n",
    "infer_model_diode = load_model(config_diode['train']['saved_weights_name'])\n",
    "\n",
    "####################################################\n",
    "#####   Load the model Cell Damage  #############\n",
    "####################################################\n",
    "with open(config_path_cellD) as config_buffer:\n",
    "        config_cellD = json.load(config_buffer)\n",
    "\n",
    "infer_model_cellD = load_model(config_cellD['train']['saved_weights_name'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Load Image paths"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "image_paths = []\n",
    "\n",
    "if os.path.isdir(input_path):\n",
    "    for inp_file in os.listdir(input_path):\n",
    "        image_paths += [input_path + inp_file]\n",
    "else:\n",
    "    image_paths += [input_path]\n",
    "\n",
    "image_paths = [inp_file for inp_file in image_paths if (inp_file[-4:] in ['.jpg', '.png', 'JPEG'])]\n",
    "\n",
    "# the main loop\n",
    "times = []\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Detection of images"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "100_Example/Mision 4_DJI_0067.jpg\n",
      "Elapsed time = 13.580645084381104\n",
      "100_Example/Mision 9_DJI_0068.jpg\n",
      "Elapsed time = 6.935424566268921\n",
      "100_Example/Mision 9_DJI_0090.jpg\n",
      "Elapsed time = 6.3714070320129395\n",
      "100_Example/Mision 10_DJI_0145.jpg\n",
      "Elapsed time = 6.74527382850647\n",
      "100_Example/Mision 1_DJI_0019.jpg\n",
      "Elapsed time = 6.775529146194458\n",
      "100_Example/Mision 11_DJI_0061.jpg\n",
      "Elapsed time = 7.002215147018433\n",
      "100_Example/Mision 9_DJI_0076.jpg\n",
      "Elapsed time = 6.889668703079224\n",
      "100_Example/Mision 4_DJI_0040.jpg\n",
      "Elapsed time = 7.327791213989258\n",
      "100_Example/Mision 10_DJI_0103.jpg\n",
      "Elapsed time = 7.31937575340271\n",
      "100_Example/Mision 4_DJI_0058.jpg\n",
      "Elapsed time = 6.648495674133301\n",
      "100_Example/Mision 9_DJI_0012.jpg\n",
      "Elapsed time = 6.7225916385650635\n",
      "100_Example/Mision 10_DJI_0010.jpg\n",
      "Elapsed time = 7.258010149002075\n",
      "100_Example/Mision 1_DJI_0005.jpg\n",
      "Elapsed time = 7.08447790145874\n",
      "100_Example/Mision 4_DJI_0061.jpg\n",
      "Elapsed time = 6.509597301483154\n",
      "100_Example/Mision 6_DJI_0008.jpg\n",
      "Elapsed time = 6.427324056625366\n",
      "100_Example/Mision 9_DJI_0043.jpg\n",
      "Elapsed time = 6.708544015884399\n",
      "100_Example/Mision 9_DJI_0086.jpg\n",
      "Elapsed time = 7.64997124671936\n",
      "100_Example/Mision 5_DJI_0057.jpg\n",
      "Elapsed time = 8.020388126373291\n",
      "100_Example/Mision 10_DJI_0177.jpg\n",
      "Elapsed time = 8.652998447418213\n",
      "100_Example/Mision 4_DJI_0087.jpg\n",
      "Elapsed time = 7.90339732170105\n",
      "100_Example/Mision 4_DJI_0020.jpg\n",
      "Elapsed time = 7.2860777378082275\n",
      "100_Example/Mision 10_DJI_0147.jpg\n",
      "Elapsed time = 7.4565253257751465\n",
      "100_Example/Mision 10_DJI_0180.jpg\n",
      "Elapsed time = 7.306499242782593\n",
      "100_Example/Mision 10_DJI_0100.jpg\n",
      "Elapsed time = 8.094278573989868\n",
      "100_Example/Mision 4_DJI_0037.jpg\n",
      "Elapsed time = 6.724163055419922\n",
      "100_Example/Mision 4_DJI_0048.jpg\n",
      "Elapsed time = 6.416969537734985\n",
      "100_Example/Mision 4_DJI_0077.jpg\n",
      "Elapsed time = 7.322848796844482\n",
      "100_Example/Mision 10_DJI_0148.jpg\n",
      "Elapsed time = 8.202306747436523\n",
      "100_Example/Mision 11_DJI_0060.jpg\n",
      "Elapsed time = 8.421032905578613\n",
      "100_Example/Mision 4_DJI_0045.jpg\n",
      "Elapsed time = 7.004019737243652\n",
      "100_Example/Mision 10_DJI_0120.jpg\n",
      "Elapsed time = 7.765641927719116\n",
      "100_Example/Mision 5_DJI_0041.jpg\n",
      "Elapsed time = 8.028720140457153\n",
      "100_Example/Mision 10_DJI_0141.jpg\n",
      "Elapsed time = 6.67255711555481\n",
      "100_Example/Mision 5_DJI_0044.jpg\n",
      "Elapsed time = 6.44356632232666\n",
      "100_Example/Mision 5_DJI_0085.jpg\n",
      "Elapsed time = 6.671105623245239\n",
      "100_Example/Mision 4_DJI_0038.jpg\n",
      "Elapsed time = 6.503889322280884\n",
      "100_Example/Mision 10_DJI_0096.jpg\n",
      "Elapsed time = 6.516649961471558\n",
      "100_Example/Mision 10_DJI_0144.jpg\n",
      "Elapsed time = 6.6470115184783936\n",
      "100_Example/Mision 10_DJI_0142.jpg\n",
      "Elapsed time = 6.744549036026001\n",
      "100_Example/Mision 7_DJI_0027.jpg\n",
      "Elapsed time = 6.6441521644592285\n",
      "100_Example/Mision 11_DJI_0019.jpg\n",
      "Elapsed time = 6.629210710525513\n",
      "100_Example/Mision 9_DJI_0083.jpg\n",
      "Elapsed time = 7.128464460372925\n",
      "100_Example/Mision 7_DJI_0002.jpg\n",
      "Elapsed time = 7.551218748092651\n",
      "100_Example/Mision 10_DJI_0094.jpg\n",
      "Elapsed time = 8.458415031433105\n",
      "100_Example/Mision 4_DJI_0017.jpg\n",
      "Elapsed time = 8.11717939376831\n",
      "100_Example/Mision 11_DJI_0010.jpg\n",
      "Elapsed time = 6.738617658615112\n",
      "100_Example/Mision 6_DJI_0001.jpg\n",
      "Elapsed time = 6.731432914733887\n",
      "100_Example/Mision 9_DJI_0084.jpg\n",
      "Elapsed time = 6.683074235916138\n",
      "100_Example/Mision 10_DJI_0146.jpg\n",
      "Elapsed time = 7.271329164505005\n",
      "100_Example/Mision 10_DJI_0011.jpg\n",
      "Elapsed time = 6.584873199462891\n",
      "100_Example/Mision 10_DJI_0004.jpg\n",
      "Elapsed time = 6.516340494155884\n",
      "100_Example/Mision 9_DJI_0025.jpg\n",
      "Elapsed time = 6.666715383529663\n",
      "100_Example/Mision 10_DJI_0054.jpg\n",
      "Elapsed time = 6.651204824447632\n",
      "100_Example/Mision 10_DJI_0095.jpg\n",
      "Elapsed time = 6.463395118713379\n",
      "100_Example/Mision 10_DJI_0152.jpg\n",
      "Elapsed time = 6.70379638671875\n",
      "100_Example/Mision 10_DJI_0027.jpg\n",
      "Elapsed time = 6.65558934211731\n",
      "100_Example/Mision 6_DJI_0051.jpg\n",
      "Elapsed time = 6.498088359832764\n",
      "100_Example/Mision 9_DJI_0082.jpg\n",
      "Elapsed time = 7.057750225067139\n",
      "100_Example/Mision 9_DJI_0077.jpg\n",
      "Elapsed time = 7.9876768589019775\n",
      "100_Example/Mision 10_DJI_0179.jpg\n",
      "Elapsed time = 7.247650861740112\n",
      "100_Example/Mision 6_DJI_0072.jpg\n",
      "Elapsed time = 6.948460102081299\n",
      "100_Example/Mision 10_DJI_0167.jpg\n",
      "Elapsed time = 7.502035617828369\n",
      "100_Example/Mision 4_DJI_0032.jpg\n",
      "Elapsed time = 6.898995399475098\n",
      "100_Example/Mision 10_DJI_0175.jpg\n",
      "Elapsed time = 7.028684616088867\n",
      "100_Example/Mision 6_DJI_0055.jpg\n",
      "Elapsed time = 6.847854137420654\n",
      "100_Example/Mision 3_DJI_0009.jpg\n",
      "Elapsed time = 7.531402826309204\n",
      "100_Example/Mision 3_DJI_0008.jpg\n",
      "Elapsed time = 8.348368406295776\n",
      "100_Example/Mision 5_DJI_0046.jpg\n",
      "Elapsed time = 7.112367391586304\n",
      "100_Example/Mision 9_DJI_0054.jpg\n",
      "Elapsed time = 7.2907304763793945\n",
      "100_Example/Mision 4_DJI_0068.jpg\n",
      "Elapsed time = 7.379649877548218\n",
      "100_Example/Mision 9_DJI_0064.jpg\n",
      "Elapsed time = 8.235641717910767\n",
      "100_Example/Mision 4_DJI_0062.jpg\n",
      "Elapsed time = 7.234843969345093\n",
      "100_Example/Mision 6_DJI_0056.jpg\n",
      "Elapsed time = 7.157646656036377\n",
      "100_Example/Mision 4_DJI_0064.jpg\n",
      "Elapsed time = 7.113090515136719\n",
      "100_Example/Mision 9_DJI_0040.jpg\n",
      "Elapsed time = 7.369828939437866\n",
      "100_Example/Mision 9_DJI_0080.jpg\n",
      "Elapsed time = 6.951316833496094\n",
      "100_Example/Mision 5_DJI_0049.jpg\n",
      "Elapsed time = 7.011293172836304\n",
      "100_Example/Mision 10_DJI_0170.jpg\n",
      "Elapsed time = 7.267280340194702\n",
      "100_Example/Mision 5_DJI_0091.jpg\n",
      "Elapsed time = 8.16886043548584\n",
      "100_Example/Mision 6_DJI_0052.jpg\n",
      "Elapsed time = 7.4664976596832275\n",
      "100_Example/Mision 9_DJI_0016.jpg\n",
      "Elapsed time = 7.098127365112305\n",
      "100_Example/Mision 10_DJI_0093.jpg\n",
      "Elapsed time = 7.144554853439331\n",
      "100_Example/Mision 4_DJI_0079.jpg\n",
      "Elapsed time = 7.273373126983643\n",
      "100_Example/Mision 9_DJI_0013.jpg\n",
      "Elapsed time = 6.812521457672119\n",
      "100_Example/Mision 5_DJI_0081.jpg\n",
      "Elapsed time = 7.042137622833252\n",
      "100_Example/Mision 9_DJI_0065.jpg\n",
      "Elapsed time = 7.287776231765747\n",
      "100_Example/Mision 4_DJI_0044.jpg\n",
      "Elapsed time = 7.068741798400879\n",
      "100_Example/Mision 10_DJI_0140.jpg\n",
      "Elapsed time = 6.873102903366089\n",
      "100_Example/Mision 10_DJI_0151.jpg\n",
      "Elapsed time = 7.133445501327515\n",
      "100_Example/Mision 10_DJI_0028.jpg\n",
      "Elapsed time = 6.867727279663086\n",
      "100_Example/Mision 4_DJI_0066.jpg\n",
      "Elapsed time = 6.939803123474121\n",
      "100_Example/Mision 4_DJI_0046.jpg\n",
      "Elapsed time = 6.948658227920532\n",
      "100_Example/Mision 5_DJI_0083.jpg\n",
      "Elapsed time = 7.099925994873047\n",
      "100_Example/Mision 6_DJI_0059.jpg\n",
      "Elapsed time = 6.925538063049316\n",
      "100_Example/Mision 1_DJI_0031.jpg\n",
      "Elapsed time = 6.821932315826416\n",
      "100_Example/Mision 9_DJI_0015.jpg\n",
      "Elapsed time = 7.032159328460693\n",
      "100_Example/Mision 5_DJI_0048.jpg\n",
      "Elapsed time = 7.372926712036133\n",
      "100_Example/Mision 10_DJI_0092.jpg\n",
      "Elapsed time = 7.052095174789429\n",
      "100_Example/Mision 4_DJI_0078.jpg\n",
      "Elapsed time = 6.999572277069092\n",
      "100_Example/Mision 11_DJI_0004.jpg\n",
      "Elapsed time = 7.106813192367554\n"
     ]
    }
   ],
   "source": [
    "for image_path in image_paths:\n",
    "    image = cv2.imread(image_path)\n",
    "    print(image_path)\n",
    "    start = time.time()\n",
    "    # predict the bounding boxes\n",
    "    boxes_soiling = get_yolo_boxes(infer_model_soiling, [image], net_h, net_w, config_soiling['model']['anchors'], obj_thresh, nms_thresh)[0]\n",
    "    boxes_diode = get_yolo_boxes(infer_model_diode, [image], net_h, net_w, config_diode['model']['anchors'], obj_thresh, nms_thresh)[0]\n",
    "    boxes_cellD = get_yolo_boxes(infer_model_cellD, [image], net_h, net_w, config_cellD['model']['anchors'], obj_thresh, nms_thresh)[0]\n",
    "    boxes_panel = get_yolo_boxes(infer_model_panel, [image], net_h, net_w, config_panel['model']['anchors'], obj_thresh, nms_thresh_panel)[0]\n",
    "    boxes_panel = [box for box in boxes_panel if box.get_score() > obj_thresh]\n",
    "    boxes_disc = disconnect(image, boxes_panel, z_thresh = 1.8)\n",
    "    print('Elapsed time = {}'.format(time.time() - start))\n",
    "    times.append(time.time() - start)\n",
    "    # Draw boxes\n",
    "    draw_boxes(image, boxes_soiling, config_soiling['model']['labels'], obj_thresh, number_color = 0)\n",
    "    draw_boxes(image, boxes_diode, config_diode['model']['labels'], obj_thresh, number_color = 2)\n",
    "    draw_boxes(image, boxes_cellD, config_cellD['model']['labels'], obj_thresh, number_color = 4)\n",
    "    draw_boxes(image, boxes_disc, ['disc'], obj_thresh, number_color = 5)\n",
    "    \n",
    "    # write the image with bounding boxes to file\n",
    "    cv2.imwrite(output_path + image_path.split('/')[-1], np.uint8(image))\n",
    "\n",
    "file = open(output_path + 'time.txt','w')\n",
    "file.write('Tiempo promedio:' + str(np.mean(times)))\n",
    "file.close()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Example One image"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "100_Example/Mision 10_DJI_0027.jpg\n",
      "Elapsed time = 5.347564220428467\n",
      "Elapsed time = 5.349539756774902\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<matplotlib.image.AxesImage at 0x7f596cdf96d0>"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 292812 characters]\n",
      "text/plain": [
       "<Figure size 864x720 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 297644 characters]\n",
      "text/plain": [
       "<Figure size 864x720 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "image_path = image_paths[55]\n",
    "\n",
    "image = cv2.imread(image_path)\n",
    "plt.figure(figsize=(12, 10))\n",
    "plt.imshow(image)\n",
    "print(image_path)\n",
    "\n",
    "start = time.time()\n",
    "# predict the bounding boxes\n",
    "boxes_soiling = get_yolo_boxes(infer_model_soiling, [image], net_h, net_w, config_soiling['model']['anchors'], obj_thresh, nms_thresh)[0]\n",
    "boxes_diode = get_yolo_boxes(infer_model_diode, [image], net_h, net_w, config_diode['model']['anchors'], obj_thresh, nms_thresh)[0]\n",
    "boxes_cellD = get_yolo_boxes(infer_model_cellD, [image], net_h, net_w, config_cellD['model']['anchors'], obj_thresh, nms_thresh)[0]\n",
    "boxes_panel = get_yolo_boxes(infer_model_panel, [image], net_h, net_w, config_panel['model']['anchors'], obj_thresh, nms_thresh_panel)[0]\n",
    "boxes_panel = [box for box in boxes_panel if box.get_score() > obj_thresh]\n",
    "boxes_disc = disconnect(image, boxes_panel, z_thresh = 1.8)\n",
    "print('Elapsed time = {}'.format(time.time() - start))\n",
    "times.append(time.time() - start)\n",
    "# Draw boxes\n",
    "draw_boxes(image, boxes_soiling, config_soiling['model']['labels'], obj_thresh, number_color = 0)\n",
    "draw_boxes(image, boxes_diode, config_diode['model']['labels'], obj_thresh, number_color = 2)\n",
    "draw_boxes(image, boxes_cellD, config_cellD['model']['labels'], obj_thresh, number_color = 4)\n",
    "draw_boxes(image, boxes_disc, ['disc'], obj_thresh, number_color = 5)\n",
    "\n",
    "print('Elapsed time = {}'.format(time.time() - start))\n",
    "times.append(time.time() - start)\n",
    "plt.figure(figsize=(12, 10))\n",
    "plt.imshow(image)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "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": 4
}
