Exemple #1
0
def mobilenet_preprocess(inp_image):
    from tensorflow.keras.applications.mobilenet import preprocess_input
    image_res = inp_image.resize((224, 224))  # Input size for Mobilenet
    img_tensor = preprocessing.image.img_to_array(image_res)
    img_tensor = np.expand_dims(img_tensor, axis=0)
    inp_to_cnn = preprocess_input(img_tensor)
    return inp_to_cnn
def upload_image():
    if 'image' not in request.files:
        return render_template('ImageML.html', prediction='No posted image. Should be attribute named image')
    file = request.files['image']
    
    if file.filename =='':
        return render_template('ImageML.html', prediction = 'You did not select an image')
    
    if file and allowed_file(file.filename):
        filename = secure_filename(file.filename)
        print("***"+filename)
        x = []
        ImageFile.LOAD_TRUNCATED_IMAGES = False
        img = Image.open(BytesIO(file.read()))
        img.load()
        img  = img.resize((IMAGE_WIDTH, IMAGE_HEIGHT), Image.ANTIALIAS)
        x  = image.img_to_array(img)
        x = np.expand_dims(x, axis=0)
        x  = preprocess_input(x)
        pred = model.predict(x)
        lst =  decode_predictions(pred, top=3)
        
        items = []
        for item in lst[0]:
            items.append({'name': item[1], 'prob': float(item[2])})
        
        response = {'pred': items}
        return render_template('ImageML.html', prediction = 'I would say the image is most likely {}'.format(response))
    else:
        return render_template('ImageML.html', prediction = 'Invalid File extension')
Exemple #3
0
def generate_samples_from_file(task,
                               file_path,
                               is_train=True,
                               preprocess_fn=None,
                               batch_size=64):
    if task not in ('strokes', 'image'):
        ValueError("Task not recognized..")
    map_fn = _stack_it if task == 'strokes' else _draw_it
    while True:
        for chunk in pd.read_csv(file_path, chunksize=batch_size):
            chunk['drawing'] = [
                map_fn(json.loads(draw)) for draw in chunk['drawing'].values
            ]
            if preprocess_fn:
                _vals = chunk['drawing'].apply(preprocess_fn).values
            else:
                _vals = chunk['drawing'].values
            batch_x = np.stack(_vals)
            if preprocess_fn:
                batch_x = preprocess_input(batch_x).astype(np.float32)
            if is_train:
                batch_y = to_categorical(np.stack(chunk['word'].values),
                                         num_classes=NB_CLASSES)
                yield (batch_x, batch_y)
            else:
                yield batch_x
def process_frame(frame):
    # resize
    camera_dim = (160, 80)  # (width, height)
    frame = cv2.resize(frame, camera_dim, interpolation=cv2.INTER_AREA)

    # crop
    top_cutoff = 40  # cut off world beyond the track
    new_height = 40
    frame = frame[top_cutoff:top_cutoff + new_height, :]

    # make it square
    square_dim = (120, 120)
    frame = cv2.resize(frame, square_dim, interpolation=cv2.INTER_AREA)

    # apply car mask
    frame = cv2.bitwise_and(frame, frame, mask=car_mask)
    frame = cv2.resize(frame, (224, 224), interpolation=cv2.INTER_AREA)

    # BGR -> RGB
    frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)

    # rescale
    frame = preprocess_input(frame)
    frame = tf.expand_dims(frame, axis=0)

    return frame
 def perform_operation(self, images):
     return_list = []
     for image in images:
         image_array = np.array(image).astype('uint8')
         image_array = preprocess_input(image_array)
         return_list.append(image_array)
     return return_list
Exemple #6
0
def get_yolo_box_tfs(tfs_url, image_data, net_h, net_w, anchors, obj_thresh,
                     nms_thresh):
    image_h, image_w, _ = image_data.shape
    input = preprocess_input(image_data, net_h, net_w)

    # preprocess the input
    # run the prediction
    # TODO: get output from tfs
    input_data = np.expand_dims(image_data, axis=0)
    input_data = mobilenet.preprocess_input(input_data)

    input_data = {
        'signature_name': 'predict',
        'instances': input_data.tolist()
    }
    print(tfs_url)
    response = requests.post(tfs_url, json=input_data)
    response_data = json.loads(response.text)
    output1 = response_data['predictions'][0]['output1']
    output2 = response_data['predictions'][0]['output2']
    output3 = response_data['predictions'][0]['output3']

    outputs = [output1, output2, output3]
    boxes = []

    for i in range(len(outputs)):
        yolo_output = np.array(outputs[i])
        bboxes = decode_netout(yolo_output, anchors, obj_thresh, net_h, net_w)
        boxes += bboxes

    correct_yolo_boxes(boxes, image_h, image_w, net_h, net_w)
    do_nms(boxes, nms_thresh)

    return boxes
Exemple #7
0
def df_to_image_array_xd(df, size, lw=6, time_color=True):
    df['drawing'] = df['drawing'].apply(json.loads)
    x = np.zeros((len(df), size, size, 1))
    for i, raw_strokes in enumerate(df.drawing.values):
        x[i, :, :, 0] = draw_cv2(raw_strokes, size=size, lw=lw, time_color=time_color)
    x = preprocess_input(x).astype(np.float32)
    return x
Exemple #8
0
def upload_image():
    # check if the post request has the file part
    if 'image' not in request.files:
        return jsonify(
            {'error': 'No posted image. Should be attribute named image.'})
    file = request.files['image']

    # if user does not select file, browser also
    # submit a empty part without filename
    if file.filename == '':
        return jsonify({'error': 'Empty filename submitted.'})
    if file and allowed_file(file.filename):
        filename = secure_filename(file.filename)
        #file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
        x = []
        ImageFile.LOAD_TRUNCATED_IMAGES = False
        img = Image.open(BytesIO(file.read()))
        img.load()
        img = img.resize((IMAGE_WIDTH, IMAGE_HEIGHT), Image.ANTIALIAS)
        x = image.img_to_array(img)
        x = np.expand_dims(x, axis=0)
        x = preprocess_input(x)
        x = x[:, :, :, 0:3]
        pred = model.predict(x)
        lst = decode_predictions(pred, top=5)

        items = []
        for itm in lst[0]:
            items.append({'name': itm[1], 'prob': float(itm[2])})

        response = {'pred': items}
        print(response)
        return jsonify(response)
    else:
        return jsonify({'error': 'File has invalid extension'})
def predict_from_img(model, img):
    """
    Predict The result of the image
    """
    height, width, _ = img.shape
    # Resize image if it is not 224x224
    if height != 224 or width != 224:
        img = cv2.resize(img, (224, 224))
    # Preprocess input img on the basis of mobilenet
    pre = preprocess_input(img)
    # Reshape based on the requirement of input
    pre = np.array(pre).reshape(-1, 224, 224, 3)
    # Find Predictions
    preds = model.predict(x=pre)
    # Copy Prediction in probablity
    probs = preds
    # Find the index which has maximum probablity (0 - 9)
    preds = preds.argmax(axis=1)
    # Store the index
    ans = preds[0]
    # Store the maximum probablity
    prob = round(probs[0][ans] * 100)

    if prob > THRESH:
        return (ans, prob)

    return (None, None)
Exemple #10
0
def make_frame_predictions(video_arr):
    #Use mobilenet to generate top three predictions for each frame
    #Will take a few minutes to run
    mobilenet_model = mobilenet.MobileNet()
    video_predictions_1 = []
    video_predictions_2 = []
    video_predictions_3 = []
    for i in range(0, len(video_arr)):
        img_array = np.expand_dims(video_arr[i], axis=0)
        pImg = mobilenet.preprocess_input(img_array)
        prediction = mobilenet_model.predict(pImg)
        results = imagenet_utils.decode_predictions(prediction)
        if i == 0:
            print(results)
        video_predictions_1.append(results[0][0][1])  #, results[0][0][2]))
        video_predictions_2.append(results[0][1][1])  #, results[0][1][2]))
        video_predictions_3.append(results[0][2][1])  #, results[0][2][2]))

    #Combine into single array of tuples
    video_predictions = [None] * len(video_predictions_1)
    for i in range(0, len(video_predictions_1)):
        video_predictions[i] = [(video_predictions_1[i]),
                                (video_predictions_2[i]),
                                (video_predictions_3[i])]

    return video_predictions
