import boto3 import datetime # Initialize AWS clients dynamodb = boto3.client('dynamodb') rekognition = boto3.client('rekognition') # Name of the DynamoDB table table_name = 'AttendanceTable' def create_table(): # Create a DynamoDB table to store attendance records table = dynamodb.create_table( TableName=table_name, KeySchema=[ { 'AttributeName': 'StudentID', 'KeyType': 'HASH' # Partition key }, { 'AttributeName': 'Timestamp', 'KeyType': 'RANGE' # Sort key } ], AttributeDefinitions=[ { 'AttributeName': 'StudentID', 'AttributeType': 'S' }, { 'AttributeName': 'Timestamp', 'AttributeType': 'N' } ], ProvisionedThroughput={ 'ReadCapacityUnits': 5, 'WriteCapacityUnits': 5 } ) # Wait for the table to be created dynamodb.get_waiter('table_exists').wait(TableName=table_name) print('Table created:', table_name) def detect_faces(image): # Use AWS Rekognition to detect faces in an image response = rekognition.detect_faces( Image={'Bytes': image}, Attributes=['DEFAULT'] ) return response['FaceDetails'] def process_image(image): # Process an image and detect faces faces = detect_faces(image) # Identify each face and store attendance records for face in faces: face_id = face['FaceId'] student_id = input('Enter student ID: ') # Prompt for student ID (you can modify this) timestamp = int(datetime.datetime.now().timestamp()) # Store attendance record in DynamoDB dynamodb.put_item( TableName=table_name, Item={ 'StudentID': {'S': student_id}, 'Timestamp': {'N': str(timestamp)}, 'FaceID': {'S': face_id} } ) print('Attendance recorded for student ID:', student_id) # Main program def main(): create_table() # Assuming you have an image named 'attendance.jpg' in the current directory with open('attendance.jpg', 'rb') as image_file: image = image_file.read() process_image(image) if __name__ == '__main__': main()
In this example, we are using the AWS SDK for Python (Boto3) to interact with Amazon DynamoDB for storing attendance records and Amazon Rekognition for face detection. The code first creates a DynamoDB table to store the attendance records. Then it uses Rekognition to detect faces in an image and prompts for the student ID associated with each face. Finally, it stores the attendance record in DynamoDB.
Please make sure to install the required dependencies (boto3
) and configure your AWS credentials before running the code. Also, note that you need to have an AWS account and the necessary permissions to create DynamoDB tables and use Rekognition services.
Remember to customize the code according to your specific requirements, such as modifying the table schema, handling errors, and integrating with other components of your attendance tracking system.