Skip to content

orianexxx/FrameComparisonModel-lambda

Repository files navigation

Frame Comparison Lambda Function

This Lambda function compares frames from different Instagram posts stored in S3. It downloads frames from the specified S3 bucket and compares them using structural similarity index (SSIM).

Requirements

  • Python 3.10
  • AWS Lambda with appropriate IAM permissions to access S3
  • Required Python packages (see requirements.txt)

Frame Comparison Model

The function uses the Structural Similarity Index (SSIM) to compare frames. SSIM is a perceptual metric that quantifies the image quality degradation between two images. It's more accurate than traditional methods like Mean Squared Error (MSE) because it considers:

  1. Luminance: The brightness of the images
  2. Contrast: The variation in brightness
  3. Structure: The spatial relationships between pixels

The SSIM score ranges from -1 to 1, where:

  • 1 indicates identical images
  • 0 indicates completely different images
  • -1 indicates inverse images

The comparison process:

  1. Converts images to grayscale to focus on structural differences
  2. Resizes images to the same dimensions if needed
  3. Applies the SSIM algorithm to calculate similarity
  4. Returns a normalized score between 0 and 1

Example interpretation:

  • Score > 0.95: Nearly identical frames
  • Score 0.80-0.95: Very similar frames
  • Score 0.60-0.80: Moderately similar frames
  • Score < 0.60: Significantly different frames

Input Parameters

The Lambda function expects the following JSON input:

{
    "monitored_shortcode": "string",
    "watched_shortcodes": ["string"],
    "platform": "instagram",
    "extension": "png"
}

Parameters Description:

  • monitored_shortcode: The Instagram post shortcode to compare against
  • watched_shortcodes: List of Instagram post shortcodes to compare with the monitored shortcode
  • platform: Platform name (default: "instagram")
  • extension: File extension (default: "png")

Output

The function returns a JSON response with the following structure:

{
    "statusCode": 200,
    "body": {
        "monitored_shortcode": "string",
        "results": [
            {
                "watched_shortcode": "string",
                "frame_comparisons": [
                    {
                        "frame_number": 0,
                        "similarity": 0.95
                    }
                ],
                "average_similarity": 0.95
            }
        ]
    }
}

S3 Structure

The function expects the following S3 structure:

oriane-frames/
└── instagram/
    └── <shortcode>/
        ├── 0.png
        ├── 1.png
        └── ...

Error Handling

The function handles various error cases:

  • Missing required parameters (400)
  • No frames found for monitored shortcode (404)
  • General errors (500)

Setup

  1. Create a new Lambda function with Python 3.10 runtime
  2. Upload the contents of this repository
  3. Configure the Lambda function with appropriate memory (recommended: 512MB) and timeout (recommended: 30 seconds)
  4. Ensure the Lambda function has IAM permissions to access the S3 bucket

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published