Exemple #11
0
def predict(ns, cluster_ip):
    data = get_image_data()
    images = preprocess_input(data)

    payload = {
        "instances": [images[0].tolist()]
    }

    #     file_path = "./dogs_image.json"
    #     with open(file_path, 'w') as outfile:
    #         json.dump(payload, outfile)
    #     print("printed")

    # sending post request to TensorFlow Serving server
    headers = {'Host': 'imagenet.' + ns + '.' + cluster_ip + '.xip.io'}
    print(headers)
    url = PREDICT_TEMPLATE.format(cluster_ip)
    print("Calling ", url)
    r = requests.post(url, json=payload, headers=headers)
    resp_json = json.loads(r.content.decode('utf-8'))
    preds = np.array(resp_json["predictions"])
    label = decode_predictions(preds, top=1)

    plt.imshow(data[0])
    plt.title(label[0])
    plt.show()
Exemple #12
0
def preprocess_image(pil_image):
    img = pil_image.resize((224, 224))
    img = img_to_array(img)
    img = img.reshape(1, 224, 224, 3)
    img = img.astype('float32')
    img = preprocess_input(img)
    return img
Exemple #13
0
def image_to_feature(image_path, model=model):
    img = np.asarray(
        PIL.Image.open(image_path).resize((128, 128),
                                          resample=PIL.Image.BILINEAR))
    x = np.stack([img], axis=0)
    print(x)
    return model.predict(preprocess_input(x))
Exemple #14
0
def get_image_data(image_paths
                   , image_shape
                   , save_path="./dogs_image.json"):
    data = []
    # image_shape = (224, 224, 3)
    target_size = image_shape[:2]

    for path in image_paths:
        image = Image.open(path).convert('RGB')
        image = np.expand_dims(image.resize(target_size), axis=0)
        data.append(image)

    data = np.concatenate(data, axis=0)
    # print(data)

    # image procsessing
    images = preprocess_input(data)

    payload = {
        # "instances": [images[0].tolist()]
        "instances": images.tolist()
    }

    # save_path = "./dogs_image.json"
    with open(save_path, 'w') as outfile:
        json.dump(payload, outfile)

    print("image converted to json")

    return data
def upload_image():
  # check if the post request has the file part
  if 'image' not in request.files:
      return jsonify({'error':'No posted image. Should be attribute named image.'})
  file = request.files['image']

  # if user does not select file, browser also
  # submit a empty part without filename
  if file.filename == '':
      return jsonify({'error':'Empty filename submitted.'})
  if file and allowed_file(file.filename):
      filename = secure_filename(file.filename)
      #file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
      x = []
      ImageFile.LOAD_TRUNCATED_IMAGES = False
      img = Image.open(BytesIO(file.read()))
      img.load()
      img = img.resize((IMAGE_WIDTH,IMAGE_HEIGHT),Image.ANTIALIAS)    
      x = image.img_to_array(img)
      x = np.expand_dims(x, axis=0)
      x = preprocess_input(x)
      x = x[:,:,:,0:3]
      probs = model.predict(np.expand_dims(img, axis=0))
      for idx in probs.argsort()[0][::-1][:5]:
        print("{:.2f}%".format(probs[0][idx]*100), "\t", label_maps_rev[idx].split("-")[-1])

      
      return jsonify("{:.2f}%".format(probs[0][idx]*100), "\t", label_maps_rev[idx].split("-")[-1])
  else:
      return jsonify({'error':'File has invalid extension'})
