Spaces:
Sleeping
Sleeping
fix: gradio example link instead of file
Browse files- api/routers/camera_stream.py +3 -1
- config/config.yaml +1 -0
- config/settings.py +1 -0
- main.py +2 -0
- services/pipeline.py +13 -3
- tests/test_pipeline.py +8 -0
- utils/profiling.py +13 -6
api/routers/camera_stream.py
CHANGED
|
@@ -44,7 +44,9 @@ async def websocket_detect(
|
|
| 44 |
logger.info(f"Client ID >>{camera_id}<< Connected...")
|
| 45 |
|
| 46 |
step_counter = itertools.count()
|
| 47 |
-
pipeline = ProcessingPipeline(
|
|
|
|
|
|
|
| 48 |
|
| 49 |
# Queue removing old images in case they were being stacked
|
| 50 |
frame_queue: asyncio.Queue = asyncio.Queue(maxsize=1)
|
|
|
|
| 44 |
logger.info(f"Client ID >>{camera_id}<< Connected...")
|
| 45 |
|
| 46 |
step_counter = itertools.count()
|
| 47 |
+
pipeline = ProcessingPipeline(
|
| 48 |
+
detector, depth_model, safety_detector, redis, config=state.settings
|
| 49 |
+
)
|
| 50 |
|
| 51 |
# Queue removing old images in case they were being stacked
|
| 52 |
frame_queue: asyncio.Queue = asyncio.Queue(maxsize=1)
|
config/config.yaml
CHANGED
|
@@ -5,6 +5,7 @@ project_name: Tracking Config
|
|
| 5 |
project_desc: |
|
| 6 |
Tracking System for detection, providing real-time camera status throught a Dashboard.
|
| 7 |
task: indoor
|
|
|
|
| 8 |
intervals:
|
| 9 |
system_metrics_seconds: 3.0 # Logging system metrics every
|
| 10 |
frames_summary_every: 30 # Number of frames to create a logs summary
|
|
|
|
| 5 |
project_desc: |
|
| 6 |
Tracking System for detection, providing real-time camera status throught a Dashboard.
|
| 7 |
task: indoor
|
| 8 |
+
experiment: false
|
| 9 |
intervals:
|
| 10 |
system_metrics_seconds: 3.0 # Logging system metrics every
|
| 11 |
frames_summary_every: 30 # Number of frames to create a logs summary
|
config/settings.py
CHANGED
|
@@ -83,6 +83,7 @@ class AppConfig(BaseSettings):
|
|
| 83 |
intervals: IntervalsConfig
|
| 84 |
redis_url: str
|
| 85 |
dagshub_user_token: str
|
|
|
|
| 86 |
|
| 87 |
@classmethod
|
| 88 |
def settings_customise_sources(
|
|
|
|
| 83 |
intervals: IntervalsConfig
|
| 84 |
redis_url: str
|
| 85 |
dagshub_user_token: str
|
| 86 |
+
experiment: bool
|
| 87 |
|
| 88 |
@classmethod
|
| 89 |
def settings_customise_sources(
|
main.py
CHANGED
|
@@ -32,6 +32,7 @@ async def lifespan(app: FastAPI):
|
|
| 32 |
logger.info("Starting Server.... ")
|
| 33 |
# asyncio.create_task(log_system_metrics(logger, logger_interval_sec=settings.intervals.system_metrics_seconds))
|
| 34 |
|
|
|
|
| 35 |
detection_model_path = hf_fetch_model(
|
| 36 |
repo_id="Ultralytics/YOLO26",
|
| 37 |
filename=settings.yolo.model_name,
|
|
@@ -54,6 +55,7 @@ async def lifespan(app: FastAPI):
|
|
| 54 |
)
|
| 55 |
app.state.safety_detection_model = YOLO_Detector(safety_detection_path)
|
| 56 |
|
|
|
|
| 57 |
app.state.redis = aioredis.from_url(settings.redis_url, decode_responses=True)
|
| 58 |
# Checking connection to redis - TODO add to health check
|
| 59 |
try:
|
|
|
|
| 32 |
logger.info("Starting Server.... ")
|
| 33 |
# asyncio.create_task(log_system_metrics(logger, logger_interval_sec=settings.intervals.system_metrics_seconds))
|
| 34 |
|
| 35 |
+
logger.info("Downloading Models..")
|
| 36 |
detection_model_path = hf_fetch_model(
|
| 37 |
repo_id="Ultralytics/YOLO26",
|
| 38 |
filename=settings.yolo.model_name,
|
|
|
|
| 55 |
)
|
| 56 |
app.state.safety_detection_model = YOLO_Detector(safety_detection_path)
|
| 57 |
|
| 58 |
+
logger.info("Connecting to Redis Server...")
|
| 59 |
app.state.redis = aioredis.from_url(settings.redis_url, decode_responses=True)
|
| 60 |
# Checking connection to redis - TODO add to health check
|
| 61 |
try:
|
services/pipeline.py
CHANGED
|
@@ -12,11 +12,12 @@ import json
|
|
| 12 |
|
| 13 |
|
| 14 |
class ProcessingPipeline:
|
| 15 |
-
def __init__(self, detector, depth_model, safety_detector, redis):
|
| 16 |
self.detector = detector
|
| 17 |
self.depth_model = depth_model
|
| 18 |
self.safety_detector = safety_detector
|
| 19 |
self.redis = redis
|
|
|
|
| 20 |
|
| 21 |
def _decode_frame(self, fb):
|
| 22 |
return cv.imdecode(np.frombuffer(fb, np.uint8), cv.IMREAD_COLOR)
|
|
@@ -39,7 +40,11 @@ class ProcessingPipeline:
|
|
| 39 |
loop = asyncio.get_running_loop()
|
| 40 |
|
| 41 |
with profile_step(
|
| 42 |
-
"frame_processing_time",
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
):
|
| 44 |
frame_bytes = await loop.run_in_executor(
|
| 45 |
None, self._decode_frame, frame_bytes
|
|
@@ -50,6 +55,7 @@ class ProcessingPipeline:
|
|
| 50 |
detection_duration_seconds,
|
| 51 |
camera_id,
|
| 52 |
frame_count,
|
|
|
|
| 53 |
):
|
| 54 |
detection_task = loop.run_in_executor(
|
| 55 |
None, self.detector.detect, frame_bytes
|
|
@@ -68,7 +74,11 @@ class ProcessingPipeline:
|
|
| 68 |
depth_points = []
|
| 69 |
if boxes_center:
|
| 70 |
with profile_step(
|
| 71 |
-
"depth_duration_seconds",
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
):
|
| 73 |
depth_points = await loop.run_in_executor(
|
| 74 |
None, self.depth_model.calculate_depth, frame_bytes, boxes_center
|
|
|
|
| 12 |
|
| 13 |
|
| 14 |
class ProcessingPipeline:
|
| 15 |
+
def __init__(self, detector, depth_model, safety_detector, redis, config):
|
| 16 |
self.detector = detector
|
| 17 |
self.depth_model = depth_model
|
| 18 |
self.safety_detector = safety_detector
|
| 19 |
self.redis = redis
|
| 20 |
+
self.config = config
|
| 21 |
|
| 22 |
def _decode_frame(self, fb):
|
| 23 |
return cv.imdecode(np.frombuffer(fb, np.uint8), cv.IMREAD_COLOR)
|
|
|
|
| 40 |
loop = asyncio.get_running_loop()
|
| 41 |
|
| 42 |
with profile_step(
|
| 43 |
+
"frame_processing_time",
|
| 44 |
+
decode_duration_seconds,
|
| 45 |
+
camera_id,
|
| 46 |
+
frame_count,
|
| 47 |
+
experiment=self.config.experiment,
|
| 48 |
):
|
| 49 |
frame_bytes = await loop.run_in_executor(
|
| 50 |
None, self._decode_frame, frame_bytes
|
|
|
|
| 55 |
detection_duration_seconds,
|
| 56 |
camera_id,
|
| 57 |
frame_count,
|
| 58 |
+
experiment=self.config.experiment,
|
| 59 |
):
|
| 60 |
detection_task = loop.run_in_executor(
|
| 61 |
None, self.detector.detect, frame_bytes
|
|
|
|
| 74 |
depth_points = []
|
| 75 |
if boxes_center:
|
| 76 |
with profile_step(
|
| 77 |
+
"depth_duration_seconds",
|
| 78 |
+
depth_duration_seconds,
|
| 79 |
+
camera_id,
|
| 80 |
+
frame_count,
|
| 81 |
+
experiment=self.config.experiment,
|
| 82 |
):
|
| 83 |
depth_points = await loop.run_in_executor(
|
| 84 |
None, self.depth_model.calculate_depth, frame_bytes, boxes_center
|
tests/test_pipeline.py
CHANGED
|
@@ -24,11 +24,15 @@ async def test_pipeline_success(mock_deps):
|
|
| 24 |
mock_deps["detector"].detect.return_value.detections = [mock_detection]
|
| 25 |
mock_deps["safety"].detect.return_value = [] # No dangers
|
| 26 |
|
|
|
|
|
|
|
|
|
|
| 27 |
pipeline = ProcessingPipeline(
|
| 28 |
detector=mock_deps["detector"],
|
| 29 |
safety_detector=mock_deps["safety"],
|
| 30 |
depth_model=mock_deps["depth"],
|
| 31 |
redis=mock_deps["redis"],
|
|
|
|
| 32 |
)
|
| 33 |
|
| 34 |
with (
|
|
@@ -51,11 +55,15 @@ async def test_pipeline_success(mock_deps):
|
|
| 51 |
async def test_pipeline_no_detections(mock_deps):
|
| 52 |
mock_deps["detector"].detect.return_value.detections = []
|
| 53 |
|
|
|
|
|
|
|
|
|
|
| 54 |
pipeline = ProcessingPipeline(
|
| 55 |
detector=mock_deps["detector"],
|
| 56 |
safety_detector=mock_deps["safety"],
|
| 57 |
depth_model=mock_deps["depth"],
|
| 58 |
redis=mock_deps["redis"],
|
|
|
|
| 59 |
)
|
| 60 |
|
| 61 |
with (
|
|
|
|
| 24 |
mock_deps["detector"].detect.return_value.detections = [mock_detection]
|
| 25 |
mock_deps["safety"].detect.return_value = [] # No dangers
|
| 26 |
|
| 27 |
+
mock_config = MagicMock()
|
| 28 |
+
mock_config.experiment = False
|
| 29 |
+
|
| 30 |
pipeline = ProcessingPipeline(
|
| 31 |
detector=mock_deps["detector"],
|
| 32 |
safety_detector=mock_deps["safety"],
|
| 33 |
depth_model=mock_deps["depth"],
|
| 34 |
redis=mock_deps["redis"],
|
| 35 |
+
config=mock_config,
|
| 36 |
)
|
| 37 |
|
| 38 |
with (
|
|
|
|
| 55 |
async def test_pipeline_no_detections(mock_deps):
|
| 56 |
mock_deps["detector"].detect.return_value.detections = []
|
| 57 |
|
| 58 |
+
mock_config = MagicMock()
|
| 59 |
+
mock_config.experiment = False
|
| 60 |
+
|
| 61 |
pipeline = ProcessingPipeline(
|
| 62 |
detector=mock_deps["detector"],
|
| 63 |
safety_detector=mock_deps["safety"],
|
| 64 |
depth_model=mock_deps["depth"],
|
| 65 |
redis=mock_deps["redis"],
|
| 66 |
+
config=mock_config,
|
| 67 |
)
|
| 68 |
|
| 69 |
with (
|
utils/profiling.py
CHANGED
|
@@ -4,7 +4,13 @@ import mlflow
|
|
| 4 |
|
| 5 |
|
| 6 |
@contextmanager
|
| 7 |
-
def profile_step(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
"""With statement utility to time block of code"""
|
| 9 |
start_time = time.time()
|
| 10 |
|
|
@@ -14,8 +20,9 @@ def profile_step(expr_name: str, prometheus_logger, camera_id, frame_count=None)
|
|
| 14 |
finally:
|
| 15 |
duration = round(time.time() - start_time, 4)
|
| 16 |
prometheus_logger.labels(camera_id).observe(duration)
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
|
|
|
|
|
| 4 |
|
| 5 |
|
| 6 |
@contextmanager
|
| 7 |
+
def profile_step(
|
| 8 |
+
expr_name: str,
|
| 9 |
+
prometheus_logger,
|
| 10 |
+
camera_id,
|
| 11 |
+
frame_count=None,
|
| 12 |
+
experiment: bool = False,
|
| 13 |
+
):
|
| 14 |
"""With statement utility to time block of code"""
|
| 15 |
start_time = time.time()
|
| 16 |
|
|
|
|
| 20 |
finally:
|
| 21 |
duration = round(time.time() - start_time, 4)
|
| 22 |
prometheus_logger.labels(camera_id).observe(duration)
|
| 23 |
+
if experiment:
|
| 24 |
+
mlflow.log_metric(
|
| 25 |
+
expr_name,
|
| 26 |
+
duration,
|
| 27 |
+
frame_count,
|
| 28 |
+
)
|