<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 purpose
upload sketches to S3
build stimulus dictionary and write to database
upload sketches to s3
Step1:  build stimulus dictionary
Step2:  upload stim dictionary to mongo (db = 'stimuli', collection='sketchpad_basic_recog')
Step4:  crop 3d objects
<ASSISTANT_TASK:>
Python Code:
upload_dir = './sketch'
import boto
runThis = 0
if runThis:
    conn = boto.connect_s3()
    b = conn.create_bucket('sketchpad_basic_pilot2_sketches')
    all_files = [i for i in os.listdir(upload_dir) if i != '.DS_Store']
    for a in all_files:
        print a
        k = b.new_key(a)
        k.set_contents_from_filename(os.path.join(upload_dir,a))
        k.set_acl('public-read')
Explanation: purpose
upload sketches to S3
build stimulus dictionary and write to database
upload sketches to s3
End of explanation
## read in experimental metadata file
path_to_metadata = '../../analysis/sketchpad_basic_pilot2_group_data.csv'
meta = pd.read_csv(path_to_metadata)
## clean up and add filename column
meta2 = meta.drop(['svg','png','Unnamed: 0'],axis=1)
filename = []
games = []
for i,row in meta2.iterrows():
    filename.append('gameID_{}_trial_{}.png'.format(row['gameID'],row['trialNum']))
    games.append([])
meta2['filename'] = filename
meta2['games'] = games
## write out metadata to json file
stimdict = meta2.to_dict(orient='records')
import json
with open('sketchpad_basic_recog_meta.js', 'w') as fout:
    json.dump(stimdict, fout)
J = json.loads(open('sketchpad_basic_recog_meta.js',mode='ru').read())
assert len(J)==len(meta2)
'{} unique games.'.format(len(np.unique(meta2.gameID.values)))
Explanation: build stimulus dictionary
End of explanation
# set vars 
auth = pd.read_csv('auth.txt', header = None) # this auth.txt file contains the password for the sketchloop user
pswd = auth.values[0][0]
user = 'sketchloop'
host = 'rxdhawkins.me' ## cocolab ip address
# have to fix this to be able to analyze from local
import pymongo as pm
conn = pm.MongoClient('mongodb://sketchloop:' + pswd + '@127.0.0.1')
db = conn['stimuli']
coll = db['sketchpad_basic_pilot2_sketches']
## actually add data now to the database
for (i,j) in enumerate(J):
    if i%100==0:
        print ('%d of %d' % (i,len(J)))
    coll.insert_one(j)
## How many sketches have been retrieved at least once? equivalent to: coll.find({'numGames':{'$exists':1}}).count()
coll.find({'numGames':{'$gte':0}}).count()
## stashed away handy querying things
# coll.find({'numGames':{'$gte':1}}).sort('trialNum')[0]
# from bson.objectid import ObjectId
# coll.find({'_id':ObjectId('5a9a003d47e3d54db0bf33cc')}).count()
Explanation: upload stim dictionary to mongo (db = 'stimuli', collection='sketchpad_basic_recog')
End of explanation
import os
from PIL import Image
def RGBA2RGB(image, color=(255, 255, 255)):
    Alpha composite an RGBA Image with a specified color.
    Simpler, faster version than the solutions above.
    Source: http://stackoverflow.com/a/9459208/284318
    Keyword Arguments:
    image -- PIL RGBA Image object
    color -- Tuple r, g, b (default 255, 255, 255)
    
    image.load()  # needed for split()
    background = Image.new('RGB', image.size, color)
    background.paste(image, mask=image.split()[3])  # 3 is the alpha channel
    return background
def load_and_crop_image(path, dest='object_cropped', imsize=224):
    im = Image.open(path)
#     if np.array(im).shape[-1] == 4:
#         im = RGBA2RGB(im)
    
    # crop to sketch only
    arr = np.asarray(im)
    if len(arr.shape)==2:
        w,h = np.where(arr!=127)
    else:
        w,h,d = np.where(arr!=127) # where the image is not white        
    if len(h)==0:
        print(path)            
    xlb = min(h)
    xub = max(h)
    ylb = min(w)
    yub = max(w)
    lb = min([xlb,ylb])
    ub = max([xub,yub])            
    im = im.crop((lb, lb, ub, ub))  
    im = im.resize((imsize, imsize), Image.ANTIALIAS)
    objname = path.split('/')[-1]
    if not os.path.exists(dest):
        os.makedirs(dest)
    im.save(os.path.join(dest,objname))
run_this = 0
if run_this:
    ## actually crop images now
    data_dir = './object'
    allobjs = ['./object/' + i for i in os.listdir(data_dir)]
    for o in allobjs:
        load_and_crop_image(o)
run_this = 0
if run_this:
    ## rename objects in folder
    data_dir = './object'
    allobjs = [data_dir + '/' + i for i in os.listdir(data_dir) if i != '.DS_Store']
    for o in allobjs:
        if len(o.split('_'))==4:
            os.rename(o, os.path.join(data_dir, o.split('/')[-1].split('_')[2] + '.png'))
Explanation: crop 3d objects
End of explanation
<END_TASK>