|
174 | 174 | "\n",
|
175 | 175 | " cv2.imwrite('segmented_objects.jpg', background)"
|
176 | 176 | ]
|
| 177 | + }, |
| 178 | + { |
| 179 | + "cell_type": "markdown", |
| 180 | + "metadata": {}, |
| 181 | + "source": [ |
| 182 | + "# generate dataset for pith detection" |
| 183 | + ] |
| 184 | + }, |
| 185 | + { |
| 186 | + "cell_type": "code", |
| 187 | + "execution_count": null, |
| 188 | + "metadata": {}, |
| 189 | + "outputs": [], |
| 190 | + "source": [ |
| 191 | + "dataset_path = \"/Users/tonymeissner/Downloads/bilder\"\n", |
| 192 | + "output_folder = \"output\"\n", |
| 193 | + "model_path = \"/Users/tonymeissner/source/tree-disk-analyzer/tree-disk-segmentation/models/yolo11s-seg-tree.pt\"" |
| 194 | + ] |
| 195 | + }, |
| 196 | + { |
| 197 | + "cell_type": "code", |
| 198 | + "execution_count": null, |
| 199 | + "metadata": {}, |
| 200 | + "outputs": [], |
| 201 | + "source": [ |
| 202 | + "# Create output directory if it doesn't exist\n", |
| 203 | + "os.makedirs(output_folder, exist_ok=True)\n", |
| 204 | + "\n", |
| 205 | + "# Function to check if file is a valid image\n", |
| 206 | + "def is_valid_image(filepath):\n", |
| 207 | + " # Skip macOS resource fork files and hidden files\n", |
| 208 | + " if os.path.basename(filepath).startswith('._') or os.path.basename(filepath).startswith('.'):\n", |
| 209 | + " return False\n", |
| 210 | + " \n", |
| 211 | + " # Check file extension\n", |
| 212 | + " if not filepath.lower().endswith('.jpg'):\n", |
| 213 | + " return False\n", |
| 214 | + " \n", |
| 215 | + " # Verify file exists\n", |
| 216 | + " if not os.path.exists(filepath):\n", |
| 217 | + " return False\n", |
| 218 | + " \n", |
| 219 | + " return True\n", |
| 220 | + "\n", |
| 221 | + "# Find all valid image files\n", |
| 222 | + "image_paths = []\n", |
| 223 | + "for file in os.listdir(dataset_path):\n", |
| 224 | + " full_path = os.path.join(dataset_path, file)\n", |
| 225 | + " if os.path.isfile(full_path) and is_valid_image(full_path):\n", |
| 226 | + " image_paths.append(full_path)\n", |
| 227 | + "\n", |
| 228 | + "print(f\"Found {len(image_paths)} valid images\")" |
| 229 | + ] |
| 230 | + }, |
| 231 | + { |
| 232 | + "cell_type": "code", |
| 233 | + "execution_count": null, |
| 234 | + "metadata": {}, |
| 235 | + "outputs": [], |
| 236 | + "source": [ |
| 237 | + "import treedisksegmentation\n", |
| 238 | + "import logging\n", |
| 239 | + "import gc\n", |
| 240 | + "\n", |
| 241 | + "def process_image(image_path, output_folder, model_path):\n", |
| 242 | + " \"\"\"Process a single image with YOLO segmentation.\"\"\"\n", |
| 243 | + " try:\n", |
| 244 | + " output_path = os.path.join(output_folder, os.path.basename(image_path))\n", |
| 245 | + "\n", |
| 246 | + " if os.path.exists(output_path):\n", |
| 247 | + " print(f\"Skipping existing output: {image_path}\")\n", |
| 248 | + " return True\n", |
| 249 | + "\n", |
| 250 | + " treedisksegmentation.configure(\n", |
| 251 | + " model_path=model_path,\n", |
| 252 | + " input_image=image_path,\n", |
| 253 | + " )\n", |
| 254 | + "\n", |
| 255 | + " result_image, masks = treedisksegmentation.run()\n", |
| 256 | + "\n", |
| 257 | + " if masks is None:\n", |
| 258 | + " print(f\"No masks detected for image: {image_path}\")\n", |
| 259 | + " return False\n", |
| 260 | + "\n", |
| 261 | + " result_image = cv2.cvtColor(result_image, cv2.COLOR_RGB2BGR)\n", |
| 262 | + " cv2.imwrite(output_path, result_image)\n", |
| 263 | + " print(f\"Successfully processed: {image_path}\")\n", |
| 264 | + " return True\n", |
| 265 | + "\n", |
| 266 | + " except Exception as e:\n", |
| 267 | + " logging.error(f\"Error processing {image_path}: {str(e)}\", exc_info=True)\n", |
| 268 | + " return False" |
| 269 | + ] |
| 270 | + }, |
| 271 | + { |
| 272 | + "cell_type": "code", |
| 273 | + "execution_count": null, |
| 274 | + "metadata": {}, |
| 275 | + "outputs": [], |
| 276 | + "source": [ |
| 277 | + "for image_path in image_paths:\n", |
| 278 | + " process_image(image_path, output_folder, model_path)\n", |
| 279 | + " print(f\"{image_paths.index(image_path)} / {len(image_paths)} images processed\")\n", |
| 280 | + " gc.collect()" |
| 281 | + ] |
177 | 282 | }
|
178 | 283 | ],
|
179 | 284 | "metadata": {
|
|
0 commit comments