<jupyter_start><jupyter_text>XRDrive Sim: Data Preprocessing<jupyter_code>import numpy as np
import time
import pandas as pd

import cv2
import matplotlib.pyplot as plt
%matplotlib inline
datasets = ['data/xrdata1','data/xrdata2','data/xrdata3','data/xrdata4']
import os
import copy
# MouseMode = False
MouseMode = True

data0 = []
for dataset in datasets:
    metadata = os.path.join(dataset, 'xrdata.csv')
    xrpic_path = os.path.join(dataset, 'xrpic')
    df = pd.read_csv(metadata, names=['name','state'])
    
    if MouseMode: dfv = copy.copy(df)
    
    df['name'] = [os.path.join(xrpic_path, xrpic) for xrpic in df['name']]
    data0.append(df)

    if MouseMode:
        xrpic_pathv = xrpic_path + 'v' # xrpicv
        
        if not os.path.exists(xrpic_pathv):
            os.makedirs(xrpic_pathv)

        for pic in dfv['name']:
            # make flip image
            imgFile = os.path.join(xrpic_path, xrpic)
            img = cv2.imread(imgFile)
            img = copy.deepcopy(img)
            img = cv2.flip(img, 1) # flip horizontally

            # save flip image
            imgFilev = os.path.join(xrpic_pathv, 'v_' + xrpic)
            cv2.imwrite(imgFilev, img)

        dfv['name'] = [os.path.join(xrpic_pathv, 'v_' + xrpic) for xrpic in dfv['name']]
        dfv['state'] = [ 'v' + state[-1] for state in dfv['state']]
        data0.append(dfv)
        
        
# join into one
joined_data = pd.concat(data0)
joined_data.head()
joined_data.to_csv('complete_data_clean', index=False)
num_class = len(joined_data['state'].unique())

ges_to_num = dict({(g,i) for i, g in enumerate(joined_data['state'].unique())})
num_to_ges = dict({(i,g) for i, g in enumerate(joined_data['state'].unique())})

import json
with open('ges2num.json', 'w') as fout:
    json.dump(ges_to_num, fout)

with open('num2ges.json', 'w') as fout:
    json.dump(num_to_ges, fout)

num_class, ges_to_num
joined_data = joined_data.replace({'state':ges_to_num})
joined_data.head()
from collections import defaultdict

processed_hand = defaultdict(list)
processed_mask = defaultdict(list)

for i, (im_path, state) in enumerate(joined_data.values):
    im_name = im_path.split('/')[-1]
    
    hand_outdir = os.path.join(im_path.split('/')[0], im_path.split('/')[1], 'hand_xrpic')
    mask_outdir = os.path.join(im_path.split('/')[0], im_path.split('/')[1], 'mask_xrpic')
    
    if not os.path.exists(hand_outdir):
        os.makedirs(hand_outdir)
    
    if not os.path.exists(mask_outdir):
        os.makedirs(mask_outdir)
        
    im = cv2.imread(im_path)

    im_ycrcb = cv2.cvtColor(im, cv2.COLOR_BGR2YCR_CB)

    skin_ycrcb_mint = np.array((0, 133, 77))
    skin_ycrcb_maxt = np.array((255, 173, 127))
    skin_ycrcb = cv2.inRange(im_ycrcb, skin_ycrcb_mint, skin_ycrcb_maxt)    
    
    _, contours, _ = cv2.findContours(skin_ycrcb, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    
    # fimd max contour
    max_area=100
    ci=0
    for i in range(len(contours)):
        cnt=contours[i]
        area = cv2.contourArea(cnt)
        if(area>max_area):
            max_area=area
            ci=i
    cnts = contours[ci]

    (x,y,w,h) = cv2.boundingRect(cnts)

    moments = cv2.moments(cnts)

    # Central mass of first order moments
    if moments['m00']!=0:
        cx = int(moments['m10']/moments['m00']) # cx = M10/M00
        cy = int(moments['m01']/moments['m00']) # cy = M01/M00
    centerMass=(cx,cy)    

    #Draw center mass
    cv2.circle(im,centerMass,1,[100,0,255],1)
    crop = im[y:y+h, x:x+w]
#     f_im = cv2.resize(crop,(200, 200), interpolation=cv2.INTER_CUBIC)
    f_im = cv2.resize(crop,(64, 64), interpolation=cv2.INTER_CUBIC)
    
    hand_out_path = os.path.join(hand_outdir, im_name)
    mask_out_path = os.path.join(mask_outdir, im_name)
    
    cv2.imwrite(hand_out_path, f_im)
    cv2.imwrite(mask_out_path, skin_ycrcb)
    
    processed_hand['name'].append(hand_out_path)
    processed_hand['state'].append(state)
    processed_mask['name'].append(mask_out_path)
    processed_mask['state'].append(state)
    
    print hand_out_path, mask_out_path
pd.DataFrame(processed_hand).to_csv('complete_hand_data_clean', index=False)
pd.DataFrame(processed_mask).to_csv('complete_mask_data_clean', index=False)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14215571662380172, 'fraction_numerical': 0.02244563946691606, 'mean_word_length': 3.3831967213114753, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '416493', 'license': 'MIT', 'n_tokens_mistral': 1738, 'n_tokens_neox': 1670, 'n_words': 283, 'path': 'xr_preprocessing_data.ipynb', 'repo_name': 'TebogoNakampe/XRDriveSim', 'size': '4277'}