Exemplo n.º 1
0
 def do_GET(self):
     """
     Testing tensorflow
     """
     classify_image.run_inference_on_image('x.jpg')
     f = self.send_head()
     if f:
         self.copyfile(f, self.wfile)
         f.close()
Exemplo n.º 2
0
def handle(msg):
    if msg["photo"]:
        chat_id = msg['chat']['id']
        f = tempfile.NamedTemporaryFile(delete=True).name + ".png"
        photo = msg['photo'][-1]["file_id"]
        bot.download_file(photo, f)
        prediction = classify_image.run_inference_on_image(f)
        bot.sendMessage(chat_id, "I think this image is a %s" % prediction)
    else:
        print("no photo")
Exemplo n.º 3
0
def handle_file():
    if request.method == 'POST':
        f = request.files['file']
        im = Image.open(f)
        rgb_im = im.convert('RGB')
        rgb_im.save('tmp.jpg')
        answer = cfi.run_inference_on_image('tmp.jpg')
        print(answer)
        # print(os.path.join(os.path.dirname(__file__),'tmp.jpg'))
        return jsonify(status="OK", human=answer[0], score=str(answer[1]))
    else:
        return jsonify(status="ERROR")
Exemplo n.º 4
0
def find_objects_in_sections(image,num_crops):
    """Function that does finer grained object recognition by cropping the image into several sections"""
    r_step, c_step = image.shape[0]//num_crops, image.shape[1]//num_crops
    object_list = []
    for i in range(num_crops):
        for j in range(num_crops):
            tf.reset_default_graph()
            cropped_image = image[i*r_step:(i+1)*r_step,j*c_step:(j+1)*c_step,:]
            inf_list = ci.run_inference_on_image(cropped_image)
            if inf_list != []:
                object_list.extend(inf_list)
    return object_list
Exemplo n.º 5
0
def image_vector(img_file):
    img_data = classify_image.run_inference_on_image(img_file)
    print(img_data)
    tags = img_data['results'][0]['result']['tag']['classes']
    weights = img_data['results'][0]['result']['tag']['probs']

    vector = [0] * current_index

    for i in range(len(tags)):
        if tags[i] in tag_indices:
            vector[tag_indices[tags[i]]] = weights[i]

    return vector
Exemplo n.º 6
0
def handler(event, context):
    bucket = TEMP_BUCKET_NAME
    key = event['key']
    lambda_id = 1
    image_id = get_time_from_id(lambda_id)
    video_name = '{}{}'.format(uuid.uuid4(), key)
    download_path = '/tmp/%s' % video_name
    s3_client.download_file(bucket, key, download_path)
    log = grab_frame(download_path, image_id, logger)
    (infer, times) = run_inference_on_image(get_image_name(image_id), logger)
    cleanup_files(download_path, get_image_name(image_id))
    output_file = video_name.split(".")[0] + ".txt"
    open("/tmp/" + output_file, "w+").write(infer)
    s3_client.upload_file("/tmp/" + output_file, bucket, output_file)
    return {'output_key': output_file}
Exemplo n.º 7
0
def handler(event, context):
    bucket = TEMP_BUCKET_NAME
    key = event['key']
    lambda_id = 1
    image_id = get_time_from_id(lambda_id)
    video_name = '{}{}'.format(uuid.uuid4(), key)
    download_path = '/tmp/%s' % video_name
    s3_client.download_file(bucket, key, download_path)
    log = grab_frame(download_path, image_id, logger)
    (infer,times) = run_inference_on_image(get_image_name(image_id), logger)
    cleanup_files(download_path, get_image_name(image_id)) 
    output_file = video_name.split(".")[0] + ".txt"
    open("/tmp/" + output_file, "w+").write(infer)
    s3_client.upload_file("/tmp/" + output_file, bucket, output_file)
    return {'output_key' : output_file}
Exemplo n.º 8
0
def inference(file_name):
    try:
        predictions, top_k, top_names = run_inference_on_image(file_name)
        print(predictions)
    except Exception as ex:
        print(ex)
        return ""
    new_url = '/static/%s' % os.path.basename(file_name)
    image_tag = '<img src="%s" width="200" height="200"></img><p>'
    new_tag = image_tag % new_url
    format_string = ''
    for node_id, human_name in zip(top_k, top_names):
        score = predictions[node_id]
        format_string += '%s (score:%.5f)<BR>' % (node_id, score)
    ret_string = new_tag + format_string + '<BR>'
    return ret_string
Exemplo n.º 9
0
    def predict(self, quote):
        logger.info(
            '---------------------------PREDICT---------------------------------'
        )
        logger.info(quote.symbol)
        df = self.price_arr[quote.symbol]
        low_list = df['low'].rolling(
            window=9, center=False).min()  # pd.rolling_min(df['low'], n)
        low_list.fillna(value=df['low'].expanding(min_periods=1).min(),
                        inplace=True)
        high_list = df['high'].rolling(
            window=9, center=False).max()  # pd.rolling_max(df['high'], n)
        high_list.fillna(value=df['high'].expanding(min_periods=1).max(),
                         inplace=True)
        rsv = (df['close'] - low_list) / (high_list - low_list) * 100

        df['k'] = pd.ewma(rsv, com=2)
        df['d'] = pd.ewma(df['k'], com=2)
        df['j'] = 3.0 * df['k'] - 2.0 * df['d']

        close = [float(x) for x in df['close']]
        # prepare macd data
        df['MACD'], df['MACDsignal'], df['MACDhist'] = talib.MACD(
            np.array(close), fastperiod=12, slowperiod=26, signalperiod=9)

        #上新
        if len(df.index) < 42:
            return
        # 停牌
        if df.iloc[-3, 0] == 0 or df.iloc[-2, 0] == 0 or df.iloc[-1, 0] == 0:
            return 0
        j = df['j']
        df['norm_j'] = j.apply(lambda x: (x - j.mean()) / (j.std()))
        bar_value = df['MACDhist'] * 2

        # print(df)
        df['norm_bar'] = bar_value.apply(lambda x: (x - bar_value.mean()) /
                                         (bar_value.std()))
        pic_path = self.create_macd_j_pic(quote.symbol, df[-3:],
                                          self.window_count)

        result = run_inference_on_image(pic_path)
        logger.info(result)
        logger.info(
            '-------------------------------------------------------------------\n'
        )
        return result
Exemplo n.º 10
0
 def recognizeObject(self):
     print "getting image"
     self.getAndSaveImage(self.img_name)
     
     # classify image
     print "getting classification"
     res = classifier.run_inference_on_image(self.img_name)[0]
     print res
     
     if type(res) == dict:
         #get label with highest probabilty 
         label = res["label_name"].split(",")[0]
         prob = str(int(res["score"]*100))
         return [label,prob]            
     else:
         print "result type is not a dict"
         return None
Exemplo n.º 11
0
    def recognizeObject(self):
        print "getting image"
        self.getAndSaveImage(self.img_name)

        # classify image
        print "getting classification"
        res = classifier.run_inference_on_image(self.img_name)[0]
        print res

        if type(res) == dict:
            #get label with highest probabilty
            label = res["label_name"].split(",")[0]
            prob = str(int(res["score"] * 100))
            return [label, prob]
        else:
            print "result type is not a dict"
            return None
Exemplo n.º 12
0
 def post(self):
     file1 = self.request.files['file1'][0]
     original_fname = file1['filename']
     file_body = file1['body']
     extension = imghdr.what('', file_body)
     if extension not in ALLOWED_EXTENSIONS:
         print("Not a valid filetype")
         self.redirect("/")
     else:
         fname = ''.join(random.choice(string.ascii_lowercase + string.digits) for x in range(6))
         final_filename = fname+'.'+extension
         file_path = os.path.join(uploads_dir, final_filename)
         output_file = open(file_path, 'wb')
         output_file.write(file_body)
         output_file.close()
         img = base64.b64encode(file_body)
         values = run_inference_on_image(file_path, FLAGS)
         self.render('prediction.html', filename=final_filename, img=img, values=values)
Exemplo n.º 13
0
    def updateText(self):
        global native
        conn = sqlite3.connect('sqlitedb.db')
        c = conn.cursor()

        image = PATH_TO_IMG + str(self.timeStamp) + ".jpg"
        label, clevel = cl.run_inference_on_image(image)

        # Take the first word of the first line and translate it
        tuple = label.partition(',')
        label = str(tuple[0])

        c.execute(
            'SELECT spanishW, definition, englishP, spanishP FROM translation where (?) = englishW',
            (label, ))
        all_rows = c.fetchall()
        trans = ""
        defin = ""
        engPhrase = ""
        spnPhrase = ""
        for row in all_rows:
            trans = str(row[0])
            defin = str(row[1])
            engPhrase = str(row[2])
            spnPhrase = str(row[3])
        if spnPhrase == "espPhrase":
            spnPhrase = None
        if engPhrase == "engPhrase":
            engPhrase = None
        if native == "english":
            self.translatedWord = trans
            self.objectLabel = "Translation of \'" + label + "\': " + trans + "\nDefinition: " + defin + "\nEnglish Phrase: " + engPhrase + "\nSpanish Phrase: " + spnPhrase
        elif native == "spanish":
            self.translatedWord = label
            self.objectLabel = "Traducción de: \'" + trans + "\': " + label + "\nFrase en Español: " + spnPhrase + "\nFrase Inglesa: " + engPhrase
        CameraScreen.insertObjectIntoHistoryDB(label, clevel, trans,
                                               self.timeStamp)
        return
