Basic image recognition with Tribune photos

Based on the Tensorflow image recognition tutorial. This uses Inception-v3 to classify images according to these 1000 classes.

In [ ]:
import os
from urllib.parse import urlparse
import pandas as pd
import requests
from IPython.display import display, HTML
In [ ]:
# Load Tribune images data
df = pd.read_csv('')
In [ ]:
# Download and unpack the pretrained model
In [ ]:
def select_images(sample):
    images = []
    rows = df.sample(sample)
    for img_id in list(rows['images']):
        img_url = '{0}-500.jpg'.format(img_id)
        images.append((img_id, img_url))
    return images

def download_image(img_url):
    current_dir = os.getcwd()
    parsed = urlparse(img_url)
    filename = os.path.join(current_dir, os.path.basename(parsed.path))
    response = requests.get(img_url, stream=True)
    with open(filename, 'wb') as fd:
        for chunk in response.iter_content(chunk_size=128):
    return filename

def recognise_images(sample=10):
    images = select_images(sample)
    for img_id, img_url in images:
        filename = download_image(img_url)
        display(HTML('<image src="{}"><br><a target="_blank" href="{}&embedded=true&toolbar=false">More details at SLNSW</a>'.format(img_url, img_id)))
        %run --image_file $filename
        #%run --image $filename
In [ ]:
In [ ]: