Exemplo n.º 1
0
    def labelImage(self, imageURL):
        app = ClarifaiApp()
        d = app.tag_urls([imageURL])
        #output list, value
        count = 0

        for key, value in d.iteritems():
            if key == 'outputs':
                subDict = value
                break

        for i in subDict:
            for key, value in i.iteritems():
                if key == 'data':
                    subDict = value
                    break

        for key, value in subDict.iteritems():
            if key == 'concepts':
                subDict = value
                break

        for i in subDict:
            for key, value in i.iteritems():
                if key == 'name':
                    words.append(value)
                    if (len(words) % 4 == 0):
                        words.append('\n')
                    break
        return words
Exemplo n.º 2
0
def getWords(url):
    subDict = None
    words = []
    app = ClarifaiApp()
    imageURL = url
    d = app.tag_urls([imageURL])

    #output list, value
    count = 0

    for key, value in d.iteritems():
        if key == 'outputs':
            subDict = value
            break

    for i in subDict:
        for key, value in i.iteritems():
            if key == 'data':
                subDict = value
                break

    for key, value in subDict.iteritems():
        if key == 'concepts':
            subDict = value
            break

    for i in subDict:
        for key, value in i.iteritems():
            if key == 'name':
                words.append(value)
                if (len(words) % 4 == 0):
                    words.append('\n')
                break

    return (" ".join(words))
Exemplo n.º 3
0
class Clarifai():
    
    def __init__(self, api_id, api_secret):
        self.api_id = api_id
        self.api_secret = api_secret

        self.app = ClarifaiApp(self.api_id, self.api_secret)

    def tag(self, filenames):
        raw_tags = self.app.tag_files(filenames)
        res = {}
        if raw_tags['status']['description'] == 'Ok':
            tagged_files = raw_tags['outputs']
            for index, f in enumerate(tagged_files):
                tags = f['data']['concepts']
                tag_dict = {}
                for tag in tags:
                    tag_dict[tag['name']] = tag['value']
                filename = filenames[index]
                res[filename] = tag_dict
        return res

    def tag_raw(self, files):
        return self.app.tag_files(files)

    def test(self):
        res = self.app.tag_urls(['https://samples.clarifai.com/metro-north.jpg'])
        if res['status']['description'] == 'Ok':
            concepts = res['outputs'][0]['data']['concepts']
            for concept in concepts:
                print(concept['name'], concept['value'])
Exemplo n.º 4
0
def get_images_by_tag(image_urls,
                      clientId,
                      clientSecret,
                      batch_size=128,
                      n=10):
    app = ClarifaiApp(clientId, clientSecret)

    # Image data store
    # Key: tag
    # Value: list of image url and tag value
    images_by_tag = defaultdict(list)
    err_count = 0

    print "Tagging {} images...".format(len(image_urls))
    for i in range(int(math.ceil(len(image_urls) / (batch_size * 1.0)))):
        # Let Clarifai do its magic
        this_batch = image_urls[i * batch_size:(i + 1) * batch_size]
        tag_data = app.tag_urls(this_batch)

        # Parse response
        if "outputs" not in tag_data:
            print "Warning: failed on {} batch".format(i)
            err_count += len(this_batch)
        else:
            for t in tag_data["outputs"]:
                try:
                    image_url = t["input"]["data"]["image"]["url"]
                    for concept in t["data"]["concepts"]:
                        tag_name = concept["name"]
                        tag_value = concept["value"]

                        if len(images_by_tag[tag_name]) < n:
                            heapq.heappush(images_by_tag[tag_name],
                                           (tag_value, image_url))
                        elif tag_value > images_by_tag[tag_name][0]:
                            heapq.heappushpop(images_by_tag[tag_name],
                                              (tag_value, image_url))

                except KeyError:
                    err_count += 1

    if err_count != 0:
        print "Warning: Failed to processing {} images".format(err_count)

    for tag_name in images_by_tag:
        images_by_tag[tag_name] = [
            str(key[1])
            for key in sorted(images_by_tag[tag_name], reverse=True)
        ]

    return images_by_tag
Exemplo n.º 5
0
def sort_view(request):
    user = check_validation(request)
    if user:
        # user logged in
        if request.method == "POST":
            # form is submitted
            sort_form = Sort_form(request.POST)
            if sort_form.is_valid():
                upload_id = sort_form.cleaned_data.get('upload')
                #get api key
                app = ClarifaiApp(api_key='da68da3ad4504bb3a86febe60f658b5b')
                #get the response
                response_data = app.tag_urls([upload_id.image_url])

                tag_urls = []
                #savethe response
                for concept in response_data['outputs'][0]['data']['concepts']:
                    tag_urls.append(concept['name'])

                print tag_urls
                #sort the response
                if "dirty" in tag_urls:
                    sort = "dirty"
                else:
                    sort = "clean"
                #savein database

                existing_tag = sortmodel.objects.filter(upload=upload_id,
                                                        user=user).first()

                if not existing_tag:
                    #not sorted
                    sortmodel.objects.create(upload=upload_id,
                                             sort_text=sort,
                                             user=user)
                    return redirect("/wall/")
                else:
                    existing_tag.delete()
                    return redirect("/wall/")
            else:
                error_msg = sort_form.errors
                print error_msg
                return HttpResponse("Error Saving in database")
        else:
            return redirect("/wall/")
    else:
        return redirect("/login/")