Exemplo n.º 14
0
 for key in image_to_question:
     print image_to_question[key][0][image_to_question[key][0][1] + 1]
     print image_to_question[key][0][len(image_to_question[key][0])-1]
 
     assert 2==1,'stop'
 '''
 dirs = os.listdir(train_image_path)
 image_list = []
 image_id = []
 for number in xrange(100):
     nos = 0
     for key in image_to_question:
         image_list.append(train_image_path+'/'+'COCO_train2014_000000'+key+'.jpg')
         image_id.append(key)
         if len(image_list) == 10:
             conv_array = classify_image.run_inference_on_image(image_list)
             sess = tf.Session()
             saver.restore(sess,'my-model-0')
             #sess.run(tf.initialize_all_variables())
             
             costs = 0
             for i in xrange(len(image_id)):
                 this_image_id = image_id[i]
                 for j in xrange(len(image_to_question[this_image_id])):
                     costs = sess.run([cost,train_step],feed_dict={conv_feature:conv_array[i],question:image_to_question[this_image_id][j][2],answer:image_to_question[this_image_id][j][-1]})[0]
                     if costs != costs:
                         print image_to_question[this_image_id][j][0]
                         print image_to_question[this_image_id][j][1]
                         print image_to_question[this_image_id][j][2 + image_to_question[this_image_id][j][1]]
                         print ss[0]
                         print image_to_question[this_image_id][j][-1]
Exemplo n.º 15
0
for msg in consumer:
    # deserialize from json
    twete = jsonpickle.decode(msg.value)

    # for each media object in tweet
    for media in twete.entities['media']:
        # base64 jpg string to bytes
        image_data = base64.b64decode(media['data'])

        # make sure image is jpeg
        if is_jpg(image_data) == False:
            print("Invalid panda {0}".format(msg.offset))
            continue

        # run tensorflow image recognition
        predictions, top_k = run_inference_on_image(image_data)

        # determine if there is a panda match, if so how good the match is (score should be > 0.5)
        for node_id in top_k:
            # giant panda = 169
            # red panda = 7
            # human_string = node_lookup.id_to_string(node_id)
            score = predictions[node_id]

            if node_id in [7, 169] and score > 0.5:
                # WE HAVE PANDA
                media['panda_node_id'] = str(node_id)

                producer.send("Panda_Image_Tweets", jsonpickle.encode(twete).encode('UTF-8'), str(twete.id).encode('UTF-8'))
                break
Exemplo n.º 16
0
import classify_image as classifier

# only required for the example image
import os
import tensorflow as tf

#FLAGS = tf.app.flags.FLAGS

if __name__ == '__main__':
    print("all labels:", classifier.all_labels())

    example_image_path = tf.app.flags.FLAGS.model_dir
    print("example_image_path:", example_image_path)
    image = os.path.join(example_image_path, 'cropped_panda.jpg')
    print(classifier.run_inference_on_image(image)[0])

    #image2 = "220px-Binturong_in_Overloon.jpg"
    #print(classifier.run_inference_on_image(image2)[0])

    #image3 = "6a010535647bf3970b016765cbf839970b-500wi.jpg"
    #print(classifier.run_inference_on_image(image3)[0])
Exemplo n.º 17
0
def run_locally(image_url):
    results = classify_image.run_inference_on_image(image_url)
Exemplo n.º 18
0
# import the necessary packages
from picamera.array import PiRGBArray
from picamera import PiCamera
import time
import cv2
import classify_image
import os

# initialize the camera and grab a reference to the raw camera capture
camera = PiCamera()
rawCapture = PiRGBArray(camera)

# allow the camera to warmup
time.sleep(0.1)

# grab an image from the camera
camera.capture(rawCapture, format="bgr")
img = rawCapture.array

BUFFER_DIR = '/var/hamster'

img = cv2.resize(img, (128, 96))

img_file = os.path.join(BUFFER_DIR, str(time.time()) + '.jpg')
cv2.imwrite(img_file, img)
classify_image.maybe_download_and_extract()
classify_image.run_inference_on_image(img_file)
#os.remove(img_file)
Exemplo n.º 19
0
def predict(image):
    ci.run_inference_on_image(image)
Exemplo n.º 20
0
import classify_image as CI
import pandas as pd
from sys import argv
'''
Takes two numerical values as input from the system
Puts them through the tensorflow classifier
Returns pandas dataframe
Merge_flag indicates if it is the last index and should merge all files together
'''

###### System arguments #######
num1 = int(argv[1])
num2 = int(argv[2])
mergeFLAG = argv[3]

###### specifies username and directory #######
username = '******'
dir_name = 'data/' + username + '/'

###### Runs the classification #######
temp = pd.read_csv('temp.csv')
temp2 = CI.run_inference_on_image(num1, num2)
temp = pd.concat([temp, temp2], ignore_index=True)
temp.to_csv('temp.csv', index=False)

###### Merges all data into one csv #######
if mergeFLAG == 'True':
    instagram = pd.read_csv(dir_name + username + '_complete.csv')
    instagram = pd.concat([instagram, temp], axis=1)
    instagram.to_csv('instagram_temp.csv', index=False)
Exemplo n.º 21
0
 def get(self, bucket_name, object_name):
     s3_img = get_file(bucket_name, object_name)
     return run_inference_on_image(s3_img.read())
Exemplo n.º 22
0
def register_account():
    access_token = request.json['token']
    user_id = request.json['user_id']
    founduser = users.find_one({"user_id": user_id})
    target = open('tokens.txt', 'a')
    target.write(
        strftime("%Y-%m-%d %H:%M:%S", gmtime()) + ' token: ' + access_token +
        ' user_id: ' + user_id + '\n')
    if not access_token:
        return 'Missing access token'

    session['access_token'] = access_token
    session['user_id'] = user_id
    if not founduser:
        api = InstagramAPI(access_token=access_token,
                           client_secret=IG_CLIENT_SECRET)

        recent_media, next_ = api.user_recent_media(user_id=user_id, count=20)

        tags = []
        num_likes = []
        dates = []
        time_of_day = []
        weights = []

        global recurring
        global pictures
        # Convert all images to vectors
        for media in recent_media:
            img_data = classify_image.run_inference_on_image(
                media.images['standard_resolution'].url)
            tags.append(img_data['results'][0]['result']['tag']['classes'])
            weights.append(img_data['results'][0]['result']['tag']['probs'])
            recurring.append(
                img_data['results'][0]['result']['tag']['classes'])
            date = int(media.created_time.strftime("%s")) * 1000
            dates.append(date)
            time_of_day.append(media.created_time.hour)
            num_likes.append(media.like_count)
            pictures.append([
                media.images['standard_resolution'].url,
                img_data['results'][0]['result']['tag']['classes']
            ])
        # Dictionary to store indices of tags
        global tag_indices
        # Iterator for indices
        global current_index

        for vector in tags:
            for tag in vector:
                if tag not in tag_indices:
                    tag_indices[tag] = current_index
                    reverse_tag_indices.append(tag)
                    current_index += 1

        data = []

        # Importances of tags
        tag_scores = {}
        # Number of images in which tags appear
        tag_count = {}

        # Generate vectors for each image by marking each tag with weight
        for i in range(len(tags)):
            vector = [0] * current_index

            for j in range(len(tags[i])):
                vector[tag_indices[tags[i][j]]] = weights[i][j]

                if tags[i][j] in tag_scores:
                    tag_scores[tags[i][j]] += weights[i][j] * num_likes[i]
                    tag_count[tags[i][j]] += 1
                else:
                    tag_scores[tags[i][j]] = weights[i][j] * num_likes[i]
                    tag_count[tags[i][j]] = 1

            # Append extra variables and number of likes
            vector.append(dates[i])
            vector.append(time_of_day[i])
            vector.append(num_likes[i])

            data.append(vector)

        # Divide tag scores by number of pictures they appear in
        for key, value in tag_scores.iteritems():
            # Maximize score if tag is in 5 pictures
            tag_scores[key] /= 5 + abs(5 - tag_count[key])
        sorted_tag_scores = sorted(tag_scores.items(),
                                   key=operator.itemgetter(1),
                                   reverse=True)

        # Top ten most important tags
        top_ten_tags = []
        for elem in sorted_tag_scores[:10]:
            top_ten_tags.append([elem[0], elem[1]])
        userid = users.insert_one({
            'user_id': user_id,
            'recurring': recurring,
            'topten': top_ten_tags,
            'pictures': pictures,
            'data': data,
            'current_index': current_index,
            'tag_indices': tag_indices
        })

    toreturn = users.find_one({"user_id": user_id})
    response = json.dumps(toreturn,
                          sort_keys=True,
                          indent=4,
                          default=json_util.default)
    pictures = []
    recurring = []
    top_ten_tags = []
    sorted_tags = []

    global model
    model = LikePredictor(toreturn['data'])
    return response
Exemplo n.º 23
0
def lookup_image(image_file_path='image.jpg'):
    classify_image.maybe_download_and_extract()
    classify_image.run_inference_on_image(image_file_path)
Exemplo n.º 24
0
with open("./fall11_urls_first_2000.txt","r") as f:
    whole_file = f.read().split("\n")
    

ltime = []
for idx_image in range(number_images):
    cimage_url = whole_file[0].split("\t")[1]
    cimage_label = whole_file[0].split("\t")[0]
    print(cimage_url)
    
    tic()
    #url = url_base + cimage_url

    #r = requests.post(url)
    classify_image.run_inference_on_image(cimage_url)
    #dd  = r.json()
    ltime.append(toc_time())
    print(ltime)
    
    #print(idx_image, dict_labels[cimage_label.split("_")[0]], dd)
    
if False:
    """
    First attempt, in case that the graph needs to be generated again
    """
    ltime = [26.585001230239868, 5.283040523529053, 17.73598027229309, 2.593003749847412, 11.248939037322998, 2.8200619220733643, 16.670985221862793, 3.09599232673645, 11.502059698104858, 4.071980237960815, 5.934020519256592, 9.711987972259521, 3.0849857330322266, 9.33296251296997, 14.225001096725464, 2.841944694519043, 11.897971153259277, 8.175994634628296, 9.545000314712524, 9.329044342041016, 3.074040412902832, 16.126007795333862, 7.4029624462127686, 11.5600426197052, 3.250026226043701, 10.976969957351685, 3.347991943359375, 7.9989013671875, 16.665969133377075, 12.187048435211182, 3.6619510650634766, 9.953018426895142, 4.9029927253723145, 7.4029929637908936, 19.406497955322266, 3.731994390487671, 18.588664293289185, 9.653867244720459, 6.47782039642334, 19.050623893737793, 3.897918462753296, 19.40959143638611, 3.9949188232421875, 13.648678302764893, 3.9149134159088135, 11.525783777236938, 16.96566653251648, 12.292791366577148, 16.960097551345825, 4.2869017124176025]

plt.plot(ltime,'x-')
plt.xlabel("Attempt")
plt.ylabel("Time in seconds")
Exemplo n.º 25
0
import core
import classify_image
import time
import json

run = True
while (run):

    while (run):
        message = core.checkTasks()
        if "task" in message:
            #core.downloadImage(message["task"])
            string, score = classify_image.run_inference_on_image(
                "images/" + str(message["task"]) + ".jpg")
            result = {
                "task": message["task"],
                "result": string,
                "score": float(score)
            }
            print(result)

            core.sendMessageToFrontend(json.dumps(result))
        else:
            time.sleep(10)
Exemplo n.º 26
0
def predict_top_5(image_url):
    print("Tensorflow is processing the image...")
    return run_inference_on_image(image_url)
Exemplo n.º 27
0
import classify_image as inception
from os import remove
from os import listdir
from os.path import isfile, join
import shutil
import sys
import augment as aug
import types
import random

image_dir = 'data/cat'
images = [
    f for f in listdir(image_dir)
    if isfile(join(image_dir, f)) and not f.startswith('.') and (
        (join(image_dir, f).endswith('.jpg')
         or join(image_dir, f).endswith('.jpeg')))
]

for i, img in enumerate(images):
    file_path = join(image_dir, img)
    description = ''

    with open(file_path, 'rb') as f:
        description, score = inception.run_inference_on_image(f.read())

    if 'cat' not in description:
        remove(file_path)
    else:
        shutil.move(file_path, join(image_dir, 'checked'))

print 'done cleaning dataset'
Exemplo n.º 28
0
import classify_image as classifier

# only required for the example image
import os
import tensorflow as tf

#FLAGS = tf.app.flags.FLAGS

if __name__ == '__main__':
  print("all labels:", classifier.all_labels())

  example_image_path = tf.app.flags.FLAGS.model_dir
  print("example_image_path:", example_image_path)
  image = os.path.join(example_image_path, 'cropped_panda.jpg')
  print(classifier.run_inference_on_image(image)[0])

  #image2 = "220px-Binturong_in_Overloon.jpg"
  #print(classifier.run_inference_on_image(image2)[0])

  #image3 = "6a010535647bf3970b016765cbf839970b-500wi.jpg"
  #print(classifier.run_inference_on_image(image3)[0])