Skip to content
Snippets Groups Projects
main_process_mating.py 5.83 KiB
Newer Older
import os, time
from process.batch_processing import BatchProcessing

#import deeplabcut

# TODO Update from examples in test_process_batch

start = time.time()

xyz_path = os.path.join(os.getcwd(), 'data/calib/examples/mosquito_escapes/to_delete/xyz_calibration_device_small.csv')
#xy_path = os.path.join(os.getcwd(), 'data/calib/20200615_xypts-rot.csv')
xy_path = os.path.join(os.getcwd(), 'data/calib/examples/mosquito_escapes/to_delete/20200615_xypts.csv')
dlt_path = os.path.join(os.getcwd(), 'data/calib/examples/mosquito_escapes/to_delete/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.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

img_process.show_plot = False

# # 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]

# > all recordings
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_to_process=rec_names_to_process, yaml_path='processes.yaml', delete_previous=True)

# > OR do each step separately
# Convert images to 8 bits and reduce frame_rate (save in cam_save_paths/_Sample)
# img_process.do_batch('sample', rec_names_to_process=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_to_process=rec_names_to_process, from_fn_name='sample')
#
# # Do 3d reconstruction of tracks
# img_process.do_batch('recon3d', rec_names_to_process=rec_names_to_process, from_fn_name='sample', dlt_path=dlt_path)
#
# # Crop all frames and save in cam_save_paths/_Cropped
# img_process.do_batch('crop', rec_names_to_process=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_to_process=rec_names_to_process, from_fn_name='crop', camn_to_process=[2], degrees=270)
#
# # Stitch all views together and save in cam_save_paths/_Stitched
# img_process.do_batch('stitch', rec_names_to_process=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_to_process=rec_names_to_process, from_fn_name='save_avi', cfg_path=dlc_cfg_path,
#                     shuffle=shuffle, trainingset_index=0, batch_size=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_to_process=rec_names_to_process, from_fn_name='analyse_dlc', model_name=model_name,
#                       tracker_method='skeleton', dlt_path=dlt_path)

# # 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_to_process=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,
#                      animal_name="fly", res_method=res_method, opt_method=opt_method, dlt_path=dlt_path)
print('> All processes as been processed (total time elapsed: {0:.4f} s)'.format(time.time() - start))