Exemple #16
0
def image_generator_xd(size, batchsize, ks, lw=6, time_color=True):
    global cnt
    global augment
    while True:
        for k in np.random.permutation(ks):
            filename = os.path.join(DP_DIR, 'train_k{}.csv.gz'.format(k))
            chunk = 0
            for df in pd.read_csv(filename, chunksize=batchsize):
                df['drawing'] = df['drawing'].apply(ast.literal_eval)
                x = np.zeros((len(df), size, size, 1))
                for i, raw_strokes in enumerate(df.drawing.values):
                    x[i, :, :, 0] = draw_cv2(raw_strokes,
                                             size=size,
                                             lw=lw,
                                             time_color=time_color)
                x = preprocess_input(x).astype(np.float32)
                #print(x.shape)
                epoch_policy = my_policies[np.random.choice(len(my_policies))]

                chunk += 1
                transformed_x = []

                if False:
                    for i in range(x.shape[0]):
                        x3 = np.dstack((x[i], x[i], x[i]))
                        final_img = apply_policy(epoch_policy, x3)

                        sing_img = final_img[:, :, 0]
                        sing_img = np.reshape(sing_img, (64, 64, 1))
                        transformed_x.append(sing_img)
                else:
                    transformed_x = x
                #transformed_x = x
                y = keras.utils.to_categorical(df.y, num_classes=NCATS)
                yield np.asarray(transformed_x), y
    def call(self, inputs, training=False):
        inputs = tf.convert_to_tensor(tf.cast(inputs, tf.float32))
        inputs = mobilenet.preprocess_input(inputs)
        base_feature_maps = self.get_base_features(inputs, training=training)

        if len(self.scales) == 1:
            base_feature_maps = [base_feature_maps]

        self.feature_shapes = []
        cls_output, loc_output = [], []

        for idx in range(len(self.scales)):
            cls = self.conv_cls[idx](base_feature_maps[idx])
            loc = self.conv_loc[idx](base_feature_maps[idx])
            self.feature_shapes.append((cls.shape[1], cls.shape[2]))
            cls = layers.Reshape((-1, self.num_classes))(cls)
            loc = layers.Reshape((-1, 4))(loc)
            cls_output.append(cls)
            loc_output.append(loc)

        if len(self.scales) == 1:
            cls_output = cls_output[0]
            loc_output = loc_output[0]
        else:
            cls_output = layers.Concatenate(axis=1)(cls_output)
            loc_output = layers.Concatenate(axis=1)(loc_output)

        return layers.concatenate([cls_output, loc_output], axis=-1)
def load_data():
    # load data
    (trainX, trainY), (testX, testY) = cifar10.load_data()
    #one-hot encoding
    trainY = to_categorical(trainY)
    testY = to_categorical(testY)
    # convert from integers to floats
    train_norm = trainX.astype('float32')
    test_norm = testX.astype('float32')
    # normalize to range 0-1
    train_norm = train_norm / 255.0
    test_norm = test_norm / 255.0
    # preprocess input
    train_norm = preprocess_input(train_norm)
    test_norm = preprocess_input(test_norm)
    return (train_norm, trainY), (test_norm, testY)
Exemple #19
0
def _transform_request(request):
    request = request.decode('utf-8')
    request = unquote(request)

    # Direct http example
    if request.startswith('http'):
        request = download_image(request)
    else:
        # Slack Label Example
        request_array = request.split('&')
        print(request_array)

        result = [
            value for value in request_array if value.startswith('text=')
        ]
        if len(result) > 0:
            request = download_image(result[0][5:])
            print(request)

    predict_img = image.load_img(request, target_size=(224, 224))
    predict_img_array = image.img_to_array(predict_img)
    predict_img_array = np.expand_dims(predict_img_array, axis=0)
    predict_preprocess_img = preprocess_input(predict_img_array)

    return predict_preprocess_img
def explain(model_name, namespace, cluster_ip):
    data = get_image_data()
    images = preprocess_input(data)

    payload = {"instances": [images[0].tolist()]}

    # sending post request to TensorFlow Serving server
    headers = {
        'Host': model_name + '.' + namespace + '.' + cluster_ip + '.xip.io'
    }
    print(headers)
    url = EXPLAIN_TEMPLATE.format(cluster_ip)
    print("Calling ", url)
    r = requests.post(url, json=payload, headers=headers, timeout=36000)
    if r.status_code == 200:
        explanation = json.loads(r.content.decode('utf-8'))
        exp_arr = np.array(explanation['anchor'])

        # save
        with open("exp_output.json", 'wb') as f:
            json.dump(exp_arr, f)  # ndarray?

        f, axarr = plt.subplots(1, 2)
        axarr[0].imshow(data[0])
        axarr[1].imshow(explanation['anchor'])
        plt.show()
    else:
        print("Received response code and content", r.status_code, r.content)
Exemple #21
0
def preproceso(imagen):
    """ Devuelve una imagen preprocesada para ser procesada por la DNN"""
    tama = Config.DNN.MobileNet.img_size
    imagen = tf.cast(imagen, tf.float32)
    imagen = tf.image.resize(imagen, (tama, tama))
    imagen = mobilenet.preprocess_input(imagen)
    return imagen
Exemple #22
0
 def preprocess_image(self, image_array_nd, target_size=(224, 224)):
     image_pil = Image.fromarray(image_array_nd)
     image_pil = image_pil.resize(target_size, Image.ANTIALIAS)
     image_array = np.array(image_pil).astype('uint8')
     image_pp = preprocess_input(image_array)
     image_pp = np.array(image_pp)[np.newaxis, :]
     return image_pp
