Last active
October 11, 2019 19:46
-
-
Save HanClinto/844f94bfd511c1b76120b102022c79dd to your computer and use it in GitHub Desktop.
Quick Jupyter notebook to auto-tag Magic card artwork from Scryfall using Resnet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Load our classifier using Keras and pre-load wieghts trained from ImageNet | |
# c.f. https://github.com/Graystripe17/ResNet50-Demo | |
from keras.applications.resnet50 import ResNet50 | |
from keras.preprocessing import image | |
from keras.applications.resnet50 import preprocess_input, decode_predictions | |
import numpy as np | |
import glob | |
from IPython.display import Image, display | |
model = ResNet50(weights='imagenet') | |
def predict(img_path): | |
img = image.load_img(img_path, target_size=(224, 224)) | |
x = image.img_to_array(img) | |
x = np.expand_dims(x, axis=0) | |
x = preprocess_input(x) | |
preds = model.predict(x) | |
decoded = decode_predictions(preds, top=10)[0]; | |
# print('Predicted:', decoded) | |
return decoded | |
# Load ScryFall bulk data (note, must manually download this) | |
import json | |
import urllib.request | |
data = [] | |
# Download bulk data if we don't already have a local copy | |
urllib.request.urlretrieve('https://archive.scryfall.com/json/scryfall-default-cards.json', 'scryfall-default-cards.json') | |
with open('scryfall-default-cards.json') as json_file: | |
data = json.load(json_file) | |
# Loop throuch every card | |
for c in data: | |
# Grab the art_crop of every card (if it exists)... | |
if 'image_uris' in c: | |
if 'art_crop' in c['image_uris']: | |
# Cache it locally | |
localpath = "art_crop/" + c['id'] + ".jpg" | |
urllib.request.urlretrieve(c['image_uris']['art_crop'], localpath) | |
# Run predictions on the image | |
preds = predict(localpath) | |
for pred in preds: | |
# Filter out anything < 40% confidence | |
if pred[2] > 0.4: | |
# HACK: Manually filter out "Comic Book" because that's a common label for this style of artwork | |
if pred[1] != 'comic_book': | |
# If we have a valid prediction, then display the image and output the data for it. | |
display(Image(filename=localpath)) | |
print(pred[1], pred[2], c['name'], c['id']) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment