|
279 | 279 | "source": [ |
280 | 280 | "## Step 5: Run Video Inference\n", |
281 | 281 | "\n", |
282 | | - "Runs motion-based insect detection and hierarchical classification on video files. Detects moving insects using background subtraction (GMM), tracks them across frames, classifies each detection, and aggregates predictions per track.\n", |
| 282 | + "Processes a video through a multi-phase pipeline: motion-based detection (GMM), Hungarian tracking, path topology confirmation, and hierarchical classification. Detection and tracking are powered by [BugSpot](../bugspot/), a lightweight core that runs on any platform.\n", |
283 | 283 | "\n", |
284 | | - "**Note:** The species list and taxonomy are automatically loaded from the model checkpoint, so you don't need to provide them again.\n", |
285 | | - "\n", |
286 | | - "**Output files generated:**\n", |
287 | | - "- `{video}_annotated.mp4` - Video with detection boxes and track paths (if `save_video=True`)\n", |
288 | | - "- `{video}_debug.mp4` - Side-by-side view with GMM motion mask (if `save_video=True`)\n", |
289 | | - "- `{video}_results.csv` - Aggregated results per track\n", |
290 | | - "- `{video}_detections.csv` - Frame-by-frame detections\n", |
291 | | - "\n", |
292 | | - "**Tip:** Set `save_video=False` to skip video rendering and only generate CSV output (faster processing)." |
| 284 | + "The species list is automatically loaded from the model checkpoint. All detection parameters can be customized via `config=` (YAML/JSON file). See [`detection_config.yaml`](../detection_config.yaml) for all parameters and defaults." |
293 | 285 | ] |
294 | 286 | }, |
295 | 287 | { |
|
299 | 291 | "outputs": [], |
300 | 292 | "source": [ |
301 | 293 | "results = bplusplus.inference(\n", |
302 | | - " hierarchical_model_path=RESNET_MULTITASK_WEIGHTS,\n", |
303 | 294 | " video_path=\"./10.mp4\",\n", |
304 | 295 | " output_dir=\"./output\",\n", |
305 | | - " # species_list=names, # Optional: override species from checkpoint\n", |
306 | | - " fps=None, # None = all frames\n", |
307 | | - " backbone=\"resnet50\", # Must match training\n", |
308 | | - " save_video=True, # Set to False to skip video rendering (only CSV output)\n", |
309 | | - " img_size=60, # Must match training\n", |
| 296 | + " hierarchical_model_path=RESNET_MULTITASK_WEIGHTS,\n", |
| 297 | + " backbone=\"resnet50\", # Must match training\n", |
| 298 | + " img_size=60, # Must match training\n", |
| 299 | + " # --- Optional ---\n", |
| 300 | + " # species_list=names, # Override species from checkpoint\n", |
| 301 | + " # fps=None, # None = all frames, or set target FPS\n", |
| 302 | + " # config=\"config.yaml\", # Custom detection parameters (YAML/JSON)\n", |
| 303 | + " # classify=False, # Detection only, NaN for classification\n", |
| 304 | + " # save_video=True, # Annotated + debug videos\n", |
| 305 | + " # crops=False, # Save crop per detection per track\n", |
| 306 | + " # track_composites=False, # Composite image per track (temporal trail)\n", |
310 | 307 | ")\n", |
311 | 308 | "\n", |
312 | | - "print(f\"Detected {results['tracks']} tracks ({results['confirmed_tracks']} confirmed)\")" |
| 309 | + "print(f\"Confirmed: {results['confirmed_tracks']} / {results['tracks']} tracks\")" |
313 | 310 | ] |
314 | 311 | }, |
315 | 312 | { |
316 | 313 | "cell_type": "markdown", |
317 | 314 | "metadata": {}, |
318 | 315 | "source": [ |
319 | | - "### Custom Detection Configuration\n", |
320 | | - "\n", |
321 | | - "The inference uses motion-based detection with configurable parameters for filtering detections. You can customize these by providing a YAML or JSON config file.\n", |
322 | | - "\n", |
323 | | - "Download a template config from: https://github.com/Tvenver/Bplusplus/releases/download/weights/detection_config.yaml\n", |
324 | | - "\n", |
325 | | - "```python\n", |
326 | | - "results = bplusplus.inference(\n", |
327 | | - " ...,\n", |
328 | | - " config=\"detection_config.yaml\"\n", |
329 | | - ")\n", |
330 | | - "```\n", |
331 | | - "\n", |
332 | | - "#### Full Configuration Parameters\n", |
333 | 316 | "\n", |
334 | | - "| Parameter | Default | Description |\n", |
335 | | - "|-----------|---------|-------------|\n", |
336 | | - "| **GMM Background Subtractor** | | *Motion detection model* |\n", |
337 | | - "| `gmm_history` | 500 | Frames to build background model |\n", |
338 | | - "| `gmm_var_threshold` | 16 | Variance threshold for foreground detection |\n", |
339 | | - "| **Morphological Filtering** | | *Noise removal* |\n", |
340 | | - "| `morph_kernel_size` | 3 | Morphological kernel size (NxN) |\n", |
341 | | - "| **Cohesiveness** | | *Filters scattered motion (plants) vs compact motion (insects)* |\n", |
342 | | - "| `min_largest_blob_ratio` | 0.80 | Min ratio of largest blob to total motion |\n", |
343 | | - "| `max_num_blobs` | 5 | Max separate blobs allowed in detection |\n", |
344 | | - "| `min_motion_ratio` | 0.15 | Min ratio of motion pixels to bbox area |\n", |
345 | | - "| **Shape** | | *Filters by contour properties* |\n", |
346 | | - "| `min_area` | 200 | Min detection area (px²) |\n", |
347 | | - "| `max_area` | 40000 | Max detection area (px²) |\n", |
348 | | - "| `min_density` | 3.0 | Min area/perimeter ratio |\n", |
349 | | - "| `min_solidity` | 0.55 | Min convex hull fill ratio |\n", |
350 | | - "| **Tracking** | | *Controls track behavior* |\n", |
351 | | - "| `min_displacement` | 50 | Min net movement for confirmation (px) |\n", |
352 | | - "| `min_path_points` | 10 | Min points before path analysis |\n", |
353 | | - "| `max_frame_jump` | 100 | Max jump between frames (px) |\n", |
354 | | - "| `max_lost_frames` | 45 | Frames before lost track deleted (e.g., 45 @ 30fps = 1.5s) |\n", |
355 | | - "| `max_area_change_ratio` | 3.0 | Max area change ratio between frames |\n", |
356 | | - "| **Tracker Matching** | | *Hungarian algorithm cost function* |\n", |
357 | | - "| `tracker_w_dist` | 0.6 | Weight for distance cost (0-1) |\n", |
358 | | - "| `tracker_w_area` | 0.4 | Weight for area cost (0-1) |\n", |
359 | | - "| `tracker_cost_threshold` | 0.3 | Max cost for valid match (0-1) |\n", |
360 | | - "| **Path Topology** | | *Confirms insect-like movement patterns* |\n", |
361 | | - "| `max_revisit_ratio` | 0.30 | Max ratio of revisited positions |\n", |
362 | | - "| `min_progression_ratio` | 0.70 | Min forward progression |\n", |
363 | | - "| `max_directional_variance` | 0.90 | Max heading variance |\n", |
364 | | - "| `revisit_radius` | 50 | Radius (px) for revisit detection |\n", |
365 | 317 | "\n" |
366 | 318 | ] |
367 | 319 | } |
|
0 commit comments