async def get_roi_contour(camera_id: str): """ Get the contour of the RoI """ validate_camera_existence(camera_id) roi_file_path = ObjectsFilteringPostProcessor.get_roi_file_path(camera_id, settings.config) roi_contour = ObjectsFilteringPostProcessor.get_roi_contour(roi_file_path) if roi_contour is None: raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=f"There is no defined RoI for {camera_id}") return roi_contour.tolist()
def __init__(self, config, source: str, logger: str): self.config = config self.camera_id = self.config.get_section_dict(source)["Id"] self.resolution = tuple([int(i) for i in self.config.get_section_dict("App")["Resolution"].split(",")]) self.birds_eye_resolution = (200, 300) self.out = None self.out_birdseye = None self.live_feed_enabled = self.config.get_boolean(source, "LiveFeedEnabled") self.track_hist = dict() self.roi_file_path = ObjectsFilteringPostProcessor.get_roi_file_path(self.camera_id, self.config)
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 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()
def map_camera(camera_name, config, options=[]): camera_dict = map_section_from_config(camera_name, config) camera = config.get(camera_name) camera_id = camera.get("Id") image_string = None if "withImage" in options: image_string = get_camera_default_image_string(camera_id) camera_dict["image"] = image_string calibration_file_path = get_camera_calibration_path(settings.config, camera_id) camera_dict["has_been_calibrated"] = validate_file_exists_and_is_not_empty(calibration_file_path) roi_file_path = ObjectsFilteringPostProcessor.get_roi_file_path(camera_id, settings.config) camera_dict["has_defined_roi"] = validate_file_exists_and_is_not_empty(roi_file_path) in_out_file_path = InOutMetric.get_in_out_file_path(camera_id, settings.config) camera_dict["has_in_out_border"] = validate_file_exists_and_is_not_empty(in_out_file_path) return camera_dict
def update(self, cv_image, objects, post_processing_data, fps, log_time): if not self.live_feed_enabled: return self.update_history(post_processing_data["tracks"]) distancings = post_processing_data.get("distances", []) dist_threshold = post_processing_data.get("dist_threshold", 0) birds_eye_window = np.zeros(self.birds_eye_resolution[::-1] + (3, ), dtype="uint8") class_id = int( get_model_json_file_or_return_default_values( self.config, self.config.get_section_dict('Detector')['Device'], self.camera_id)["variables"]["ClassID"]) roi_contour = ObjectsFilteringPostProcessor.get_roi_contour( self.roi_file_path) if roi_contour is not None: color = (41, 127, 255) # #ff7f29 (255, 127, 41) visualization_utils.draw_contour(cv_image, roi_contour, color) output_dict = visualization_utils.visualization_preparation( objects, distancings, dist_threshold) category_index = { class_id: { "id": class_id, "name": "Pedestrian", } } # TODO: json file for detector config face_index = { 0: "YES", 1: "NO", -1: "N/A", } # Assign object's color to corresponding track history for i, track_id in enumerate(output_dict["track_ids"]): self.track_hist[track_id][1].append( output_dict["detection_colors"][i]) # Draw bounding boxes and other visualization factors on input_frame visualization_utils.visualize_boxes_and_labels_on_image_array( cv_image, output_dict["detection_boxes"], output_dict["detection_classes"], output_dict["detection_scores"], output_dict["detection_colors"], output_dict["track_ids"], category_index, instance_masks=output_dict.get("detection_masks"), use_normalized_coordinates=True, line_thickness=3, face_labels=output_dict["face_labels"], face_index=face_index) # TODO: Implement perspective view for objects birds_eye_window = visualization_utils.birds_eye_view( birds_eye_window, output_dict["detection_boxes"], output_dict["violating_objects"]) # Put occupancy to the frame # region # -_- -_- -_- -_- -_- -_- -_- -_- -_- -_- -_- -_- -_- -_- txt_fps = 'Occupancy = ' + str(len(objects)) # (0, 0) is the top-left (x,y); normalized number between 0-1 origin = (0.05, 0.93) visualization_utils.text_putter(cv_image, txt_fps, origin) # -_- -_- -_- -_- -_- -_- -_- -_- -_- -_- -_- -_- -_- -_- # endregion # visualize tracks # region # -_- -_- -_- -_- -_- -_- -_- -_- -_- -_- -_- -_- -_- -_- visualization_utils.draw_tracks(cv_image, self.track_hist, radius=1, thickness=1) # -_- -_- -_- -_- -_- -_- -_- -_- -_- -_- -_- -_- -_- -_- # endregion self.out.write(cv_image) self.out_birdseye.write(birds_eye_window)