Exemplo n.º 6
0
def doStuff():
    print 'ID: ' + cnf.ConfigVars['clarifai_id']
    print 'SECRET: ' + cnf.ConfigVars['clarifai_secret']
    os.environ['CLARIFAI_APP_ID'] = cnf.ConfigVars['clarifai_id']
    os.environ['CLARIFAI_APP_SECRET'] = cnf.ConfigVars['clarifai_secret']
    app = ClarifaiApp(cnf.ConfigVars['clarifai_id'],
                      cnf.ConfigVars['clarifai_secret'])
    #response = app.tag_urls(['https://samples.clarifai.com/metro-north.jpg'])
    #response = app.tag_urls(['https://s-media-cache-ak0.pinimg.com/originals/38/66/81/3866811a0fb059d23af7b522ba046d23.jpg'])
    #response = app.tag_urls(['http://demandware.edgesuite.net/sits_pod52/dw/image/v2/AAFF_PRD/on/demandware.static/-/Sites-masterCatalogTeva/default/dw101b8a41/images/grey/1003986-BLK_M1.jpg'])
    response = app.tag_urls([
        'https://s-media-cache-ak0.pinimg.com/736x/7d/c4/f0/7dc4f0009b51ac636dbc3449bdeca1b8.jpg'
    ])
    #http://demandware.edgesuite.net/sits_pod52/dw/image/v2/AAFF_PRD/on/demandware.static/-/Sites-masterCatalogTeva/default/dw101b8a41/images/grey/1003986-BLK_M1.jpg
    #https://s-media-cache-ak0.pinimg.com/736x/7d/c4/f0/7dc4f0009b51ac636dbc3449bdeca1b8.jpg
    #response = app.tag_urls(['https://www.sashapua.com/wp-content/uploads/2010/10/hooker-610x360.jpg'])
    return response
Exemplo n.º 7
0
    "C:/Users/burcu/Desktop/labels/" + folder + str(lowerlimit) + "_" +
    str(upperlimit) + ".txt", "w")
fileWithNames = open(
    "C:/Users/burcu/Desktop/labels/" + folder + str(lowerlimit) + "_" +
    str(upperlimit) + "_names.txt", "w")
batchsize = 128
for i in range(lowerlimit, upperlimit, batchsize):
    print("starting: " + str(i))
    k = min(batchsize, upperlimit - i)
    names = [None] * (k)
    urls = [None] * (k)
    for j in range(0, k):
        dir = listdir[i + j]
        names[j] = dir
        urls[j] = "http://46.101.173.29/_test/" + dir + "/w342.jpg"

    response = app.tag_urls(urls)

    for j in range(0, k):
        fileWithNames.write(names[j] + ',')
        for l in range(0, len(response['outputs'][j]['data']['concepts'])):
            label = response['outputs'][j]['data']['concepts'][l]
            file.write(label['name'] + ',' + str(label['value']))
            fileWithNames.write(label['name'] + ',' + str(label['value']))
            if (l < len(response['outputs'][j]['data']['concepts']) - 1):
                file.write(',')
                fileWithNames.write(',')
        file.write('\n')
        fileWithNames.write('\n')
file.close()
Exemplo n.º 8
0
        saved_args = cPickle.load(f)
    with open(os.path.join('save', 'words_vocab.pkl'), 'rb') as f:
        words, vocab = cPickle.load(f)
    model = Model(saved_args, True)
    with tf.Session() as sess:
        tf.global_variables_initializer().run()
        saver = tf.train.Saver(tf.global_variables())
        ckpt = tf.train.get_checkpoint_state('save')
        if ckpt and ckpt.model_checkpoint_path:
            saver.restore(sess, ckpt.model_checkpoint_path)
            return model.sample(sess, words, vocab, 50, search, 1, 1, 4)


#Get tags from Clarifai
app = ClarifaiApp()
data = app.tag_urls(
    ['https://static.pexels.com/photos/126407/pexels-photo-126407.jpeg'])
d = [a['name'] for a in data['outputs'][0]['data']['concepts']]

text_from_rnn = ""
for a in d:
    try:
        s = str(sample(a))
        text_from_rnn += s + '\n'
    except Exception:
        s = ""
text_from_rnn += s + '\n'
print text_from_rnn

orig_text = open('data/messages/input.txt').read()

model_a = markovify.Text(text_from_rnn)