Newer
Older
import os, time
from core.images_processing import ImagesProcessing
#import deeplabcut
start = time.time()
xyz_path = os.path.join(os.getcwd(), 'calib_files/xyz_calibration_device_small.csv')
#xy_path = os.path.join(os.getcwd(), 'calib_files/20200615_xypts-rot.csv')
xy_path = os.path.join(os.getcwd(), 'calib_files/20200615_xypts.csv')
dlt_path = os.path.join(os.getcwd(), 'calib_files/20200615_DLTcoefs-py.csv')
dlc_cfg_path = '/home/user/Desktop/Antoine/_DLC/config.yaml'
img_size = (896, 896)
# Generate dlt coefficients
#gen_dlt(3, img_size, xyz_path, xy_path, dlt_path)
img_process = ImagesProcessing(3, dlt_path)
img_process.cam_paths = {1: '/media/user/MosquitoLanding_Photron3_Backup/Photron1/_MatingKinematics',
2: '/media/user/MosquitoLanding_Photron3_Backup/Photron2/_MatingKinematics',
3: '/media/user/MosquitoLanding_Photron3_Backup/Photron3/_MatingKinematics'}
img_process.cam_save_paths = {1: '/media/user/MosquitoLanding_Photron3_Backup/Photron1/_MatingKinematics/_Process',
2: '/media/user/MosquitoLanding_Photron3_Backup/Photron2/_MatingKinematics/_Process',
3: '/media/user/MosquitoLanding_Photron3_Backup/Photron3/_MatingKinematics/_Process'}
img_process.update_leading_zero = False
img_process.init_paths_names()
img_process.max_diff_date_s = 90
# # Fill dict with name of recordings to process
# # > either all recordings from a given date
# date_to_process = '20200303'
# rec_names_to_process = {}
# for camn in range(1, img_process.nb_cam + 1):
# rec_names_to_process[camn] = [s for s in img_process.all_folders_rec_cam[camn] if date_to_process in s]
rec_names_to_process = {}
for camn in range(1, img_process.nb_cam + 1):
rec_names_to_process[camn] = img_process.all_folders_rec_cam[camn]
# # Keep only rec_names that haven't been processed yet
# non_processed_rec_names = {1: [], 2: [], 3: []}
# for camn in range(1, img_process.nb_cam + 1):
# for i, rec_name in enumerate(rec_names_to_process[camn]):
# if not os.path.exists(os.path.join(img_process.cam_save_paths[camn], '_Avi', rec_name)):
# non_processed_rec_names[camn].append(rec_names_to_process[camn][i])
#
# rec_names_to_process = non_processed_rec_names
# -----------------------------------------------------------------------------------------------------------------------
# Will prepare recordings to be used by Deeplabcut
# > do everything automatically use 'multi_processes'
# Do cropping, rotating, stitching, etc at once and save as .avi (save much time and space)
img_process.do_batch('multi_processes', rec_names=rec_names_to_process, yaml_path='processes.yaml', delete_previous=True)
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
# > OR do each step separately
# Convert image to 8 bits and reduce framerate (save in cam_save_paths/_Sample)
# img_process.do_batch('sample', rec_names=rec_names_to_process, step_frame=50, delete_previous=True)
#
# # Do 2d tracking (blob detection) on images in cam_save_paths/_Sample
# img_process.do_batch('track2d', rec_names=rec_names_to_process, from_fn_name='sample')
#
# # Do 3d reconstruction of tracks
# img_process.do_batch('recon3d', rec_names=rec_names_to_process, from_fn_name='sample')
#
# # Crop all frames and save in cam_save_paths/_Cropped
# img_process.do_batch('crop', rec_names=rec_names_to_process, from_fn_name='sample', height_crop=200, width_crop=200, delete_previous=True)
#
# # Rotate view 2 (to always have piston coming from right side)
# img_process.do_batch('rotate', rec_names=rec_names_to_process, from_fn_name='crop', camns=[2], degrees=270)
#
# # Stitch all views together and save in cam_save_paths/_Stitched
# img_process.do_batch('stitch', rec_names=rec_names_to_process, from_fn_name='crop', delete_previous=True)
# -----------------------------------------------------------------------------------------------------------------------
# Run Deeplabcut analysis + Fit 3D skeleton to 2D coordinate from Deeplabcut
dlc_cfg_path = '/home/user/Desktop/Antoine/_maDLC/config.yaml'
shuffle = 31
iteration = 70000
model_name = 'DLC_resnet50_MatingKinematicsshuffle{0}_{1}'.format(shuffle, iteration)
res_method = '3d' # '2d', '3d' or '2d_geo'
opt_method = 'leastsq' # 'powell' or 'leastsq' or 'least_squares'
body_param_names = ['yaw_a', 'pitch_a', 'roll_a', 'x_com', 'y_com', 'z_com']
wing_param_names = ['stroke_a', 'deviation_a', 'rotation_a']
# # Track features using DeepLabCut
# img_process.do_batch('analyse_dlc', rec_names=rec_names_to_process, from_fn_name='save_avi', cfg_path=dlc_cfg_path,
# shuffle=shuffle, trainingsetindex=0, batchsize=5, save_avi=False, model_name=model_name, delete_previous=True)
# # Load (+ filtering and unscrambling) 2d coords from DLC + Reverse processes (unstitch, rotate back, uncrop) + Reconstruct 3d coord
# img_process.do_batch('load_dlc', rec_names=rec_names_to_process, from_fn_name='analyse_dlc', model_name=model_name,
# tracker_method='skeleton')
# # Optimize fit of skeleton to find body and wings angles
# img_process.multiprocessing = True
# img_process.threshold_likelihood = 0.85
# img_process.do_batch('fit_skeleton', rec_names=rec_names_to_process, from_fn_name='load_dlc', model_name=model_name,
# csv_path=csv_wings_geo_path, body_param_names=body_param_names, wing_param_names=wing_param_names,
# res_method=res_method, opt_method=opt_method)
print('> All processes as been processed (total time elapsed: {0:.4f} s)'.format(time.time() - start))