示例#1
0
def predictImageClasse():

    model = MobileNet()

    img = np.asarray(Image.open('./image.jpg'))
    img = resize(img, [224, 224])
    imshow(img)
    X = np.reshape(img, [1, 224, 224, 3])

    preds = model.predict(X)

    #The 5 most likely between 1000 image net classes
    print(tabulate(decode_predictions(preds, top=5)[0], headers=['Name', 'Probability']))

    return decode_predictions(preds, top=5)[0]
 def predict(self, images, top=None):
     if top is None:
         top = self.num_classes
     images = preprocess_input(images)
     predictions = self.model.predict(images)
     labels = decode_predictions(predictions, top=top)
     return labels
示例#3
0
def predict():

    # POST
    if request.method == 'POST':

        # ファイルが読み込まれていない場合は'/predict'に戻る
        if 'file' not in request.files:
            flash('No file.')
            return redirect(url_for('predict'))

        # ファイルが読み込まれている場合はそのファイルを読み込む
        file = request.files['file']
        if file.filename == '':
            flash('No file.')
            return redirect(url_for('predict'))

        # 読み込んだファイルを処理する
        if file and is_allowed_file(file.filename):

            # 安全なファイル名を作成して画像ファイルを保存
            filename = secure_filename(file.filename)
            #filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename)
            filepath = filename
            file.save(filepath)

            # 学習済みのMobileNetをロード
            # 構造とともに学習済みの重みも読み込まれる
            model = MobileNet(weights='imagenet')
            # model.summary()

            # 引数で指定した画像ファイルを読み込む
            # サイズはVGG16のデフォルトである224x224にリサイズされる
            img = image.load_img(filepath, target_size=(224, 224))

            # 画像ファイルをサーバから削除
            os.remove(filepath)

            # 読み込んだPIL形式の画像をarrayに変換
            x = image.img_to_array(img)

            # 3次元テンソル(rows, cols, channels) を
            # 4次元テンソル (samples, rows, cols, channels) に変換
            # 入力画像は1枚なのでsamples=1でよい
            x = np.expand_dims(x, axis=0)

            # Top-5のクラスを予測する
            # VGG16の1000クラスはdecode_predictions()で文字列に変換される
            pred = model.predict(preprocess_input(x))
            top = decode_predictions(pred, top=5)[0]
            scores = pred[0]

            # 予測結果をリストに格納する
            results = []
            for i in range(5):
                score_rounddown = int(top[i][2]*1000000) / 10000.0
                results.append([top[i][1], score_rounddown])

            return render_template('result.html', results=results)

    return render_template('predict.html')
def predict():
    # initialize the data dictionary that will be returned from the
    # view
    data = {"success": False}
    # ensure an image was properly uploaded to our endpoint
    if flask.request.method == "POST":
        if flask.request.files.get("image"):
            # read the image in PIL format
            image = flask.request.files["image"].read()
            image = Image.open(io.BytesIO(image))

            # preprocess the image and prepare it for classification
            image = prepare_image(image, target=(224, 224))

            # classify the input image and then initialize the list
            # of predictions to return to the client
            preds = model.predict(image)
            results = decode_predictions(preds)
            data["predictions"] = []

            # loop over the results and add them to the list of
            # returned predictions
            for (imagenetID, label, prob) in results[0]:
                r = {"label": label, "probability": float(prob)}
                data["predictions"].append(r)

            # indicate that the request was a success
            data["success"] = True

    # return the data dictionary as a JSON response
    return flask.jsonify(data)
示例#5
0
def label_image(image, model, verbose=False):
    # Reference: https://github.com/glouppe/blackbelt/

    # convert the image pixels to a numpy array
    image = img_to_array(image)

    # reshape data for the model
    image = np.expand_dims(image, axis=0)

    # prepare the image for the VGG model
    image = preprocess_input(image)

    # predict the probability across all output classes
    yhat = model.predict(image)

    if verbose and model.output_names[0] == 'predictions':
        # convert the probabilities to class labels
        labels = decode_predictions(yhat)

        # retrieve the most likely result, e.g. highest probability
        label = labels[0][0]

        # print the classification
        print('%s (%.2f%%)' % (label[1], label[2] * 100))

    return yhat
