{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "EPcg39zEQY9T"
   },
   "source": [
    "# Yolo V3 Object Detection\n",
    "Notebook is copied from face_extraction.ipynb.\n",
    "\n",
    "Useful [resource](https://medium.com/@amrokamal_47691/yolo-yolov2-and-yolov3-all-you-want-to-know-7e3e92dc4899) on YOLO"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 34
    },
    "colab_type": "code",
    "executionInfo": {
     "elapsed": 3438,
     "status": "ok",
     "timestamp": 1590344047273,
     "user": {
      "displayName": "Vincent La",
      "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GgURuojY3AHW_TPmPkZW4KsBTfKl2ldHQY4Lb0=s64",
      "userId": "02730725867920307275"
     },
     "user_tz": 420
    },
    "id": "sNI9YJehDOYd",
    "outputId": "43712fc6-9f29-498a-e9ae-9b52657f719e"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Mounted at /content/drive\n"
     ]
    }
   ],
   "source": [
    "from google.colab import drive\n",
    "drive.mount('/content/drive', force_remount=True)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 187
    },
    "colab_type": "code",
    "executionInfo": {
     "elapsed": 4491,
     "status": "ok",
     "timestamp": 1590344048373,
     "user": {
      "displayName": "Vincent La",
      "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GgURuojY3AHW_TPmPkZW4KsBTfKl2ldHQY4Lb0=s64",
      "userId": "02730725867920307275"
     },
     "user_tz": 420
    },
    "id": "jP7qWA05EGQC",
    "outputId": "b6930023-a7ea-499a-a972-69c93837eae9"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "10_10.mp4\n",
      "101_10.mp4\n",
      "101_11.mp4\n",
      "101_12.mp4\n",
      "101_13.mp4\n",
      "101_14.mp4\n",
      "101_15.mp4\n",
      "101_16.mp4\n",
      "101_17.mp4\n",
      "101_18.mp4\n"
     ]
    }
   ],
   "source": [
    "!ls drive/'My Drive'/cs231n-project/datasets/emotiw/train | head\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 136
    },
    "colab_type": "code",
    "executionInfo": {
     "elapsed": 10831,
     "status": "ok",
     "timestamp": 1590344054724,
     "user": {
      "displayName": "Vincent La",
      "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GgURuojY3AHW_TPmPkZW4KsBTfKl2ldHQY4Lb0=s64",
      "userId": "02730725867920307275"
     },
     "user_tz": 420
    },
    "id": "FA4epeBeIJY_",
    "outputId": "70069136-d227-4600-976f-630d10798cc6"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Requirement already satisfied: dlib in /usr/local/lib/python3.6/dist-packages (19.18.0)\n",
      "Requirement already satisfied: face_recognition in /usr/local/lib/python3.6/dist-packages (1.3.0)\n",
      "Requirement already satisfied: face-recognition-models>=0.3.0 in /usr/local/lib/python3.6/dist-packages (from face_recognition) (0.3.0)\n",
      "Requirement already satisfied: dlib>=19.7 in /usr/local/lib/python3.6/dist-packages (from face_recognition) (19.18.0)\n",
      "Requirement already satisfied: Pillow in /usr/local/lib/python3.6/dist-packages (from face_recognition) (7.0.0)\n",
      "Requirement already satisfied: numpy in /usr/local/lib/python3.6/dist-packages (from face_recognition) (1.18.4)\n",
      "Requirement already satisfied: Click>=6.0 in /usr/local/lib/python3.6/dist-packages (from face_recognition) (7.1.2)\n"
     ]
    }
   ],
   "source": [
    "!pip3 install dlib\n",
    "!pip3 install face_recognition"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 34
    },
    "colab_type": "code",
    "executionInfo": {
     "elapsed": 14091,
     "status": "ok",
     "timestamp": 1590344057993,
     "user": {
      "displayName": "Vincent La",
      "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GgURuojY3AHW_TPmPkZW4KsBTfKl2ldHQY4Lb0=s64",
      "userId": "02730725867920307275"
     },
     "user_tz": 420
    },
    "id": "KICsoMbG2FuG",
    "outputId": "2b6bf400-d46e-465e-ce16-cc0fdc5d27a6"
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Using TensorFlow backend.\n"
     ]
    }
   ],
   "source": [
    "import glob\n",
    "import json\n",
    "import math\n",
    "import os\n",
    "import pickle \n",
    "import sys\n",
    "\n",
    "import pprint\n",
    "\n",
    "# enter the foldername in your Drive where you have saved the project contents\n",
    "TRAINFOLDERNAME = 'datasets/emotiw/train'\n",
    "MODELFOLDERNAME = 'datasets/emotiw/models'\n",
    "assert TRAINFOLDERNAME is not None, \"[!] Enter the foldername.\"\n",
    "assert MODELFOLDERNAME is not None, \"[!] Enter the foldername.\"\n",
    "\n",
    "PROJECT_PATH = os.path.join('/content', 'drive', 'My Drive', 'cs231n-project')\n",
    "TRAIN_PATH = os.path.join(PROJECT_PATH, TRAINFOLDERNAME)\n",
    "MODEL_PATH = os.path.join(PROJECT_PATH, MODELFOLDERNAME)\n",
    "\n",
    "sys.path.append(TRAIN_PATH)\n",
    "\n",
    "from concurrent.futures import ThreadPoolExecutor\n",
    "import cv2\n",
    "import face_recognition\n",
    "from google.colab.patches import cv2_imshow\n",
    "from keras.models import load_model\n",
    "from keras.preprocessing.image import load_img\n",
    "from keras.preprocessing.image import img_to_array\n",
    "import numpy as np\n",
    "\n",
    "from yolov3 import yolo3_one_file_to_detect_them_all as yolo3_functions\n",
    "\n",
    "# Note: No trailing slash\n",
    "DOWNSAMPLE_RATIO = 1\n",
    "SAMPLE_EVERY = 10 # eg. if using 30 FPS and SAMPLE_EVERY=10, we would produce at 3 FPS\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 0,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "n0CRLSIMRTWe"
   },
   "outputs": [],
   "source": [
    "def create_yolov3_model():\n",
    "    \"\"\"\n",
    "    Create YOLO V3 Model using Pretrained weights\n",
    "\n",
    "    Mostly following this tutorial: https://machinelearningmastery.com/how-to-perform-object-detection-with-yolov3-in-keras/\n",
    "    \"\"\"\n",
    "    print('Defining the YOLO v3 Model')\n",
    "    model = yolo3_functions.make_yolov3_model()\n",
    "\n",
    "    print('Loading the model weights')\n",
    "    weight_file = os.path.join(TRAIN_PATH, 'yolov3', 'yolov3.weights')\n",
    "    weight_reader = yolo3_functions.WeightReader(weight_file)\n",
    "\n",
    "    # Next, call the load_weights() function of the WeightReader instance\n",
    "    # passing in our defined Keras model to set the weights into the layers.\n",
    "    print('Setting the model weights')\n",
    "    weight_reader.load_weights(model)\n",
    "\n",
    "    # Next, save this model to a Keras compatible .h5 model file ready for later use.\n",
    "    print('Saving the model')\n",
    "    model.save(os.path.join(MODEL_PATH, 'yolov3_model.h5'))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 0,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "v_lGc7ItRzBs"
   },
   "outputs": [],
   "source": [
    "def process_video(video_dir, video_name, video_num, total_videos, model):\n",
    "    \"\"\"\n",
    "    Processes a video by extracting the faces from each frame and creating a \n",
    "    list of list of faces which is then saved as a pickled object.\n",
    "\n",
    "    NOTE: Faces are not guaranteed to be the same across frames.\n",
    "          eg. 'face 1' in frame 1 may not be the same as 'face 1' in frame 2\n",
    "\n",
    "    Pickle Object Format:\n",
    "        [\n",
    "            [\n",
    "                [frame 1, face 1],\n",
    "                [frame 1, face 2],\n",
    "                [frame 1, face 3]\n",
    "            ],\n",
    "            [\n",
    "                [frame 2, face 1],\n",
    "                [frame 2, face 2]\n",
    "            ],\n",
    "            ...\n",
    "        ]\n",
    "\n",
    "    \"\"\"\n",
    "    print(f\"Processing video {video_num}/{total_videos} with name {video_name}\\n\")\n",
    "    vidcap = cv2.VideoCapture(os.path.join(video_dir, video_name))\n",
    "\n",
    "    input_length = int(vidcap.get(cv2.CAP_PROP_FRAME_COUNT))\n",
    "    frame_width = int(vidcap.get(cv2.CAP_PROP_FRAME_WIDTH))\n",
    "    frame_height = int(vidcap.get(cv2.CAP_PROP_FRAME_HEIGHT))\n",
    "    fps = int(vidcap.get(cv2.CAP_PROP_FPS))\n",
    "\n",
    "    metadata = {}\n",
    "    metadata[video_name] = {}\n",
    "    faces_all_frames = []\n",
    "    success, orig_image = vidcap.read()\n",
    "    count = 0\n",
    "    i = 1\n",
    "    while success:\n",
    "        if count % SAMPLE_EVERY == 0:\n",
    "            print(f\"   Working on {video_name} frame {count} video num {video_num}\")\n",
    "            # NOTE:\n",
    "            # cv2_imshow is used by Colab in place of cv2.imshow('image', image) as \n",
    "            # the latter command is known to crash Jupyter sessions in Colab\n",
    "\n",
    "            if video_num == 0 and count == 0:\n",
    "                print(f\"Showing sample frame for {video_name} with shape {orig_image.shape}\")\n",
    "                print(\"=\" * 20)\n",
    "\n",
    "            image_h, image_w = orig_image.shape[:2]\n",
    "            print('printing type image')\n",
    "            print(type(orig_image))\n",
    "\n",
    "            if video_num == 0 and count == 0:\n",
    "                print(\"Original frame: \")\n",
    "                cv2_imshow(orig_image)\n",
    "                print(\"\")\n",
    "\n",
    "            # Convert from BGR color (OpenCV) to RGB color (face_recognition)\n",
    "            # rgb_image = image[:, :, ::-1]\n",
    "\n",
    "            # https://machinelearningmastery.com/how-to-perform-object-detection-with-yolov3-in-keras/\n",
    "            # The first step is to load the Keras model. This might be the slowest part of making a prediction.\n",
    "            # Resize the image because YOLO expects 416 by 416\n",
    "            # define the expected input shape for the model\n",
    "            input_w, input_h = 416, 416\n",
    "            image = cv2.resize(orig_image, (input_w, input_h), interpolation = cv2.INTER_CUBIC)\n",
    "            print('Shape of image is {}'.format(image.shape))\n",
    "\n",
    "            # scale pixel values to [0, 1]\n",
    "            image = image.astype('float32')\n",
    "            image /= 255.0\n",
    "\n",
    "            # add a dimension so that we have one sample\n",
    "            image = np.expand_dims(image, 0)\n",
    "\n",
    "            # We can now feed the photo into the Keras model and make a prediction.\n",
    "            # Make Prediction\n",
    "            yhat = model.predict(image)\n",
    "\n",
    "            print('Summarize the shape of the list of arrays')\n",
    "            print([a.shape for a in yhat])\n",
    "\n",
    "            # define the anchors\n",
    "            anchors = [[116,90, 156,198, 373,326], [30,61, 62,45, 59,119], [10,13, 16,30, 33,23]]\n",
    "            \n",
    "            # define the probability threshold for detected objects\n",
    "            class_threshold = 0.6\n",
    "            boxes = list()\n",
    "            for i in range(len(yhat)):\n",
    "                # decode the output of the network\n",
    "                boxes += yolo3_functions.decode_netout(yhat[i][0], anchors[i], class_threshold, input_h, input_w)\n",
    "            \n",
    "            # correct the sizes of the bounding boxes for the shape of the image\n",
    "            yolo3_functions.correct_yolo_boxes(boxes, image_h=image_h, image_w=image_w, net_h=input_h, net_w=input_w)\n",
    "            \n",
    "            # suppress non-maximal boxes\n",
    "            yolo3_functions.do_nms(boxes, 0.5)\n",
    "            \n",
    "            # define the labels\n",
    "            # Double check all the categories that YoloV3 can predict \n",
    "            labels = [\"person\", \"bicycle\", \"car\", \"motorbike\", \"aeroplane\", \"bus\", \"train\", \"truck\",\n",
    "              \"boat\", \"traffic light\", \"fire hydrant\", \"stop sign\", \"parking meter\", \"bench\",\n",
    "              \"bird\", \"cat\", \"dog\", \"horse\", \"sheep\", \"cow\", \"elephant\", \"bear\", \"zebra\", \"giraffe\",\n",
    "              \"backpack\", \"umbrella\", \"handbag\", \"tie\", \"suitcase\", \"frisbee\", \"skis\", \"snowboard\",\n",
    "              \"sports ball\", \"kite\", \"baseball bat\", \"baseball glove\", \"skateboard\", \"surfboard\",\n",
    "              \"tennis racket\", \"bottle\", \"wine glass\", \"cup\", \"fork\", \"knife\", \"spoon\", \"bowl\", \"banana\",\n",
    "              \"apple\", \"sandwich\", \"orange\", \"broccoli\", \"carrot\", \"hot dog\", \"pizza\", \"donut\", \"cake\",\n",
    "              \"chair\", \"sofa\", \"pottedplant\", \"bed\", \"diningtable\", \"toilet\", \"tvmonitor\", \"laptop\", \"mouse\",\n",
    "              \"remote\", \"keyboard\", \"cell phone\", \"microwave\", \"oven\", \"toaster\", \"sink\", \"refrigerator\",\n",
    "              \"book\", \"clock\", \"vase\", \"scissors\", \"teddy bear\", \"hair drier\", \"toothbrush\"]\n",
    "           \n",
    "            # get the details of the detected objects\n",
    "            v_boxes, v_labels, v_scores = yolo3_functions.get_boxes(boxes, labels, class_threshold)\n",
    "            \n",
    "            # summarize what we found\n",
    "            for i in range(len(v_boxes)):\n",
    "                print(v_labels[i], v_scores[i])\n",
    "            \n",
    "            # draw what we found\n",
    "            yolo3_functions.draw_boxes(orig_image, v_boxes, v_labels, v_scores)\n",
    "\n",
    "            v_box_locations = [(v_box.xmin, v_box.xmax, v_box.ymin, v_box.ymax) for v_box in v_boxes]\n",
    "\n",
    "            metadata[video_name][count] = {\n",
    "                'vboxes': v_box_locations,\n",
    "                'v_labels': v_labels,\n",
    "                'v_scores': v_scores,\n",
    "            }\n",
    "\n",
    "        success, orig_image = vidcap.read()\n",
    "        count += 1\n",
    "    video_num += 1\n",
    "    vidcap.release()\n",
    "\n",
    "    return metadata\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 1000
    },
    "colab_type": "code",
    "executionInfo": {
     "elapsed": 24767,
     "status": "ok",
     "timestamp": 1590344068698,
     "user": {
      "displayName": "Vincent La",
      "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GgURuojY3AHW_TPmPkZW4KsBTfKl2ldHQY4Lb0=s64",
      "userId": "02730725867920307275"
     },
     "user_tz": 420
    },
    "id": "KVEtW75SR44c",
    "outputId": "6ab17b38-5543-4ec7-fa94-9ce46b449c76"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Defining the YOLO v3 Model\n",
      "Loading the model weights\n",
      "Setting the model weights\n",
      "loading weights of convolution #0\n",
      "loading weights of convolution #1\n",
      "loading weights of convolution #2\n",
      "loading weights of convolution #3\n",
      "no convolution #4\n",
      "loading weights of convolution #5\n",
      "loading weights of convolution #6\n",
      "loading weights of convolution #7\n",
      "no convolution #8\n",
      "loading weights of convolution #9\n",
      "loading weights of convolution #10\n",
      "no convolution #11\n",
      "loading weights of convolution #12\n",
      "loading weights of convolution #13\n",
      "loading weights of convolution #14\n",
      "no convolution #15\n",
      "loading weights of convolution #16\n",
      "loading weights of convolution #17\n",
      "no convolution #18\n",
      "loading weights of convolution #19\n",
      "loading weights of convolution #20\n",
      "no convolution #21\n",
      "loading weights of convolution #22\n",
      "loading weights of convolution #23\n",
      "no convolution #24\n",
      "loading weights of convolution #25\n",
      "loading weights of convolution #26\n",
      "no convolution #27\n",
      "loading weights of convolution #28\n",
      "loading weights of convolution #29\n",
      "no convolution #30\n",
      "loading weights of convolution #31\n",
      "loading weights of convolution #32\n",
      "no convolution #33\n",
      "loading weights of convolution #34\n",
      "loading weights of convolution #35\n",
      "no convolution #36\n",
      "loading weights of convolution #37\n",
      "loading weights of convolution #38\n",
      "loading weights of convolution #39\n",
      "no convolution #40\n",
      "loading weights of convolution #41\n",
      "loading weights of convolution #42\n",
      "no convolution #43\n",
      "loading weights of convolution #44\n",
      "loading weights of convolution #45\n",
      "no convolution #46\n",
      "loading weights of convolution #47\n",
      "loading weights of convolution #48\n",
      "no convolution #49\n",
      "loading weights of convolution #50\n",
      "loading weights of convolution #51\n",
      "no convolution #52\n",
      "loading weights of convolution #53\n",
      "loading weights of convolution #54\n",
      "no convolution #55\n",
      "loading weights of convolution #56\n",
      "loading weights of convolution #57\n",
      "no convolution #58\n",
      "loading weights of convolution #59\n",
      "loading weights of convolution #60\n",
      "no convolution #61\n",
      "loading weights of convolution #62\n",
      "loading weights of convolution #63\n",
      "loading weights of convolution #64\n",
      "no convolution #65\n",
      "loading weights of convolution #66\n",
      "loading weights of convolution #67\n",
      "no convolution #68\n",
      "loading weights of convolution #69\n",
      "loading weights of convolution #70\n",
      "no convolution #71\n",
      "loading weights of convolution #72\n",
      "loading weights of convolution #73\n",
      "no convolution #74\n",
      "loading weights of convolution #75\n",
      "loading weights of convolution #76\n",
      "loading weights of convolution #77\n",
      "loading weights of convolution #78\n",
      "loading weights of convolution #79\n",
      "loading weights of convolution #80\n",
      "loading weights of convolution #81\n",
      "no convolution #82\n",
      "no convolution #83\n",
      "loading weights of convolution #84\n",
      "no convolution #85\n",
      "no convolution #86\n",
      "loading weights of convolution #87\n",
      "loading weights of convolution #88\n",
      "loading weights of convolution #89\n",
      "loading weights of convolution #90\n",
      "loading weights of convolution #91\n",
      "loading weights of convolution #92\n",
      "loading weights of convolution #93\n",
      "no convolution #94\n",
      "no convolution #95\n",
      "loading weights of convolution #96\n",
      "no convolution #97\n",
      "no convolution #98\n",
      "loading weights of convolution #99\n",
      "loading weights of convolution #100\n",
      "loading weights of convolution #101\n",
      "loading weights of convolution #102\n",
      "loading weights of convolution #103\n",
      "loading weights of convolution #104\n",
      "loading weights of convolution #105\n",
      "Saving the model\n"
     ]
    }
   ],
   "source": [
    "create_yolov3_model()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 1000,
     "output_embedded_package_id": "1HmVwdpkJ7nAJsBJbFcm7BM2Ef4TTbWry"
    },
    "colab_type": "code",
    "executionInfo": {
     "elapsed": 100582,
     "status": "ok",
     "timestamp": 1590344144524,
     "user": {
      "displayName": "Vincent La",
      "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GgURuojY3AHW_TPmPkZW4KsBTfKl2ldHQY4Lb0=s64",
      "userId": "02730725867920307275"
     },
     "user_tz": 420
    },
    "id": "pYzgVGo8SlxZ",
    "outputId": "5b6f408c-153e-4363-fe85-6501c6d5549f"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Output hidden; open in https://colab.research.google.com to view."
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Testing for one video\n",
    "# Note you might get an error that says UserWarning: No training configuration found in save file\n",
    "# Seems like it's ok to ignore for now: https://github.com/fizyr/keras-retinanet/issues/207\n",
    "print('Loading YOLO v3 Keras Model')\n",
    "model = load_model(os.path.join(MODEL_PATH, 'yolov3_model.h5'))\n",
    "metadata = process_video(video_dir=TRAIN_PATH, video_name=\"2_1.mp4\", video_num=0, total_videos=10, model=model)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 1000
    },
    "colab_type": "code",
    "executionInfo": {
     "elapsed": 100705,
     "status": "ok",
     "timestamp": 1590344144662,
     "user": {
      "displayName": "Vincent La",
      "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GgURuojY3AHW_TPmPkZW4KsBTfKl2ldHQY4Lb0=s64",
      "userId": "02730725867920307275"
     },
     "user_tz": 420
    },
    "id": "SSdynIqMspTX",
    "outputId": "014d106d-d863-4292-ff6f-e7d3339337b0"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'2_1.mp4': {0: {'v_labels': ['person',\n",
      "                              'person',\n",
      "                              'person',\n",
      "                              'person',\n",
      "                              'person',\n",
      "                              'tvmonitor',\n",
      "                              'person',\n",
      "                              'person'],\n",
      "                 'v_scores': [92.55717992782593,\n",
      "                              99.32825565338135,\n",
      "                              96.99269533157349,\n",
      "                              96.49882912635803,\n",
      "                              99.48840141296387,\n",
      "                              82.02412128448486,\n",
      "                              72.56042957305908,\n",
      "                              80.83866834640503],\n",
      "                 'vboxes': [(0, 123, 46, 334),\n",
      "                            (191, 383, 18, 379),\n",
      "                            (333, 458, 30, 374),\n",
      "                            (388, 492, 20, 402),\n",
      "                            (102, 229, 84, 352),\n",
      "                            (56, 441, 11, 447),\n",
      "                            (188, 242, 6, 157),\n",
      "                            (85, 155, 66, 239)]},\n",
      "             10: {'v_labels': ['person',\n",
      "                               'person',\n",
      "                               'person',\n",
      "                               'person',\n",
      "                               'person',\n",
      "                               'tvmonitor'],\n",
      "                  'v_scores': [97.35585451126099,\n",
      "                               89.28497433662415,\n",
      "                               99.17716979980469,\n",
      "                               94.09701228141785,\n",
      "                               93.08160543441772,\n",
      "                               73.23048710823059],\n",
      "                  'vboxes': [(323, 452, 35, 326),\n",
      "                             (0, 113, 53, 333),\n",
      "                             (186, 379, 15, 375),\n",
      "                             (385, 493, 22, 399),\n",
      "                             (104, 220, 76, 364),\n",
      "                             (54, 436, 13, 447)]},\n",
      "             20: {'v_labels': ['person',\n",
      "                               'person',\n",
      "                               'person',\n",
      "                               'person',\n",
      "                               'person',\n",
      "                               'tvmonitor'],\n",
      "                  'v_scores': [82.79736638069153,\n",
      "                               73.54065775871277,\n",
      "                               93.23654770851135,\n",
      "                               99.30883646011353,\n",
      "                               94.15586590766907,\n",
      "                               78.61786484718323],\n",
      "                  'vboxes': [(312, 428, 28, 326),\n",
      "                             (1, 106, 47, 333),\n",
      "                             (85, 199, 58, 344),\n",
      "                             (184, 370, 8, 383),\n",
      "                             (372, 493, 18, 400),\n",
      "                             (51, 439, 9, 456)]},\n",
      "             30: {'v_labels': ['person',\n",
      "                               'person',\n",
      "                               'person',\n",
      "                               'person',\n",
      "                               'person',\n",
      "                               'tvmonitor'],\n",
      "                  'v_scores': [85.04635691642761,\n",
      "                               88.33048939704895,\n",
      "                               96.26598358154297,\n",
      "                               99.44911003112793,\n",
      "                               91.30313992500305,\n",
      "                               66.667640209198],\n",
      "                  'vboxes': [(298, 430, 21, 333),\n",
      "                             (-1, 116, 38, 348),\n",
      "                             (84, 202, 53, 346),\n",
      "                             (174, 368, 10, 382),\n",
      "                             (367, 493, 24, 392),\n",
      "                             (63, 424, 0, 461)]},\n",
      "             40: {'v_labels': ['person',\n",
      "                               'person',\n",
      "                               'person',\n",
      "                               'person',\n",
      "                               'person',\n",
      "                               'person'],\n",
      "                  'v_scores': [91.82323217391968,\n",
      "                               95.71577310562134,\n",
      "                               98.67913126945496,\n",
      "                               99.55897927284241,\n",
      "                               95.20530104637146,\n",
      "                               96.48763537406921],\n",
      "                  'vboxes': [(289, 438, 15, 342),\n",
      "                             (0, 96, 43, 347),\n",
      "                             (84, 208, 56, 348),\n",
      "                             (150, 359, 25, 364),\n",
      "                             (367, 494, 22, 396),\n",
      "                             (66, 139, 50, 218)]},\n",
      "             50: {'v_labels': ['person',\n",
      "                               'person',\n",
      "                               'person',\n",
      "                               'person',\n",
      "                               'person',\n",
      "                               'tvmonitor',\n",
      "                               'person',\n",
      "                               'person'],\n",
      "                  'v_scores': [90.0563657283783,\n",
      "                               99.60424304008484,\n",
      "                               99.35174584388733,\n",
      "                               91.84811115264893,\n",
      "                               96.36315703392029,\n",
      "                               71.69594168663025,\n",
      "                               67.21392869949341,\n",
      "                               61.619603633880615],\n",
      "                  'vboxes': [(1, 108, 46, 335),\n",
      "                             (84, 204, 58, 347),\n",
      "                             (154, 354, 32, 364),\n",
      "                             (297, 430, 17, 375),\n",
      "                             (369, 493, 15, 399),\n",
      "                             (77, 408, 12, 450),\n",
      "                             (284, 347, 25, 153),\n",
      "                             (66, 138, 39, 311)]},\n",
      "             60: {'v_labels': ['person',\n",
      "                               'person',\n",
      "                               'person',\n",
      "                               'person',\n",
      "                               'person',\n",
      "                               'person',\n",
      "                               'tvmonitor',\n",
      "                               'person',\n",
      "                               'person'],\n",
      "                  'v_scores': [96.70511484146118,\n",
      "                               65.13259410858154,\n",
      "                               99.45477843284607,\n",
      "                               99.14534091949463,\n",
      "                               91.66932106018066,\n",
      "                               96.00441455841064,\n",
      "                               64.59804177284241,\n",
      "                               71.03583812713623,\n",
      "                               74.38929080963135],\n",
      "                  'vboxes': [(0, 101, 42, 335),\n",
      "                             (41, 144, 62, 312),\n",
      "                             (80, 208, 60, 344),\n",
      "                             (152, 352, 33, 362),\n",
      "                             (294, 431, 16, 377),\n",
      "                             (369, 494, 13, 402),\n",
      "                             (74, 413, 10, 452),\n",
      "                             (165, 217, 0, 87),\n",
      "                             (279, 352, 28, 152)]},\n",
      "             70: {'v_labels': ['person',\n",
      "                               'person',\n",
      "                               'person',\n",
      "                               'person',\n",
      "                               'person',\n",
      "                               'person',\n",
      "                               'tvmonitor',\n",
      "                               'person'],\n",
      "                  'v_scores': [81.25280141830444,\n",
      "                               97.69538640975952,\n",
      "                               97.94414043426514,\n",
      "                               99.3894875049591,\n",
      "                               97.39837050437927,\n",
      "                               95.32923102378845,\n",
      "                               60.59151887893677,\n",
      "                               81.2296986579895],\n",
      "                  'vboxes': [(1, 109, 43, 337),\n",
      "                             (28, 154, 60, 330),\n",
      "                             (102, 218, 60, 346),\n",
      "                             (150, 355, 32, 364),\n",
      "                             (306, 430, 31, 363),\n",
      "                             (371, 494, 19, 398),\n",
      "                             (71, 410, 4, 459),\n",
      "                             (285, 347, 31, 173)]},\n",
      "             80: {'v_labels': ['person',\n",
      "                               'person',\n",
      "                               'person',\n",
      "                               'person',\n",
      "                               'person',\n",
      "                               'person',\n",
      "                               'tvmonitor',\n",
      "                               'person'],\n",
      "                  'v_scores': [77.4164080619812,\n",
      "                               91.72790050506592,\n",
      "                               99.61099624633789,\n",
      "                               96.73118591308594,\n",
      "                               95.14124393463135,\n",
      "                               98.1213390827179,\n",
      "                               62.19985485076904,\n",
      "                               87.11501359939575],\n",
      "                  'vboxes': [(4, 102, 28, 344),\n",
      "                             (45, 153, 61, 332),\n",
      "                             (140, 364, 30, 370),\n",
      "                             (307, 429, 27, 367),\n",
      "                             (372, 494, 9, 404),\n",
      "                             (105, 219, 66, 365),\n",
      "                             (78, 404, 10, 450),\n",
      "                             (287, 346, 34, 168)]},\n",
      "             90: {'v_labels': ['person',\n",
      "                               'person',\n",
      "                               'person',\n",
      "                               'person',\n",
      "                               'person',\n",
      "                               'person',\n",
      "                               'tvmonitor',\n",
      "                               'person'],\n",
      "                  'v_scores': [96.33894562721252,\n",
      "                               88.39572072029114,\n",
      "                               77.95645594596863,\n",
      "                               99.4691789150238,\n",
      "                               93.85527968406677,\n",
      "                               97.96686172485352,\n",
      "                               83.637136220932,\n",
      "                               77.89674997329712],\n",
      "                  'vboxes': [(307, 423, 38, 310),\n",
      "                             (0, 103, 44, 340),\n",
      "                             (40, 153, 67, 322),\n",
      "                             (175, 374, 20, 376),\n",
      "                             (367, 494, 10, 403),\n",
      "                             (75, 210, 63, 361),\n",
      "                             (64, 424, 13, 449),\n",
      "                             (294, 343, 31, 151)]},\n",
      "             100: {'v_labels': ['person',\n",
      "                                'person',\n",
      "                                'person',\n",
      "                                'person',\n",
      "                                'person',\n",
      "                                'tvmonitor',\n",
      "                                'person',\n",
      "                                'person'],\n",
      "                   'v_scores': [97.08964824676514,\n",
      "                                98.54891896247864,\n",
      "                                99.57587718963623,\n",
      "                                99.57835078239441,\n",
      "                                92.68172979354858,\n",
      "                                68.18132400512695,\n",
      "                                61.17677688598633,\n",
      "                                67.68649816513062],\n",
      "                   'vboxes': [(302, 435, 28, 331),\n",
      "                              (0, 107, 52, 335),\n",
      "                              (84, 203, 55, 347),\n",
      "                              (163, 386, 27, 371),\n",
      "                              (384, 493, 14, 400),\n",
      "                              (113, 443, 19, 446),\n",
      "                              (172, 231, 3, 163),\n",
      "                              (73, 143, 57, 214)]},\n",
      "             110: {'v_labels': ['person',\n",
      "                                'person',\n",
      "                                'person',\n",
      "                                'person',\n",
      "                                'person',\n",
      "                                'person',\n",
      "                                'person'],\n",
      "                   'v_scores': [97.6650059223175,\n",
      "                                99.16821718215942,\n",
      "                                99.07457828521729,\n",
      "                                99.13824200630188,\n",
      "                                63.58157992362976,\n",
      "                                75.685715675354,\n",
      "                                77.40837931632996],\n",
      "                   'vboxes': [(1, 110, 54, 339),\n",
      "                              (87, 198, 57, 345),\n",
      "                              (141, 370, 24, 365),\n",
      "                              (287, 458, 27, 370),\n",
      "                              (169, 224, 9, 159),\n",
      "                              (84, 152, 58, 215),\n",
      "                              (431, 491, 40, 370)]},\n",
      "             120: {'v_labels': ['person',\n",
      "                                'person',\n",
      "                                'person',\n",
      "                                'person',\n",
      "                                'person',\n",
      "                                'person'],\n",
      "                   'v_scores': [99.42789673805237,\n",
      "                                89.81438279151917,\n",
      "                                98.99359941482544,\n",
      "                                86.90922856330872,\n",
      "                                80.98921179771423,\n",
      "                                70.37965059280396],\n",
      "                   'vboxes': [(284, 488, 10, 361),\n",
      "                              (0, 128, 24, 347),\n",
      "                              (133, 285, 50, 348),\n",
      "                              (418, 480, 34, 114),\n",
      "                              (265, 319, 62, 193),\n",
      "                              (98, 182, 46, 230)]}}}\n"
     ]
    }
   ],
   "source": [
    "pprint.pprint(metadata)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 1000
    },
    "colab_type": "code",
    "id": "8OdoPISG6HaB",
    "outputId": "bb103809-d80d-4de1-ea89-37b9a9dd1c71"
   },
   "outputs": [],
   "source": [
    "# Running over all videos now\n",
    "\n",
    "# Getting a list of all video files in a directory\n",
    "file_path_names = glob.glob(os.path.join(TRAIN_PATH, '*.mp4'))\n",
    "filenames = [os.path.basename(file_path_name) for file_path_name in file_path_names]\n",
    "\n",
    "# # Just testing a few, can comment out later\n",
    "# filenames = ['344_7.mp4', '260_13.mp4', '220_4.mp4', '245_8.mp4']\n",
    "\n",
    "print(filenames)\n",
    "\n",
    "# Process all videos, run YOLO, and store output\n",
    "metadata_all = {}\n",
    "for video_filename in filenames:\n",
    "    print('Processing video: {}'.format(video_filename))\n",
    "    metadata = process_video(video_dir=TRAIN_PATH, video_name=video_filename, video_num=0, total_videos=10, model=model)\n",
    "    metadata_all.update(metadata)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 0,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "CZNl1GLMCUxZ"
   },
   "outputs": [],
   "source": [
    "with open(os.path.join(TRAIN_PATH, 'yolov3', 'metadata_all.json'), 'w') as fp:\n",
    "    json.dump(metadata_all, fp, sort_keys=True, indent=4, separators=(',', ': '))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 0,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "Jwvn0TyM3NEs"
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 0,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "31piHUFySjX3"
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "accelerator": "GPU",
  "colab": {
   "collapsed_sections": [],
   "name": "yolov3_object_detection.ipynb",
   "provenance": []
  },
  "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.6.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
