3/30/2022

AWS Lambda invoke example code

 Refer to code

..

import boto3
import botocore
from botocore.exceptions import ClientError
from botocore.config import Config
import json


#params
ACCESS_KEY = 'your_key'
SECRET_KEY = 'your_sec_key'
REGION = 'your_region' #ex) 'eu-west-1'
LAMBDA_ARN = 'your lambda arn' #ex)arn:aws:lambda:eu-west-1:1211:function:functionname'
your_input_value = 'your_input_value'


#define labmda client
lambda_client = boto3.client('lambda',
aws_access_key_id=ACCESS_KEY,
aws_secret_access_key=SECRET_KEY,
region_name = REGION,
config = Config(read_timeout=600))

#invoke
futs = lambda_client.invoke(
FunctionName = LAMBDA_ARN,
InvocationType = "RequestResponse", #event
Payload = json.dumps({'your_input_key': your_input_value})
)

#parsing result
res = futs['Payload'].read().decode()
res_dict = json.loads(res)

#print result as dict
print(res_dict, type(res_dict))

..


www.marearts.com

πŸ™‡πŸ»‍♂️


3/29/2022

python list string to list

 

.

import ast
x = '[ "A","B","C" , " D"]'
x = ast.literal_eval(x)
>>> x
['A', 'B', 'C', ' D']

.



www.marearts.com


3/15/2022

TypeError: ‘tuple’ object does not support item assignment

 

Here is simple solution


..

x = (1,2,3)
y = list(x) #convert tuple -> list
y[0] = 10 #modify element value
x = tuple(y) #revert list -> tuple
print(x)

..


Thank you.

www.marearts.com

πŸ™‡πŸ»‍♂️

3/09/2022

save labelme json data

Save json and image for labelme


..

def make_labelme_json(list_ltrb, img, label, img_full_fn):

h, w, _ = img.shape
shape_dict =[]

for v in list_ltrb:
x1, y1, x2, y2 = v
adict={"label": label, "points":[[x1,y1],[x2,y2]], "group_id": None, "shape_type":"rectangle", "flags":{}}
shape_dict.append(adict)

image_string = cv2.imencode('.jpg', img)[1]
image_string = base64.b64encode(image_string).decode()
#make string image dict
wdict={"version":"4.5.6", "flags": {}, "shapes":shape_dict, "imagePath": img_full_fn, "imageData":image_string}

return wdict


def save_json_img(wdict, labelme_path, fn_only, img):
#save json
str_type = json.dumps(wdict) #dic to json
prefix_fn = "{}{}".format(labelme_path, fn_only)
f= open("{}.json".format(prefix_fn),"w+")
f.write(str_type)
f.close

#save image
cv2.imwrite("{}.jpg".format(prefix_fn), img)

wdict = make_labelme_json(list_ltrb, img,'lp', './'+fn )
save_json_img(wdict, labelme_path, fn_only, img)

..

3/08/2022

python dict keys to list

 refer to example code

..

dictionary = {"a": 1, "b": 2, "c": 3}
key_iterable = dictionary.keys()
key_list = list(key_iterable)
print(key_list)

..



www.marearts.com

Thank you. πŸ™‡πŸ»‍♂️

3/07/2022

get pdf file list in aws s3 bucket

Get all pdf file list in s3 bucket.

Change extension file name properly.


..

#s3 client
import boto3
from botocore.exceptions import ClientError

s3_client = boto3.client('s3')
ACCESS_KEY = 'key'
SECRET_KEY = 'secret_key'
s3_client = boto3.client(
's3',
aws_access_key_id=ACCESS_KEY,
aws_secret_access_key=SECRET_KEY,
region_name = 'eu-west-1'
)

#get pdf list
paginator = s3_client.get_paginator('list_objects_v2')
pages = paginator.paginate(Bucket='bucket_name')

cnt = 0
pdf_list = []
for page in pages:
for obj in page['Contents']:
#get file name
fn = obj['Key']
#check extension name
if fn[-3:] == 'pdf':
pdf_list.append(fn)
#print count, filename
print(f'cnt:{cnt}, fn:{fn}')
#increase count
cnt += 1

..


other code for search bucket + subfolder

..

operation_parameters = {'Bucket': 'bucket_name','Prefix': 'sub_folder_name'}
pages = paginator.paginate(**operation_parameters)

..


www.marearts.com

thank you.

πŸ™‡πŸ»‍♂️

3/02/2022

flatten, unflatten Pytorch, torch, tensor reshape

 flatten -> unflatten


..

#batch, ch, row, col
input_size=torch.empty(10, 1, 256, 256)
print('input shape: ',input_size.shape)

#flatten batch, ch*row*col
reshape = torch.flatten(input_size, start_dim=1)
# reshape = input_size.reshape(-1, 1*256*256)
print('reshape or flatten: ',reshape.shape)

#unflatten batch, ch, row, col
reshape = reshape.reshape(-1,1,256,256)
print('unflatten: ',reshape.shape)

..


input shape:  torch.Size([10, 1, 256, 256])
reshape or flatten:  torch.Size([10, 65536])
unflatten:  torch.Size([10, 1, 256, 256])


Thank you.
πŸ™‡πŸ»‍♂️

3/01/2022

print update, rewrite python in console

 

..

print(f'iter:{i}/{total}',sep='',end="\r",flush=True)

..


Thank you.

jupyter notebook, update 1 line printed string, python

 


refer to code.

...

from IPython.display import display

cnt=0
dh = display('Test2',display_id=True)
while True:
cnt += 1
dh.update(f'cnt:{cnt}') #print cnt and update
if cnt>100:
break

...


Thank you.

www.marearts.com

πŸ™‡πŸ»‍♂️

check image is single color or not, python , PIL image

 refer to example code

..

#get image data
image = Image.open(xxx)
image = image.convert('RGB')

#skip if image has one color
colors = image.getcolors(image.size[0]*image.size[1])
imgH = image.size[1]
imgW = image.size[0]
#print(f'img object w:{imgW}, h:{imgH}')
if len(colors) == 1:
continue

..


www.marearts.com

Thank you.

πŸ™‡πŸ»‍♂️