Skip to content

Commit

Permalink
Update handler and Dynamo DB
Browse files Browse the repository at this point in the history
  • Loading branch information
Ipsit Sahoo (Student) committed Mar 19, 2023
1 parent 87b3e76 commit 031c210
Show file tree
Hide file tree
Showing 7 changed files with 136 additions and 9 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
*.mp4
*.json
*.json
*.csv
Binary file removed Frames/image-001.jpeg
Binary file not shown.
Binary file removed Frames/image-002.jpeg
Binary file not shown.
Binary file removed Frames/image-003.jpeg
Binary file not shown.
Binary file removed Frames/image-004.jpeg
Binary file not shown.
79 changes: 71 additions & 8 deletions handler.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import boto3
import colorama
import face_recognition
import glob
import os
import pickle
from botocore.exceptions import ClientError
from termcolor import cprint
import colorama

colorama.init()
input_bucket = 'inputbucket-cse546'
output_bucket = "546proj2output"
output_bucket = "outputbucket-cse546"
encoding_filename = "encoding"
frames_path = os.path.join(os.getcwd(), "Frames")

# Function to read the 'encoding' file
def open_encoding(filename):
Expand All @@ -18,7 +19,14 @@ def open_encoding(filename):
file.close()
return data

def face_recognition_handler(file_name):
def face_recognition_handler(file_name):

# 0. Build the baseline
frames_path = os.path.join(os.getcwd(), "Frames")
if not os.path.exists(frames_path):
os.makedirs(frames_path)


local_file_path = download_file_s3(file_name)
encoded_data = open_encoding(encoding_filename)

Expand Down Expand Up @@ -52,8 +60,32 @@ def face_recognition_handler(file_name):
if faceName:
break

# Search facename in DynamoDB
return faceName
# 3. Search facename in DynamoDB

result = search_in_dynamodb(facename=faceName)

# 4. Upload CSV file to S3 bucket.
upload_status = False
csvFileName = None
if result:
csvFileName = file_name.split('.')[0] + ".csv"
with open(f'{csvFileName}', 'w') as f:
f.writelines(result)

upload_csv_to_bucket(
csv_file=csvFileName)
upload_status = True

# 5. Clean Up if Step 4 succeeds
if upload_status:
# Remove all files in Frames folder
print("Clean up script starts...")
files = glob.glob(frames_path + "/*")
for f in files:
cprint(f"Deleting file {f}", "red")
os.remove(f)

return result


# Define function to download file from S3 bucket
Expand All @@ -69,12 +101,43 @@ def download_file_s3(file_name):
return local_file_path


if not os.path.exists(frames_path):
os.makedirs(frames_path)
# Search for facename in Dynamo DB
def search_in_dynamodb(facename):
dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table('Student_data')
if facename:
response = table.get_item(Key={
"name": facename
})

return f"{response['Item']['name']},{response['Item']['major']},{response['Item']['year']}"
else:
return None

# Upload csv file to S3 bucket
def upload_csv_to_bucket(csv_file):
s3 = boto3.resource('s3')
bucket = s3.Bucket(output_bucket)

local_file_path = os.path.join(os.getcwd(), csv_file)
cprint(f"Local CSV path : {local_file_path}", "white")

if os.path.exists(local_file_path):
cprint(f'Uploading file to S3 : {output_bucket}', "blue")
try:
response = bucket.upload_file(local_file_path, csv_file)
except ClientError as e:
cprint(e, "red")
exit(-1)
else:
cprint(f"CSV file : {local_file_path} not found.", "red")
exit(-1)


# This will change to actual format
# face_recognition_handler(event, context)
print(face_recognition_handler('test_0.mp4'))

# print(search_in_dynamodb("president_biden"))


63 changes: 63 additions & 0 deletions upload_data_to_dynamo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import boto3
import json
import os
import time
from botocore.exceptions import ClientError

dynamo_client = boto3.resource(service_name='dynamodb', region_name='us-east-1')
table_name='Student_data'

# delete table if it exists
try:
student_data_table = dynamo_client.Table(table_name)
student_data_table.delete()

print(f"Deleting {student_data_table.name}...")
student_data_table.wait_until_not_exists()
except ClientError as ce:
print(ce.response['Error'])

# Create the table
try:
dynamo_client.create_table(
TableName=table_name,
AttributeDefinitions=[
{
'AttributeName': 'name',
'AttributeType': 'S'
},
],
TableClass='STANDARD',
KeySchema=[
{
'AttributeName': 'name',
'KeyType': 'HASH'
},
],
BillingMode='PROVISIONED',
ProvisionedThroughput={
'ReadCapacityUnits': 5,
'WriteCapacityUnits': 5
},
)

print(f'Table {table_name} successfully created')
except ClientError as ce:
print(ce.response['Error'])
exit(-1)


student_data_table = dynamo_client.Table(table_name)

student_data_table.wait_until_exists()

if os.path.exists('./student_data.json'):
data = json.load(open('./student_data.json'))
else:
print('File student.json does not exist')
exit(-1)

# Upload data to table
for d in data:
print(f'Adding {d}')
student_data_table.put_item(Item=d)

0 comments on commit 031c210

Please sign in to comment.