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).
- Python 3.10
- AWS Lambda with appropriate IAM permissions to access S3
- Required Python packages (see requirements.txt)
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:
- Luminance: The brightness of the images
- Contrast: The variation in brightness
- 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:
- Converts images to grayscale to focus on structural differences
- Resizes images to the same dimensions if needed
- Applies the SSIM algorithm to calculate similarity
- 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
The Lambda function expects the following JSON input:
{
"monitored_shortcode": "string",
"watched_shortcodes": ["string"],
"platform": "instagram",
"extension": "png"
}monitored_shortcode: The Instagram post shortcode to compare againstwatched_shortcodes: List of Instagram post shortcodes to compare with the monitored shortcodeplatform: Platform name (default: "instagram")extension: File extension (default: "png")
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
}
]
}
}The function expects the following S3 structure:
oriane-frames/
└── instagram/
└── <shortcode>/
├── 0.png
├── 1.png
└── ...
The function handles various error cases:
- Missing required parameters (400)
- No frames found for monitored shortcode (404)
- General errors (500)
- Create a new Lambda function with Python 3.10 runtime
- Upload the contents of this repository
- Configure the Lambda function with appropriate memory (recommended: 512MB) and timeout (recommended: 30 seconds)
- Ensure the Lambda function has IAM permissions to access the S3 bucket