class MetamindFeaturesExtractor(ImageExtractor): ''' Uses the MetaMind API to extract features with an existing classifier. Args: api_key (str): A valid key for the MetaMind API. Only needs to be passed the first time a MetaMindExtractor is initialized. classifier (str, int): The name or ID of the MetaMind classifier to use. If None or 'general', defaults to the general image classifier. Otherwise, must be an integer ID for the desired classifier. ''' def __init__(self, api_key=None, classifier=None): ImageExtractor.__init__(self) api_key = get_api_key() if api_key is None else api_key if api_key is None: raise ValueError("A valid MetaMind API key must be passed the " "first time a MetaMind extractor is initialized.") set_api_key(api_key, verbose=False) # TODO: Can add a lookup dictionary somewhere that has name --> ID # translation for commonly used classifiers. if classifier is None: self.classifier = general_image_classifier else: self.classifier = ClassificationModel(id=classifier) def apply(self, img): data = img.data temp_file = tempfile.mktemp() + '.png' cv2.imwrite(temp_file, data) labels = self.classifier.predict(temp_file, input_type='files') os.remove(temp_file) time.sleep(1.0) # Prevents server error somewhat return Value(img, self, {'labels': labels})
# install metamind api first # run the command: pip install MetaMindApi --upgrade try: from metamind.api import ClassificationData, ClassificationModel, set_api_key except ImportError: print "Could not import metamind.api packages" # api key in your profile, might need to change it to work set_api_key( 'Authorization: Basic wC5gH0A9hi37QAQA3i5oH045ofG1jNV07FhLQ1iwe5rmIJBtET') # need classifier id, classifier has to be public classifier = ClassificationModel(id='YOUR_CLASSIFIER_ID') # change urls to image urls for ingredients we trained for print classifier.predict([ 'http://www.grubdaily.com/wp-content/uploads/2011/01/IMG_4514-copy.jpg', 'http://static.chefkoch-cdn.de/ck.de/rezepte/1/1642/103048-960x720-spaghetti-carbonara.jpg' ], input_type='urls')
set_api_key("IpdP8N0nsPmYstaqwqL1CWpPWfxxETCj5BzQWa7ANN6ChZ9PYS") #Load the classifier we trained on the 3 class Rotten Tomatoes movie reviews classifier = ClassificationModel(id=25412) #Define the file paths to our data tsv_tweets = "datasets/tweets/" tweets_out = "datasets/tweets_out/" tweet_types = ["recent"] #Loop through each folder of good, bad and recent tweets and run #the classifier on it for type in tweet_types: rel_path = tsv_tweets + type + "/" rel_out_path = tweets_out + type + "/" #Loop through each movie tweet file for movie_tweets_file in os.listdir(rel_path): movie_name = movie_tweets_file[:-4] print "Predicting... " + rel_path + movie_tweets_file #Write the output to a new file f = open(rel_out_path + movie_name + ".json", 'w') print >>f, classifier.predict(rel_path + movie_tweets_file, input_type="tsv") f.close() print " Done!"
#filter text filtered_statuses = {} for status in master.keys(): if "#cheermeup" in status.lower(): filtered_statuses[status] = master[status] elif "whenever i'm feeling" not in status.lower() and "when i'm feeling" not in status.lower() and not status.startswith("RT"): if "video" not in status.lower() and "@" not in status and "http" not in status.lower(): if "if i'm feeling" not in status.lower(): filtered_statuses[status] = master[status] #filter positivity #my_dataset = ClassificationData(private=True, data_type="text", name="Tweets") i = 0 final_tweets = {} for status in filtered_statuses.keys(): i += 1 if "#cheermeup" in status.lower(): i -= 1 final_tweets[status] = filtered_statuses[status] elif (classifier.predict(status, input_type='text'))[0]["label"] != "positive": final_tweets[status] = filtered_statuses[status] print "Query Meta# " + str(i) #printing i = 0 for status in final_tweets: i += 1 print status + "\n" print i #final_tweets is a dictionary, the keys will be the most depressing of messages, and their value is the post's id #
#!/usr/bin/python from Slicer import Slicer from metamind.api import ClassificationData, ClassificationModel, set_api_key import cv2 import sys if len(sys.argv) < 3: print ("Usage: Tester.py <key file> <picture>") exit(1) with open(sys.argv[1], "r") as apikey: key = apikey.read() key = key.rstrip() set_api_key(key) classifier = ClassificationModel(id=25011) print ("-----") s = Slicer(sys.argv[2], "out.jpg", True) s.create_slices(150) i = 0 for slic in s.slics: cv2.imwrite("CURRENT" + str(i) + ".jpg", slic) print classifier.predict(["CURRENT" + str(i) + ".jpg"], input_type="files") i += 1
from metamind.api import set_api_key, twitter_text_classifier, ClassificationData, ClassificationModel, set_api_key set_api_key("5eqwiKI50ym253djlf84VEgQptIb5odohKFpgS1SSWOdeGDzQ3") training_data = ClassificationData(private=True, data_type="text", name="RT snippets training data") training_data.add_samples() classifier = ClassificationModel(private=True, name="RT movie classifier") classifier.fit(training_data) print classifier.predict("This company is the worst and is losing money", input_type="text")
from metamind.api import ClassificationData, ClassificationModel, set_api_key from story_teller import * import os set_api_key(os.environ.get('METAMIND_KEY')) #print getPostBetweenScores((200,300), 1) #print getContentWithLabel(1) training_data = ClassificationData(private=True, data_type='text', name='hn_stories_2labels_800_samples') #training_data = ClassificationData(id=184417) labels = ('0To15', '150Plus') samples = getContentWithLabel(400, labels) training_data.add_samples(samples, input_type='text') classifier = ClassificationModel(private=True, name='HN score predictor_2labels') #classifier = ClassificationModel(id=27906) classifier.fit(training_data) randomPost = getRandomPost() prediction = classifier.predict(randomPost['content'], input_type='text') print randomPost['score'] print prediction[0]['label'], prediction[0]['confidence'] #print 'prediction of score %d is %s with confidence %f' %(randomPost['score'], prediction['label'], prediction['probability'])
data = [] ds = get_dataset(VAL_DIR) for key in ds.keys(): data.append(ds[key]) # setup metamind api_key = 'bZQv0loHZItIA6f6Nkw1vZCbyzSgrBb3wGSaQoPnCX0lOo0dAE' path = '/Users/patrickhop/Desktop/metamind/backups/' set_api_key(api_key) cf = ClassificationModel(id=40417) # split up dataset data = random.sample(data, 4000) # inference pred = cf.predict(data, input_type="files") probs = map(lambda x: x['probability'], pred) inferences = map(lambda x: x['label'], pred) # fill confusion matrix # get max-gain ims ims_and_probs = zip(data, probs, inferences) hards = filter(lambda x: x[1] <= .6 and x[1] >= .4, ims_and_probs) print '' print hards print len(hards) # find the hards, copy them into extracted folder for i in xrange(0, len(hards)): shutil.copy2(hards[i][0], '/home/patrick/last_stand/extracted/' + str(random.randint(0,100000)) + '.jpg')