示例#6
0
def bionic_visual_cortex():
    data = {"success": False}
    if request.method == 'POST':
        if request.files.get('file'):
            # read the file
            file = request.files['file']
            filename = file.filename
            filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename)
            file.save(filepath)

            #format image for processing
            image_size = (224, 224)
            im = keras.preprocessing.image.load_img(filepath,
                                                    target_size=image_size,
                                                    grayscale=False)
            image = prepare_image(im)

            #classify image with accuracy predictions
            global graph
            with graph.as_default():
                preds = model.predict(image)
                results = decode_predictions(preds)
                print(results)

                data["predictions"] = []
                for (imagenetID, label, prob) in results[0]:
                    r = {"label": label, "probability": float(prob)}
                    data["predictions"].append(r)
                data["success"] = True

        return jsonify(data)

    return '''
示例#7
0
def predict(image):
    model = MobileNet()

    pred = model.predict(image)
    decoded_predictions = decode_predictions(pred, top=10)
    response = 'MobileNet predictions:   ' + str(decoded_predictions[0][0:5])
    print(response)
    np.argmax(pred[0])
    return response
def predict(image):
    image = image.resize((224, 224), Image.ANTIALIAS)
    image = img_to_array(image)
    image = image.reshape((1, image.shape[0], image.shape[1], image.shape[2]))
    image = preprocess_input(image)
    yhat = model.predict(image)
    label = decode_predictions(yhat)
    label = label[0][0]
    return label[1].replace("_", " ")
示例#9
0
 def predict(self, img):
     img = image.load_img(img, target_size=(128, 128))
     img = image.img_to_array(img).reshape(1, 128, 128, 3)
     pred = self.model.predict(preprocess_input(img))
     top = decode_predictions(pred, top=5)
     desc = []
     score = []
     for i in range(5):
         desc.append(top[0][i][1])
         score.append(top[0][i][2] * 100)
     return desc, score
    def classify(self, url):
        original = self.load_resize_img(url)
        numpy_image = img_to_array(original)
        image_batch = np.expand_dims(numpy_image, axis=0)

        processed_image = preprocess_input(image_batch.copy())
        labels = []
        with self.graph.as_default():
            with self.session.as_default():
                predictions = self.model.predict(processed_image)
                labels = decode_predictions(predictions, top=5)
        return labels[0]
示例#11
0
def heatmap_for_top_pred(img_path, model, figsizeX):
    img = image.load_img(img_path, target_size=(224, 224))
    x = image.img_to_array(img)
    x = np.expand_dims(x, axis=0)
    x = preprocess_input(x)

    conv_out, preds = model.predict(x)
    decode_preds = decode_predictions(preds, top=13)

    analysed_preds = 0
    top13_preds = preds[0].argsort()[-13:][::-1]
    analyzed_class = top13_preds[analysed_preds]

    top_class_output = model.output[1][:, analyzed_class]
    last_conv_layer = model.get_layer('conv_pw_13')

    grads = K.gradients(top_class_output, last_conv_layer.output)[0]
    pooled_grads = K.mean(grads, axis=(0, 1, 2))
    iterate = K.function([model.input],
                         [pooled_grads, last_conv_layer.output[0]])
    pooled_grads_value, conv_layer_output_value = iterate([x])

    for i in range(1024):
        conv_layer_output_value[:, :, i] *= pooled_grads_value[i]

    heatmap = np.mean(conv_layer_output_value, axis=-1)

    heatmap = np.maximum(heatmap, 0)
    heatmap /= np.max(heatmap)

    heatmap = cv2.resize(heatmap, (224, 224))
    heatmap = np.uint8(255 * heatmap)

    left, up, down, right = get_bounds(heatmap, percentile=95)

    rect = patches.Rectangle((left, up), (right - left), (down - up),
                             linewidth=1,
                             edgecolor='r',
                             facecolor='none')

    fig, axes = plt.subplots(nrows=1, ncols=1, figsize=(figsizeX, figsizeX))
    axes.imshow(img, alpha=0.7)
    axes.imshow(heatmap, cmap='jet', alpha=0.3)
    left, up, down, right = get_bounds(heatmap, percentile=95)
    rect = patches.Rectangle((left, up), (right - left), (down - up),
                             linewidth=1,
                             edgecolor='r',
                             facecolor='none')
    axes.add_patch(rect)
    axes.set_title('Heat map and bounding box for prediction: ' +
                   str(decode_preds[0][analysed_preds][1]))

    return None
示例#12
0
def getPrediction(filename):

    model = MobileNet()
    image = load_img('images/' + filename, target_size=(224, 224))
    image = img_to_array(image)
    image = image.reshape((1, image.shape[0], image.shape[1], image.shape[2]))
    image = preprocess_input(image)
    yhat = model.predict(image)
    label = decode_predictions(yhat)
    label = label[0][0]
    print('%s (%.2f%%)' % (label[1], label[2] * 100))
    return label[1], label[2] * 100
示例#13
0
    def mbn(self, name):
        img_path = name
        img = image.load_img(img_path, target_size=(224, 224))
        x = image.img_to_array(img)
        x = np.expand_dims(x, axis=0)

        x = preprocess_input(x)
        features = model.predict(x)
        preds = decode_predictions(features, top=1)[0]
        english_prediction = preds[0][1]

        return dictionary[english_prediction]
示例#14
0
    def run(self):
        global not_exist
        global camera
        global rawCapture
        global graph

        with graph.as_default():
            # allow the camera to warmup
            time.sleep(0.1)

            # capture frames from camera
            for frame in camera.capture_continuous(rawCapture,
                                                   format="bgr",
                                                   use_video_port=True):
                print("read a frame")
                # grab the raw NumPy array representing the image, then initialize the timestamp
                # and occupied/unoccupied text
                image = frame.array
                # machine learning
                # resize to mobile size(224, 224)
                img = Image.fromarray(np.uint8(image))
                img = img.resize((224, 224))
                x = img
                pred_data = np.expand_dims(x, axis=0)
                preds = model.predict(preprocess_input(pred_data))
                results = decode_predictions(preds, top=1)[0]
                item = ''
                for result in results:
                    # print(result)
                    label = result[1]
                    item = label
                    accu = str(result[2])
                # gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

                # show the frame
                new_label = label + accu
                image = cv2.putText(image, new_label, (10, 25),
                                    cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 0),
                                    2)

                cv2.imshow("Frame", image)
                key = cv2.waitKey(1) & 0xFF
                # 止まるように
                if item == stop_item:
                    not_exist = False
                # clear the stream in preparation for the next frame
                rawCapture.truncate(0)

                # if the `q` key was pressed, break from the loop
                if key == ord("q"):
                    break
def predict(filepath):
    #read image
    #img = cv2.imread(filepath)
    #resize image
    #img_resize = cv2.resize(img, (224,224))

    img = image.load_img(filepath, target_size=(224, 224))
    x = image.img_to_array(img)
    x = np.expand_dims(x, axis=0)
    x = preprocess_input(x)
    preds = loaded_model.predict(x)
    preds = decode_predictions(preds, top=3)[0]
    os.remove(filepath)
    return preds
示例#16
0
def image_classification(paths):
    classification_dict = {}
    model = MobileNet(input_shape=None, alpha=1.0, depth_multiplier=1, include_top=True, weights='imagenet', input_tensor=None, pooling=None, classes=1000)
    for path in paths:
        image = mnetv2_loadimage(path)
        preds = model.predict(image)
        prediction = decode_predictions(preds, top=1)[0][0]
        img_class = prediction[1]
        img_confidence = prediction[2]
        if img_class in classification_dict:
            classification_dict[img_class].append((path, img_confidence))
        else:
            classification_dict[img_class] = [(path, img_confidence)]
    return classification_dict
示例#17
0
def mbn(name):
    print('requesting MBN...')
    dictionary = pickle.load(open('dict.p', 'rb'))
    img_path = name
    img = image.load_img(img_path, target_size=(224, 224))
    x = image.img_to_array(img)
    x = np.expand_dims(x, axis=0)

    x = preprocess_input(x)
    features = model.predict(x)
    preds = decode_predictions(features, top=1)[0]
    english_prediction = preds[0][1]

    return dictionary[english_prediction]
    def classify(self):
        self.write(
            "------------------------------------------------------------")

        self.write("classify start")
        self.write(self.filename)

        input = np.stack([self.image])

        predictions = self.model.predict(input)
        results = decode_predictions(predictions, top=5)[0]
        for result in results:
            self.write(str(result))

        self.write("classify end")
示例#19
0
def main(_):

    host, port = FLAGS.server.split(':')
    channel = implementations.insecure_channel(host, int(port))
    stub = prediction_service_pb2.beta_create_PredictionService_stub(channel)

    # Send request
    #with open(FLAGS.image, 'rb') as f:
    # See prediction_service.proto for gRPC request/response details.

    img = read_image_as_nparr_RGB(FLAGS.image, shape=(224, 224))
    img = preprocess_input(img)
    img = np.expand_dims(img, axis=0)

    for _ in range(5):
        s = time.time()

        request = predict_pb2.PredictRequest()
        request.model_spec.name = 'mobilenet-classify'
        request.model_spec.signature_name = 'predict'
        request.inputs['images'].CopyFrom(
            tf.make_tensor_proto(img, shape=img.shape)
        )

        result = stub.Predict(request, 20.0)  # 10 secs timeout
        #print result.ListFields()
        #print tf.contrib.util.make_ndarray(result.ListFields()[0][1].get('scores'))
        #print tf.contrib.util.make_ndarray(result.ListFields()[0][1].get('features')).shape
        sh = decode_predictions( np.array([result.outputs['features'].float_val]) )

        #print(result)
        print 'Total Run Time:', time.time() - s, sh


    for _ in range(5):
        s = time.time()

        request = predict_pb2.PredictRequest()
        request.model_spec.name = 'mobilenet-alpha-1-228-bottleneck'
        request.model_spec.signature_name = 'predict'
        request.inputs['images'].CopyFrom(
            tf.make_tensor_proto(img, shape=img.shape)
        )

        result = stub.Predict(request, 20.0)  # 10 secs timeout
        sh = np.array( result.outputs['features'].float_val ).shape
        #print(result)
        print 'Total Run Time:', time.time() - s, sh
示例#20
0
def upload():
    if request.method == 'POST':
        # Get the file from post request
        f = request.files['file']

        # Save the file to ./uploads
        basepath = os.path.dirname(__file__)
        file_path = os.path.join(basepath, 'uploads',
                                 secure_filename(f.filename))
        f.save(file_path)

        # Make prediction
        preds = model_predict(file_path, model)

        pred_class = decode_predictions(preds, top=5)  # ImageNet Decode
        result = str(pred_class[0][0][1])  # Convert to string
        return 'The model MobileNet predicts this image as : ' + result
    return None
def graph_reco(path):
    model = MobileNet(weights='imagenet')

    img_path = path
    img = image.load_img(img_path, 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)
    predict_list = decode_predictions(preds, top=3)[0]
    to_trans = []
    for i in predict_list:
        to_trans.append(' '.join(str(i[1]).split('_')))
    translator = tr.Youdao_translate()
    res = []
    for i in to_trans:
        res.append(translator.get_translation(i))

    return res
示例#22
0
def img_pred(image):

    # 読み込んだ画像を行列に変換
    img_array = img_to_array(image)

    # 3次元を4次元に変換
    img_dims = np.expand_dims(img_array, axis=0)

    # Top3のクラスの予測
    with graph.as_default():
        preds = model.predict(preprocess_input(img_dims))

    # imagenet_class_index.json をダウンロードし、その中から認識結果を表示
    results = decode_predictions(preds, top=3)[0]

    # resultsを整形
    result = [
        result[1] + " : " + "{:.2%}".format(result[2]) for result in results
    ]
    return result
示例#23
0
def upload():
    if request.method == 'POST':
        # Get the file from post request
        f = request.files['image']

        # Save the file to ./uploads
        basepath = os.path.dirname(__file__)
        file_path = os.path.join(
            basepath, 'uploads', secure_filename(f.filename))
        f.save(file_path)

        # Make prediction
        preds = model_predict(file_path, model)

        # Process your result for human
        # pred_class = preds.argmax(axis=-1)            # Simple argmax
        pred_class = decode_predictions(preds, top=1)   # ImageNet Decode
        result = str(pred_class[0][0][1])               # Convert to string
        return result
    return None
示例#24
0
def is_dog(img_path):
    """Predicts if image contains a dog
    
    Args:
        img_path (str): String containing path to image for inference
    
    Returns:
        bool: Returns whether the image contains a dog according to model
    """
    K.clear_session()
    img = image.load_img(img_path, target_size=(224, 224))
    x = image.img_to_array(img)
    x = np.expand_dims(x, axis=0)
    x = preprocess_input(x)
    model = MobileNet(weights='imagenet')

    preds = model.predict(x)
    preds = decode_predictions(preds, top=1)[0][0]
    print(preds)
    label = int(preds[0][2:])

    return 2085620 <= label and label <= 2113978
示例#25
0
def camera():
    global does_exist
    for frame in my_camera.capture_continuous(rawCapture,
                                              format="bgr",
                                              use_video_port=True):
        # grab the raw NumPy array representing the image, then initialize the timestamp
        # and occupied/unoccupied text
        image = frame.array

        # machine learning
        # resize to mobile net size(224, 224)
        img = Image.fromarray(np.uint8(image))
        img = img.resize((224, 224))
        x = img
        pred_data = np.expand_dims(x, axis=0)
        with graph.as_default():
            preds = model.predict(preprocess_input(pred_data))
            results = decode_predictions(preds, top=1)[0]
            for result in results:
                # print(result)
                label = result[1]
                accu = str(result[2])
            # gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

            # show the frame
            new_label = label + accu
            image = cv2.putText(image, new_label, (10, 25),
                                cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 0), 2)

        cv2.imshow("Frame", image)
        key = cv2.waitKey(1) & 0xFF

        if label == stop_item:
            does_exist = True

        rawCapture.truncate(0)
        time.sleep(video_frame_step)
        if key == ord("q"):
            break
def eval_boxes(input_image,images,coord_ini,coord_fin,N,model,model_input_shape,threshold=0.5,dic=None):
   "paso las imágenes cortadas y evaluo prob, si tiene mayor que threshold guardo coordenadas, label y prob"
   good_crops=[]
   coord=[]
   probs=[]
   time_to_resize=0
   time_to_predict=0
   time_to_preprocess=0
   time_to_decodepreds=0
   time_appending=0
   for i in range(N):
        time_1=datetime.datetime.now()
        imag_rsz = cv2.resize(images[i], (model_input_shape, model_input_shape)) #no hace big copy
        time_2=datetime.datetime.now()
        to_pred = np.expand_dims(imag_rsz, axis=0)
        to_pred = preprocess_input(to_pred)
        time_3=datetime.datetime.now()
        preds = model.predict(to_pred)
        time_4=datetime.datetime.now()
        cod,name,max_prob = decode_predictions(preds, top=1)[0][0]
        time_5=datetime.datetime.now()
        if max_prob>threshold:
            good_crops.append(images[i])
            coord.append((coord_ini[i],coord_fin[i]))
            probs.append((name,max_prob))
        time_6=datetime.datetime.now()
        time_to_resize+=(time_2-time_1).total_seconds()
        time_to_preprocess+=(time_3-time_2).total_seconds()
        time_to_predict+=(time_4-time_3).total_seconds()
        time_to_decodepreds+=(time_5-time_4).total_seconds()
        time_appending+=(time_6-time_5).total_seconds()
  # print(len(probs),'shapeprob')
   print('time resizing: ',time_to_resize)
   print('time preprocesing: ',time_to_preprocess)
   print('time predicting: ', time_to_predict)
   print('time decoding preds: ', time_to_decodepreds)
   print('time appending', time_appending)
   draw_boxes(input_image,coord,probs)
   return good_crops,coord,probs
def lambda_handler(event, context):

    try:
        bucket_name = event['Records'][0]['s3']['bucket']['name']
        file_key = event['Records'][0]['s3']['object']['key']

        print('Reading {} from S3 bucket {}'.format(file_key, bucket_name))
        obj = s3.get_object(Bucket=bucket_name, Key=file_key)

        s3Img = obj['Body'].read()
        img = image.load_img(BytesIO(s3Img), target_size=(224, 224))
        img = image.img_to_array(img)
        img = np.expand_dims(img, axis=0)
        pImg = preprocess_input(img)

        preds = model.predict(pImg)
        prediction = decode_predictions(preds, top=1)[0][0]
        print(prediction)

        result = {
            "status": "ok",
            "name": file_key,
            "result": str(prediction[1]),
            "confidence": float(prediction[2])
        }

        snsTopicArn = os.environ["snsTopicArn"]
        response = sns.publish(TargetArn=snsTopicArn,
                               Message=json.dumps(
                                   {'default': json.dumps(result)}),
                               MessageStructure='json')

        print(response)
    except Exception as e:
        print(e)
        raise e
def transform_predictions_to_api_response(predictions, model_name):
    """ Transform predictions into readable response
    Args:
        predictions: prediction object from tensorflow
        model_name: name of the model used for this predictions
    Returns:
        transformed result object as array
    """
    decoded_predictions = {}
    if model_name == 'inception_v3':
        decoded_predictions = inception_v3.decode_predictions(
            np.array(predictions['predictions']))[0]
    elif model_name == 'mobilenet':
        decoded_predictions = mobilenet.decode_predictions(
            np.array(predictions['predictions']))[0]
    elif model_name == 'mobilenet_v2':
        decoded_predictions = mobilenet_v2.decode_predictions(
            np.array(predictions['predictions']))[0]

    result = []
    for prediction in decoded_predictions:
        result.append({'label': prediction[1], 'score': prediction[2]})

    return result
示例#29
0
    def _predict_image(self, img_list, top=10):
        """
        画像判定モデルを用いて、何が写っているか判定する。
        :param img_list: 判定画像のリスト
        :param top: 上位何位まで判定するか
        :return: 検知結果 (単語ID, 名称, 判定スコア)のタプルがtopで指定された数のリストになる。
        """
        # 判定画像を配列化する。
        img_array_list = []
        for img in img_list:
            resized = img.resize((224, 224))
            img_array = image.img_to_array(resized)
            img_array = preprocess_input(img_array)
            img_array_list.append(img_array)

        # バッチサイズ分ずつ取り出し、判定にかける。
        ret = []
        for index in range(0, len(img_array_list), BATCH):
            x = np.array(img_array_list[index:index + BATCH])
            preds = self._model.predict(x)
            decoded = decode_predictions(preds, top=top)
            ret.extend(decoded)

        return ret
示例#30
0
def get_predictions(model, image):
    preds = model.predict(image)
    dec_preds = decode_predictions(preds)[0]
    _, label1, conf1 = decode_predictions(preds)[0][0]
    return label1, conf1, dec_preds
示例#31
0
rawCapture = PiRGBArray(camera, size=(640, 480))

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

for frame in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True):
    # frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    img = Image.fromarray(np.uint8(frame))
    img = img.resize((224, 224))

    img = image.img_to_array(img)

    image = np.expand_dims(image, axis=0)

    preds = model.predict(preprocess_input(image))
    results = decode_predictions(preds, top=1)[0]
    for result in results:
        # print(result)
        label = result[1]
        prob = round(result[2] * 100, 2)
    frame = cv2.putText(frame, label, (10, 25), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 0), 2)



    # show the frame
    cv2.imshow("Frame", image)
    key = cv2.waitKey(1) & 0xFF

    # clear the stream in preparation for the next frame
    rawCapture.truncate(0)