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)
config_path = '/Users/Ben/Desktop/Light_8_Fish_Ventral_DLC/L8FV-Ben-2020-07-04/config.yaml'
video_path = '/Users/Ben/Desktop/Light_8_Fish_Ventral_DLC/L8FV-Ben-2020-07-04/videos/iteration-3/'

video_files = os.listdir(video_path)

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, video_path + file)

for file in video_files:
    if file.endswith(".mp4"):

        print(file)

        deeplabcut.filterpredictions(config_path, [video_path + file],
                                     videotype='mp4',
                                     shuffle=1,
                                     track_method='box')

        deeplabcut.create_labeled_video(config_path, [video_path + file],
                                        videotype='.mp4',
                                        shuffle=1,
                                        draw_skeleton=True,
Ejemplo n.º 3
0
                                        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))
            deeplabcut.convert_raw_tracks_to_h5(config_path,
                                                main_video_folder_path + file)

#Need to do this each time to update with new videos
video_files = os.listdir(main_video_folder_path)
#fix the messy bits in the files so all points exist
for file in video_files:
    if file.endswith("_bx.h5") and single_vid in 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]
Ejemplo n.º 4
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)