Created
January 17, 2022 18:15
-
-
Save jcheong0428/785394bd3710e309790c1a99170d7da5 to your computer and use it in GitHub Desktop.
VEST_output_loader.ipynb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"nbformat": 4, | |
"nbformat_minor": 0, | |
"metadata": { | |
"colab": { | |
"name": "VEST_output_loader.ipynb", | |
"provenance": [], | |
"collapsed_sections": [], | |
"authorship_tag": "ABX9TyMDzK8B3hDk6GZFP0URKN2W", | |
"include_colab_link": true | |
}, | |
"kernelspec": { | |
"name": "python3", | |
"display_name": "Python 3" | |
}, | |
"language_info": { | |
"name": "python" | |
} | |
}, | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "view-in-github", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"<a href=\"https://colab.research.google.com/gist/jcheong0428/785394bd3710e309790c1a99170d7da5/vest_output_loader.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"source": [ | |
"# Example code to parse and load the VEST json output into a Pandas dataframe\n", | |
"_Written by Jin Hyun Cheong_\n", | |
"\n", | |
"[VEST](https://jinhyuncheong.com/vest/vest.html) is a video event segmentation tool to code start and stop times of events in a video clip. In the VEST json outputs, segments include start and stop times while points only include start times. This code shows how you can use the `parse_vest_json()` function to get dataframe for the segmentations and points. " | |
], | |
"metadata": { | |
"id": "cJDB8SmVOxbn" | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 11, | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 928 | |
}, | |
"id": "zyRBO8dxDVkj", | |
"outputId": "9f5aaedc-af9a-4c06-cc16-3383b53820b9" | |
}, | |
"outputs": [ | |
{ | |
"output_type": "display_data", | |
"data": { | |
"text/html": [ | |
"\n", | |
" <div id=\"df-7219f9d9-df45-410a-b4a0-f89df9c60bde\">\n", | |
" <div class=\"colab-df-container\">\n", | |
" <div>\n", | |
"<style scoped>\n", | |
" .dataframe tbody tr th:only-of-type {\n", | |
" vertical-align: middle;\n", | |
" }\n", | |
"\n", | |
" .dataframe tbody tr th {\n", | |
" vertical-align: top;\n", | |
" }\n", | |
"\n", | |
" .dataframe thead th {\n", | |
" text-align: right;\n", | |
" }\n", | |
"</style>\n", | |
"<table border=\"1\" class=\"dataframe\">\n", | |
" <thead>\n", | |
" <tr style=\"text-align: right;\">\n", | |
" <th></th>\n", | |
" <th>startTime</th>\n", | |
" <th>endTime</th>\n", | |
" <th>id</th>\n", | |
" <th>labelText</th>\n", | |
" </tr>\n", | |
" </thead>\n", | |
" <tbody>\n", | |
" <tr>\n", | |
" <th>peaks.segment.0</th>\n", | |
" <td>4.660444</td>\n", | |
" <td>7.660444</td>\n", | |
" <td>peaks.segment.0</td>\n", | |
" <td>1</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>peaks.segment.1</th>\n", | |
" <td>5.190149</td>\n", | |
" <td>8.190149</td>\n", | |
" <td>peaks.segment.1</td>\n", | |
" <td>1</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>peaks.segment.2</th>\n", | |
" <td>5.868703</td>\n", | |
" <td>8.868703</td>\n", | |
" <td>peaks.segment.2</td>\n", | |
" <td>2</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>peaks.segment.3</th>\n", | |
" <td>6.057861</td>\n", | |
" <td>9.057861</td>\n", | |
" <td>peaks.segment.3</td>\n", | |
" <td>2</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>peaks.segment.4</th>\n", | |
" <td>6.202695</td>\n", | |
" <td>9.202695</td>\n", | |
" <td>peaks.segment.4</td>\n", | |
" <td>2</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>peaks.segment.5</th>\n", | |
" <td>6.361782</td>\n", | |
" <td>9.361782</td>\n", | |
" <td>peaks.segment.5</td>\n", | |
" <td>2</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>peaks.segment.6</th>\n", | |
" <td>6.516530</td>\n", | |
" <td>9.516530</td>\n", | |
" <td>peaks.segment.6</td>\n", | |
" <td>2</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>peaks.segment.7</th>\n", | |
" <td>6.676493</td>\n", | |
" <td>9.676493</td>\n", | |
" <td>peaks.segment.7</td>\n", | |
" <td>2</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>peaks.segment.8</th>\n", | |
" <td>6.801770</td>\n", | |
" <td>9.801770</td>\n", | |
" <td>peaks.segment.8</td>\n", | |
" <td>2</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>peaks.segment.9</th>\n", | |
" <td>14.178363</td>\n", | |
" <td>17.178363</td>\n", | |
" <td>peaks.segment.9</td>\n", | |
" <td>2</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>peaks.segment.10</th>\n", | |
" <td>14.662548</td>\n", | |
" <td>17.662548</td>\n", | |
" <td>peaks.segment.10</td>\n", | |
" <td>1</td>\n", | |
" </tr>\n", | |
" </tbody>\n", | |
"</table>\n", | |
"</div>\n", | |
" <button class=\"colab-df-convert\" onclick=\"convertToInteractive('df-7219f9d9-df45-410a-b4a0-f89df9c60bde')\"\n", | |
" title=\"Convert this dataframe to an interactive table.\"\n", | |
" style=\"display:none;\">\n", | |
" \n", | |
" <svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\"viewBox=\"0 0 24 24\"\n", | |
" width=\"24px\">\n", | |
" <path d=\"M0 0h24v24H0V0z\" fill=\"none\"/>\n", | |
" <path d=\"M18.56 5.44l.94 2.06.94-2.06 2.06-.94-2.06-.94-.94-2.06-.94 2.06-2.06.94zm-11 1L8.5 8.5l.94-2.06 2.06-.94-2.06-.94L8.5 2.5l-.94 2.06-2.06.94zm10 10l.94 2.06.94-2.06 2.06-.94-2.06-.94-.94-2.06-.94 2.06-2.06.94z\"/><path d=\"M17.41 7.96l-1.37-1.37c-.4-.4-.92-.59-1.43-.59-.52 0-1.04.2-1.43.59L10.3 9.45l-7.72 7.72c-.78.78-.78 2.05 0 2.83L4 21.41c.39.39.9.59 1.41.59.51 0 1.02-.2 1.41-.59l7.78-7.78 2.81-2.81c.8-.78.8-2.07 0-2.86zM5.41 20L4 18.59l7.72-7.72 1.47 1.35L5.41 20z\"/>\n", | |
" </svg>\n", | |
" </button>\n", | |
" \n", | |
" <style>\n", | |
" .colab-df-container {\n", | |
" display:flex;\n", | |
" flex-wrap:wrap;\n", | |
" gap: 12px;\n", | |
" }\n", | |
"\n", | |
" .colab-df-convert {\n", | |
" background-color: #E8F0FE;\n", | |
" border: none;\n", | |
" border-radius: 50%;\n", | |
" cursor: pointer;\n", | |
" display: none;\n", | |
" fill: #1967D2;\n", | |
" height: 32px;\n", | |
" padding: 0 0 0 0;\n", | |
" width: 32px;\n", | |
" }\n", | |
"\n", | |
" .colab-df-convert:hover {\n", | |
" background-color: #E2EBFA;\n", | |
" box-shadow: 0px 1px 2px rgba(60, 64, 67, 0.3), 0px 1px 3px 1px rgba(60, 64, 67, 0.15);\n", | |
" fill: #174EA6;\n", | |
" }\n", | |
"\n", | |
" [theme=dark] .colab-df-convert {\n", | |
" background-color: #3B4455;\n", | |
" fill: #D2E3FC;\n", | |
" }\n", | |
"\n", | |
" [theme=dark] .colab-df-convert:hover {\n", | |
" background-color: #434B5C;\n", | |
" box-shadow: 0px 1px 3px 1px rgba(0, 0, 0, 0.15);\n", | |
" filter: drop-shadow(0px 1px 2px rgba(0, 0, 0, 0.3));\n", | |
" fill: #FFFFFF;\n", | |
" }\n", | |
" </style>\n", | |
"\n", | |
" <script>\n", | |
" const buttonEl =\n", | |
" document.querySelector('#df-7219f9d9-df45-410a-b4a0-f89df9c60bde button.colab-df-convert');\n", | |
" buttonEl.style.display =\n", | |
" google.colab.kernel.accessAllowed ? 'block' : 'none';\n", | |
"\n", | |
" async function convertToInteractive(key) {\n", | |
" const element = document.querySelector('#df-7219f9d9-df45-410a-b4a0-f89df9c60bde');\n", | |
" const dataTable =\n", | |
" await google.colab.kernel.invokeFunction('convertToInteractive',\n", | |
" [key], {});\n", | |
" if (!dataTable) return;\n", | |
"\n", | |
" const docLinkHtml = 'Like what you see? Visit the ' +\n", | |
" '<a target=\"_blank\" href=https://colab.research.google.com/notebooks/data_table.ipynb>data table notebook</a>'\n", | |
" + ' to learn more about interactive tables.';\n", | |
" element.innerHTML = '';\n", | |
" dataTable['output_type'] = 'display_data';\n", | |
" await google.colab.output.renderOutput(dataTable, element);\n", | |
" const docLink = document.createElement('div');\n", | |
" docLink.innerHTML = docLinkHtml;\n", | |
" element.appendChild(docLink);\n", | |
" }\n", | |
" </script>\n", | |
" </div>\n", | |
" </div>\n", | |
" " | |
], | |
"text/plain": [ | |
" startTime endTime id labelText\n", | |
"peaks.segment.0 4.660444 7.660444 peaks.segment.0 1\n", | |
"peaks.segment.1 5.190149 8.190149 peaks.segment.1 1\n", | |
"peaks.segment.2 5.868703 8.868703 peaks.segment.2 2\n", | |
"peaks.segment.3 6.057861 9.057861 peaks.segment.3 2\n", | |
"peaks.segment.4 6.202695 9.202695 peaks.segment.4 2\n", | |
"peaks.segment.5 6.361782 9.361782 peaks.segment.5 2\n", | |
"peaks.segment.6 6.516530 9.516530 peaks.segment.6 2\n", | |
"peaks.segment.7 6.676493 9.676493 peaks.segment.7 2\n", | |
"peaks.segment.8 6.801770 9.801770 peaks.segment.8 2\n", | |
"peaks.segment.9 14.178363 17.178363 peaks.segment.9 2\n", | |
"peaks.segment.10 14.662548 17.662548 peaks.segment.10 1" | |
] | |
}, | |
"metadata": {} | |
}, | |
{ | |
"output_type": "display_data", | |
"data": { | |
"text/html": [ | |
"\n", | |
" <div id=\"df-d1f82dbb-a520-4310-947b-004284f1521f\">\n", | |
" <div class=\"colab-df-container\">\n", | |
" <div>\n", | |
"<style scoped>\n", | |
" .dataframe tbody tr th:only-of-type {\n", | |
" vertical-align: middle;\n", | |
" }\n", | |
"\n", | |
" .dataframe tbody tr th {\n", | |
" vertical-align: top;\n", | |
" }\n", | |
"\n", | |
" .dataframe thead th {\n", | |
" text-align: right;\n", | |
" }\n", | |
"</style>\n", | |
"<table border=\"1\" class=\"dataframe\">\n", | |
" <thead>\n", | |
" <tr style=\"text-align: right;\">\n", | |
" <th></th>\n", | |
" <th>startTime</th>\n", | |
" <th>id</th>\n", | |
" <th>labelText</th>\n", | |
" </tr>\n", | |
" </thead>\n", | |
" <tbody>\n", | |
" <tr>\n", | |
" <th>peaks.point.0</th>\n", | |
" <td>11.075451</td>\n", | |
" <td>peaks.point.0</td>\n", | |
" <td>222</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>peaks.point.1</th>\n", | |
" <td>11.217249</td>\n", | |
" <td>peaks.point.1</td>\n", | |
" <td>222</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>peaks.point.2</th>\n", | |
" <td>11.361147</td>\n", | |
" <td>peaks.point.2</td>\n", | |
" <td>222</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>peaks.point.3</th>\n", | |
" <td>11.460108</td>\n", | |
" <td>peaks.point.3</td>\n", | |
" <td>222</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>peaks.point.4</th>\n", | |
" <td>11.571997</td>\n", | |
" <td>peaks.point.4</td>\n", | |
" <td>222</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>peaks.point.5</th>\n", | |
" <td>11.708106</td>\n", | |
" <td>peaks.point.5</td>\n", | |
" <td>222</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>peaks.point.6</th>\n", | |
" <td>11.854410</td>\n", | |
" <td>peaks.point.6</td>\n", | |
" <td>222</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>peaks.point.7</th>\n", | |
" <td>11.990123</td>\n", | |
" <td>peaks.point.7</td>\n", | |
" <td>222</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>peaks.point.8</th>\n", | |
" <td>12.147687</td>\n", | |
" <td>peaks.point.8</td>\n", | |
" <td>222</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>peaks.point.9</th>\n", | |
" <td>12.729908</td>\n", | |
" <td>peaks.point.9</td>\n", | |
" <td>1</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>peaks.point.10</th>\n", | |
" <td>12.889067</td>\n", | |
" <td>peaks.point.10</td>\n", | |
" <td>1</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>peaks.point.11</th>\n", | |
" <td>13.040318</td>\n", | |
" <td>peaks.point.11</td>\n", | |
" <td>1</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>peaks.point.12</th>\n", | |
" <td>13.175818</td>\n", | |
" <td>peaks.point.12</td>\n", | |
" <td>1</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>peaks.point.13</th>\n", | |
" <td>13.303282</td>\n", | |
" <td>peaks.point.13</td>\n", | |
" <td>1</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>peaks.point.14</th>\n", | |
" <td>13.423370</td>\n", | |
" <td>peaks.point.14</td>\n", | |
" <td>1</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>peaks.point.15</th>\n", | |
" <td>13.538035</td>\n", | |
" <td>peaks.point.15</td>\n", | |
" <td>1</td>\n", | |
" </tr>\n", | |
" </tbody>\n", | |
"</table>\n", | |
"</div>\n", | |
" <button class=\"colab-df-convert\" onclick=\"convertToInteractive('df-d1f82dbb-a520-4310-947b-004284f1521f')\"\n", | |
" title=\"Convert this dataframe to an interactive table.\"\n", | |
" style=\"display:none;\">\n", | |
" \n", | |
" <svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\"viewBox=\"0 0 24 24\"\n", | |
" width=\"24px\">\n", | |
" <path d=\"M0 0h24v24H0V0z\" fill=\"none\"/>\n", | |
" <path d=\"M18.56 5.44l.94 2.06.94-2.06 2.06-.94-2.06-.94-.94-2.06-.94 2.06-2.06.94zm-11 1L8.5 8.5l.94-2.06 2.06-.94-2.06-.94L8.5 2.5l-.94 2.06-2.06.94zm10 10l.94 2.06.94-2.06 2.06-.94-2.06-.94-.94-2.06-.94 2.06-2.06.94z\"/><path d=\"M17.41 7.96l-1.37-1.37c-.4-.4-.92-.59-1.43-.59-.52 0-1.04.2-1.43.59L10.3 9.45l-7.72 7.72c-.78.78-.78 2.05 0 2.83L4 21.41c.39.39.9.59 1.41.59.51 0 1.02-.2 1.41-.59l7.78-7.78 2.81-2.81c.8-.78.8-2.07 0-2.86zM5.41 20L4 18.59l7.72-7.72 1.47 1.35L5.41 20z\"/>\n", | |
" </svg>\n", | |
" </button>\n", | |
" \n", | |
" <style>\n", | |
" .colab-df-container {\n", | |
" display:flex;\n", | |
" flex-wrap:wrap;\n", | |
" gap: 12px;\n", | |
" }\n", | |
"\n", | |
" .colab-df-convert {\n", | |
" background-color: #E8F0FE;\n", | |
" border: none;\n", | |
" border-radius: 50%;\n", | |
" cursor: pointer;\n", | |
" display: none;\n", | |
" fill: #1967D2;\n", | |
" height: 32px;\n", | |
" padding: 0 0 0 0;\n", | |
" width: 32px;\n", | |
" }\n", | |
"\n", | |
" .colab-df-convert:hover {\n", | |
" background-color: #E2EBFA;\n", | |
" box-shadow: 0px 1px 2px rgba(60, 64, 67, 0.3), 0px 1px 3px 1px rgba(60, 64, 67, 0.15);\n", | |
" fill: #174EA6;\n", | |
" }\n", | |
"\n", | |
" [theme=dark] .colab-df-convert {\n", | |
" background-color: #3B4455;\n", | |
" fill: #D2E3FC;\n", | |
" }\n", | |
"\n", | |
" [theme=dark] .colab-df-convert:hover {\n", | |
" background-color: #434B5C;\n", | |
" box-shadow: 0px 1px 3px 1px rgba(0, 0, 0, 0.15);\n", | |
" filter: drop-shadow(0px 1px 2px rgba(0, 0, 0, 0.3));\n", | |
" fill: #FFFFFF;\n", | |
" }\n", | |
" </style>\n", | |
"\n", | |
" <script>\n", | |
" const buttonEl =\n", | |
" document.querySelector('#df-d1f82dbb-a520-4310-947b-004284f1521f button.colab-df-convert');\n", | |
" buttonEl.style.display =\n", | |
" google.colab.kernel.accessAllowed ? 'block' : 'none';\n", | |
"\n", | |
" async function convertToInteractive(key) {\n", | |
" const element = document.querySelector('#df-d1f82dbb-a520-4310-947b-004284f1521f');\n", | |
" const dataTable =\n", | |
" await google.colab.kernel.invokeFunction('convertToInteractive',\n", | |
" [key], {});\n", | |
" if (!dataTable) return;\n", | |
"\n", | |
" const docLinkHtml = 'Like what you see? Visit the ' +\n", | |
" '<a target=\"_blank\" href=https://colab.research.google.com/notebooks/data_table.ipynb>data table notebook</a>'\n", | |
" + ' to learn more about interactive tables.';\n", | |
" element.innerHTML = '';\n", | |
" dataTable['output_type'] = 'display_data';\n", | |
" await google.colab.output.renderOutput(dataTable, element);\n", | |
" const docLink = document.createElement('div');\n", | |
" docLink.innerHTML = docLinkHtml;\n", | |
" element.appendChild(docLink);\n", | |
" }\n", | |
" </script>\n", | |
" </div>\n", | |
" </div>\n", | |
" " | |
], | |
"text/plain": [ | |
" startTime id labelText\n", | |
"peaks.point.0 11.075451 peaks.point.0 222\n", | |
"peaks.point.1 11.217249 peaks.point.1 222\n", | |
"peaks.point.2 11.361147 peaks.point.2 222\n", | |
"peaks.point.3 11.460108 peaks.point.3 222\n", | |
"peaks.point.4 11.571997 peaks.point.4 222\n", | |
"peaks.point.5 11.708106 peaks.point.5 222\n", | |
"peaks.point.6 11.854410 peaks.point.6 222\n", | |
"peaks.point.7 11.990123 peaks.point.7 222\n", | |
"peaks.point.8 12.147687 peaks.point.8 222\n", | |
"peaks.point.9 12.729908 peaks.point.9 1\n", | |
"peaks.point.10 12.889067 peaks.point.10 1\n", | |
"peaks.point.11 13.040318 peaks.point.11 1\n", | |
"peaks.point.12 13.175818 peaks.point.12 1\n", | |
"peaks.point.13 13.303282 peaks.point.13 1\n", | |
"peaks.point.14 13.423370 peaks.point.14 1\n", | |
"peaks.point.15 13.538035 peaks.point.15 1" | |
] | |
}, | |
"metadata": {} | |
} | |
], | |
"source": [ | |
"import pandas as pd\n", | |
"\n", | |
"def parse_vest_json(f):\n", | |
" \"\"\"Parse json output from VEST\n", | |
"\n", | |
" Args:\n", | |
" f: path to VEST json output\n", | |
" \n", | |
" Returns:\n", | |
" segmentations dataframe\n", | |
" points dataframe\n", | |
" \"\"\"\n", | |
" seg_df, pts_df = pd.DataFrame(), pd.DataFrame()\n", | |
"\n", | |
" df = pd.read_json(f, lines=True)\n", | |
" seg_cols = [col for col in df.columns if 'segment' in col]\n", | |
" _seg_df = df[seg_cols].dropna().T\n", | |
" for rowix, row in _seg_df.iterrows():\n", | |
" seg_df = pd.concat([seg_df, pd.DataFrame(row.values[0],index=[row.name])])\n", | |
"\n", | |
" pts_cols = [col for col in df.columns if 'point' in col]\n", | |
" _pts_df = df[pts_cols].dropna().T\n", | |
" for rowix, row in _pts_df.iterrows():\n", | |
" pts_df = pd.concat([pts_df, pd.DataFrame(row.values[0], index=[row.name])])\n", | |
" return seg_df, pts_df\n", | |
"\n", | |
"\n", | |
"path_to_file = \"/content/segmentation_results.json\"\n", | |
"seg_df, pts_df = parse_vest_json(f=path_to_file)\n", | |
"display(seg_df, pts_df)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"" | |
], | |
"metadata": { | |
"id": "U22G7yVjD4Tc" | |
}, | |
"execution_count": null, | |
"outputs": [] | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment