Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix gesture collection notebook to handle both training and testing dataset creation #4

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
138 changes: 67 additions & 71 deletions gesture_data_collection_pose.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,7 @@
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"Matplotlib created a temporary config/cache directory at /tmp/matplotlib-bhsk56tr because the default path (/home/mikyas/.cache/matplotlib) is not a writable directory; it is highly recommended to set the MPLCONFIGDIR environment variable to a writable directory, in particular to speed up the import of Matplotlib and to better support multiprocessing.\n"
]
}
],
"outputs": [],
"source": [
"import json\n",
"import cv2\n",
Expand All @@ -28,7 +20,7 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 2,
"metadata": {},
"outputs": [
{
Expand All @@ -37,7 +29,7 @@
"<All keys matched successfully>"
]
},
"execution_count": 4,
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
Expand Down Expand Up @@ -78,7 +70,7 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -91,7 +83,7 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -107,32 +99,28 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"/home/mikyas/data_collection/hand_dataset_train\n",
"/home/mikyas/data_collection/hand_dataset_train\n",
"/home/mikyas/data_collection/hand_dataset_train\n",
"/home/mikyas/data_collection/hand_dataset_train\n",
"/home/mikyas/data_collection/hand_dataset_train\n",
"/home/mikyas/data_collection/hand_dataset_train\n"
]
}
],
"outputs": [],
"source": [
"dir_datasets = '/home/mikyas/data_collection/' #give the path to where you want to save you collected data\n",
"dataset_name = \"hand_dataset_train\" #change this to hand_dataset_test when you are collecting data for test\n",
"no_of_classes = 6\n",
"dir_datasets = 'dataset/' #give the path to where you want to save you collected data\n",
"dataset_name = \"hand_dataset_2class\" #change this to hand_dataset_test when you are collecting data for test\n",
"no_of_classes = 2\n",
"create_directories_for_classes(no_of_classes, dir_datasets, dataset_name )"
]
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"train_or_test = \"testing\" # training or testing"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
Expand All @@ -141,7 +129,7 @@
"False"
]
},
"execution_count": 8,
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -162,7 +150,7 @@
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -200,7 +188,7 @@
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -224,7 +212,7 @@
},
{
"cell_type": "code",
"execution_count": 11,
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -243,7 +231,7 @@
},
{
"cell_type": "code",
"execution_count": 12,
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -265,7 +253,7 @@
},
{
"cell_type": "code",
"execution_count": 13,
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -281,7 +269,7 @@
},
{
"cell_type": "code",
"execution_count": 14,
"execution_count": 13,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -295,13 +283,13 @@
},
{
"cell_type": "code",
"execution_count": 15,
"execution_count": 14,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "375b9e6d61384dc1beb1b7cc25a0666b",
"model_id": "7396f41d9c194036a0002914d2b4c55f",
"version_major": 2,
"version_minor": 0
},
Expand All @@ -315,7 +303,7 @@
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "380b01ad84b340c1a9342ccea6ad1b46",
"model_id": "000c286288c0469e8401c945fbd51b21",
"version_major": 2,
"version_minor": 0
},
Expand All @@ -329,7 +317,7 @@
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "d954768e10cf46fb963d60c733baf892",
"model_id": "1f08ce283f0a4a19ba25ce3f2ab0593b",
"version_major": 2,
"version_minor": 0
},
Expand All @@ -343,7 +331,7 @@
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "13412c28b4ca46a7b41ab27decca0214",
"model_id": "1c85b52eabc74d578bffa6d1137faacf",
"version_major": 2,
"version_minor": 0
},
Expand All @@ -364,7 +352,7 @@
},
{
"cell_type": "code",
"execution_count": 16,
"execution_count": 15,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -383,7 +371,7 @@
},
{
"cell_type": "code",
"execution_count": 17,
"execution_count": 16,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -392,7 +380,7 @@
},
{
"cell_type": "code",
"execution_count": 18,
"execution_count": 17,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -401,7 +389,7 @@
},
{
"cell_type": "code",
"execution_count": 19,
"execution_count": 18,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -410,7 +398,7 @@
},
{
"cell_type": "code",
"execution_count": 20,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -419,34 +407,41 @@
},
{
"cell_type": "code",
"execution_count": 21,
"execution_count": 19,
"metadata": {},
"outputs": [],
"source": [
"def generate_labels(dir_, dataset_name):\n",
" labels = []\n",
" starting_label = 1\n",
" for i in range(len(os.listdir(dir_))):\n",
" for i in range(no_of_classes):\n",
" dir_to_check = os.path.join(dir_,\"%s\" % (i+1))\n",
" for j in range(len(os.listdir(dir_to_check))):\n",
" labels.append(starting_label)\n",
" starting_label+=1\n",
" labels_to_dict = {\"labels\": labels}\n",
" with open((dir_+'.json'), 'w') as outfile:\n",
" if train_or_test == \"training\":\n",
" filename = dir_+'.json'\n",
" elif train_or_test == \"testing\": \n",
" filename = dir_+'_testing.json'\n",
" else:\n",
" print(\"ERROR: check your `train_or_test` variable\")\n",
" \n",
" with open(filename, 'w') as outfile:\n",
" json.dump(labels_to_dict, outfile)\n",
" return labels "
]
},
{
"cell_type": "code",
"execution_count": 22,
"execution_count": 20,
"metadata": {},
"outputs": [],
"source": [
"def rename_images(dir_):\n",
" overall_count = 0\n",
" #dir_ = dir_+dataset_name\n",
" for i in range(len(os.listdir(dir_))):\n",
" for i in range(no_of_classes):\n",
" dir_to_check = os.path.join(dir_,\"%s\" % (i+1))\n",
" dir_to_check+='/'\n",
" for count, filename in enumerate(os.listdir(dir_to_check)):\n",
Expand All @@ -459,45 +454,46 @@
},
{
"cell_type": "code",
"execution_count": 23,
"execution_count": 21,
"metadata": {},
"outputs": [],
"source": [
"generate_labels(dir_, dataset_name)\n",
"rename_images(dir_)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Pick a cell to execute depending on Training or Testing"
]
},
{
"cell_type": "code",
"execution_count": 41,
"execution_count": 22,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'/home/mikyas/chitoku/data_collection/hand_dataset_test/hand_dataset_test.json'"
]
},
"execution_count": 41,
"metadata": {},
"output_type": "execute_result"
}
],
"outputs": [],
"source": [
"import shutil\n",
"dir_training = dir_datasets +'/training/'#change this to /test/ when you are collecting data for test\n",
"dir_training = dir_datasets +'/' + train_or_test + '/'#change this to /test/ when you are collecting data for test\n",
"try:\n",
" os.makedirs(dir_training)\n",
"except FileExistsError:\n",
" print(os.path.join(\"The following directory was not created because it already exsists\", dir_ , ))\n",
"for i in range(len(os.listdir(dir_))):\n",
"for i in range(no_of_classes):\n",
" dir_to_check = os.path.join(dir_,\"%s\" % (i+1))+'/'\n",
" for count, filename in enumerate(os.listdir(dir_to_check)):\n",
" src = dir_to_check+filename\n",
" shutil.move(src,dir_training)\n",
" os.rmdir(dir_to_check)\n",
"shutil.move(dir_training,dir_)\n",
"shutil.move(dir_+'.json',dir_)"
"if train_or_test == \"training\":\n",
" shutil.move(dir_+'.json',dir_)\n",
"elif train_or_test == \"testing\": \n",
" shutil.move(dir_+'_testing.json',dir_)\n",
"else:\n",
" print(\"ERROR: check your `train_or_test` variable\")"
]
},
{
Expand Down