Refactor repository
Browse files- app.py +43 -117
- examples/images/forest.jpg +2 -2
- examples/images/{coast.jpg → ocean.jpg} +2 -2
- examples/images/river.jpg +3 -0
- examples/videos/sea.mp4 +3 -0
- examples/videos/seaa.mp4 +3 -0
- utils/config.py +17 -23
app.py
CHANGED
|
@@ -5,7 +5,6 @@ import uuid
|
|
| 5 |
import logging
|
| 6 |
|
| 7 |
import torch
|
| 8 |
-
import spaces
|
| 9 |
import trackers
|
| 10 |
import numpy as np
|
| 11 |
import gradio as gr
|
|
@@ -13,12 +12,9 @@ import imageio.v3 as iio
|
|
| 13 |
import supervision as sv
|
| 14 |
|
| 15 |
from pathlib import Path
|
| 16 |
-
from functools import lru_cache
|
| 17 |
from typing import List, Optional, Tuple
|
| 18 |
|
| 19 |
from PIL import Image
|
| 20 |
-
from transformers import AutoModelForObjectDetection, AutoImageProcessor
|
| 21 |
-
from transformers.image_utils import load_image
|
| 22 |
from pipeline import build_pipeline
|
| 23 |
from utils import cfg, load_config, load_onnx_model
|
| 24 |
|
|
@@ -28,26 +24,19 @@ DETECTORS = {
|
|
| 28 |
"yolo8n-640": 'downloads/yolo8n-640.onnx',
|
| 29 |
"yolo8n-416": 'downloads/yolo8n-416.onnx',
|
| 30 |
}
|
| 31 |
-
|
| 32 |
DEFAULT_CONFIDENCE_THRESHOLD = 0.6
|
| 33 |
|
| 34 |
-
TORCH_DTYPE = torch.float32
|
| 35 |
|
| 36 |
# Image
|
| 37 |
IMAGE_EXAMPLES = [
|
| 38 |
-
{"path": "./examples/images/forest.jpg", "
|
| 39 |
-
{"path": "./examples/images/
|
| 40 |
-
{
|
| 41 |
-
"path": None,
|
| 42 |
-
"use_url": True,
|
| 43 |
-
"url": "https://live.staticflickr.com/65535/33021460783_1646d43c54_b.jpg",
|
| 44 |
-
"label": "Flickr Image",
|
| 45 |
-
},
|
| 46 |
]
|
| 47 |
|
| 48 |
# Video
|
| 49 |
MAX_NUM_FRAMES = 250
|
| 50 |
-
BATCH_SIZE = 4
|
| 51 |
ALLOWED_VIDEO_EXTENSIONS = {".mp4", ".avi", ".mov"}
|
| 52 |
VIDEO_OUTPUT_DIR = Path("static/videos")
|
| 53 |
VIDEO_OUTPUT_DIR.mkdir(parents=True, exist_ok=True)
|
|
@@ -59,10 +48,8 @@ class TrackingAlgorithm:
|
|
| 59 |
|
| 60 |
TRACKERS = [None, TrackingAlgorithm.BYTETRACK, TrackingAlgorithm.DEEPSORT, TrackingAlgorithm.SORT]
|
| 61 |
VIDEO_EXAMPLES = [
|
| 62 |
-
{"path": "./examples/videos/
|
| 63 |
-
{"path": "./examples/videos/forest.mp4", "label": "Local Video", "tracker": TrackingAlgorithm.BYTETRACK, "classes": "
|
| 64 |
-
{"path": "./examples/videos/fast_and_furious.mp4", "label": "Local Video", "tracker": None, "classes": "all"},
|
| 65 |
-
{"path": "./examples/videos/break_dance.mp4", "label": "Local Video", "tracker": None, "classes": "all"},
|
| 66 |
]
|
| 67 |
|
| 68 |
|
|
@@ -92,102 +79,64 @@ def detect_objects(
|
|
| 92 |
images: List[np.ndarray] | np.ndarray,
|
| 93 |
confidence_threshold: float = DEFAULT_CONFIDENCE_THRESHOLD,
|
| 94 |
target_size: Optional[Tuple[int, int]] = None,
|
| 95 |
-
batch_size: int = BATCH_SIZE,
|
| 96 |
classes: Optional[List[str]] = None,
|
| 97 |
):
|
| 98 |
-
|
| 99 |
-
# device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 100 |
-
# model, image_processor = get_model_and_processor(checkpoint)
|
| 101 |
-
# model = model.to(device)
|
| 102 |
-
|
| 103 |
-
# load_config(cfg, f'configs/{checkpoint}.yaml')
|
| 104 |
-
# pipeline = build_pipeline(cfg.pipeline)
|
| 105 |
-
# load_onnx_model(pipeline.detector, 'downloads/yolo8n-416.onnx')
|
| 106 |
-
# config.detector.thresholds.confidence = confidence_threshold
|
| 107 |
config.defrost()
|
| 108 |
-
config.detector.thresholds.confidence = confidence_threshold
|
| 109 |
config.freeze()
|
| 110 |
pipeline = get_pipeline(config, onnx_path)
|
| 111 |
-
|
|
|
|
| 112 |
if classes is not None:
|
| 113 |
-
wrong_classes = [cls for cls in classes if cls not in
|
| 114 |
if wrong_classes:
|
| 115 |
gr.Warning(f"Classes not found in model config: {wrong_classes}")
|
| 116 |
-
keep_ids = [
|
| 117 |
else:
|
| 118 |
keep_ids = None
|
| 119 |
|
| 120 |
if isinstance(images, np.ndarray) and images.ndim == 4:
|
| 121 |
images = [x for x in images] # split video array into list of images
|
| 122 |
|
| 123 |
-
batches = [images[i:i + batch_size] for i in range(0, len(images), batch_size)]
|
| 124 |
-
|
| 125 |
results = []
|
| 126 |
-
for
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 135 |
|
| 136 |
-
batch_results = []
|
| 137 |
-
for i in range(len(batch)):
|
| 138 |
-
img = batch[i]
|
| 139 |
-
output_ = pipeline(img)
|
| 140 |
-
output = {
|
| 141 |
-
"scores": torch.from_numpy(output_.confidence) if isinstance(output_.confidence, np.ndarray) else output_.confidence,
|
| 142 |
-
"labels": torch.from_numpy(output_.class_id) if isinstance(output_.class_id, np.ndarray) else output_.class_id,
|
| 143 |
-
"boxes": torch.from_numpy(output_.xyxy) if isinstance(output_.xyxy, np.ndarray) else output_.xyxy,
|
| 144 |
-
}
|
| 145 |
-
batch_results.append(output)
|
| 146 |
|
| 147 |
|
| 148 |
-
# postprocess outputs
|
| 149 |
-
# if target_size:
|
| 150 |
-
# target_sizes = [target_size] * len(batch)
|
| 151 |
-
# else:
|
| 152 |
-
# target_sizes = [(image.shape[0], image.shape[1]) for image in batch]
|
| 153 |
-
|
| 154 |
-
# batch_results = image_processor.post_process_object_detection(
|
| 155 |
-
# outputs, target_sizes=target_sizes, threshold=confidence_threshold
|
| 156 |
-
# )
|
| 157 |
-
|
| 158 |
-
results.extend(batch_results)
|
| 159 |
-
|
| 160 |
# # move results to cpu
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
|
| 167 |
# return results, model.config.id2label
|
| 168 |
return results, pipeline.detector.get_category_mapping()
|
| 169 |
|
| 170 |
|
| 171 |
def process_image(
|
| 172 |
-
|
| 173 |
image: Optional[Image.Image] = None,
|
| 174 |
-
url: Optional[str] = None,
|
| 175 |
-
use_url: bool = False,
|
| 176 |
confidence_threshold: float = DEFAULT_CONFIDENCE_THRESHOLD,
|
| 177 |
):
|
| 178 |
-
if not use_url:
|
| 179 |
-
url = None
|
| 180 |
-
|
| 181 |
-
if (image is None) ^ bool(url):
|
| 182 |
-
raise ValueError(f"Either image or url must be provided, but not both.")
|
| 183 |
-
|
| 184 |
-
if url:
|
| 185 |
-
image = load_image(url)
|
| 186 |
|
| 187 |
-
load_config(cfg, f'configs/{
|
| 188 |
results, id2label = detect_objects(
|
| 189 |
config=cfg.pipeline,
|
| 190 |
-
onnx_path=DETECTORS[
|
| 191 |
images=[np.array(image)],
|
| 192 |
confidence_threshold=confidence_threshold,
|
| 193 |
)
|
|
@@ -297,11 +246,10 @@ def process_video(
|
|
| 297 |
|
| 298 |
box_annotator = sv.BoxAnnotator(color, color_lookup=color_lookup, thickness=1)
|
| 299 |
label_annotator = sv.LabelAnnotator(color, color_lookup=color_lookup, text_scale=0.5)
|
| 300 |
-
trace_annotator = sv.TraceAnnotator(color, color_lookup=color_lookup, thickness=1, trace_length=100)
|
| 301 |
|
| 302 |
# preprocess classes
|
| 303 |
if classes != "all":
|
| 304 |
-
classes_list = [cls.strip()
|
| 305 |
else:
|
| 306 |
classes_list = None
|
| 307 |
|
|
@@ -328,7 +276,6 @@ def process_video(
|
|
| 328 |
labels = [f"#{tracker_id} {id2label[class_id]}" for class_id, tracker_id in zip(detections.class_id, detections.tracker_id)]
|
| 329 |
annotated_frame = box_annotator.annotate(scene=frame, detections=detections)
|
| 330 |
annotated_frame = label_annotator.annotate(scene=annotated_frame, detections=detections, labels=labels)
|
| 331 |
-
annotated_frame = trace_annotator.annotate(scene=annotated_frame, detections=detections)
|
| 332 |
annotated_frames.append(annotated_frame)
|
| 333 |
|
| 334 |
else:
|
|
@@ -354,17 +301,10 @@ def create_image_inputs() -> List[gr.components.Component]:
|
|
| 354 |
interactive=True,
|
| 355 |
elem_classes="input-component",
|
| 356 |
),
|
| 357 |
-
gr.Checkbox(label="Use Image URL Instead", value=False),
|
| 358 |
-
gr.Textbox(
|
| 359 |
-
label="Image URL",
|
| 360 |
-
placeholder="https://example.com/image.jpg",
|
| 361 |
-
visible=False,
|
| 362 |
-
elem_classes="input-component",
|
| 363 |
-
),
|
| 364 |
gr.Dropdown(
|
| 365 |
choices=list(DETECTORS.keys()),
|
| 366 |
label="Select Model Checkpoint",
|
| 367 |
-
value=
|
| 368 |
elem_classes="input-component",
|
| 369 |
),
|
| 370 |
gr.Slider(
|
|
@@ -390,7 +330,7 @@ def create_video_inputs() -> List[gr.components.Component]:
|
|
| 390 |
gr.Dropdown(
|
| 391 |
choices=list(DETECTORS.keys()),
|
| 392 |
label="Select Model Checkpoint",
|
| 393 |
-
value=
|
| 394 |
elem_classes="input-component",
|
| 395 |
),
|
| 396 |
gr.Dropdown(
|
|
@@ -434,6 +374,8 @@ with gr.Blocks(theme=gr.themes.Ocean()) as demo:
|
|
| 434 |
- **Image** and **Video** modes are supported.
|
| 435 |
- Select a model and adjust the confidence threshold to see detections!
|
| 436 |
- On video mode, you can enable tracking powered by [Supervision](https://github.com/roboflow/supervision) and [Trackers](https://github.com/roboflow/trackers) from Roboflow.
|
|
|
|
|
|
|
| 437 |
""",
|
| 438 |
elem_classes="header-text",
|
| 439 |
)
|
|
@@ -445,8 +387,6 @@ with gr.Blocks(theme=gr.themes.Ocean()) as demo:
|
|
| 445 |
with gr.Group():
|
| 446 |
(
|
| 447 |
image_input,
|
| 448 |
-
use_url,
|
| 449 |
-
url_input,
|
| 450 |
image_model_checkpoint,
|
| 451 |
image_confidence_threshold,
|
| 452 |
) = create_image_inputs()
|
|
@@ -461,10 +401,8 @@ with gr.Blocks(theme=gr.themes.Ocean()) as demo:
|
|
| 461 |
gr.Examples(
|
| 462 |
examples=[
|
| 463 |
[
|
| 464 |
-
|
| 465 |
example["path"],
|
| 466 |
-
example["url"],
|
| 467 |
-
example["use_url"],
|
| 468 |
DEFAULT_CONFIDENCE_THRESHOLD,
|
| 469 |
]
|
| 470 |
for example in IMAGE_EXAMPLES
|
|
@@ -472,8 +410,6 @@ with gr.Blocks(theme=gr.themes.Ocean()) as demo:
|
|
| 472 |
inputs=[
|
| 473 |
image_model_checkpoint,
|
| 474 |
image_input,
|
| 475 |
-
url_input,
|
| 476 |
-
use_url,
|
| 477 |
image_confidence_threshold,
|
| 478 |
],
|
| 479 |
outputs=[image_output],
|
|
@@ -501,7 +437,7 @@ with gr.Blocks(theme=gr.themes.Ocean()) as demo:
|
|
| 501 |
|
| 502 |
gr.Examples(
|
| 503 |
examples=[
|
| 504 |
-
[example["path"],
|
| 505 |
for example in VIDEO_EXAMPLES
|
| 506 |
],
|
| 507 |
inputs=[video_input, video_checkpoint, video_tracker, video_classes, video_confidence_threshold],
|
|
@@ -511,27 +447,19 @@ with gr.Blocks(theme=gr.themes.Ocean()) as demo:
|
|
| 511 |
label="Select a video example to populate inputs",
|
| 512 |
)
|
| 513 |
|
| 514 |
-
# Dynamic visibility for URL input
|
| 515 |
-
use_url.change(
|
| 516 |
-
fn=lambda x: gr.update(visible=x),
|
| 517 |
-
inputs=use_url,
|
| 518 |
-
outputs=url_input,
|
| 519 |
-
)
|
| 520 |
-
|
| 521 |
# Image clear button
|
| 522 |
image_clear_button.click(
|
| 523 |
fn=lambda: (
|
| 524 |
None,
|
| 525 |
False,
|
| 526 |
"",
|
| 527 |
-
|
| 528 |
DEFAULT_CONFIDENCE_THRESHOLD,
|
| 529 |
None,
|
| 530 |
),
|
| 531 |
outputs=[
|
| 532 |
image_input,
|
| 533 |
-
|
| 534 |
-
url_input,
|
| 535 |
image_model_checkpoint,
|
| 536 |
image_confidence_threshold,
|
| 537 |
image_output,
|
|
@@ -542,7 +470,7 @@ with gr.Blocks(theme=gr.themes.Ocean()) as demo:
|
|
| 542 |
video_clear_button.click(
|
| 543 |
fn=lambda: (
|
| 544 |
None,
|
| 545 |
-
|
| 546 |
None,
|
| 547 |
"all",
|
| 548 |
DEFAULT_CONFIDENCE_THRESHOLD,
|
|
@@ -564,8 +492,6 @@ with gr.Blocks(theme=gr.themes.Ocean()) as demo:
|
|
| 564 |
inputs=[
|
| 565 |
image_model_checkpoint,
|
| 566 |
image_input,
|
| 567 |
-
url_input,
|
| 568 |
-
use_url,
|
| 569 |
image_confidence_threshold,
|
| 570 |
],
|
| 571 |
outputs=[image_output],
|
|
|
|
| 5 |
import logging
|
| 6 |
|
| 7 |
import torch
|
|
|
|
| 8 |
import trackers
|
| 9 |
import numpy as np
|
| 10 |
import gradio as gr
|
|
|
|
| 12 |
import supervision as sv
|
| 13 |
|
| 14 |
from pathlib import Path
|
|
|
|
| 15 |
from typing import List, Optional, Tuple
|
| 16 |
|
| 17 |
from PIL import Image
|
|
|
|
|
|
|
| 18 |
from pipeline import build_pipeline
|
| 19 |
from utils import cfg, load_config, load_onnx_model
|
| 20 |
|
|
|
|
| 24 |
"yolo8n-640": 'downloads/yolo8n-640.onnx',
|
| 25 |
"yolo8n-416": 'downloads/yolo8n-416.onnx',
|
| 26 |
}
|
| 27 |
+
DEFAULT_DETECTOR = list(DETECTORS.keys())[0]
|
| 28 |
DEFAULT_CONFIDENCE_THRESHOLD = 0.6
|
| 29 |
|
|
|
|
| 30 |
|
| 31 |
# Image
|
| 32 |
IMAGE_EXAMPLES = [
|
| 33 |
+
{"path": "./examples/images/forest.jpg", "label": "Local Image"},
|
| 34 |
+
{"path": "./examples/images/river.jpg", "label": "Local Image"},
|
| 35 |
+
{"path": "./examples/images/ocean.jpg", "label": "Local Image"},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
]
|
| 37 |
|
| 38 |
# Video
|
| 39 |
MAX_NUM_FRAMES = 250
|
|
|
|
| 40 |
ALLOWED_VIDEO_EXTENSIONS = {".mp4", ".avi", ".mov"}
|
| 41 |
VIDEO_OUTPUT_DIR = Path("static/videos")
|
| 42 |
VIDEO_OUTPUT_DIR.mkdir(parents=True, exist_ok=True)
|
|
|
|
| 48 |
|
| 49 |
TRACKERS = [None, TrackingAlgorithm.BYTETRACK, TrackingAlgorithm.DEEPSORT, TrackingAlgorithm.SORT]
|
| 50 |
VIDEO_EXAMPLES = [
|
| 51 |
+
{"path": "./examples/videos/sea.mp4", "label": "Local Video", "tracker": TrackingAlgorithm.BYTETRACK, "classes": "Person, Boat"},
|
| 52 |
+
{"path": "./examples/videos/forest.mp4", "label": "Local Video", "tracker": TrackingAlgorithm.BYTETRACK, "classes": "LightVehicle, Person, Boat"},
|
|
|
|
|
|
|
| 53 |
]
|
| 54 |
|
| 55 |
|
|
|
|
| 79 |
images: List[np.ndarray] | np.ndarray,
|
| 80 |
confidence_threshold: float = DEFAULT_CONFIDENCE_THRESHOLD,
|
| 81 |
target_size: Optional[Tuple[int, int]] = None,
|
|
|
|
| 82 |
classes: Optional[List[str]] = None,
|
| 83 |
):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
config.defrost()
|
| 85 |
+
config.detector.thresholds.confidence = float(confidence_threshold)
|
| 86 |
config.freeze()
|
| 87 |
pipeline = get_pipeline(config, onnx_path)
|
| 88 |
+
id2label = pipeline.detector.get_category_mapping()
|
| 89 |
+
label2id = {v: k for k, v in pipeline.detector.get_category_mapping().items()}
|
| 90 |
if classes is not None:
|
| 91 |
+
wrong_classes = [cls for cls in classes if cls not in label2id]
|
| 92 |
if wrong_classes:
|
| 93 |
gr.Warning(f"Classes not found in model config: {wrong_classes}")
|
| 94 |
+
keep_ids = [label2id[cls] for cls in classes if cls in label2id]
|
| 95 |
else:
|
| 96 |
keep_ids = None
|
| 97 |
|
| 98 |
if isinstance(images, np.ndarray) and images.ndim == 4:
|
| 99 |
images = [x for x in images] # split video array into list of images
|
| 100 |
|
|
|
|
|
|
|
| 101 |
results = []
|
| 102 |
+
for img in tqdm.tqdm(images, desc="Processing frames"):
|
| 103 |
+
output_ = pipeline(img)
|
| 104 |
+
output_reshaped = {
|
| 105 |
+
"scores": torch.from_numpy(output_.confidence) if isinstance(output_.confidence, np.ndarray) else output_.confidence,
|
| 106 |
+
"labels": torch.from_numpy(output_.class_id) if isinstance(output_.class_id, np.ndarray) else output_.class_id,
|
| 107 |
+
"boxes": torch.from_numpy(output_.xyxy) if isinstance(output_.xyxy, np.ndarray) else output_.xyxy,
|
| 108 |
+
}
|
| 109 |
+
results.append(output_reshaped)
|
| 110 |
+
if target_size:
|
| 111 |
+
# Resize boxes to target size
|
| 112 |
+
scale_x = target_size[0] / img.shape[1]
|
| 113 |
+
scale_y = target_size[1] / img.shape[0]
|
| 114 |
+
output_reshaped["boxes"][:, [0, 2]] *= scale_x
|
| 115 |
+
output_reshaped["boxes"][:, [1, 3]] *= scale_y
|
| 116 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 117 |
|
| 118 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
# # move results to cpu
|
| 120 |
+
for i, result in enumerate(results):
|
| 121 |
+
results[i] = {k: v for k, v in result.items()}
|
| 122 |
+
if keep_ids is not None:
|
| 123 |
+
keep = torch.isin(results[i]["labels"], torch.tensor(keep_ids))
|
| 124 |
+
results[i] = {k: v[keep] for k, v in results[i].items()}
|
| 125 |
|
| 126 |
# return results, model.config.id2label
|
| 127 |
return results, pipeline.detector.get_category_mapping()
|
| 128 |
|
| 129 |
|
| 130 |
def process_image(
|
| 131 |
+
model: str = DEFAULT_DETECTOR,
|
| 132 |
image: Optional[Image.Image] = None,
|
|
|
|
|
|
|
| 133 |
confidence_threshold: float = DEFAULT_CONFIDENCE_THRESHOLD,
|
| 134 |
):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 135 |
|
| 136 |
+
load_config(cfg, f'configs/{model}.yaml')
|
| 137 |
results, id2label = detect_objects(
|
| 138 |
config=cfg.pipeline,
|
| 139 |
+
onnx_path=DETECTORS[model],
|
| 140 |
images=[np.array(image)],
|
| 141 |
confidence_threshold=confidence_threshold,
|
| 142 |
)
|
|
|
|
| 246 |
|
| 247 |
box_annotator = sv.BoxAnnotator(color, color_lookup=color_lookup, thickness=1)
|
| 248 |
label_annotator = sv.LabelAnnotator(color, color_lookup=color_lookup, text_scale=0.5)
|
|
|
|
| 249 |
|
| 250 |
# preprocess classes
|
| 251 |
if classes != "all":
|
| 252 |
+
classes_list = [cls.strip() for cls in classes.split(",")]
|
| 253 |
else:
|
| 254 |
classes_list = None
|
| 255 |
|
|
|
|
| 276 |
labels = [f"#{tracker_id} {id2label[class_id]}" for class_id, tracker_id in zip(detections.class_id, detections.tracker_id)]
|
| 277 |
annotated_frame = box_annotator.annotate(scene=frame, detections=detections)
|
| 278 |
annotated_frame = label_annotator.annotate(scene=annotated_frame, detections=detections, labels=labels)
|
|
|
|
| 279 |
annotated_frames.append(annotated_frame)
|
| 280 |
|
| 281 |
else:
|
|
|
|
| 301 |
interactive=True,
|
| 302 |
elem_classes="input-component",
|
| 303 |
),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 304 |
gr.Dropdown(
|
| 305 |
choices=list(DETECTORS.keys()),
|
| 306 |
label="Select Model Checkpoint",
|
| 307 |
+
value=DEFAULT_DETECTOR,
|
| 308 |
elem_classes="input-component",
|
| 309 |
),
|
| 310 |
gr.Slider(
|
|
|
|
| 330 |
gr.Dropdown(
|
| 331 |
choices=list(DETECTORS.keys()),
|
| 332 |
label="Select Model Checkpoint",
|
| 333 |
+
value=DEFAULT_DETECTOR,
|
| 334 |
elem_classes="input-component",
|
| 335 |
),
|
| 336 |
gr.Dropdown(
|
|
|
|
| 374 |
- **Image** and **Video** modes are supported.
|
| 375 |
- Select a model and adjust the confidence threshold to see detections!
|
| 376 |
- On video mode, you can enable tracking powered by [Supervision](https://github.com/roboflow/supervision) and [Trackers](https://github.com/roboflow/trackers) from Roboflow.
|
| 377 |
+
|
| 378 |
+
For more details and source code, visit the [GitHub Repository](https://github.com/eadali/PiSAR).
|
| 379 |
""",
|
| 380 |
elem_classes="header-text",
|
| 381 |
)
|
|
|
|
| 387 |
with gr.Group():
|
| 388 |
(
|
| 389 |
image_input,
|
|
|
|
|
|
|
| 390 |
image_model_checkpoint,
|
| 391 |
image_confidence_threshold,
|
| 392 |
) = create_image_inputs()
|
|
|
|
| 401 |
gr.Examples(
|
| 402 |
examples=[
|
| 403 |
[
|
| 404 |
+
DEFAULT_DETECTOR,
|
| 405 |
example["path"],
|
|
|
|
|
|
|
| 406 |
DEFAULT_CONFIDENCE_THRESHOLD,
|
| 407 |
]
|
| 408 |
for example in IMAGE_EXAMPLES
|
|
|
|
| 410 |
inputs=[
|
| 411 |
image_model_checkpoint,
|
| 412 |
image_input,
|
|
|
|
|
|
|
| 413 |
image_confidence_threshold,
|
| 414 |
],
|
| 415 |
outputs=[image_output],
|
|
|
|
| 437 |
|
| 438 |
gr.Examples(
|
| 439 |
examples=[
|
| 440 |
+
[example["path"], DEFAULT_DETECTOR, example["tracker"], example["classes"], DEFAULT_CONFIDENCE_THRESHOLD]
|
| 441 |
for example in VIDEO_EXAMPLES
|
| 442 |
],
|
| 443 |
inputs=[video_input, video_checkpoint, video_tracker, video_classes, video_confidence_threshold],
|
|
|
|
| 447 |
label="Select a video example to populate inputs",
|
| 448 |
)
|
| 449 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 450 |
# Image clear button
|
| 451 |
image_clear_button.click(
|
| 452 |
fn=lambda: (
|
| 453 |
None,
|
| 454 |
False,
|
| 455 |
"",
|
| 456 |
+
DEFAULT_DETECTOR,
|
| 457 |
DEFAULT_CONFIDENCE_THRESHOLD,
|
| 458 |
None,
|
| 459 |
),
|
| 460 |
outputs=[
|
| 461 |
image_input,
|
| 462 |
+
|
|
|
|
| 463 |
image_model_checkpoint,
|
| 464 |
image_confidence_threshold,
|
| 465 |
image_output,
|
|
|
|
| 470 |
video_clear_button.click(
|
| 471 |
fn=lambda: (
|
| 472 |
None,
|
| 473 |
+
DEFAULT_DETECTOR,
|
| 474 |
None,
|
| 475 |
"all",
|
| 476 |
DEFAULT_CONFIDENCE_THRESHOLD,
|
|
|
|
| 492 |
inputs=[
|
| 493 |
image_model_checkpoint,
|
| 494 |
image_input,
|
|
|
|
|
|
|
| 495 |
image_confidence_threshold,
|
| 496 |
],
|
| 497 |
outputs=[image_output],
|
examples/images/forest.jpg
CHANGED
|
Git LFS Details
|
|
Git LFS Details
|
examples/images/{coast.jpg → ocean.jpg}
RENAMED
|
File without changes
|
examples/images/river.jpg
ADDED
|
Git LFS Details
|
examples/videos/sea.mp4
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:ac442758ffff7f1d006d682e5adbf1f332b1f0d8ddc7d66b90bce8ced9ae6029
|
| 3 |
+
size 2650443
|
examples/videos/seaa.mp4
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:3ebf5645f405a4378c4352d797bcbb8bdf101b811a043de9ff08db13382b483b
|
| 3 |
+
size 4884605
|
utils/config.py
CHANGED
|
@@ -2,29 +2,23 @@ from .yacs import CfgNode
|
|
| 2 |
|
| 3 |
cfg = CfgNode(new_allowed=True)
|
| 4 |
cfg.save_dir = "./"
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
cfg.
|
| 9 |
-
cfg.
|
| 10 |
-
cfg.
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
cfg.
|
| 14 |
-
cfg.
|
| 15 |
-
cfg.
|
| 16 |
-
cfg.device =
|
| 17 |
-
|
| 18 |
-
#
|
| 19 |
-
cfg.
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
cfg.log = CfgNode()
|
| 23 |
-
cfg.log.interval = 50
|
| 24 |
-
|
| 25 |
-
# testing
|
| 26 |
-
cfg.test = CfgNode()
|
| 27 |
-
# size of images for each device
|
| 28 |
|
| 29 |
|
| 30 |
def load_config(cfg, args_cfg):
|
|
|
|
| 2 |
|
| 3 |
cfg = CfgNode(new_allowed=True)
|
| 4 |
cfg.save_dir = "./"
|
| 5 |
+
cfg.pipeline = CfgNode(new_allowed=True)
|
| 6 |
+
|
| 7 |
+
# Detector config
|
| 8 |
+
cfg.pipeline.detector = CfgNode(new_allowed=True)
|
| 9 |
+
cfg.pipeline.detector.model = "yolov8n"
|
| 10 |
+
cfg.pipeline.detector.categories = []
|
| 11 |
+
cfg.pipeline.detector.thresholds = CfgNode(new_allowed=True)
|
| 12 |
+
cfg.pipeline.detector.thresholds.confidence = 0.6
|
| 13 |
+
cfg.pipeline.detector.thresholds.iou = 0.4
|
| 14 |
+
cfg.pipeline.detector.slicing = CfgNode(new_allowed=True)
|
| 15 |
+
cfg.pipeline.detector.slicing.overlap = 0.2
|
| 16 |
+
cfg.pipeline.detector.device = "cpu"
|
| 17 |
+
|
| 18 |
+
# Tracker config
|
| 19 |
+
cfg.pipeline.tracker = CfgNode(new_allowed=True)
|
| 20 |
+
cfg.pipeline.tracker.algorithm = "dummytrack"
|
| 21 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
|
| 24 |
def load_config(cfg, args_cfg):
|