Skip to content

Repository files navigation

Flickr Photo Tagger

Flickr Photo Tagger is a tool that retrives photos from Flickr albums and tags them according to labels determined by an Amazon Rekognition model.

Features

  • Keeps track of processed albums
  • Two built-in processing modes:
    • Batch processing: process next 10 unprocessed albums
    • Single album processing
  • Can easily be integrated into custom workflows via Step Functions
  • Chunking: breaks large albums into chunks to process which helps avoid timeouts and also helps reduce cost
  • Skips images that are too small or too large to ensure efficiency
  • Optional feature: Label-specific photo tagging - if a label is in the album name, tag all photos in that album with that label

For more about the architecture and for an example use case, see these slides.

File Structure

File Description
album_processor.py Album-related functions with methods for discovery, processing and tracking
config.py Configuration and environment setup. Environment variables and constants go here
flickr_client.py Functions for interacting with the Flickr API.
image_processor.py Functions for downloading, filtering and processing singular photos
lambda_function.py AWS Lambda function handler - main entrypoint that orchestrates all operations
rekognition_service.py All functions related to operating the AWS Rekognition model

How to Use

  1. Create an AWS Rekognition model and obtain a Flickr API key
  2. Create a new database in DynamoDB
  3. Create an AWS Lambda function (Recommended settings: 256 MB memory and 15 min timeout) and upload the files described in the previous section as the source code
  4. Configuration:
    • Configure the following environment variables for the lambda function accordingly:

      • DDB_TABLE_NAME: Database name from DynamoDB
      • FLICKR_API_KEY: Flickr API key
      • FLICKR_API_SECRET: Flickr API secret
      • FLICKR_OAUTH_TOKEN: Flickr OAuth token
      • FLICKR_OAUTH_TOKEN_SECRET: Flickr OAuth token secret
      • FLICKR_USER_ID: Flicker User ID
      • REKOGNITION_MODEL_ARN: From your rekognition model
      • REKOGNITION_PROJECT_ARN: From your rekognition model
      • TEST_ALBUM_LIMIT: # of albums to process during testing (Recommended: 3)
      • TEST_MODE: Set to true if you are testing, otherwise set to false
    • Optional: In config.py, configure label-specific photo tagging

      # Known tags for label-specific photo tagging
      COMPANY_TAGS = [] # add any labels that if it appears in the album name, all photos in the album will be tagged with that label
  5. Run the lambda function! (Note: By default, it executes batch processing)

Next Steps

1. Custom Workflows via Step Functions

Example: Processing all unprocessed photos.
Create a new state machine on AWS Step Functions with the following definition:

{
  "Comment": "Photo tagging workflow with Rekognition model management using waiters",
  "StartAt": "StartRekognitionModel",
  "States": {
    "StartRekognitionModel": {
      "Type": "Task",
      "Resource": "arn:aws:states:::lambda:invoke",
      "Parameters": {
        "FunctionName": "firstphototagger",
        "Payload": {
          "action": "start_model"
        }
      },
      "ResultPath": "$.modelStartResult",
      "Next": "GetAlbumList",
      "TimeoutSeconds": 900,
      "Comment": "Start model and wait for it to be running (up to 15 minutes)",
      "Retry": [
        {
          "ErrorEquals": [
            "States.TaskFailed"
          ],
          "IntervalSeconds": 60,
          "MaxAttempts": 2,
          "BackoffRate": 2
        }
      ]
    },
    "GetAlbumList": {
      "Type": "Task",
      "Resource": "arn:aws:states:::lambda:invoke",
      "Parameters": {
        "FunctionName": "firstphototagger",
        "Payload": {
          "action": "discover"
        }
      },
      "ResultPath": "$.albumDiscovery",
      "Next": "ProcessAlbums"
    },
    "ProcessAlbums": {
      "Type": "Map",
      "ItemsPath": "$.albumDiscovery.Payload.unprocessed_albums",
      "MaxConcurrency": 5,
      "Iterator": {
        "StartAt": "ProcessSingleAlbum",
        "States": {
          "ProcessSingleAlbum": {
            "Type": "Task",
            "Resource": "arn:aws:states:::lambda:invoke",
            "Parameters": {
              "FunctionName": "firstphototagger",
              "Payload.$": "$"
            },
            "End": true,
            "Retry": [
              {
                "ErrorEquals": [
                  "States.TaskFailed"
                ],
                "IntervalSeconds": 30,
                "MaxAttempts": 2,
                "BackoffRate": 2
              }
            ]
          }
        }
      },
      "ResultPath": "$.processingResults",
      "Next": "StopRekognitionModel"
    },
    "StopRekognitionModel": {
      "Type": "Task",
      "Resource": "arn:aws:states:::lambda:invoke",
      "Parameters": {
        "FunctionName": "firstphototagger",
        "Payload": {
          "action": "stop_model"
        }
      },
      "ResultPath": "$.modelStopResult",
      "Next": "WorkflowComplete",
      "Retry": [
        {
          "ErrorEquals": [
            "States.TaskFailed"
          ],
          "IntervalSeconds": 30,
          "MaxAttempts": 3,
          "BackoffRate": 2
        }
      ]
    },
    "WorkflowComplete": {
      "Type": "Pass",
      "Result": {
        "message": "Photo tagging workflow completed successfully",
        "model_managed": true
      },
      "End": true
    }
  }
}

2. Processing photos on a regular basis

You can use Amazon Eventbridge to run workflows to process images on a regular basis (e.g process photos once a month)

About

Automates tagging photos on Flickr

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages