<commit_before>{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import os\n",
    "import numpy\n",
    "from IPython.display import Image\n",
    "from scipy import ndimage"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "test_path = \"../captures/testing/IMG_2114.PNG\"\n",
    "Image(filename=test_path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "combined_image = ndimage.imread(test_path)\n",
    "combined_image.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "def split(image):\n",
    "    split_height = image.shape[0] / 2\n",
    "    return image[:split_height], image[split_height:]\n",
    "camera_image, depth_image = split(combined_image)\n",
    "print camera_image.shape, depth_image.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "depth_image[0].shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "def convert_to_depth(image):\n",
    "    height = image.shape[0]\n",
    "    width = image.shape[1]\n",
    "    depths = numpy.zeros((height, width), dtype=numpy.float32)\n",
    "    BYTE_MAX = 255\n",
    "    CHANNEL_MAX = 8\n",
    "    MAX_RED_VALUE = BYTE_MAX - CHANNEL_MAX\n",
    "    CHANNELS_MAX = CHANNEL_MAX * CHANNEL_MAX\n",
    "    INVALID = float('Nan')\n",
    "    found_orientation = False\n",
    "    orientation = [1, 0, 0, 0]\n",
    "    for y in xrange(0, height):\n",
    "        row = image[y]\n",
    "        depth_row = depths[y]\n",
    "        for x in xrange(0, width):\n",
    "            pixel = row[x]\n",
    "            red = pixel[0]\n",
    "            if x == 0 and y == 0 and red == BYTE_MAX:\n",
    "                found_orientation = True\n",
    "                depth_row[x] = INVALID\n",
    "                continue\n",
    "            if found_orientation:\n",
    "                for c in range(0, len(orientation)):\n",
    "                    orientation[c] = ((2.0 * pixel[c]) / BYTE_MAX) - 1\n",
    "                found_orientation = False\n",
    "                depth_row[x] = INVALID\n",
    "                continue\n",
    "            if red == 0:\n",
    "                depth_row[x] = INVALID\n",
    "                continue\n",
    "            depth_row[x] = (MAX_RED_VALUE - red) * CHANNELS_MAX + ((pixel[1] - red) * CHANNEL_MAX) + (pixel[2] - red)\n",
    "    return depths, orientation\n",
    "depths, attitude = convert_to_depth(depth_image)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "depths.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "attitude"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "depths[0][0:4]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "depths[120][300:310]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
<commit_msg>Implement decode using numpy operations.<commit_after>{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import os\n",
    "import numpy\n",
    "import timeit\n",
    "from IPython.display import Image\n",
    "from scipy import ndimage"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "test_path = \"../captures/testing/IMG_2114.PNG\"\n",
    "Image(filename=test_path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "combined_image = ndimage.imread(test_path)\n",
    "combined_image.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "def split(image):\n",
    "    split_height = image.shape[0] / 2\n",
    "    return image[:split_height], image[split_height:]\n",
    "camera_image, depth_image = split(combined_image)\n",
    "print camera_image.shape, depth_image.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "depth_image[0].shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def convert_to_depth(image):\n",
    "    height = image.shape[0]\n",
    "    width = image.shape[1]\n",
    "    depths = numpy.zeros((height, width), dtype=numpy.float32)\n",
    "    BYTE_MAX = 255\n",
    "    CHANNEL_MAX = 8\n",
    "    MAX_RED_VALUE = BYTE_MAX - CHANNEL_MAX\n",
    "    CHANNELS_MAX = CHANNEL_MAX * CHANNEL_MAX\n",
    "    INVALID = float('Nan')\n",
    "    found_orientation = False\n",
    "    orientation = [1, 0, 0, 0]\n",
    "    for y in xrange(0, height):\n",
    "        row = image[y]\n",
    "        depth_row = depths[y]\n",
    "        for x in xrange(0, width):\n",
    "            pixel = row[x]\n",
    "            red = pixel[0]\n",
    "            if x == 0 and y == 0 and red == BYTE_MAX:\n",
    "                found_orientation = True\n",
    "                depth_row[x] = INVALID\n",
    "                continue\n",
    "            if found_orientation:\n",
    "                for c in range(0, len(orientation)):\n",
    "                    orientation[c] = ((2.0 * pixel[c]) / BYTE_MAX) - 1\n",
    "                found_orientation = False\n",
    "                depth_row[x] = INVALID\n",
    "                continue\n",
    "            if red == 0:\n",
    "                depth_row[x] = INVALID\n",
    "                continue\n",
    "            depth_row[x] = (MAX_RED_VALUE - red) * CHANNELS_MAX + ((pixel[1] - red) * CHANNEL_MAX) + (pixel[2] - red)\n",
    "    return depths, orientation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "depths, attitude = convert_to_depth(depth_image)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "depths.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "attitude"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "depths[0][0:4]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "depths[120][300:310]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def decode_depth(image):\n",
    "    BYTE_MAX = 255\n",
    "    CHANNEL_MAX = 8.0\n",
    "    MAX_RED_VALUE = BYTE_MAX - CHANNEL_MAX\n",
    "    CHANNELS_MAX = CHANNEL_MAX * CHANNEL_MAX\n",
    "    orientation = [1, 0, 0, 0]\n",
    "    \n",
    "    if numpy.array_equal(image[0, 0], [BYTE_MAX, 0, 0, BYTE_MAX]):\n",
    "        print \"Found attitude\"\n",
    "        pixel = image[0, 1]\n",
    "        for c in range(len(orientation)):\n",
    "            orientation[c] = ((2.0 * pixel[c]) / BYTE_MAX) - 1\n",
    "        image[0, 0] = [0, 0, 0, BYTE_MAX]\n",
    "        image[0, 1] = [0, 0, 0, BYTE_MAX]\n",
    "            \n",
    "\n",
    "    red = image[:, :, 0]\n",
    "    green = image[:, :, 1]\n",
    "    blue = image[:, :, 2]\n",
    "\n",
    "    depth = ((MAX_RED_VALUE - red) * CHANNELS_MAX) + ((green - red) * CHANNEL_MAX) + (blue - red)\n",
    "    return depth, orientation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "depths, attitude = decode_depth(depth_image)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "depths.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "depths[120][300:310]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "attitude"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "timeit.timeit(\"depths, attitude = decode_depth(depth_image)\", number=10, setup=\"from __main__ import decode_depth,depth_image\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "timeit.timeit(\"depths, attitude = convert_to_depth(depth_image)\", number=10, setup=\"from __main__ import convert_to_depth,depth_image\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}

{'fraction_non_alphanumeric': 0.19996364297400473, 'fraction_numerical': 0.013633884748227595, 'mean_word_length': 2.1491127647395536, 'pattern_counts': {'":': 216, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6846174', 'n_tokens_mistral': 4317, 'n_tokens_neox': 4126, 'n_words': 712}