def start_videoanalyser(): print("Starting DeepLabCut") model = load_model( r"D:\DeepPoseKit-Data-master\datasets\fly\best_model_densenet.h5") experiment_enabled = False video_output = True if experiment_enabled: print("Initializing experiment") experiment = ExampleExperiment() experiment.start_experiment() # some variables initialization all_rows = [] index = 0 while video.isOpened(): ret, frame = video.read() if ret: scmap, locref, pose = get_pose(frame, config, sess, inputs, outputs) peaks = find_local_peaks_new(scmap, locref, ANIMALS_NUMBER, config) skeletons = calculate_skeletons(peaks, ANIMALS_NUMBER) if skeletons: for skeleton in skeletons: if experiment_enabled: result, response = experiment.check_skeleton( frame, skeleton) plot_triggers_response(frame, response) out_frame = plot_bodyparts(frame, skeletons) else: out_frame = frame cv2.imshow('stream', out_frame) if video_output: video_file.write(out_frame) if experiment_enabled: all_rows.append( create_row(index, skeletons, experiment_enabled, experiment.get_trial())) else: all_rows.append( create_row(index, skeletons, experiment_enabled, None)) index += 1 else: break if cv2.waitKey(1) & 0xFF == ord('q'): break if experiment_enabled: experiment.stop_experiment() if video_output: print('Saving analyzed video for {}'.format(video_name)) video_file.release() video.release() create_dataframes(all_rows)
def get_analysed_frames(self) -> tuple: """ The main magic is happening here Getting the data from DLC processes Plotting the data Checking data in experiments Gathering data to a series """ if self._dlc_running: analysed_frames = {} analysis_time = None if CROP: frame_height = CROP_Y[1] - CROP_Y[0] frame_width = CROP_X[1] - CROP_X[0] else: frame_width,frame_height = RESOLUTION for camera in self._multiprocessing: if self._multiprocessing[camera]['output'].full(): if self._start_time is None: self._start_time = time.time() # getting the first frame here # Getting the analysed data analysed_index, peaks, analysis_time = self._multiprocessing[camera]['output'].get() skeletons = calculate_skeletons(peaks, ANIMALS_NUMBER) print('', end='\r', flush=True) # this is the line you should not remove analysed_frame , depth_map, input_time = self.get_stored_frames(camera, analysed_index) delay_time = time.time() - input_time # Calculating FPS and plotting the data on frame self.calculate_fps(analysis_time if analysis_time != 0 else 0.01) frame_time = time.time() - self._start_time analysed_image = plot_metadata_frame( plot_bodyparts(analysed_frame, skeletons), frame_width, frame_height, self._fps, frame_time) # Experiments if self._experiment.experiment_finished and self._experiment_running: self._experiment_running = False if self._experiment_running and not self._experiment.experiment_finished: if ANIMALS_NUMBER > 1 and not FLATTEN_MA and not PASS_SEPARATE: self._experiment.check_skeleton(analysed_image,skeletons) else: for skeleton in skeletons: self._experiment.check_skeleton(analysed_image, skeleton) # Gathering data as pd.Series for output if self._data_output: self.append_row(camera, analysed_index, skeletons, self._experiment_running, self._experiment.get_trial(), self._start_time) analysed_frames[camera] = analysed_image return analysed_frames, analysis_time