Skip to content

Free Motion Capture for Everyone πŸ’€βœ¨

TypeScript

added route to create `session`

Jone844d19bb262
diff --git a/bin/windows/run_web_server.cmd b/bin/windows/run_web_server.cmd
index 58c68762..b06cbf92 100644
--- a/bin/windows/run_web_server.cmd
+++ b/bin/windows/run_web_server.cmd
@@ -1 +1 @@
-uvicorn --factory src.api.app_factory:create_app --port 8080 --host 0.0.0.0 --workers 4 --ws websockets
\ No newline at end of file
+uvicorn --factory src.api.app_factory:create_app --port 8080 --host 0.0.0.0 --workers 4 --ws websockets --reload
\ No newline at end of file
diff --git a/src/api/routes/__init__.py b/src/api/routes/__init__.py
index e02a713d..8837c62c 100644
--- a/src/api/routes/__init__.py
+++ b/src/api/routes/__init__.py
@@ -2,7 +2,8 @@ from src.api.routes.camera.cam_data_ws import cam_ws_router
from src.api.routes.camera.camera_route import camera_router
from src.api.routes.health.health_check_route import healthcheck_router
from src.api.routes.home.home import home_router
+from src.api.routes.session.session_router import session_router
from src.api.routes.startup.startup import startup_router
# REGISTER NEW ROUTES HERE
-enabled_routers = [healthcheck_router, camera_router, cam_ws_router, startup_router, home_router]
+enabled_routers = [healthcheck_router, camera_router, cam_ws_router, startup_router, home_router, session_router]
diff --git a/src/api/routes/camera/camera_route.py b/src/api/routes/camera/camera_route.py
index e3308fde..33fa89f3 100644
--- a/src/api/routes/camera/camera_route.py
+++ b/src/api/routes/camera/camera_route.py
@@ -1,17 +1,15 @@
from fastapi import APIRouter
-from src.api.services.user_config import UserConfigService
+from src.api.services.user_config import UserConfigService, WebcamConfigModel
from src.cameras.detection.cam_singleton import get_or_create_cams
-from src.config.webcam_config import WebcamConfig
camera_router = APIRouter()
@camera_router.post("/camera/config")
-async def config_cam(webcam_config: WebcamConfig):
+async def config_cam(webcam_config_model: WebcamConfigModel):
s = UserConfigService()
- s.set_webcam_config(webcam_config)
- return s.webcam_configs
+ return s.save_webcam_config_to_disk(webcam_config_model)
@camera_router.get("/camera/detect")
diff --git a/src/api/routes/session/session_router.py b/src/api/routes/session/session_router.py
new file mode 100644
index 00000000..dd08e1cf
--- /dev/null
+++ b/src/api/routes/session/session_router.py
@@ -0,0 +1,33 @@
+import logging
+from pathlib import Path
+
+from fastapi import APIRouter
+from pydantic import BaseModel
+
+from src.config.home_dir import os_independent_home_dir, create_session_id
+
+logger = logging.getLogger(__name__)
+
+session_router = APIRouter()
+
+BASE_FOLDER_NAME = "freemocap_data"
+
+
+class SessionCreateModel(BaseModel):
+ user_session_str: str
+
+
+class SessionResponse(BaseModel):
+ session_id: str
+ session_path: str
+
+
+@session_router.post("/session/create")
+async def create_session(session_create_model: SessionCreateModel):
+ session_id = create_session_id(session_create_model.user_session_str)
+ base_save_path = Path(os_independent_home_dir(), BASE_FOLDER_NAME)
+ session_path = base_save_path / session_id
+ logger.info(f'Creating session folder at: {str(session_path)}')
+ session_path.mkdir(parents=True, exist_ok=False)
+ return SessionResponse(session_id=session_id,
+ session_path=str(session_path))
diff --git a/src/api/services/user_config.py b/src/api/services/user_config.py
index 5737c77e..c9e3d42b 100644
--- a/src/api/services/user_config.py
+++ b/src/api/services/user_config.py
@@ -1,24 +1,23 @@
-from typing import Dict
+import json
+from pathlib import Path
-from singleton_decorator import singleton
+from pydantic import BaseModel
-from src.config.webcam_config import WebcamConfig
+class WebcamConfigModel(BaseModel):
+ webcam_id: str = '0'
+ exposure: int = -6
+ resolution_width: int = 800
+ resolution_height: int = 600
+ session_path: str
-@singleton
-class UserConfigService:
- _dict_of_webcam_configs: Dict[str, WebcamConfig] = {}
-
- @property
- def webcam_configs(self):
- return self._dict_of_webcam_configs
- def webcam_config_by_id(self, webcam_id_as_str: str) -> WebcamConfig:
- if webcam_id_as_str in self._dict_of_webcam_configs:
- return self._dict_of_webcam_configs[webcam_id_as_str]
- else:
- return WebcamConfig(webcam_id=int(webcam_id_as_str))
+class UserConfigService:
+ """take things like WebcamConfigModel and save it to a text file or whatever"""
- def set_webcam_config(self, webcam_config: WebcamConfig):
- as_str = str(webcam_config.webcam_id)
- self._dict_of_webcam_configs[as_str] = webcam_config
+ def save_webcam_config_to_disk(self, webcam_config_model: WebcamConfigModel):
+ webcam_config_json = webcam_config_model.dict(exclude={'session_path'})
+ webcam_config_json_path = Path(webcam_config_model.session_path) / 'webcam_config.json'
+ with open(webcam_config_json_path, 'w') as outfile:
+ outfile.write(str(webcam_config_json))
+ return webcam_config_json
diff --git a/src/config/home_dir.py b/src/config/home_dir.py
new file mode 100644
index 00000000..fc0213f5
--- /dev/null
+++ b/src/config/home_dir.py
@@ -0,0 +1,14 @@
+import time
+from pathlib import Path
+
+
+def os_independent_home_dir():
+ return str(Path.home())
+
+
+def create_session_id(string_tag: str = None):
+ session_id = 'session_' + time.strftime("%m-%d-%Y-%H_%M_%S")
+ if string_tag is None:
+ return session_id
+ else:
+ return session_id + '_' + string_tag
diff --git a/src/config/webcam_config.py b/src/config/webcam_config.py
index 8402f76e..ebb15162 100644
--- a/src/config/webcam_config.py
+++ b/src/config/webcam_config.py
@@ -1,10 +1,8 @@
-from pathlib import Path
-from pydantic import BaseModel
+from pydantic import BaseModel
-def _get_home_dir():
- return str(Path.home())
+from src.config.home_dir import os_independent_home_dir
class WebcamConfig(BaseModel):
@@ -15,4 +13,4 @@ class WebcamConfig(BaseModel):
save_video: bool = False
# fourcc: str = "MP4V"
fourcc: str = "MJPG"
- base_save_video_dir = _get_home_dir()
+ base_save_video_dir = os_independent_home_dir()
diff --git a/src/session_pipeline/session_pipeline_orchestrator.py b/src/session_pipeline/session_pipeline_orchestrator.py
index 2a4f0238..81c3d2a7 100644
--- a/src/session_pipeline/session_pipeline_orchestrator.py
+++ b/src/session_pipeline/session_pipeline_orchestrator.py
@@ -8,6 +8,7 @@ import cv2
from src.cameras.multicam_manager.cv_camera_manager import OpenCVCameraManager
from src.cameras.persistence.video_writer.save_options_dataclass import SaveOptions
from src.config.data_paths import freemocap_data_path
+from src.config.home_dir import create_session_id
from src.core_processor.camera_calibration.camera_calibrator import CameraCalibrator
from src.core_processor.fps.timestamp_manager import TimestampManager
from src.core_processor.mediapipe_skeleton_detection.mediapipe_skeleton_detection import MediaPipeSkeletonDetection
@@ -29,7 +30,7 @@ class SessionPipelineOrchestrator:
if session_id is not None:
self._session_id = session_id
else:
- self._session_id = 'session_' + time.strftime("%m-%d-%Y-%H_%M_%S")
+ self._session_id = create_session_id()
self._session_start_time_unix_ns = time.time_ns()
self._visualizer_gui = QTVisualizerAndGui()