async def add_or_replace_roi_contour(camera_id: str, body: ContourRoI, reboot_processor: Optional[bool] = True): """ Define a RoI for a camera or replace its current one. A RoI is defined by a vector of [x,y] 2-tuples, that map to coordinates in the image. """ validate_camera_existence(camera_id) roi_file_path = ObjectsFilteringPostProcessor.get_roi_file_path(camera_id, settings.config) dir_path = Path(roi_file_path).parents[0] Path(dir_path).mkdir(parents=True, exist_ok=True) roi_contour = np.array(body.contour_roi, dtype=int) np.savetxt(roi_file_path, roi_contour, delimiter=',', fmt='%i') restart_processor() if reboot_processor else True return roi_contour.tolist()
async def add_or_replace_in_out_boundaries(camera_id: str, body: InOutBoundaries, reboot_processor: Optional[bool] = True): """ Create or replace the In/Out boundaries for a camera. Each In/Out boundary in the list is represented by a name and: Two coordinates `[x,y]` are given in 2-tuples `[A,B]`. These points form a **line**. - If someone crosses the **line** while having **A** to their right, they are going in the `in` direction (entering). - Crossing the **line** while having **A** to their left means they are going in the `out` direction (leaving). """ validate_camera_existence(camera_id) in_out_file_path = InOutMetric.get_in_out_file_path(camera_id, settings.config) dir_path = Path(in_out_file_path).parents[0] Path(dir_path).mkdir(parents=True, exist_ok=True) in_out_boundaries = body.dict() with open(in_out_file_path, "w") as outfile: json.dump(in_out_boundaries, outfile) restart_processor() if reboot_processor else True return body
async def remove_in_out_boundaries(camera_id: str, reboot_processor: Optional[bool] = True): """ Delete the defined In/Out boundaries for a camera. """ validate_camera_existence(camera_id) in_out_file_path = InOutMetric.get_in_out_file_path(camera_id, settings.config) if not validate_file_exists_and_is_not_empty(in_out_file_path): detail = f"There is no defined In/Out Boundary for {camera_id}" raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=detail) os.remove(in_out_file_path) success = restart_processor() if reboot_processor else True return handle_response(None, success, status.HTTP_204_NO_CONTENT)
async def remove_roi_contour(camera_id: str, reboot_processor: Optional[bool] = True): """ Delete the defined RoI for a camera. """ validate_camera_existence(camera_id) roi_file_path = ObjectsFilteringPostProcessor.get_roi_file_path(camera_id, settings.config) if not validate_file_exists_and_is_not_empty(roi_file_path): detail = f"There is no defined RoI for {camera_id}" raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=detail) os.remove(roi_file_path) success = restart_processor() if reboot_processor else True return handle_response(None, success, status.HTTP_204_NO_CONTENT)
async def modify_ml_model(camera_id: str, model_parameters: MLModelDTO, reboot_processor: Optional[bool] = True): validate_camera_existence(camera_id) parameters = pascal_case_to_snake_case(model_parameters) base_path = os.path.join(get_source_config_directory(settings.config), camera_id) models_directory_path = os.path.join(base_path, "ml_models") json_file_path = os.path.join(models_directory_path, f"model_{parameters['Device']}.json") model_name = parameters["Name"] del parameters["Name"] del parameters["Device"] # Create or modify .json file json_content = {"model_name": model_name, "variables": parameters} if os.path.exists(json_file_path): with open(json_file_path, 'w') as outfile: json.dump(json_content, outfile) else: # Hypothesis: source config directory (base_path) should always exists. if not os.path.exists(models_directory_path): Path(models_directory_path).mkdir(parents=True, exist_ok=True) with open(json_file_path, 'x+') as outfile: json.dump(json_content, outfile) # Reboot processor if set success = True if reboot_processor: success = restart_processor() return handle_response(json_content, success, decamelize=False)