Skip to content

Instantly share code, notes, and snippets.

@jpf jpf/ Secret
Created Jun 23, 2019

What would you like to do?
import os
import requests
import percache
iteration = '<YOUR ITERATION ID HERE>'
confidance = 0.999
cache = percache.Cache('ms-predict-cache-{}'.format(iteration))
base_url = '<YOUR PROJECT ID HERE>/classify/iterations/{}/image'.format(iteration)
headers = {
'Prediction-Key': '<YOUR PREDICTION KEY HERE>',
def classify(url, filename):
files = {
'file': (f, open(filename, 'rb'))
response =, headers=headers, files=files).json()
return response
def get_tags(filename):
list_tags_cmd = '{} -N -l {}'.format(tag_cmd, filename)
tags = []
rv = os.popen(list_tags_cmd).read().strip()
if rv != '':
tags = rv.split(',')
return tags
print('Starting using {}'.format(iteration))
dir_path = 'fetch-kats/thumbnails/'
tag_cmd = '~/brew/bin/tag'
for f in os.listdir(dir_path):
if '' in f:
filename = dir_path + f
# 'fetch-kats/thumbnails/'
tags = get_tags(filename)
if len(tags) > 0:
print('.', end='', flush=True)
response = classify(base_url, filename)
predictions = {}
if 'predictions' not in response:
for prediction in response['predictions']:
predictions[prediction['tagName']] = prediction['probability']
krazy = predictions['krazy']
if krazy < confidance:
print(',', end='', flush=True)
cmd = '{} -a "Purple" {}'.format(tag_cmd, filename)
print("\n{}: {}".format(f, krazy))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.