November 14, 2017

Build A Command Line App That Sorts Your Photos by Quality

Table of Contents:

With our new Landscape Quality and Portrait Quality models, photographers can now quickly identify which images are of high quality and prioritize working on them while leaving the rest for later. In this post, we’ll walk through building a command line application that will sort all of your photos by quality using these models.

We’ve recently introduced two new public models that are specialized in recognizing photo quality. With camera memory ever increasing, the quantity of photos being taken is skyrocketing. With our new Landscape Quality and Portrait Quality models photographers can now quickly identify which images are of high quality and prioritize working on them while leaving the rest for later. In this post we’ll walk through building a command line application that will sort all of your photos by quality using these models.

Clarifai API

Before we begin let’s break down how our visual recognition technology makes inferences on photo quality. Our Data Strategists trained these models to look at photos with strict criteria:

Poor quality photo attributes:

  • Severe chromatic aberration
  • Red eyes
  • Extremely backlit
  • Unnatural vignetting, often digitally added
  • Check out the Model Gallery for more photo quality attributes!

Good quality photo attributes:

 

    • Good lighting
    • Sharp and in focus
    • If retouching is present, it is not obvious (no completely airbrushed skin)
    • Not too much grain/noise (**unless it’s the artist’s intention)
  • Check out the Model Gallery for more photo quality attributes!

What you need to begin

  1. A Clarifai API Key. Sign up for free if you don’t have one.
  2. A dataset of images to sort. The ones I’m using are available here.
  3. The Python CLI library called Click. Install it by running ‘pip install click‘ from your terminal.

Let’s get started!

Our app will consist of two files. The first will be isort.py where our logic lives and the second will be setup.pywhich will help us use the program we’re writing as a command line tool.

Let’s start by writing a simple CLI that just outputs “hello world”. Once we have that setup we’ll add logic for it to accept parameters and call Clarifai.

isort.py

import click

@click.command()
def quality():
    click.echo("Hello World")

if __name__ == '__main__':
    quality()

setup.py

from setuptools import setup

setup(
    name="isort",
    version='0.1',
    py_modules=['isort'],
    install_requires=[
        'Click',
        'Clarifai',
    ],
    entry_points='''
        [console_scripts]
        isort=isort:quality
    ''',
)

The first file isort.py is fairly straightforward. We start by importing the Click library. We then wrap our function in `@click.command` decorator. This converts function quality(): into a command that can be invoked from the command line. It is also added to  Click’s help interface.

For setup.py we can specify the name of our app as well as version and requirements. This is useful when packaging your program to be distributed in PyPI. Click also looks at the `entry_points` field to see which function is the starting point.

After saving the two files you can run the program by entering the following in your terminal:

$ virtualenv tutorial
$ source tutorial/bin/activate .
$ pip install --editable .
$ isort

You should see Hello World as the response!

Adding Computer Vision with Clarifai

Next we’ll modify isort.py to add the Clarifai Python library. We’ll also add the ability to accept the image directory path as an argument.

import click
from clarifai.rest import ClarifaiApp
from clarifai.rest import Image as ClImage
import os
import json

app = ClarifaiApp(api_key='your_api_key')

@click.command()
@click.argument('path', type=click.Path(exists=True), required=True)
def quality(path):
    """Organizes photos based on quality."""
    click.echo("Hello World")
if __name__ == '__main__':
    quality()

We’ve created an instance of `ClarifaiApp` which will be used to make API requests. We also added Click’s argument handler which will ensure that a valid file path is passed in.

Adding logic to sort images with Clarifai

Finally let’s add the code for interacting with Clarifai and sorting the images by quality.

import click
from clarifai.rest import ClarifaiApp
from clarifai.rest import Image as ClImage
import os
import json

app = ClarifaiApp(api_key='your_api_key')

@click.command()
@click.argument('path', type=click.Path(exists=True), required=True)
def quality(path):
    model = app.models.get('Landscape Quality')
    """Organizes photos based on quality."""
    if not os.path.exists(path+"/"+'good'):
        os.mkdir(path+"/"+"good")
    if not os.path.exists(path+"/"+'bad'):
        os.mkdir(path+"/"+"bad")

    for filename in os.listdir(path):
        if filename.endswith(".png") or filename.endswith(".jpg") or filename.endswith("jpg") or filename.endswith("JPG"):
            image = ClImage(file_obj=open(path+"/"+filename, 'rb'))
            jsonResp = model.predict([image])
            goodphoto = jsonResp['outputs'][0]['data']['concepts'][0]['name']
            if goodphoto =='high quality':
               os.rename(path+"/"+filename, path+"/good/"+filename)
               print filename+": Good Photo"
            elif goodphoto =='low quality':
                os.rename(path+"/"+filename, path+"/bad/"+filename)
                print filename+": BAD Photo"

if __name__ == '__main__':
    quality()

We traverse through all of the image files in the given path and send them through the Landscape Print Qualitymodel. Then we grab the output result and check if it is `1` which denotes a high quality photo. Finally we  move the file into the respective folder and print out a message to the terminal.

We’ve built a command line application using Click that will sort images by photo quality in a flash. For next steps, you can swap the model we’re using to the Portrait Photo Quality recognition model, any pre-built model from our model gallery, or any model you’ve custom trained.

Artificial Intelligence Glossary, 2021 Edition