Пример #1
0
 def convert2_tracklets(self, event):
     shuffle = self.shuffle.GetValue()
     trainingsetindex = self.trainingset.GetValue()
     if self.overwrite.GetStringSelection() == "Yes":
         overwrite = True
     else:
         overwrite = False
     deeplabcut.convert_detections2tracklets(
         self.config,
         self.filelist,
         videotype=self.videotype.GetValue(),
         shuffle=shuffle,
         trainingsetindex=trainingsetindex,
         edgewisecondition=True,
         overwrite=overwrite,
         track_method=self.trackertypes.GetValue(),
     )
Пример #2
0
 def convert2_tracklets(self, event):
     shuffle = self.shuffle.GetValue()
     trainingsetindex = self.trainingset.GetValue()
     if self.overwrite.GetStringSelection() == "Yes":
         overwrite = True
     else:
         overwrite = False
     deeplabcut.convert_detections2tracklets(
         self.config,
         self.filelist,
         videotype=self.videotype.GetValue(),
         shuffle=shuffle,
         trainingsetindex=trainingsetindex,
         overwrite=overwrite,
         track_method=self.trackertypes.GetValue(),
         calibrate=self.calibrate.GetStringSelection() == "Yes",
         window_size=self.winsize.GetValue(),
         identity_only=self.identity_toggle.GetStringSelection() == "Yes",
     )
    print("Video analyzed.")

    print("Create video with all detections...")
    scorer, _ = auxiliaryfunctions.GetScorerName(cfg, 1, TRAIN_SIZE)

    deeplabcut.create_video_with_all_detections(
        config_path, [new_video_path], shuffle=1, displayedbodyparts=["bodypart1"]
    )

    print("Video created.")

    print("Convert detections to tracklets...")
    deeplabcut.convert_detections2tracklets(
        config_path,
        [new_video_path],
        "mp4",
        track_method=TESTTRACKER
    )
    print("Tracklets created...")

    deeplabcut.stitch_tracklets(
        config_path,
        [new_video_path],
        "mp4",
        output_name=os.path.splitext(new_video_path)[0] + scorer + "_el.h5",
        track_method=TESTTRACKER
    )

    print("Plotting trajectories...")
    deeplabcut.plot_trajectories(
        config_path,
def Get_Percent_Good(coords):
    bbs, it, mh = coords[0], coords[1], coords[2]

    current_video_path = test_video_folder_path.format(bbs=bbs, it=it, mh=mh)

    #Writes a new file here for the future labeling and things to use
    i_cfg_file = open(inference_cfg_path, "w")
    i_cfg_file.write(inference_cfg_text.format(bbs=bbs, it=it, mh=mh))
    i_cfg_file.close()

    #Creates the folder and videos
    if not os.path.isdir(current_video_path):
        os.mkdir(current_video_path)

        for v in og_video_files:
            shutil.copy2(main_video_folder_path + v, current_video_path + v)

        for p in og_pickle_files:
            shutil.copy2(main_video_folder_path + p, current_video_path + p)

    #analyze video
    #No longer needed since they are the same each time
    #deeplabcut.analyze_videos(config_path,[current_video_path], videotype='.mp4', save_as_csv = True)

    #convert to tracklets
    deeplabcut.convert_detections2tracklets(config_path, [current_video_path],
                                            videotype='mp4',
                                            shuffle=1,
                                            trainingsetindex=0,
                                            track_method='box')

    video_files = os.listdir(current_video_path)

    #Convert to h5s
    for file in video_files:
        if file.endswith("_bx.pickle"):
            h5_name = file.replace("pickle", "h5")
            video_name = file.replace

            if h5_name in video_files:
                print("h5 for {} already exists!".format(file))
            else:
                print("Converting {} to h5!".format(file))
                deeplabcut.convert_raw_tracks_to_h5(config_path,
                                                    current_video_path + file)

    #Need to do this each time to update with new videos
    video_files = os.listdir(current_video_path)
    #fix the messy bits in the files so all points exist
    for file in video_files:
        if file.endswith("_bx.h5"):

            print(file)

            if file.startswith("2020_6"):
                x_len = 1920
                y_len = 800
            else:
                x_len = 2368
                y_len = 1152

            rep_values = [x_len - x_len / 10, y_len / 2, 0]

            hf = h5py.File(current_video_path + file, 'r+')
            table = hf['df_with_missing/table']

            n_table = np.array(table)
            new_table = n_table

            for i in range(n_table.shape[0]):
                dlc_data = n_table[i][1]
                dlc_data = np.nan_to_num(dlc_data)

                for j in range(len(dlc_data)):
                    # j%3 gives 0 = x, 1 = y, 2 = prob
                    pos = j % 3

                    #if there is no data then replace it with default
                    if dlc_data[j] == 0:
                        new_table[i][1][j] = rep_values[pos]

                        if pos != 2:
                            print(
                                "No Points. Set to {} in position {}.".format(
                                    rep_values[pos], pos))

                    #if x is out of range replace with default
                    if pos == 0 and (dlc_data[j] > x_len or dlc_data[j] < 0):
                        new_table[i][1][j] = rep_values[pos]
                        print(
                            "Out of X Bounds: Replaced {} with {} in position {}."
                            .format(dlc_data[j], rep_values[pos], pos))

                    #if y is out of range replace with default
                    if pos == 1 and (dlc_data[j] > y_len or dlc_data[j] < 0):
                        new_table[i][1][j] = rep_values[pos]
                        print(
                            "Out of Y Bounds: Replaced {} with {} in position {}."
                            .format(dlc_data[j], rep_values[pos], pos))

                    #This is all to allow me to just correct the first n frames and only extract those

            table[...] = new_table
            hf.close()

    video_files = os.listdir(current_video_path)
    #Now create the filtered videos and CSVs
    for file in video_files:
        if file.endswith(".mp4"):

            print(file)

            deeplabcut.filterpredictions(config_path,
                                         [current_video_path + file],
                                         videotype='mp4',
                                         shuffle=1,
                                         track_method='box')
            deeplabcut.create_labeled_video(config_path,
                                            [current_video_path + file],
                                            videotype='.mp4',
                                            shuffle=1,
                                            draw_skeleton=True,
                                            track_method='box',
                                            save_frames=False,
                                            filtered=True)

    video_files = os.listdir(current_video_path)
    #Now finally get the percent of "good" points
    files = []
    good_percents = []

    for file_name in video_files:
        if file_name.endswith(".csv"):

            in_csv = open(current_video_path + file_name, "r")

            #Skip the first 4
            skip_lines = 0

            good_points = 0
            total_points = 0

            for line in in_csv:
                if skip_lines >= 4:
                    lis = line.split(",")[:-24]

                    #Off by 1 for the first row of frame counts
                    probs = np.asarray(lis[3::3]).astype(np.float)

                    #If there are no zeros in probs
                    good_points += np.sum(probs > prob_cutoff)
                    total_points += len(probs)

                else:
                    skip_lines += 1

                    lis = line.split(",")[:-24]

            files.append(file_name)
            good_percents.append(round(good_points / total_points * 100, 2))

            in_csv.close()
            #out_csv.close()

    files = np.asarray(files)
    good_percents = np.asarray(good_percents)

    sort_percent = np.flip(good_percents[np.argsort(good_percents)])
    sorted_files = np.flip(files[np.argsort(good_percents)])

    out_file = open(
        current_video_path +
        "PGV_{bbs}_{it}_{mh}.txt".format(bbs=bbs, it=it, mh=mh), "w")

    for i in range(len(sorted_files)):
        out_file.write("{} : {}% good points\n".format(sorted_files[i][:22],
                                                       sort_percent[i]))
        print("{} : {}% good points".format(sorted_files[i][:22],
                                            sort_percent[i]))

    out_file.close()

    score = np.sum((100 - good_percents)**2)

    main_csv = open(
        main_video_folder_path + "PGV_all.csv".format(bbs=bbs, it=it, mh=mh),
        "a")
    main_csv.write(
        "{bbs}_{it}_{mh},{bbs},{it},{mh},{v0},{v1},{v2},{v3},{v4},{m},{score}\n"
        .format(bbs=bbs,
                it=it,
                mh=mh,
                v0=good_percents[0],
                v1=good_percents[1],
                v2=good_percents[2],
                v3=good_percents[3],
                v4=good_percents[4],
                m=np.mean(good_percents),
                score=score))
    main_csv.close()

    shutil.rmtree(current_video_path)

    return (score)
Пример #5
0
#Empty string to do all of them, fill it with the ID to just run it with one
single_vid = ""

#Writes a new file here for the future labeling and things to use
i_cfg_file = open(inference_cfg_path, "w")
i_cfg_file.write(inference_cfg_text.format(bbs=bbs, it=it, mh=mh))
i_cfg_file.close()

#analyze video
#No longer needed since they are the same each time
#deeplabcut.analyze_videos(config_path,[current_video_path], videotype='.mp4', save_as_csv = True)

#convert to tracklets
deeplabcut.convert_detections2tracklets(config_path, [main_video_folder_path],
                                        videotype='mp4',
                                        shuffle=1,
                                        trainingsetindex=0,
                                        track_method='box')

video_files = os.listdir(main_video_folder_path)

#Convert to h5s
for file in video_files:
    if file.endswith("_bx.pickle") and single_vid in file:
        h5_name = file.replace("pickle", "h5")
        video_name = file.replace

        if h5_name in video_files:
            print("h5 for {} already exists!".format(file))
        else:
            print("Converting {} to h5!".format(file))
                              "mp4",
                              robust_nframes=True)
    print("Video analyzed.")

    print("Create video with all detections...")
    scorer, _ = auxiliaryfunctions.GetScorerName(cfg, 1, TRAIN_SIZE)
    deeplabcut.create_video_with_all_detections(config_path, [new_video_path],
                                                scorer)
    print("Video created.")

    edgewisecondition = True
    print("Convert detections...")
    deeplabcut.convert_detections2tracklets(
        config_path,
        [new_video_path],
        "mp4",
        track_method="box",
        edgewisecondition=edgewisecondition,
    )
    deeplabcut.convert_detections2tracklets(config_path, [new_video_path],
                                            "mp4",
                                            track_method="skeleton")

    print("Extracting maps...")
    deeplabcut.extract_save_all_maps(config_path, Indices=[0, 1, 2])

    print("Analyzing video...")
    deeplabcut.analyze_videos(config_path, [new_video_path],
                              "mp4",
                              save_as_csv=True)
    print("Video analyzed.")
    print("Analyzing video...")
    deeplabcut.analyze_videos(config_path, [new_video_path], "mp4", robust_nframes=True,allow_growth=True)

    print("Video analyzed.")

    print("Create video with all detections...")
    scorer, _ = auxiliaryfunctions.GetScorerName(cfg, 1, TRAIN_SIZE)
    deeplabcut.create_video_with_all_detections(
        config_path, [new_video_path], shuffle=1, displayedbodyparts=["bodypart1"]
    )
    print("Video created.")

    print("Convert detections to tracklets...")
    deeplabcut.convert_detections2tracklets(
        config_path, [new_video_path], "mp4", track_method="box"
    )
    deeplabcut.convert_detections2tracklets(
        config_path, [new_video_path], "mp4", track_method="ellipse"
    )
    print("Tracklets created...")

    deeplabcut.stitch_tracklets(
        config_path,
        [new_video_path],
        "mp4",
        output_name=os.path.splitext(new_video_path)[0] + scorer + "_el.h5",
    )

    print("Plotting trajectories...")
    deeplabcut.plot_trajectories(
Пример #8
0
import numpy as np

config_path = '/PATH_TO_CONFIG/config.yaml'

videos = glob.glob('/PATH_TO_VIDEOS/*.mp4')

print('analyzing ' + str(len(videos)) + ' videos:')
print(*videos, sep="\n")

deeplabcut.analyze_videos(config_path,
                          videos,
                          shuffle=1,
                          batchsize=8,
                          dynamic=(True, 0.5, 100))
deeplabcut.create_video_with_all_detections(
    config_path, videos, 'DLC_resnet50_aedesNov16shuffle1_80000')
deeplabcut.convert_detections2tracklets(config_path,
                                        videos,
                                        shuffle=1,
                                        videotype='mp4',
                                        trainingsetindex=0,
                                        track_method='box')

trackletPickles = glob.glob('/PATH_TO_VIDEOS/*_bx.pickle')

print('Converting ' + str(len(trackletPickles)) + ' tracklets:')
print(*trackletPickles, sep="\n")

for tracklet in trackletPickles:
    deeplabcut.convert_raw_tracks_to_h5(config_path, tracklet)