-
Notifications
You must be signed in to change notification settings - Fork 6
/
analysis.py
161 lines (118 loc) · 5.56 KB
/
analysis.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#!python36
# -*- coding: utf-8 -*-
"""
NCams Toolbox
Copyright 2019-2020 Charles M Greenspon, Anton Sobinov
https://github.com/CMGreenspon/NCams
Script for running an analysis of the recordings from multiple cameras.
Intended to be used in an interactive environment (e.g. Spyder).
Has following steps:
0. Import modules
1. Load configurations
2. Either 'a' or 'b':
a. Make a new DLC project (this or 2b)
b. Load existing labeled frames
3. Triangulation from multiple cameras
4. Make markered videos
5. Interactive demonstration with a time slider
For more details on the camera data structures and dicts, see help(ncams.camera_tools).
deeplabcut module is licensed under GNU Lesser General Public License v3.0
see lgpl-3.0.md
DeepLabCut2.0 Toolbox (deeplabcut.org)
© A. & M. Mathis Labs
https://github.com/AlexEMG/DeepLabCut
"""
# %% 0 Imports
import os
import deeplabcut
import ncams
BASE_DIR = os.path.join('C:\\', 'FLIR_cameras', 'PublicExample')
# can be used to restrict DLC memory, needs changes to DLC installation, see
# https://github.com/nishbo/DeepLabCut
os.environ['DLC_PER_PROCESS_GPU_MEMORY_FRACTION'] = '0.9'
# %% 1 Load configurations
cdatetime = '2019.12.19_10.38.38'
camera_config_dir = os.path.join(BASE_DIR, 'camconf_'+cdatetime)
camera_config = ncams.yaml_to_config(os.path.join(camera_config_dir, 'config.yaml'))
calibration_config, pose_estimation_config = ncams.load_camera_config(camera_config)
# Load a session config from a file
session_full_filename = os.path.join(BASE_DIR, 'exp_session_2019.12.20_11.11.21_AS_CMG_11_subset',
'session_config.yaml')
session_config = ncams.import_session_config(session_full_filename)
# which videos do you want to train on?
training_videos = [os.path.join(session_config['session_path'],
session_config['cam_dicts'][cs]['ud_video'])
for cs in camera_config['serials']]
# %% 2a Make a new DLC project (this or 2b)
dlc_prj_name = '8cams11subset'
scorer = 'AS'
prj_date = '2020-01-04'
config_path = deeplabcut.create_new_project(
dlc_prj_name, scorer, training_videos,
working_directory=session_config['session_path'], copy_videos=False)
dlc_proj_name = '-'.join([dlc_prj_name, scorer, prj_date])
proj_path = os.path.join(session_config['session_path'], dlc_proj_name)
labeled_csv_path = os.path.join(proj_path, 'labeled_videos')
if not os.path.isdir(labeled_csv_path):
os.mkdir(labeled_csv_path)
print('New config_path: "{}"'.format(config_path))
# Edit the config file to represent your tracking
# DLC Cropping
deeplabcut.extract_frames(config_path, mode='automatic', algo='uniform', crop=False,
userfeedback=False)
deeplabcut.label_frames(config_path)
deeplabcut.check_labels(config_path)
deeplabcut.create_training_dataset(config_path)
# 0 is the GPU number, see in nvidia-smi
deeplabcut.train_network(config_path, gputouse=0, saveiters=25000, maxiters=250000)
deeplabcut.evaluate_network(config_path, plotting=False)
# DLC estimation
deeplabcut.analyze_videos(config_path, training_videos,
gputouse=0, save_as_csv=True, destfolder=labeled_csv_path)
deeplabcut.create_labeled_video(config_path, training_videos, destfolder=labeled_csv_path,
draw_skeleton=True)
# %% 2b Load existing labeled frames
dlc_prj_name = 'CMGPretrainedNetwork'
scorer = 'CMG'
prj_date = '2019-12-03'
dlc_proj_name = '-'.join([dlc_prj_name, scorer, prj_date])
proj_path = os.path.join(BASE_DIR, dlc_proj_name)
config_path = os.path.join(proj_path, 'config.yaml')
print('Existing config_path: "{}"'.format(config_path))
labeled_csv_path = os.path.join(proj_path, 'labeled_videos')
if not os.path.isdir(labeled_csv_path):
os.mkdir(labeled_csv_path)
analyzed_training_videos = []
for serial in camera_config['serials']:
analyzed_training_videos.append(os.path.join(
proj_path, 'labeled_videos',
'cam{}DLC_resnet50_CMGPretrainedNetworkDec3shuffle1_250000_labeled.mp4'.format(serial)))
analyzed_training_videos_dir = [os.path.join(proj_path, 'labeled_videos')]
# %% Refinement?
deeplabcut.extract_frames(config_path, mode='automatic', algo='uniform', crop=False,
userfeedback=False)
deeplabcut.label_frames(config_path)
deeplabcut.merge_datasets(config_path)
deeplabcut.create_training_dataset(config_path)
deeplabcut.train_network(config_path, gputouse=0, saveiters=25000, maxiters=250000)
# %% 3 Triangulation from multiple cameras
method = 'full_rank'
threshold = 0.9
triangulated_path = os.path.join(proj_path, 'triangulated_{}_{}'.format(method, threshold))
if not os.path.exists(triangulated_path):
os.mkdir(triangulated_path)
triangulated_csv = os.path.join(triangulated_path, 'triangulated_points.csv')
ncams.triangulate(
camera_config, triangulated_csv, calibration_config, pose_estimation_config, labeled_csv_path,
threshold=threshold, method=method, undistorted_data=True)
# filter the triangulated points in 3D space
triangulated_csv_p = os.path.join(triangulated_path, 'triangulated_points_smoothed.csv')
ncams.process_triangulated_data(triangulated_csv, output_csv=triangulated_csv_p)
# %% 4 Make markered video
serial = 19335177
video_path = camera_config['dicts'][serial]['ud_video']
ncams.make_triangulation_video(video_path, triangulated_csv_p, skeleton_config=config_path)
# %% 5 Interactive demonstration with a slider
# This sometimes breaks in Spyder, try running as an executable, commenting out parts of
# 'analysis.py' that are not needed.
ncams.reconstruction.interactive_3d_plot(video_path, triangulated_csv_p, skeleton_path=config_path)