Skip to content

Made writing of data output continuous #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 25 additions & 18 deletions DeepLabStream.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import os
import sys
import time
import csv
from importlib.util import find_spec

import click
Expand All @@ -19,7 +20,7 @@

from utils.generic import VideoManager, WebCamManager, GenericManager
from utils.configloader import RESOLUTION, FRAMERATE, OUT_DIR, MODEL_NAME, MULTI_CAM, STACK_FRAMES, \
ANIMALS_NUMBER, STREAMS, STREAMING_SOURCE, MODEL_ORIGIN
ANIMALS_NUMBER, STREAMS, STREAMING_SOURCE, MODEL_ORIGIN, CSV_DELIMITER
from utils.plotter import plot_bodyparts, plot_metadata_frame
from utils.poser import load_deeplabcut, load_dpk, load_dlc_live, get_pose, calculate_skeletons,\
find_local_peaks_new, get_ma_pose
Expand Down Expand Up @@ -57,17 +58,17 @@ def create_row(index, animal_skeletons, experiment_status, experiment_trial, sta
# creating joints columns
for num, animal in enumerate(animal_skeletons):
for joint, value in animal.items():
row_dict[("Animal{}".format(num + 1),
joint, 'x')], row_dict[("Animal{}".format(num + 1), joint, 'y')] = value
row_dict["Animal{}".format(num + 1) + '_' + joint + '_x'], \
row_dict["Animal{}".format(num + 1) + '_' + joint + '_y'] = value
# optional time column
if start_time is not None:
row_dict[('Time', '', '')] = round(time.time() - start_time, 3)
row_dict["Time"] = round(time.time() - start_time, 3)
# experiment columns
row_dict[('Experiment', 'Status', '')] = experiment_status
row_dict["Experiment_Status"] = experiment_status
if experiment_trial is None and experiment_status:
row_dict[('Experiment', 'Trial', '')] = 'InterTrial'
row_dict["Experiment_Trial"] = "InterTrial"
else:
row_dict[('Experiment', 'Trial', '')] = experiment_trial
row_dict["Experiment_Trial"] = experiment_trial
row = pd.Series(row_dict, name=index)
return row

Expand Down Expand Up @@ -533,34 +534,40 @@ def finish_streaming(self):
#####################
def create_output(self):
"""
Create lists to contain serialized skeletons
Create files to contain data output
"""
for camera in self.enabled_cameras:
self._data_output[camera] = []
file_path = OUT_DIR + '/DataOutput{}'.format(camera) + '-' + time.strftime('%d%m%Y-%H%M%S') + '.csv'
self._data_output[camera] = file_path

def append_row(self, camera, index, animal_skeletons, experiment_status, experiment_trial, start_time=None):
"""
Create a pd.Series for each frame from each camera with joints position and store it
Create a pd.Series for each frame from each camera with joints position and write it
:param experiment_trial: current trial name
:param experiment_status: current experiment status
:param camera: camera name
:param index: frame index
:param animal_skeletons: skeletons for that frame
:param start_time: (optional) starting time point for Time column
"""
row = create_row(index, animal_skeletons, experiment_status, experiment_trial, start_time)
self._data_output[camera].append(row)
row = create_row(index,
animal_skeletons,
experiment_status,
experiment_trial,
start_time)
first_row = not os.path.exists(self._data_output[camera])
with open(self._data_output[camera], mode='a', newline='') as f:
csv_file = csv.writer(f, delimiter=CSV_DELIMITER, quoting=csv.QUOTE_NONE)
if first_row:
csv_file.writerow(["Frame", *row.index.values])
csv_file.writerow([index, *row.to_list()])

def create_dataframes(self):
"""
Outputting dataframes to csv
"""
for num, camera in enumerate(self._data_output):
print("Saving database for device {}".format(camera))
df = pd.DataFrame(self._data_output[camera])
df.index.name = 'Frame'
df.to_csv(OUT_DIR + '/DataOutput{}'.format(camera) + '-' + time.strftime('%d%m%Y-%H%M%S') + '.csv', sep=';')
print("Database saved")
pass
# now there is only void

######
# meta
Expand Down
3 changes: 3 additions & 0 deletions settings.ini
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ CAMERA_SOURCE = 0
#you can use "camera", "ipwebcam" or "video" to select your input source
STREAMING_SOURCE = video

[Data Output]
CSV_DELIMITER = ;

[Pose Estimation]
#possible origins are: DLC, DLC-LIVE, MADLC, DEEPPOSEKIT
MODEL_ORIGIN = ORIGIN
Expand Down
6 changes: 6 additions & 0 deletions utils/configloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ def get_script_path():
#IPWEBCAM
PORT = dsc_config['IPWEBCAM'].get('PORT')

# Data output

CSV_DELIMITER = dsc_config['Data Output'].get('CSV_DELIMITER')
if len(CSV_DELIMITER) > 1 or not isinstance(CSV_DELIMITER, str):
CSV_DELIMITER = ';'


# experiment
EXP_ORIGIN = dsc_config['Experiment'].get('EXP_ORIGIN')
Expand Down