{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from skimage.feature import hog\n",
    "import numpy as np\n",
    "import cv2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "from dask import delayed\n",
    "import dask"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_hog_features(\n",
    "    img, orient, pix_per_cell, cell_per_block, feature_vec = True\n",
    "):\n",
    "\n",
    "    features = hog(\n",
    "        img,\n",
    "        orientations = orient,\n",
    "        pixels_per_cell = (pix_per_cell, pix_per_cell),\n",
    "        cells_per_block = (cell_per_block, cell_per_block),\n",
    "        transform_sqrt = True,\n",
    "        visualise = False,\n",
    "        feature_vector = feature_vec,\n",
    "    )\n",
    "    return features\n",
    "\n",
    "\n",
    "def bin_spatial(img, size = (16, 16)):\n",
    "    return cv2.resize(img, size).ravel()\n",
    "\n",
    "\n",
    "def img_features(\n",
    "    feature_image, hist_bins, orient, pix_per_cell, cell_per_block, spatial_size\n",
    "):\n",
    "    features = []\n",
    "    features.append(bin_spatial(feature_image, size = spatial_size))\n",
    "    features.append(\n",
    "        get_hog_features(feature_image, orient, pix_per_cell, cell_per_block)\n",
    "    )\n",
    "    return features\n",
    "\n",
    "\n",
    "def extract_features(img):\n",
    "    img = cv2.resize(img, (350, 125))\n",
    "    file_features = img_features(\n",
    "        img,\n",
    "        hist_bins = 32,\n",
    "        orient = 8,\n",
    "        pix_per_cell = 8,\n",
    "        cell_per_block = 2,\n",
    "        spatial_size = (32, 32),\n",
    "    )\n",
    "    return np.concatenate(file_features)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pickle\n",
    "\n",
    "with open('detect-dataset.pkl', 'rb') as fopen:\n",
    "    dataset = pickle.load(fopen)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict_keys(['positive', 'negative'])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dataset.keys()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "  0%|          | 0/10000 [00:00<?, ?it/s]/home/husein/.local/lib/python3.6/site-packages/skimage/feature/_hog.py:239: skimage_deprecation: Argument `visualise` is deprecated and will be changed to `visualize` in v0.16\n",
      "  'be changed to `visualize` in v0.16', skimage_deprecation)\n",
      "100%|██████████| 10000/10000 [04:01<00:00, 41.38it/s]\n"
     ]
    }
   ],
   "source": [
    "from tqdm import tqdm\n",
    "\n",
    "positives = []\n",
    "for i in tqdm(range(len(dataset['positive'][:10000]))):\n",
    "    r = extract_features(dataset['positive'][i])\n",
    "    positives.append(r)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 10000/10000 [04:01<00:00, 41.34it/s]\n"
     ]
    }
   ],
   "source": [
    "from tqdm import tqdm\n",
    "\n",
    "negatives = []\n",
    "for i in tqdm(range(len(dataset['negative'][:10000]))):\n",
    "    r = extract_features(dataset['negative'][i])\n",
    "    negatives.append(r)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pickle\n",
    "\n",
    "with open('features.pkl', 'wb') as fopen:\n",
    "    pickle.dump({'positive': positives, 'negative': negatives}, fopen)"
   ]
  },
  {
   "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.6.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
