def clarifai(): clarifai = ClarifaiCustomModel() concept_name = 'piyali' PIYALI_POSITIVES = [ 'http://anshulkgupta.com/hackmit/piyali1.png', 'http://anshulkgupta.com/hackmit/piyali2.png', 'http://anshulkgupta.com/hackmit/piyali3.png', 'http://anshulkgupta.com/hackmit/piyali4.png' ] for positive_example in PIYALI_POSITIVES: clarifai.positive(positive_example, concept_name) PIYALI_NEGATIVES = [ 'http://anshulkgupta.com/hackmit/anshul1.png', 'http://anshulkgupta.com/hackmit/anshul2.png', 'http://anshulkgupta.com/hackmit/annie1.png', 'http://anshulkgupta.com/hackmit/annie2.png' ] for negative_example in PIYALI_NEGATIVES: clarifai.negative(negative_example, concept_name) clarifai.train(concept_name) PIYALI_TEST = [ 'http://anshulkgupta.com/hackmit/piyali-test1.png' ] NOT_PIYALI = [ 'http://anshulkgupta.com/hackmit/annie-test1.png', 'http://anshulkgupta.com/hackmit/anshul-test1.png', 'http://anshulkgupta.com/hackmit/anshul-test2.png' ] data = [] for test in PIYALI_TEST + NOT_PIYALI: result = clarifai.predict(test, concept_name) data.append([result['status']['message'], result['urls'][0]['score'], result['urls'][0]['url']]) return render_template('clarifai.html', data=data)
def trainTextbook(textbook, textbooks): clarifai = ClarifaiCustomModel() concept_name = encode.NumericToAlpha(textbook.isbn[1:]) PHISH_POSITIVES = [ textbook.imgURL #pilexample.filterBlur(textbook.imgURL) ] for positive_example in PHISH_POSITIVES: clarifai.positive(positive_example, concept_name) PHISH_NEGATIVES = [] for t in textbooks: if t != textbook: PHISH_NEGATIVES.append(t.imgURL) for negative_example in PHISH_NEGATIVES: clarifai.negative(negative_example, concept_name) clarifai.train(concept_name) result = clarifai.predict(textbook.imgURL, concept_name) print result['status']['message'], "%0.3f" % result['urls'][0]['score'], result['urls'][0]['url'] print concept_name print decode.alphaToNumeric(concept_name)
def build(self): # instantiate clarifai client clarifai = ClarifaiCustomModel(app_id, app_secret) concept_name = 'phish' # find some positive and negative examples PHISH_POSITIVES = [ 'http://clarifai-test.s3.amazonaws.com/phish/positive/3652848536_c72244dc88_o.jpg', 'http://clarifai-test.s3.amazonaws.com/phish/positive/4840976460_8463f9f319_b.jpg', 'http://clarifai-test.s3.amazonaws.com/phish/positive/4904257471_20c0ff714f_b.jpg', 'http://clarifai-test.s3.amazonaws.com/phish/positive/4904842036_6806f5fd25_b.jpg', 'http://clarifai-test.s3.amazonaws.com/phish/positive/4904845798_aaf3392666_b.jpg', 'http://clarifai-test.s3.amazonaws.com/phish/positive/6030148539_5d6da277c0_b.jpg', 'http://clarifai-test.s3.amazonaws.com/phish/positive/9381652037_7e5e7665ab_k.jpg' ] # add the positive example images to the model for positive_example in PHISH_POSITIVES: clarifai.positive(positive_example, concept_name) # negatives are not required but will help if you want to discriminate between similar concepts PHISH_NEGATIVES = [ 'http://clarifai-test.s3.amazonaws.com/phish/negative/5587410471_cf932bf9fa_o.jpg', 'http://clarifai-test.s3.amazonaws.com/phish/negative/7367377586_f5e7c59ef8_k.jpg', 'http://clarifai-test.s3.amazonaws.com/phish/negative/8422034157_1fbe437d3a_b.jpg', 'http://clarifai-test.s3.amazonaws.com/phish/negative/8464327405_5eaf39e6e2_o.jpg', 'http://clarifai-test.s3.amazonaws.com/phish/negative/8804958484_9dcba3da19_k.jpg', 'http://clarifai-test.s3.amazonaws.com/phish/negative/8805067594_f2abc5c751_k.jpg', 'http://clarifai-test.s3.amazonaws.com/phish/negative/9583629691_a1594637a9_k.jpg' ] # add the negative example images to the model for negative_example in PHISH_NEGATIVES: clarifai.negative(negative_example, concept_name) # train the model clarifai.train(concept_name) PHISH_EXAMPLES = [ 'https://clarifai-test.s3.amazonaws.com/photo-1-11-e1342391144673.jpg', 'https://clarifai-test.s3.amazonaws.com/DSC01226-e1311293061704.jpg' ] NOT_PHISH = [ 'https://clarifai-test.s3.amazonaws.com/2141620332_2b741028b3.jpg', 'https://clarifai-test.s3.amazonaws.com/grateful_dead230582_15-52.jpg' ] # If everything works correctly, the confidence that true positive images are of Phish should be # significantly greater than 0.5, which is the same as choosing at random. The confidence that true # negative images are Phish should be significantly less than 0.5. # use the model to predict whether the test images are Phish or not for test in PHISH_EXAMPLES + NOT_PHISH: result = clarifai.predict(test, concept_name) print result['status']['message'], "%0.3f" % result['urls'][0]['score'], result['urls'][0]['url'] return Label(text='xxx')
class Trainer(object): def __init__(self, jfp): self.clarifai = ClarifaiCustomModel(app_id, app_secret) self.jdata = getAllJSONData() def train(self, negatives_threshold=1.0, dist=2): for concept in self.jdata: for url in self.jdata[concept]["resources"]: print(concept + " positive: " + url) self.clarifai.positive(url, concept) filtered = getConceptsByGPS( latitude=self.jdata[concept]["latitude"], longitude=self.jdata[concept]["longitude"], jdata=self.jdata, distance=dist) for cid, _ in filtered: for url in self.jdata[cid]["resources"]: if random() <= negatives_threshold: print(concept + " negative: " + url) self.clarifai.negative(url, cid) self.clarifai.train(concept)
from clarifai_basic import ClarifaiCustomModel from json import dumps from classifications import language concept = ClarifaiCustomModel() for model in language.keys(): print "current model to train: " + model for url in language[model]: concept.positive(url, model) #print "training url:%s on model %s" % (url, model) for key, value in language.iteritems(): if key != model: for neg_url in value: print neg_url concept.negative(neg_url, model) concept.train(model) #print "making url:%s from model: %s a negative case." %(neg_url, key) ''' for url in language['letter_c']: concept.positive(url, "letter_c") for neg_url in language['letter_a']: concept.negative(neg_url, "letter_c") for neg_url in language['letter_b']: concept.negative(neg_url, "letter_c") for neg_url in language['applause']:
except urllib2.HTTPError, e: POSITIVES.remove(i) except urllib2.URLError, e: POSITIVES.remove(i) for i in NEGATIVES: try: urllib2.urlopen(i) except urllib2.HTTPError, e: NEGATIVES.remove(i) except urllib2.URLError, e: NEGATIVES.remove(i) concept_name = 'car' for positive_example in POSITIVES: try: clarifai.positive(positive_example, concept_name) except socket.gaierror: print 'ignoring failed address lookup for: ', positive_example for negative_example in NEGATIVES: try: clarifai.negative(negative_example, concept_name) except socket.gaierror: print 'ignoring failed address lookup for: ', negative_example clarifai.train(concept_name) concept_name = 'automobile' for positive_example in POSITIVES: clarifai.positive(positive_example, concept_name) clarifai.train(concept_name) concept_name = 'auto' for positive_example in POSITIVES:
Positive_File = str(sys.argv[2]) Negative_File = str(sys.argv[3]) print "Start" # instantiate clarifai client clarifai = ClarifaiCustomModel() #[line.strip() for line in open("C:/name/MyDocuments/numbers", 'r')] # add the positive example images to the model print "Positive" for positive_example in [ line.strip() for line in (open(Positive_File)).readlines() ]: try: clarifai.positive(positive_example, Model_name) except: print "oops" print str(positive_example) # negatives are not required but will help if you want to discriminate between similar concepts print "Negative Examples" # add the negative example images to the model for negative_example in [ line.strip() for line in (open(Negative_File)).readlines() ]: try: clarifai.negative(negative_example, Model_name) except: print "oops" print str(negative_example)
for concept in jdata: allurls += jdata[concept]["resources"] LEARN = False # Percentage of negative pictures that are used for training # set to 1 for 100% NEGATIVEPERCENTAGE = 0.1 if LEARN: for concept in jdata: for url in allurls: if url in jdata[concept]["resources"]: print(concept + " positive: " + url) clarifai.positive(url, concept) else: if random() <= NEGATIVEPERCENTAGE: print(concept + " negative: " + url) clarifai.negative(url, concept) clarifai.train(concept) print("Trained " + jdata[concept]["name"] + " as " + concept) else: testurls = [ "http://static.panoramio.com/photos/large/23208353.jpg", "http://geschichtspuls.de/wp-content/uploads/2008/bmw-museum-p0044099.jpg", "http://thumbs.dreamstime.com/z/towers-frauenkirche-cathedral-church-munich-6210116.jpg", "http://static.theculturetrip.com/images/56-256156-munich-opera-festival.jpg" ] for url in testurls:
#instantiate a new Clarifai object from clarifai_basic import ClarifaiCustomModel clarifai = ClarifaiCustomModel() # Give a few positive examples and a name for the custom concept. clarifai.positive('https://upload.wikimedia.org/wikipedia/commons/0/02/Durian.jpg', 'durian') clarifai.positive('https://www.organicfacts.net/wp-content/uploads/2013/05/Banana21.jpg', 'banana') clarifai.positive('http://thumbs.dreamstime.com/z/sweet-sop-2898990.jpg', 'sweetsop') clarifai.positive('http://www.centraliowaminiclub.org/wp-content/uploads/2015/08/pie.jpg', 'pie') clarifai.positive('http://media.mercola.com/assets/images/food-facts/lychee-nutrition-facts.jpg', 'lychee') # Give a few negative examples and a name for the custom concept. clarifai.negative('https://www.organicfacts.net/wp-content/uploads/2013/05/Banana21.jpg', 'durian') clarifai.negative('https://upload.wikimedia.org/wikipedia/commons/0/02/Durian.jpg', 'banana') clarifai.negative('http://dreamatico.com/data_images/apple/apple-7.jpg', 'sweetsop') clarifai.negative('https://upload.wikimedia.org/wikipedia/commons/8/8f/NYC-Diner-Bacon-Cheeseburger.jpg', 'pie') clarifai.negative('http://www.newhealthguide.org/images/10415496/image001.jpg', 'lychee') # Train the platform to learn your custom concept. clarifai.train('durian') clarifai.train('pie') clarifai.train('banana') clarifai.train('sweetsop') clarifai.train('lychee')
# BOOK TRAINER # #import all the stuffs from clarifai_basic import ClarifaiCustomModel concept = ClarifaiCustomModel() #assign the stuffs image_url = "" tag = "" ISBN_DICT = {tag:image_url} #Provide some positive example images for concept concept.positive(image_url, tag );
__author__ = 'montanawong' from clarifai.client import ClarifaiApi from clarifai_basic import ClarifaiCustomModel concept = ClarifaiCustomModel() clarifai_api = ClarifaiApi() # assumes environment variables are set. url = "http://cdn.hitfix.com/photos/5621843/Grumpy-Cat.jpg" concept.positive(url, "nelly") concept.train('nelly') #result = #concept.predict('https://pbs.twimg.com/profile_images/616542814319415296/#McCTpH_E.jpg', 'nelly') #confidence = result['urls'][0]['score'] #print confidence #result = clarifai_api.tag_images(open('images/cat.jpg')) result = clarifai_api.tag_image_urls(url) print result
from clarifai_basic import ClarifaiCustomModel from json import dumps from classifications import language concept = ClarifaiCustomModel() image_url = 'http://i.imgur.com/m4yadmu.jpg' model = 'letter_o' concept.positive(image_url, model) concept.train(model) for word in language: if word != model: print word concept.negative(image_url, word) concept.train(word) print concept.predict(image_url, model)
from clarifai_basic import ClarifaiCustomModel import os os.environ["CLARIFAI_APP_ID"]="f_LGpdh9gta77vih9bOl-96qNU4Nbn5_x6j412N_" os.environ["CLARIFAI_APP_SECRET"]="_daBl2t7eC9nAGb-IBOdYLfm1uqoCQH6MPlvAJR1" concept = ClarifaiCustomModel() concept.positive('https://upload.wikimedia.org/wikipedia/commons/thumb/5/59/BMW_E90_Kirrinsannassa_2.jpg/280px-BMW_E90_Kirrinsannassa_2.jpg', 'car') concept.train('car')
# pprint.pprint(categories) concept = ClarifaiCustomModel() # train model for each category for cat in categories: print cat['alias'] if cat['alias'] in DONE: continue # get images to train with images = bing.image_search(query=cat['title']) for image in images: print image print images.index(image) concept.positive(image, cat['alias']) # choose random food neg_index = random.randint(0, len(categories)-1) while neg_index == categories.index(cat): neg_index = random.randint(0, len(categories)-1) # find negative examples neg_images = bing.image_search(query=categories[neg_index]['title']) for image in neg_images: print image print neg_images.index(image) concept.negative(image, cat['alias']) concept.train(cat['alias'])
import os import sys from clarifai_basic import ClarifaiCustomModel concept = ClarifaiCustomModel() filename = sys.argv[1] tag = sys.argv[2] tagArray = ['HeavyMetal','Rap', 'Alternative','Pop','Country','Classical','Electronic'] with open(filename) as f: for line in f: line = line.rstrip() print line for genre in tagArray: try: if genre == tag: print 'Yes' concept.positive(line,tag) #else: #concept.negative(line,tag) #pass except: print 'Fail' , line pass concept.train(tag)
COLOGNE_POSITIVES = [ 'http://www.pcgames.de/screenshots/original/2012/01/Koelner_Dom.jpg', 'http://www.stadt-touristen.de/wp-content/uploads/2014/05/K%C3%B6ln-K%C3%B6lner-Dom.jpg', 'https://thejarrells.files.wordpress.com/2011/06/img_2440_2.jpg', 'https://upload.wikimedia.org/wikipedia/commons/5/55/Dmthoth_Koelner_dom_001.JPG' ] COLOGNE_NEGATIVES = [ 'http://www.dresden.citysam.de/fotos-dresden-p/tagesausfluege/meissen-2.jpg', 'http://rotary1880.de/bayreuth_eremitage/bilder/Meissen5.JPG', 'http://sachsen-blogger.de/wp-content/uploads/2013/07/DSCF4292.jpg' ] for pos in COLOGNE_POSITIVES: clarifai.positive(pos, concept_name) for neg in COLOGNE_NEGATIVES: clarifai.negative(neg, concept_name) clarifai.train(concept_name) COLOGNE_EXAMPLES = [ 'https://upload.wikimedia.org/wikipedia/commons/6/65/Hasak_-_Der_Dom_zu_K%C3%B6ln_-_Bild_02_Westseite.jpg', 'https://upload.wikimedia.org/wikipedia/commons/thumb/0/03/Cologne_cathedrale_vue_sud.jpg/1024px-Cologne_cathedrale_vue_sud.jpg', 'http://www.adventuresofagoodman.com/wp-content/uploads/2012/10/Me-in-front-of-Kolner-Dam-Cologne-Germany.jpg' ] NOT_COLOGNE = [ 'https://upload.wikimedia.org/wikipedia/commons/3/39/Meissner-dom1.jpg', 'http://2.bp.blogspot.com/_I_Lf6SbZKI4/S9lrcYiNBHI/AAAAAAAACKw/119YjFzwne4/s1600/meissen%2B3.JPG'
from clarifai_basic import ClarifaiCustomModel # assumes environment variables are set. clarifai_api = ClarifaiCustomModel( "Qu6z2uKlfDgqa7Atn1HlOBa3pakRBQHflQicLNr_", "8WgQ8D4Dp9IW1JilxEfjXjgg1geq1zpaeT3P7Rk3") files = ["dent", "perfect"] for fileName in files: f = open("images/" + fileName + ".txt") urls = [url.strip() for url in f.readlines()] for url in urls: print("Training positive " + fileName +": " + url) clarifai_api.positive(url, fileName) f.close() for other in files: if other != fileName: f = open("images/" + fileName + ".txt") urls = [url.strip() for url in f.readlines()] for url in urls: print("Training negative " + other +": " + url) clarifai_api.positive(url, fileName) f.close() clarifai_api.train(fileName) print("Trained: " + fileName)
startIndex = 4 * sI searchUrl = ( "http://ajax.googleapis.com/ajax/services/search/images?v=1.0&q=" + searchTerm + "+lol&start=" + str(startIndex) ) f = fetcher.open(searchUrl) deserialized_output = json.load(f) for i in xrange(min(len(deserialized_output["responseData"]["results"]), 10)): imageUrl = deserialized_output["responseData"]["results"][i]["unescapedUrl"] googleimages.append(imageUrl) for images in googleimages: try: concept.positive(images, searchTerm) except ApiError: pass concept.train(searchTerm) print champion """ try: result = concept.predict('https://www.panerabread.com/panerabread/menu/details/smoked-ham-and-swiss-sandwich-whole.jpg/_jcr_content/renditions/smoked-ham-and-swiss-sandwich-whole.desktop.jpeg', searchTerm) except ApiError: pass print result """
if len(sys.argv) < 4: print( "syntax: python .\predict.py [URL|File] [Result State] [Good|Bad] [if Bad: expected State") exit() # assumes environment variables are set. clarifai_api = ClarifaiCustomModel( "Qu6z2uKlfDgqa7Atn1HlOBa3pakRBQHflQicLNr_", "8WgQ8D4Dp9IW1JilxEfjXjgg1geq1zpaeT3P7Rk3") possibleStates=["dent", "perfect"] url = sys.argv[1] state = sys.argv[2].lower predictResult = sys.argv[3].lower() if predictResult == "good": clarifai_api.positive(url, state) for s in possibleStates: if s != state: clarifai_api.negative(url, s) clarifai_api.train(state) if predictResult == "bad": expectedState = sys.argv[4].lower() clarifai_api.positive(url, expectedState) for s in possibleStates: if s != expectedState: clarifai_api.negative(url, s) clarifai_api.train(expectedState)