# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

import os
import numpy as np
import json

overlap_size = 36
window_size = 45
vals = []
train_list = []
val_list = []
test_list = []


def frame_preprocess(vid, vid_class):
    for start in range(0, len(vid)-window_size, window_size - overlap_size):
        currCase = np.empty([window_size,25,2])
        for index in range(0,window_size):
            currCase[index] = vid[start+index]
        vals.append((currCase, vid_class))


vid_class = 0
for vid_folder in os.listdir('bhujangasan/'):
    folder_name = 'bhujangasan/' + str(vid_folder) + '/'
    l = [] 
    for frame in os.listdir(folder_name):
        with open(folder_name + frame) as json_data:
            d = json.load(json_data)
            try:
                data = d['people'][0]['pose_keypoints_2d']
                #remove confidence values
                for j in range(2,52,2):
                    data.pop(j)
            except:
                    print ("Failed at" + frame)
                    Xdata = Xdata
                    Ydata = Ydata
                    stk = np.dstack((Xdata, Ydata))
                    l.append(stk)
                    print ("Added this to list" + frame)
                    continue
            json_data.close
        npdata = np.asarray(data)
        #print(npdata.shape)
        Xdata = data[::2]
        #print(len(Xdata))
        Ydata = data[1::2]
        stk = np.dstack((Xdata, Ydata))
        l.append(stk)
        #y_vals.append(vid_class)
        #print(Xdata, Ydata)
    frame_preprocess(l, vid_class)

# +
for i in range(int(0.60*len(vals))):
    train_list.append(vals[i])
    
for i in range(int(0.60*len(vals)), int(0.60*len(vals))+int(0.20*len(vals))):
    val_list.append(vals[i])
    
for i in range(int(0.60*len(vals))+int(0.20*len(vals)), len(vals)):
    test_list.append(vals[i])
# -

vals = []

vid_class = 1
for vid_folder in os.listdir('padamasan/'):
    folder_name = 'padamasan/' + str(vid_folder) + '/'
    l = [] 
    for frame in os.listdir(folder_name):
        with open(folder_name + frame) as json_data:
            d = json.load(json_data)
            try:
                data = d['people'][0]['pose_keypoints_2d']
                #remove confidence values
                for j in range(2,52,2):
                    data.pop(j)
            except:
                    print ("Failed at" + frame)
                    Xdata = Xdata
                    Ydata = Ydata
                    stk = np.dstack((Xdata, Ydata))
                    l.append(stk)
                    print ("Added this to list" + frame)
                    continue
            json_data.close
        npdata = np.asarray(data)
        #print(npdata.shape)
        Xdata = data[::2]
        #print(len(Xdata))
        Ydata = data[1::2]
        stk = np.dstack((Xdata, Ydata))
        l.append(stk)
    frame_preprocess(l, vid_class)

# +
for i in range(int(0.60*len(vals))):
    train_list.append(vals[i])
    
for i in range(int(0.60*len(vals)), int(0.60*len(vals))+int(0.20*len(vals))):
    val_list.append(vals[i])
    
for i in range(int(0.60*len(vals))+int(0.20*len(vals)), len(vals)):
    test_list.append(vals[i])
# -

vals = []

vid_class = 2
for vid_folder in os.listdir('shavasan/'):
    folder_name = 'shavasan/' + str(vid_folder) + '/'
    l = [] 
    for frame in os.listdir(folder_name):
        with open(folder_name + frame) as json_data:
            d = json.load(json_data)
            try:
                data = d['people'][0]['pose_keypoints_2d']
                #remove confidence values
                for j in range(2,52,2):
                    data.pop(j)
            except:
                    print ("Failed at" + frame)
                    Xdata = Xdata
                    Ydata = Ydata
                    stk = np.dstack((Xdata, Ydata))
                    l.append(stk)
                    print ("Added this to list" + frame)
                    continue
                    
            json_data.close
        npdata = np.asarray(data)
        #print(npdata.shape)
        Xdata = data[::2]
        #print(len(Xdata))
        Ydata = data[1::2]
        stk = np.dstack((Xdata, Ydata))
        l.append(stk)
    frame_preprocess(l, vid_class)

# +
for i in range(int(0.60*len(vals))):
    train_list.append(vals[i])
    
for i in range(int(0.60*len(vals)), int(0.60*len(vals))+int(0.20*len(vals))):
    val_list.append(vals[i])
    
for i in range(int(0.60*len(vals))+int(0.20*len(vals)), len(vals)):
    test_list.append(vals[i])
# -

vals = []

vid_class = 3
for vid_folder in os.listdir('tadasan/'):
    folder_name = 'tadasan/' + str(vid_folder) + '/'
    l = [] 
    for frame in os.listdir(folder_name):
        with open(folder_name + frame) as json_data:
            d = json.load(json_data)
            try:
                data = d['people'][0]['pose_keypoints_2d']
                #remove confidence values
                for j in range(2,52,2):
                    data.pop(j)
            except:
                    print ("Failed at" + frame)
                    Xdata = Xdata
                    Ydata = Ydata
                    stk = np.dstack((Xdata, Ydata))
                    l.append(stk)
                    print ("Added this to list" + frame)
                    continue
            json_data.close
        npdata = np.asarray(data)
        #print(npdata.shape)
        Xdata = data[::2]
        #print(len(Xdata))
        Ydata = data[1::2]
        stk = np.dstack((Xdata, Ydata))
        l.append(stk)
        
        
    frame_preprocess(l, vid_class)

# +
for i in range(int(0.60*len(vals))):
    train_list.append(vals[i])
    
for i in range(int(0.60*len(vals)), int(0.60*len(vals))+int(0.20*len(vals))):
    val_list.append(vals[i])
    
for i in range(int(0.60*len(vals))+int(0.20*len(vals)), len(vals)):
    test_list.append(vals[i])
# -

val = []

vid_class = 4
for vid_folder in os.listdir('trikonasan/'):
    folder_name = 'trikonasan/' + str(vid_folder) + '/'
    l = [] 
    for frame in os.listdir(folder_name):
        with open(folder_name + frame) as json_data:
            d = json.load(json_data)
            try:
                data = d['people'][0]['pose_keypoints_2d']
                #remove confidence values
                for j in range(2,52,2):
                    data.pop(j)
            except:
                    print ("Failed at" + frame)
                    Xdata = Xdata
                    Ydata = Ydata
                    stk = np.dstack((Xdata, Ydata))
                    l.append(stk)
                    print ("Added this to list" + frame)
                    continue
            json_data.close
        npdata = np.asarray(data)
        #print(npdata.shape)
        Xdata = data[::2]
        #print(len(Xdata))
        Ydata = data[1::2]
        stk = np.dstack((Xdata, Ydata))
        l.append(stk)
        
        
    frame_preprocess(l, vid_class)

# +
for i in range(int(0.60*len(vals))):
    train_list.append(vals[i])
    
for i in range(int(0.60*len(vals)), int(0.60*len(vals))+int(0.20*len(vals))):
    val_list.append(vals[i])
    
for i in range(int(0.60*len(vals))+int(0.20*len(vals)), len(vals)):
    test_list.append(vals[i])
# -

vals = []

vid_class = 5
for vid_folder in os.listdir('vrikshasan/'):
    folder_name = 'vrikshasan/' + str(vid_folder) + '/'
    l = [] 
    for frame in os.listdir(folder_name):
        with open(folder_name + frame) as json_data:
            d = json.load(json_data)
            try:
                data = d['people'][0]['pose_keypoints_2d']
                #remove confidence values
                for j in range(2,52,2):
                    data.pop(j)
            except:
                    print ("Failed at" + frame)
                    Xdata = Xdata
                    Ydata = Ydata
                    stk = np.dstack((Xdata, Ydata))
                    l.append(stk)
                    print ("Added this to list" + frame)
                    continue
            json_data.close
        npdata = np.asarray(data)
        Xdata = data[::2]
        Ydata = data[1::2]
        stk = np.dstack((Xdata, Ydata))
        l.append(stk)
        
        
    frame_preprocess(l, vid_class)

# +
for i in range(int(0.60*len(vals))):
    train_list.append(vals[i])
    
for i in range(int(0.60*len(vals)), int(0.60*len(vals))+int(0.20*len(vals))):
    val_list.append(vals[i])
    
for i in range(int(0.60*len(vals))+int(0.20*len(vals)), len(vals)):
    test_list.append(vals[i])
# -

X_train = []
y_train = []
X_test = []
y_test = []
X_val = []
y_val = []

for i in range(0, len(train_list)):
    X_train.append(train_list[i][0])
for i in range(0, len(train_list)):
    y_train.append(train_list[i][1])

for i in range(0, len(val_list)):
    X_val.append(val_list[i][0])
for i in range(0, len(val_list)):
    y_val.append(val_list[i][1])

for i in range(0, len(test_list)):
    X_test.append(test_list[i][0])
for i in range(0, len(test_list)):
    y_test.append(test_list[i][1])

X_train = np.array(X_train)
y_train = np.array(y_train)

X_val = np.array(X_val)
y_val = np.array(y_val)

X_test = np.array(X_test)
y_test = np.array(y_test)

np.save("data_files/X_train_eachCateg", X_train)
np.save("data_files/X_test_eachCateg", X_test)
np.save("data_files/X_val_eachCateg", X_val)
np.save("data_files/y_train_eachCateg", y_train)
np.save("data_files/y_test_eachCateg", y_test)
np.save("data_files/y_val_eachCateg", y_val)



StarcoderdataJupyterScriptsDedupFiltered