Spaces:
Build error
Build error
Upload dataset_format_conversion.ipynb
Browse files- dataset_format_conversion.ipynb +133 -0
dataset_format_conversion.ipynb
ADDED
|
@@ -0,0 +1,133 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"cells": [
|
| 3 |
+
{
|
| 4 |
+
"cell_type": "markdown",
|
| 5 |
+
"source": [
|
| 6 |
+
"### YOLO to Standard format conversion script"
|
| 7 |
+
],
|
| 8 |
+
"metadata": {
|
| 9 |
+
"collapsed": false
|
| 10 |
+
}
|
| 11 |
+
},
|
| 12 |
+
{
|
| 13 |
+
"cell_type": "code",
|
| 14 |
+
"execution_count": null,
|
| 15 |
+
"outputs": [],
|
| 16 |
+
"source": [
|
| 17 |
+
"import cv2\n",
|
| 18 |
+
"import os\n",
|
| 19 |
+
"from tqdm import tqdm\n",
|
| 20 |
+
"\n",
|
| 21 |
+
"base_dir = os.getcwd()\n",
|
| 22 |
+
"\n",
|
| 23 |
+
"# Paths to the original dataset\n",
|
| 24 |
+
"ORIGINAL_TRAIN_DIR = os.path.join(base_dir,'dataset/train/images')\n",
|
| 25 |
+
"ORIGINAL_VAL_DIR = os.path.join(base_dir,'dataset/valid/images')\n",
|
| 26 |
+
"ORIGINAL_TEST_DIR = os.path.join(base_dir,'dataset/test/images')\n",
|
| 27 |
+
"TRAIN_LABELS_DIR = os.path.join(base_dir,'dataset/train/labels')\n",
|
| 28 |
+
"VAL_LABELS_DIR = os.path.join(base_dir,'dataset/valid/labels')\n",
|
| 29 |
+
"TEST_LABELS_DIR = os.path.join(base_dir,'dataset/test/labels')\n",
|
| 30 |
+
"\n",
|
| 31 |
+
"# Paths to the cropped images based on labels\n",
|
| 32 |
+
"CROPPED_TRAIN_DIR = os.path.join(base_dir,'cropped_dataset/train')\n",
|
| 33 |
+
"CROPPED_VAL_DIR = os.path.join(base_dir,'cropped_dataset/valid')\n",
|
| 34 |
+
"CROPPED_TEST_DIR = os.path.join(base_dir,'cropped_dataset/test')\n",
|
| 35 |
+
"\n",
|
| 36 |
+
"def preprocess_dataset(images_dir, labels_dir, cropped_dir):\n",
|
| 37 |
+
" if not os.path.exists(cropped_dir):\n",
|
| 38 |
+
" os.makedirs(cropped_dir)\n",
|
| 39 |
+
"\n",
|
| 40 |
+
" for label in ['awake', 'sleepy']:\n",
|
| 41 |
+
" label_dir = os.path.join(cropped_dir, label)\n",
|
| 42 |
+
" if not os.path.exists(label_dir):\n",
|
| 43 |
+
" os.makedirs(label_dir)\n",
|
| 44 |
+
"\n",
|
| 45 |
+
" for img_name in tqdm(os.listdir(images_dir), desc=f'Processing images in {images_dir}'):\n",
|
| 46 |
+
" img_path = os.path.join(images_dir, img_name)\n",
|
| 47 |
+
" label_path = os.path.join(labels_dir, os.path.splitext(img_name)[0] + '.txt')\n",
|
| 48 |
+
"\n",
|
| 49 |
+
" if not os.path.exists(label_path):\n",
|
| 50 |
+
" continue\n",
|
| 51 |
+
"\n",
|
| 52 |
+
" with open(label_path, 'r') as f:\n",
|
| 53 |
+
" labels = f.readlines()\n",
|
| 54 |
+
"\n",
|
| 55 |
+
" image = cv2.imread(img_path)\n",
|
| 56 |
+
" height, width, _ = image.shape\n",
|
| 57 |
+
"\n",
|
| 58 |
+
" for label in labels:\n",
|
| 59 |
+
" if label.strip() == '':\n",
|
| 60 |
+
" continue\n",
|
| 61 |
+
"\n",
|
| 62 |
+
" label_parts = label.strip().split()\n",
|
| 63 |
+
" if len(label_parts) != 5:\n",
|
| 64 |
+
" continue\n",
|
| 65 |
+
"\n",
|
| 66 |
+
" class_id, x_center, y_center, bbox_width, bbox_height = map(float, label_parts)\n",
|
| 67 |
+
"\n",
|
| 68 |
+
" # Convert to pixel coordinates\n",
|
| 69 |
+
" x1 = int((x_center - bbox_width / 2) * width)\n",
|
| 70 |
+
" y1 = int((y_center - bbox_height / 2) * height)\n",
|
| 71 |
+
" x2 = int((x_center + bbox_width / 2) * width)\n",
|
| 72 |
+
" y2 = int((y_center + bbox_height / 2) * height)\n",
|
| 73 |
+
"\n",
|
| 74 |
+
" # Ensure bounding box coordinates are within image boundaries\n",
|
| 75 |
+
" x1 = max(0, x1)\n",
|
| 76 |
+
" y1 = max(0, y1)\n",
|
| 77 |
+
" x2 = min(width, x2)\n",
|
| 78 |
+
" y2 = min(height, y2)\n",
|
| 79 |
+
"\n",
|
| 80 |
+
" # Crop the face region\n",
|
| 81 |
+
" face_img = image[y1:y2, x1:x2]\n",
|
| 82 |
+
"\n",
|
| 83 |
+
" # Determine the label for saving the cropped face\n",
|
| 84 |
+
" if int(class_id) == 0:\n",
|
| 85 |
+
" face_label_dir = os.path.join(cropped_dir, 'awake')\n",
|
| 86 |
+
" else:\n",
|
| 87 |
+
" face_label_dir = os.path.join(cropped_dir, 'sleepy')\n",
|
| 88 |
+
"\n",
|
| 89 |
+
" # Save the cropped face image\n",
|
| 90 |
+
" face_img_path = os.path.join(face_label_dir, img_name)\n",
|
| 91 |
+
" cv2.imwrite(face_img_path, face_img)"
|
| 92 |
+
],
|
| 93 |
+
"metadata": {
|
| 94 |
+
"collapsed": false
|
| 95 |
+
}
|
| 96 |
+
},
|
| 97 |
+
{
|
| 98 |
+
"cell_type": "code",
|
| 99 |
+
"execution_count": null,
|
| 100 |
+
"outputs": [],
|
| 101 |
+
"source": [
|
| 102 |
+
"# Preprocess the datasets\n",
|
| 103 |
+
"preprocess_dataset(ORIGINAL_TRAIN_DIR, TRAIN_LABELS_DIR, CROPPED_TRAIN_DIR)\n",
|
| 104 |
+
"preprocess_dataset(ORIGINAL_VAL_DIR, VAL_LABELS_DIR, CROPPED_VAL_DIR)\n",
|
| 105 |
+
"preprocess_dataset(ORIGINAL_TEST_DIR, TEST_LABELS_DIR, CROPPED_TEST_DIR)\n"
|
| 106 |
+
],
|
| 107 |
+
"metadata": {
|
| 108 |
+
"collapsed": false
|
| 109 |
+
}
|
| 110 |
+
}
|
| 111 |
+
],
|
| 112 |
+
"metadata": {
|
| 113 |
+
"kernelspec": {
|
| 114 |
+
"display_name": "Python 3",
|
| 115 |
+
"language": "python",
|
| 116 |
+
"name": "python3"
|
| 117 |
+
},
|
| 118 |
+
"language_info": {
|
| 119 |
+
"codemirror_mode": {
|
| 120 |
+
"name": "ipython",
|
| 121 |
+
"version": 2
|
| 122 |
+
},
|
| 123 |
+
"file_extension": ".py",
|
| 124 |
+
"mimetype": "text/x-python",
|
| 125 |
+
"name": "python",
|
| 126 |
+
"nbconvert_exporter": "python",
|
| 127 |
+
"pygments_lexer": "ipython2",
|
| 128 |
+
"version": "2.7.6"
|
| 129 |
+
}
|
| 130 |
+
},
|
| 131 |
+
"nbformat": 4,
|
| 132 |
+
"nbformat_minor": 0
|
| 133 |
+
}
|