def model_predict(img, model):
    img = img.resize((224, 224))
    x = image.img_to_array(img)
    x = np.expand_dims(x, axis=0)
    x = preprocess_input(x)

    preds = model.predict(x)
    return preds
Exemple #24
0
def MobileNet(image_bytes):

    image_batch = np.expand_dims(image_bytes, axis=0)
    processed_imgs = mobilenet.preprocess_input(image_batch)
    mobilenet_features = mobilenet_extractor.predict(processed_imgs)
    flattened_features = mobilenet_features.flatten()
    # normalized_features = flattened_features / norm(flattened_features)
    return flattened_features
 def prepare_input(features):
     """
     Preprocess input features as required
     for MobileNet input layer
 """
     features = preprocess_input(features)
     features = tf.image.resize(features, size=(224, 224))
     return features
Exemple #26
0
 def convert_image(self, image_src):
     img = image.load_img('C:\\Users\\LG\\PycharmProjects\\Finger\\finger' +
                          image_src,
                          target_size=(224, 224))
     x = image.img_to_array(img)
     x = np.expand_dims(x, axis=0)
     x = preprocess_input(x)
     return x
def classify_image(filepath: Path, model: Model) -> np.ndarray:
    # preprocess image
    img = image.load_img(str(filepath), target_size=(224, 224))
    x = image.img_to_array(img)
    x = np.expand_dims(x, axis=0)
    x = preprocess_input(x)
    # predict with model
    return model.predict(x)
Exemple #28
0
def prepare_cv_image_2_keras_image(img, resolution):
    # convert the color from BGR to RGB then convert to PIL array
    cvt_image = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    im_pil = Image.fromarray(cvt_image)
    # resize the array (image) then PIL image
    im_resized = im_pil.resize(resolution)
    img_array = image.img_to_array(im_resized)
    image_array_expanded = np.expand_dims(img_array, axis=0)
    return preprocess_input(image_array_expanded)
Exemple #29
0
def classify(arg):
    #'./data/people',
    data,sample = arg.split('/')
    path = os.path.join(data,sample)
    print(path)
    dirs = os.listdir(path)
    num_test=4
    classified_list={}
    model = MobileNet(weights='imagenet')
    if os.path.exists(os.path.join('./cloth_label.txt')):
        f = open('./cloth_label.txt','r')
        categories = f.read().split()
        f.close()
    else :
        return False
    print(categories)
    for category in categories:
        classified_list[category]=0

    for dir in dirs :
        dir_path = os.path.join(data,sample,dir)
        files = os.listdir(dir_path)
        for file in files:
            name,extension = os.path.splitext(os.path.join(dir_path,file))
            if extension=='.jpg' or extension=='.png' or extension=='.jpeg':     
                src = os.path.join(dir_path,file)
                print("src : "+src)
                img = image.load_img(src,target_size=(224,224))
                x = image.img_to_array(img)
                x = np.expand_dims(x,axis=0)
                x=preprocess_input(x)
                preds = model.predict(x)
                labels = decode_predictions(preds,top=num_test)[0]
                flag =False
                
                for label in labels:
                    label = list(label)
                    #print(classified_list.get(label[1]))
                    if classified_list.get(label[1])!=None:
                        """ save_dir='./cloth_and_people'
                        shutil.move(src,dst) """
                        """ if not(os.path.isdir(os.path.join(save_dir))):
                            os.makedirs(os.path.join(save_dir))
                        dst = os.path.join(save_dir,file) """
                        flag=True
                        break
                if not(flag):
                    label = 'etc'
                    new_filename = 'etc_'+file
                    print(new_filename)
                    os.rename(os.path.join(dir_path,file),os.path.join(dir_path,new_filename))
                    """ save_dir='./etc' """
                    """ if dir =='./data/cloth_and_people':
                        save_dir = 'etc_'+str(num_test)
                    else:
                        save_dir='etc_'+str(num_test)+'_etc' """
                    """ if not(os.path.exists(save_dir)):
Exemple #30
0
def generate_crop(path):
    img = load_img(path, target_size=(224, 224))
    img_array = img_to_array(img)
    img = preprocess_input(img_array)
    # Modify this to be random
    image = random_crop(img, size=[112, 112, 3])
    # Add Padding
    cropped_padded = pad_image(image.numpy())
    return cropped_padded