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')
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 classify(url): concept = ClarifaiCustomModel() highscore = 0 classification = None for d in data: score = concept.predict(url, d["alias"])["urls"][0]["score"] if score > 0.9: return d if score > highscore and score > 0.5: classification = d highscore = score print classification["alias"] print score return classification
def predict(url): concept = ClarifaiCustomModel() max_confidence = 0.0 classification = None for word in language.keys(): #print word result = concept.predict(url, word) confidence = result['urls'][0]['score'] print word, confidence if confidence > max_confidence: max_confidence = confidence classification = word if classification == None: return None else: return (classification, max_confidence)
def goToBookScouterURL(url_in): clarifai = ClarifaiCustomModel() probMax = 0 maxScoreISBN = "" print "Searching textbooks...." for isbn in isbnList.getISBNList(): result = clarifai.predict(url_in, encode.NumericToAlpha(isbn)) data = json.dumps(result) jdata = json.loads(data) jresults = jdata['urls'][0]['score'] #print str(jresults) + ":" + encode.NumericToAlpha(isbn) if result['urls'][0]['score'] > probMax: probMax = result['urls'][0]['score'] maxScoreISBN = isbn isbn = maxScoreISBN home = "http://bookscouter.com" ext1 = "/prices.php?isbn=" ext2 = "&searchbutton=Sell" url = home + ext1 + isbn + ext2 print "Found Textbook" print "ISBN-10 "+isbn+" with probablity: "+"%0.3f" % probMax webbrowser.open_new(url) #go to bookscouter.com
import sys from clarifai_basic import ClarifaiCustomModel concept = ClarifaiCustomModel() url = sys.argv[1] max = 0 prediction = '' tagArray = ['HeavyMetal','Rap', 'Alternative','Pop','Country','Classical','Electronic'] for genre in tagArray: #print genre result = concept.predict(url,genre) confidence = float(result['urls'][0]['score']) #if confidence > max: #max = confidence #prediction = genre print genre, 'Confidence rate: ', confidence #print prediction
from clarifai_basic import ClarifaiCustomModel import sys if len(sys.argv) < 2: print("please provide a valid URL or File Path") print("syntax: python .\predict.py [URL|File]") exit() # assumes environment variables are set. clarifai_api = ClarifaiCustomModel("Qu6z2uKlfDgqa7Atn1HlOBa3pakRBQHflQicLNr_", "8WgQ8D4Dp9IW1JilxEfjXjgg1geq1zpaeT3P7Rk3") url = sys.argv[1] result = clarifai_api.predict(url, 'perfect') perfect = result["urls"][0]["score"] result = clarifai_api.predict(url, 'dent') dent = result["urls"][0]["score"] output = {"url": url, "state": "undef", "score": 0} if dent > perfect: output["state"] = "dent" output["score"] = dent else: output["state"] = "perfect" output["score"] = perfect
from clarifai_basic import ClarifaiCustomModel import sys clarifai = ClarifaiCustomModel() result = clarifai.predict('http://comps.canstockphoto.com/can-stock-photo_csp10316699.jpg', 'test5') print result "http://pad2.whstatic.com/images/thumb/b/bb/Get-Rid-of-Skin-Moles-Step-2-Version-3.jpg/670px-Get-Rid-of-Skin-Moles-Step-2-Version-3.jpg" result = clarifai.predict("http://pad2.whstatic.com/images/thumb/b/bb/Get-Rid-of-Skin-Moles-Step-2-Version-3.jpg/670px-Get-Rid-of-Skin-Moles-Step-2-Version-3.jpg", 'test5') print result result = clarifai.predict("http://cancerousmolepictures.com/large/6/Cancerous-Mole-Pictures-1.jpg" , 'test5') print result
try: urllib2.urlopen(i) except urllib2.HTTPError, e: NEGATIVES.remove(i) except urllib2.URLError, e: NEGATIVES.remove(i) concept_name = 'people' for positive_example in POSITIVES: clarifai.positive(positive_example, concept_name) for negative_example in NEGATIVES: clarifai.negative(negative_example, concept_name) clarifai.train(concept_name) #XXXXXXXXXXXXXXXXXXX USAGE XXXXXXXXXXXXXXXXXXXXXXXXXXXXX EXAMPLES = [ 'https://blog-blogmediainc.netdna-ssl.com/SportsBlogcom/filewarehouse/37676/4b8c8d0728f0fb6e78b1071445770fab.jpg', 'http://www.africacradle.com/wp-content/uploads/2015/08/article-2441512-02650200000005DC-411_634x380.jpg' ] NOT = [ 'https://clarifai-test.s3.amazonaws.com/2141620332_2b741028b3.jpg', 'https://clarifai-test.s3.amazonaws.com/grateful_dead230582_15-52.jpg' ] for test in EXAMPLES + NOT: result = clarifai.predict(test, 'car') ans = 'ACCEPTED' if (result['urls'][0]['score']>0.7) else 'REJECTED' print result['status']['message'], "%0.3f" % result['urls'][0]['score'], ans, result['urls'][0]['url']
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: print(url) results = [] for concept in jdata: result = clarifai.predict(url, concept) results.append([concept, result["urls"][0]["score"]]) results = sorted(results, key=lambda pair: pair[1], reverse=True) pprint(results)
import sys from clarifai_basic import ClarifaiCustomModel good = False concept = ClarifaiCustomModel() filename = sys.argv[1] g = sys.argv[2] tagArray = ['HeavyMetal','Rap', 'Alternative','Pop','Country','Classical','Electronic'] file = open('ElecExcel.txt','w') file.write('{url, genre, HeavyMetal, Rap, Alternative, Pop, Country, Classical, Electronic}\n') with open(filename) as f: for line in f: good = False line = line.rstrip() try: for genre in tagArray: result = concept.predict(line,genre) good = True except: good = False if(good is True): print line file.write('{') file.write(line) file.write(', ') file.write(g) for genre in tagArray: file.write(', ') result = concept.predict(line,genre) confidence = float(result['urls'][0]['score']) file.write(repr(confidence)) file.write('}\n')
] 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' ] for test in COLOGNE_EXAMPLES + NOT_COLOGNE: result = clarifai.predict(test, concept_name) print result['status']['message'], "%0.3f" % result['urls'][0]['score'], result['urls'][0]['url']
import sys if len(sys.argv) < 2: print("please provide a valid URL or File Path") print("syntax: python .\predict.py [URL|File]") exit() # assumes environment variables are set. clarifai_api = ClarifaiCustomModel( "Qu6z2uKlfDgqa7Atn1HlOBa3pakRBQHflQicLNr_", "8WgQ8D4Dp9IW1JilxEfjXjgg1geq1zpaeT3P7Rk3") url = sys.argv[1] result = clarifai_api.predict(url, 'perfect') perfect = result["urls"][0]["score"] result = clarifai_api.predict(url, 'dent') dent = result["urls"][0]["score"] output = { "url": url, "state": "undef", "score": 0 } if dent > perfect: output["state"] = "dent" output["score"] = dent
from clarifai_basic import ClarifaiCustomModel # assumes environment variables are set. clarifai_api = ClarifaiCustomModel( "Qu6z2uKlfDgqa7Atn1HlOBa3pakRBQHflQicLNr_", "8WgQ8D4Dp9IW1JilxEfjXjgg1geq1zpaeT3P7Rk3") result = clarifai_api.predict('http://www.bodyshopzone.com/archives/collision/side_impact_1/cxeqia01apr_Equinox_tbone.jpg', 'dent') print("Dent: ") print(result) #TGhikOhhzTI/AAAAAAAAAPo/nYmF9hULrCU/s1600/IMG_1437.JPG', 'scratch') #print("Scratch: ") #print(result) result = clarifai_api.predict('http://www.bodyshopzone.com/archives/collision/side_impact_1/cxeqia01apr_Equinox_tbone.jpg', 'perfect') print("Perfect: ") print(result)
testConcept.negative('https://absolutelymindboggling.files.wordpress.com/2012/04/pepsi-bottle.jpg', TEST_TAG) testConcept.negative('http://static.caloriecount.about.com/images/medium/pepsi-diet-183174.jpg', TEST_TAG) testConcept.negative('http://couponing4you.net/wp-content/uploads/2015/07/pepsi.jpg', TEST_TAG) testConcept.negative('https://stephanieolivieri.files.wordpress.com/2014/02/pepsi-cola_1940s.png', TEST_TAG) testConcept.train(TEST_TAG) if __name__ == "__main__": conceptTrainer() #Run the custom concept trainer. done = False while not done: result = None inputURL = raw_input("Enter an image URL to test: ") if inputURL != None: result = testConcept.predict(inputURL, TEST_TAG) if result['status']['message'] == 'Success': confidenceScore = result['urls'][0]['score'] print "Your confidence score is = " + str(confidenceScore) + " Hope this makes sense!" else: print "We encountered some weird error!" else: print "ERROR IN URL!@#$%^" doneVal = raw_input("Are we done here? (y/n): ") if doneVal == 'y' or doneVal == 'Y' or doneVal == 'Yes' or doneVal == 'yes': done = True elif doneVal == 'n' or doneVal == 'N' or doneVal == 'No' or doneVal == 'no': print "All right then, let's go again!" else: print "That's not a valid input, so I'm just going to do this all over again."
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)
class Client(object): def __init__(self): self.clarifai = ClarifaiCustomModel(app_id, app_secret) self.jdata = getAllJSONData() def processImage(self, latitude, longitude, url, homelat, homelong): concept = self.__processImage(latitude, longitude, url) if concept is None: return None return self.__getInfos(concept, homelat, homelong) def __processImage(self, latitude, longitude, url): filtered = getConceptsByGPS(latitude, longitude, self.jdata, 1) results = [] for concept, _ in filtered: result = self.clarifai.predict(url, concept) results.append([concept, result["urls"][0]["score"]]) results = sorted(results, key=lambda pair: pair[1], reverse=True) if len(results) == 0: return None result = results[0] if result[1] < 0.5: return None return result[0] def processImageDebug(self, url): print("Start recognizing...") filtered = getConceptsByGPS(0, 0, self.jdata, 40000) results = [] count = len(filtered) for concept, _ in filtered: result = self.clarifai.predict(url, concept) results.append([concept, result["urls"][0]["score"]]) results = sorted(results, key=lambda pair: pair[1], reverse=True) if len(results) == 0: return None result = results[0] if result[1] < 0.5: return None return result[0] def getInfos(self, concept): target = self.jdata[concept] name = target["name"] city = target["city"] return (name, city) def __getInfos(self, concept, homelat, homelong): target = self.jdata[concept] name = target["name"] city = "NiHier" # target["city"] tlong = target["longitude"] tlat = target["latitude"] distance = distance_on_unit_sphere(homelat, homelong, tlat, tlong) return (name, distance, city)
# 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']) # Our output is the following. Your results will vary as there are some non-deterministic elements # of the algorithms used. # Success 0.797 http://phishthoughts.com/wp-content/uploads/2012/07/photo-1-11-e1342391144673.jpg # Success 0.706 http://bobmarley.cdn.junip.com/wp-content/uploads/2014/10/DSC01226-e1311293061704.jpg # Success 0.356 http://farm3.static.flickr.com/2161/2141620332_2b741028b3.jpg # Success 0.273 http://www.mediaspin.com/joel/grateful_dead230582_15-52.jpg