Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
93 changes: 40 additions & 53 deletions examples/time_in_zone/inference_file_example.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import argparse
import sys

import cv2
import numpy as np
Expand All @@ -16,13 +16,24 @@


def main(
source_video_path: str,
zone_configuration_path: str,
model_id: str,
confidence: float,
iou: float,
classes: list[int],
source_video_path: str,
model_id: str = "yolov8s-640",
confidence: float = 0.3,
iou: float = 0.7,
classes: list[int] = [],
) -> None:
"""
Calculating detections dwell time in zones, using video file.

Args:
zone_configuration_path: Path to the zone configuration JSON file
source_video_path: Path to the source video file
model_id: Roboflow model ID
confidence: Confidence level for detections (0 to 1)
iou: IOU threshold for non-max suppression
classes: List of class IDs to track. If empty, all classes are tracked
"""
model = get_model(model_id=model_id)
tracker = sv.ByteTrack(minimum_matching_threshold=0.5)
video_info = sv.VideoInfo.from_video_path(video_path=source_video_path)
Expand Down Expand Up @@ -78,50 +89,26 @@ def main(


if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Calculating detections dwell time in zones, using video file."
)
parser.add_argument(
"--zone_configuration_path",
type=str,
required=True,
help="Path to the zone configuration JSON file.",
)
parser.add_argument(
"--source_video_path",
type=str,
required=True,
help="Path to the source video file.",
)
parser.add_argument(
"--model_id", type=str, default="yolov8s-640", help="Roboflow model ID."
)
parser.add_argument(
"--confidence_threshold",
type=float,
default=0.3,
help="Confidence level for detections (0 to 1). Default is 0.3.",
)
parser.add_argument(
"--iou_threshold",
default=0.7,
type=float,
help="IOU threshold for non-max suppression. Default is 0.7.",
)
parser.add_argument(
"--classes",
nargs="*",
type=int,
default=[],
help="List of class IDs to track. If empty, all classes are tracked.",
)
args = parser.parse_args()

main(
source_video_path=args.source_video_path,
zone_configuration_path=args.zone_configuration_path,
model_id=args.model_id,
confidence=args.confidence_threshold,
iou=args.iou_threshold,
classes=args.classes,
)
try:
# Try to import jsonargparse for CLI parsing
from jsonargparse import ArgumentParser
except ImportError:
# Fallback if jsonargparse is not installed
print("Warning: jsonargparse not installed. Using plain positional arguments.")
# Positional args: zone_configuration_path, source_video_path, [model_id], [confidence], [iou], [classes]
if len(sys.argv) < 3:
raise ValueError("Insufficient arguments provided.")
main(
zone_configuration_path=sys.argv[1],
source_video_path=sys.argv[2],
model_id=sys.argv[3] if len(sys.argv) > 3 else "yolov8s-640",
confidence=float(sys.argv[4]) if len(sys.argv) > 4 else 0.3,
iou=float(sys.argv[5]) if len(sys.argv) > 5 else 0.7,
classes=[int(x) for x in sys.argv[6:]] if len(sys.argv) > 6 else [],
)
else:
# Use jsonargparse for automatic CLI if import succeeded
parser = ArgumentParser()
parser.add_function_arguments(main)
args = parser.parse_args()
main(**vars(args))
93 changes: 40 additions & 53 deletions examples/time_in_zone/inference_naive_stream_example.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import argparse
import sys

import cv2
import numpy as np
Expand All @@ -16,13 +16,24 @@


def main(
rtsp_url: str,
zone_configuration_path: str,
model_id: str,
confidence: float,
iou: float,
classes: list[int],
rtsp_url: str,
model_id: str = "yolov8s-640",
confidence: float = 0.3,
iou: float = 0.7,
classes: list[int] = [],
) -> None:
"""
Calculating detections dwell time in zones, using RTSP stream.

Args:
zone_configuration_path: Path to the zone configuration JSON file
rtsp_url: Complete RTSP URL for the video stream
model_id: Roboflow model ID
confidence: Confidence level for detections (0 to 1)
iou: IOU threshold for non-max suppression
classes: List of class IDs to track. If empty, all classes are tracked
"""
model = get_model(model_id=model_id)
tracker = sv.ByteTrack(minimum_matching_threshold=0.5)
frames_generator = get_stream_frames_generator(rtsp_url=rtsp_url)
Expand Down Expand Up @@ -88,50 +99,26 @@ def main(


if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Calculating detections dwell time in zones, using RTSP stream."
)
parser.add_argument(
"--zone_configuration_path",
type=str,
required=True,
help="Path to the zone configuration JSON file.",
)
parser.add_argument(
"--rtsp_url",
type=str,
required=True,
help="Complete RTSP URL for the video stream.",
)
parser.add_argument(
"--model_id", type=str, default="yolov8s-640", help="Roboflow model ID."
)
parser.add_argument(
"--confidence_threshold",
type=float,
default=0.3,
help="Confidence level for detections (0 to 1). Default is 0.3.",
)
parser.add_argument(
"--iou_threshold",
default=0.7,
type=float,
help="IOU threshold for non-max suppression. Default is 0.7.",
)
parser.add_argument(
"--classes",
nargs="*",
type=int,
default=[],
help="List of class IDs to track. If empty, all classes are tracked.",
)
args = parser.parse_args()

main(
rtsp_url=args.rtsp_url,
zone_configuration_path=args.zone_configuration_path,
model_id=args.model_id,
confidence=args.confidence_threshold,
iou=args.iou_threshold,
classes=args.classes,
)
try:
# Try to import jsonargparse for CLI parsing
from jsonargparse import ArgumentParser
except ImportError:
# Fallback if jsonargparse is not installed
print("Warning: jsonargparse not installed. Using plain positional arguments.")
# Positional args: zone_configuration_path, rtsp_url, [model_id], [confidence], [iou], [classes]
if len(sys.argv) < 3:
raise ValueError("Insufficient arguments provided.")
main(
zone_configuration_path=sys.argv[1],
rtsp_url=sys.argv[2],
model_id=sys.argv[3] if len(sys.argv) > 3 else "yolov8s-640",
confidence=float(sys.argv[4]) if len(sys.argv) > 4 else 0.3,
iou=float(sys.argv[5]) if len(sys.argv) > 5 else 0.7,
classes=[int(x) for x in sys.argv[6:]] if len(sys.argv) > 6 else [],
)
else:
# Use jsonargparse for automatic CLI if import succeeded
parser = ArgumentParser()
parser.add_function_arguments(main)
args = parser.parse_args()
main(**vars(args))
93 changes: 40 additions & 53 deletions examples/time_in_zone/inference_stream_example.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import argparse
import sys

import cv2
import numpy as np
Expand Down Expand Up @@ -77,13 +77,24 @@ def on_prediction(self, result: dict, frame: VideoFrame) -> None:


def main(
rtsp_url: str,
zone_configuration_path: str,
model_id: str,
confidence: float,
iou: float,
classes: list[int],
rtsp_url: str,
model_id: str = "yolov8s-640",
confidence: float = 0.3,
iou: float = 0.7,
classes: list[int] = [],
) -> None:
"""
Calculating detections dwell time in zones, using RTSP stream.

Args:
zone_configuration_path: Path to the zone configuration JSON file
rtsp_url: Complete RTSP URL for the video stream
model_id: Roboflow model ID
confidence: Confidence level for detections (0 to 1)
iou: IOU threshold for non-max suppression
classes: List of class IDs to track. If empty, all classes are tracked
"""
sink = CustomSink(zone_configuration_path=zone_configuration_path, classes=classes)

pipeline = InferencePipeline.init(
Expand All @@ -103,50 +114,26 @@ def main(


if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Calculating detections dwell time in zones, using RTSP stream."
)
parser.add_argument(
"--zone_configuration_path",
type=str,
required=True,
help="Path to the zone configuration JSON file.",
)
parser.add_argument(
"--rtsp_url",
type=str,
required=True,
help="Complete RTSP URL for the video stream.",
)
parser.add_argument(
"--model_id", type=str, default="yolov8s-640", help="Roboflow model ID."
)
parser.add_argument(
"--confidence_threshold",
type=float,
default=0.3,
help="Confidence level for detections (0 to 1). Default is 0.3.",
)
parser.add_argument(
"--iou_threshold",
default=0.7,
type=float,
help="IOU threshold for non-max suppression. Default is 0.7.",
)
parser.add_argument(
"--classes",
nargs="*",
type=int,
default=[],
help="List of class IDs to track. If empty, all classes are tracked.",
)
args = parser.parse_args()

main(
rtsp_url=args.rtsp_url,
zone_configuration_path=args.zone_configuration_path,
model_id=args.model_id,
confidence=args.confidence_threshold,
iou=args.iou_threshold,
classes=args.classes,
)
try:
# Try to import jsonargparse for CLI parsing
from jsonargparse import ArgumentParser
except ImportError:
# Fallback if jsonargparse is not installed
print("Warning: jsonargparse not installed. Using plain positional arguments.")
# Positional args: zone_configuration_path, rtsp_url, [model_id], [confidence], [iou], [classes]
if len(sys.argv) < 3:
raise ValueError("Insufficient arguments provided.")
main(
zone_configuration_path=sys.argv[1],
rtsp_url=sys.argv[2],
model_id=sys.argv[3] if len(sys.argv) > 3 else "yolov8s-640",
confidence=float(sys.argv[4]) if len(sys.argv) > 4 else 0.3,
iou=float(sys.argv[5]) if len(sys.argv) > 5 else 0.7,
classes=[int(x) for x in sys.argv[6:]] if len(sys.argv) > 6 else [],
)
else:
# Use jsonargparse for automatic CLI if import succeeded
parser = ArgumentParser()
parser.add_function_arguments(main)
args = parser.parse_args()
main(**vars(args))
Loading