-
-
Save jpf/ee6edf11cba7d236fcc074c1e108d097 to your computer and use it in GitHub Desktop.
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
import os | |
import requests | |
import percache | |
iteration = '<YOUR ITERATION ID HERE>' | |
confidance = 0.999 | |
cache = percache.Cache('ms-predict-cache-{}'.format(iteration)) | |
base_url = 'https://westus2.api.cognitive.microsoft.com/customvision/v3.0/Prediction/<YOUR PROJECT ID HERE>/classify/iterations/{}/image'.format(iteration) | |
headers = { | |
'Prediction-Key': '<YOUR PREDICTION KEY HERE>', | |
} | |
@cache | |
def classify(url, filename): | |
files = { | |
'file': (f, open(filename, 'rb')) | |
} | |
response = requests.post(url, headers=headers, files=files).json() | |
return response | |
@cache | |
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 'chroniclingamerica.loc.gov' in f: | |
continue | |
filename = dir_path + f | |
# 'fetch-kats/thumbnails/newspapers.com-1918-02-03-page-48-id-457845669.jpeg' | |
tags = get_tags(filename) | |
if len(tags) > 0: | |
print('.', end='', flush=True) | |
continue | |
response = classify(base_url, filename) | |
predictions = {} | |
if 'predictions' not in response: | |
continue | |
for prediction in response['predictions']: | |
predictions[prediction['tagName']] = prediction['probability'] | |
krazy = predictions['krazy'] | |
if krazy < confidance: | |
print(',', end='', flush=True) | |
continue | |
cmd = '{} -a "Purple" {}'.format(tag_cmd, filename) | |
os.system(cmd) | |
print("\n{}: {}".format(f, krazy)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment