def analyse_video(self, video_code):
        video_images = []
        timestamp = video_code_to_timestamp(video_code)
        video_bouts = self.fish_bouts.get_group(video_code)
        video_path = os.path.join(self.video_directory, timestamp + '.avi')
        tracking_path = os.path.join(self.tracking_directory, video_code + '.csv')
        if os.path.exists(tracking_path):
            tracking_info = read_csv(tracking_path, **self.dtype)
            cap = cv2.VideoCapture(video_path)
            tracked_frames = tracking_info[tracking_info['tracked']].index
            bout_starts, bout_ends = video_bouts[['start', 'end']].values.T
            exclude_frames = np.concatenate([np.arange(start, end) for (start, end) in zip(bout_starts, bout_ends)])
            frame_numbers = tracked_frames[np.isin(tracked_frames, exclude_frames, assume_unique=True, invert=True)]
            random = np.random.choice(frame_numbers, 100, replace=False)
            for f in random:
                cap.set(cv2.CAP_PROP_POS_FRAMES, f)
                ret, frame = cap.read()
                frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
                bg = background_division(frame, self.background)
                ret, threshed = cv2.threshold(bg, 8, 255, cv2.THRESH_BINARY)

                midpoint, heading = tracking_info.loc[f, self.cols].values
                midpoint = np.array(midpoint) / 2.

                img = rotate_and_centre_image(threshed, midpoint, heading, 0)
                video_images.append(img)
            assert len(video_images) == 100
            video_images = np.array(video_images, dtype='float32')
            average = np.mean(video_images, axis=0)
            variance = np.var(video_images, axis=0)
        else:  # in case there is an error with the tracking
            average, variance = np.empty((2, 500, 504), dtype='float32') * np.nan
        self.images[video_code] = [(average, variance)]
    def analyse_video(self, video_code):
        video_images = []
        timestamp = video_code_to_timestamp(video_code)
        video_bouts = self.fish_bouts.get_group(video_code)
        video_path = os.path.join(self.video_directory, timestamp + '.avi')
        tracking_path = os.path.join(self.tracking_directory, video_code + '.csv')
        if os.path.exists(tracking_path):
            tracking_info = read_csv(tracking_path, **self.dtype)
            cap = cv2.VideoCapture(video_path)
            for idx, bout_info in video_bouts.iterrows():
                bout_images = []
                for frame_number in (bout_info.start, bout_info.end):
                    cap.set(cv2.CAP_PROP_POS_FRAMES, frame_number)
                    ret, frame = cap.read()
                    frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
                    bg = background_division(frame, self.background)
                    ret, threshed = cv2.threshold(bg, 8, 255, cv2.THRESH_BINARY)

                    left, right, heading = tracking_info.loc[frame_number, self.cols].values
                    midpoint = np.array([left, right]).mean(axis=0) / 2.

                    img = rotate_and_centre_image(threshed, midpoint, heading, 0)
                    bout_images.append(img)
                video_images.append(bout_images)
        self.images[video_code] = video_images
Пример #3
0
if __name__ == "__main__":

    exemplars = pd.read_csv(paths['exemplars'],
                            dtype={
                                'ID': str,
                                'video_code': str
                            })
    to_check = exemplars[pd.isnull(exemplars['clean'])]

    for idx, bout_info in to_check.iterrows():
        if idx % 100 == 0:
            print 'Analysed: {}/{}'.format(idx, len(exemplars))
        fish_info = experiment.data[experiment.data['ID'] ==
                                    bout_info.ID].iloc[0]
        date_folder = fish_info.date.replace('-', '_')
        video_file = video_code_to_timestamp(bout_info.video_code) + '.avi'
        video_path = os.path.join(experiment.video_directory, date_folder,
                                  fish_info['name'], video_file)
        v = Video(video_path)
        v.scroll(first_frame=bout_info.start,
                 last_frame=bout_info.end,
                 name='exemplar_' + str(idx))
        if v.enter():
            exemplars.loc[idx, 'clean'] = True
        elif v.space():
            exemplars.loc[idx, 'clean'] = False
        else:
            break

    exemplars.to_csv(paths['exemplars'], index=False)
Пример #4
0
                                          'ID': str,
                                          'video_code': str
                                      })

    random_examples = np.random.choice(capture_strike_info.index,
                                       100,
                                       replace=False)
    random_examples = capture_strike_info.loc[random_examples]

    strike_frames = []
    index_order = []
    for fish_ID, fish_strikes in random_examples.groupby('ID'):
        fish_info = experiment.data[experiment.data['ID'] == fish_ID].iloc[0]
        date_folder = fish_info.date.replace('-', '_')
        for video_code, video_strikes in fish_strikes.groupby('video_code'):
            video_file = video_code_to_timestamp(video_code) + '.avi'
            video_path = os.path.join(experiment.video_directory, date_folder,
                                      fish_info['name'], video_file)
            v = Video(video_path)
            for idx, bout_info in video_strikes.iterrows():
                v.scroll(first_frame=bout_info.start, last_frame=bout_info.end)
                if v.enter():
                    strike_frames.append(v.frame_number)
                elif v.space():
                    strike_frames.append(np.nan)
                else:
                    sys.exit()
                index_order.append(idx)

    reordered = random_examples.loc[index_order]
    reordered['strike_frame'] = strike_frames