예제 #1
0
def load_images_test(name, size):
    x_images = []
    y_images = []
    for file in tqdm(os.listdir(name)):
        image = Image.open(name + file)
        if image.mode != "RGB":
            image.convert("RGB")
        x_image = image.resize(
            (size[0] // 2,
             size[1] // 2))  #change to 3, 4, or 8 for varying scale factors
        x_image = x_image.resize(size, Image.BICUBIC)
        x_image = np.array(x_image)
        y_image = image.resize(size)
        y_image = np.array(y_image)
        x_images.append(x_image)
        y_images.append(y_image)
    x_images = np.array(x_images)
    y_images = np.array(y_images)
    x_images = x_images / 255
    y_images = y_images / 255
    x_images = x_images.reshape(
        x_images.shape[0], size[0] // 1, size[1] // 1,
        3)  #for grayscale, change the number of channels to 1
    y_images = y_images.reshape(
        y_images.shape[0], size[0] // 1, size[1] // 1,
        3)  #for grayscale, change the number of channels to 1
    return x_images, y_images
예제 #2
0
def load_images_train(name, size):
    x_images = []
    y_images = []
    for file in tqdm(os.listdir(name)):
        image = Image.open(name + file)
        if image.mode != "RGB":  # comment these lines if using grasycale images
            image.convert(
                "RGB")  # comment these lines if using grasycale images
        #train for multiple scale factors
        x_image1 = image.resize((size[0] // 2, size[1] // 2))
        x_image1 = x_image1.resize(size, Image.BICUBIC)
        x_image2 = image.resize((size[0] // 3, size[1] // 3))
        x_image2 = x_image2.resize(size, Image.BICUBIC)
        x_image3 = image.resize((size[0] // 4, size[1] // 4))
        x_image3 = x_image3.resize(size, Image.BICUBIC)
        x_image4 = image.resize((size[0] // 8, size[1] // 8))
        x_image4 = x_image4.resize(size, Image.BICUBIC)

        x_image1 = np.array(x_image1)
        y_image1 = image.resize(size)
        y_image1 = np.array(y_image1)
        x_images.append(x_image1)
        y_images.append(y_image1)

        x_image2 = np.array(x_image2)
        y_image2 = image.resize(size)
        y_image2 = np.array(y_image2)
        x_images.append(x_image2)
        y_images.append(y_image2)

        x_image3 = np.array(x_image3)
        y_image3 = image.resize(size)
        y_image3 = np.array(y_image3)
        x_images.append(x_image3)
        y_images.append(y_image3)

        x_image4 = np.array(x_image4)
        y_image4 = image.resize(size)
        y_image4 = np.array(y_image4)
        x_images.append(x_image4)
        y_images.append(y_image4)

    x_images = np.array(x_images)
    y_images = np.array(y_images)
    x_images = x_images / 255
    y_images = y_images / 255
    x_images = x_images.reshape(
        x_images.shape[0], size[0] // 1, size[1] // 1,
        3)  #for grayscale, change the number of channels to 1
    y_images = y_images.reshape(
        y_images.shape[0], size[0] // 1, size[1] // 1,
        3)  #for grayscale, change the number of channels to 1
    return x_images, y_images
예제 #3
0
def adjust(img, boxes, gray=False):
    #TODO https://www.jianshu.com/p/05374b86e85b
    if gray:
        images = np.zeros([boxes, shape[0], 32, 320, 1])
    else:
        images = np.zeros([boxes.shape[0], 32, 320, 3])
    t_boxes = boxes.copy()
    for i, box in enumerate(t_boxes):
        # 原图的四个角点    661,449,731,467,725,488,656,469
        pts1 = box.astype('float32')
        # 变换后分别在左上、右上、左下、右下四个点
        w = box[:, 0].max() - box[:, 0].min()
        h = box[:, 1].max() - box[:, 1].min()

        pts2 = np.float32([[0, 0], [w, 0], [w, h], [0, h]])
        # 生成透视变换矩阵
        M = cv2.getPerspectiveTransform(pts1, pts2)
        # 进行透视变换
        dst = cv2.warpPerspective(img, M, (w, h))
        h, w, _ = dst.shape

        n_w = int(w * (32 / h) + 0.5)
        if n_w > 320: n_w = 320
        dst = Image.fromarray(dst)
        image = dst.resize([n_w, 32])
        # print(image.size)
        if gray:
            image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
            image = image.convert('L')
        image = np.asarray(image)
        images[i, :, :n_w, :] = image
    return images
예제 #4
0
def upload_file():
    if request.method == 'POST':
        if 'file' not in request.files:
            print('ファイルがアプロードされていません')
            return redirect(request.url)
        file = request.files['file']
        if file.filename == '':
            print('ファイルがアプロードされていません')
            return redirect(request.url)
        if file and allowed_file(file.filename):
            filename = secure_filename(file.filename)
            file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))

            filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename)

            model = load_model('models/cloudcnn.h5')
            model = model_from_json(open('models/cloudmodel.json').read())

            image = Image.open(filepath)
            image = image.convert('RGB')
            image = image.resize((image_size, image_size))
            data = np.asarray(image)
            X = []
            X.append(data)
            X = np.array(X)

            result = model.predict([X])[0]
            predicted = result.argmax()
            percentage = int(result[predicted] * 100)

            return flash("その雲はおそらく" + classes[predicted] + ",で、その確率は" +
                         str(percentage) + "%ぐらいです!")
예제 #5
0
def preprocess_image(image,target_size):
    if image.mode !="RGB":
        image=image.convert("RGB")
    image = image.resize(target_size)
    image = img_to_array(image)
    image = np.expand_dims(image,axis=0)
    return image
예제 #6
0
def predict():
    message = request.get_json(force=True)
    img = message['image']
    names = [
        "Bread", "Dairy product", "Dessert", "Egg", "Fried food", "Meat",
        "Noodles/Pasta", "Rice", "Seafood", "Soup", "Vegetable/Fruit"
    ]
    #img = image.load_img(img, target_size=(224, 224))

    message = request.get_json(force=True)
    encoded = message['image']
    decoded = base64.b64decode(encoded)
    img = Image.open(io.BytesIO(decoded))
    if img.mode != "RGB":
        img = image.convert("RGB")
    img = img.resize((224, 224))
    x = img_to_array(img)
    x = np.expand_dims(x, axis=0)
    x = preprocess_input(x)
    print(x.shape)
    with g.as_default():
        preds = model.predict(x)
    import operator
    index, value = max(enumerate(max(preds)), key=operator.itemgetter(1))
    print('Predicted:', names[index], value)
    prediction, value = names[index], value
    print(prediction)
    response = {'prediction': {'prediction': prediction, 'value': str(value)}}
    return jsonify(response)
예제 #7
0
def predict():
    # initialize the data dictionary that will be returned from the view
    response = {"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))
            image = image.convert("RGB")
            image = image.resize((image_w, image_h))

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

            pred = model.predict(image)
            index = np.argmax(pred)
            pred_value = foods_sorted[index]

            response["predictions"] = pred_value
            response["success"] = True

    # return the data dictionary as a JSON response
    return flask.jsonify(response)
예제 #8
0
    def char_detect(self, img_path):
        ''' Run tesseract ocr on an image supplied
            as an image path.
        '''
        with PyTessBaseAPI() as ocr_api:
            with Image.open(img_path) as image:
                # fill image alpha channel if it exists
                if image.mode in ('RGBA', 'LA'):
                    image = self.strip_alpha_channel(image)
                
                # will need a better preprocessing approach here
                # if we stay with tesseract:
                image = image.convert('L')
                # image = image.filter(ImageFilter.SHARPEN)
                # image = image.filter(ImageFilter.EDGE_ENHANCE)


                ocr_api.SetImage(image)
                raw_chars = ocr_api.GetUTF8Text()
                # char_confs = ocr_api.AllWordConfidences()

                text = self.cleanup_text(raw_chars)

                # utf encode the clean raw output
                clean_chars = [i.encode('utf-8') for i in text['text']]

                return dict(tokens=text['tokens'], text=clean_chars)
예제 #9
0
    def _extract_face(self, filename):
        """[summary]

        Args:
            filename ([type]): [description]

        Returns:
            [type]: [description]
        """
        if self._model_name == 'facenet_keras.h5':
            required_size = (160, 160)
        else:
            required_size = (200, 200)
        # load image from file
        image = Image.open(filename)
        # convert to RGB, if needed
        image = image.convert('RGB')
        # convert to array
        pixels = np.asarray(image)
        # create the detector, using default weights
        detector = MTCNN()
        # detect faces in the image
        results = detector.detect_faces(pixels)
        # extract the bounding box from the first face
        x1, y1, width, height = results[0]['box']
        # deal with negative pixel index
        x1, y1 = abs(x1), abs(y1)
        x2, y2 = x1 + width, y1 + height
        # extract the face
        face = pixels[y1:y2, x1:x2]
        # resize pixels to the model size
        image = Image.fromarray(face)
        image = image.resize(required_size)
        face_array = np.asarray(image)
        return face_array
def preprocess_image(image, target_size):
    if image.mode != 'RGB':
        image = image.convert("RGB")
    image = image.resize(target_size)
    image = img_to_array(image)
    image = image.reshape(1, target_size[0], target_size[1], 3)
    print(image)
    return image
예제 #11
0
def preprocess_image(image, target_size):
  if image.mode != "RGB":
      image = image.convert("RGB")
  image = image.resize(target_size)
  image = img_to_array(image)   # to numpy array
  image= np.expand_dims(image, axis=0)  # expands the dimensions of img

  return image    # preprocessed image returned to be passed to keras model
예제 #12
0
파일: app.py 프로젝트: TushaarBedi/Learning
def prepare_image(image):
    if image.mode != "RGB":
        image = image.convert("RGB")
    image_size = (28, 28)
    image = image.resize(image_size)
    image = img_to_array(image)[:, :, 0]
    image /= 255
    image = 1 - image
    return image.reshape((1, 28, 28, 1))
예제 #13
0
    def adequacao_imagem(self, image, target_size):

        if image.mode != "RGB":
            image = image.convert("RGB")
        image = image.resize(target_size)
        image = img_to_array(image)
        image = np.expand_dims(image, axis=0)

        return image
예제 #14
0
def preprocess_image(image,target_size=(26,26)):
    if image.mode != "L":
        image = image.convert("L") #Convert to grayscale
    image = PIL.ImageOps.invert(image) # Flip Black/white pixels
    image = image.resize(target_size) # Size appropriately
    arr = img_to_array(image)
    arr = np.expand_dims(arr, axis=0)
    arr /= 255.
    return arr
예제 #15
0
def processImage(selectedImage):
    normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406],
                                     std=[0.229, 0.224, 0.225])
    preprocess = transforms.Compose(
        [transforms.Resize((224, 224)),
         transforms.ToTensor(), normalize])
    image = Image.open(selectedImage)
    image = image.convert('RGB')
    return image
예제 #16
0
def prepare_image(image, target):
    if image.mode != 'RGB':
        image = image.convert('RGB')

    resized_img = image.resize(target)
    img_as_array = image.img_to_array(resized_img)
    expanded_img = np.expand_dims(img_as_array, axis=0)
    processed_img = preprocess_input(expanded_img)

    return processed_img
예제 #17
0
def prepare_image(image):
    if image.mode != "RGB":
        image = image.convert("RGB")
    image_size = (299, 299)
    image = image.resize(image_size)
    image = img_to_array(image)
    image = np.expand_dims(image, axis=0)
    image = preprocess_input(image)
    # return the processed image
    return image
예제 #18
0
파일: app.py 프로젝트: TushaarBedi/Learning
def prepare_image(image):
    if image.mode != "RGB":
        image = image.convert("RGB")
    image_size = (28, 28)
    image = image.resize(image_size)
    # image.save("fromform.png")
    image = img_to_array(image)[:, :, 0]
    image /= 255
    image = 1 - image
    return image.flatten().reshape(-1, 28 * 28)
예제 #19
0
def preprocess_image(image, target):
    if image.mode != 'RGB':
        image = image.convert("RGB")
    # resize image input and preprocess it
    image = image.resize(target)
    image = img_to_array(image)
    image = np.expand_dims(image, axis=0)
    image = preprocess_input(image)

    # return the processed image
    return image
예제 #20
0
def upload_file():

    global graph
    with graph.as_default():
        if request.method == 'POST':

            myfile = request.form['snapShot'].split(',')
            imgdata = base64.b64decode(myfile[1])
            image = Image.open(io.BytesIO(imgdata))

            # 保存
            basename = datetime.now().strftime("%Y%m%d-%H%M%S")
            image.save(os.path.join(UPLOAD_FOLDER, basename + ".png"))
            filepath = os.path.join(UPLOAD_FOLDER, basename + ".png")

            image = image.convert('RGB')
            image = image.resize((image_size, image_size))
            data = np.asarray(image)
            X = []
            X.append(data)
            X = np.array(X).astype('float32')
            X /= 256

            result = model.predict([X])[0]
            result_proba = model.predict_proba(X, verbose=1)[0]

            percentage = (result_proba * 100).astype(float)
            array_sort = sorted(list(zip(percentage, classes, classes_img)),
                                reverse=True)
            percentage, array_class, array_img = zip(*array_sort)

            pred_answer1 = "ラベル:" + \
                str(array_class[0]) + ",確率:"+str(percentage[0])+"%"
            pred_answer2 = "ラベル:" + \
                str(array_class[1]) + ",確率:"+str(percentage[1])+"%"
            pred_answer3 = "ラベル:" + \
                str(array_class[2]) + ",確率:"+str(percentage[2])+"%"

            img_src1 = array_img[0]
            img_src2 = array_img[1]
            img_src3 = array_img[2]

            basename = datetime.now().strftime("%Y%m%d-%H%M%S")
            filepath3 = UPLOAD_FOLDER + basename + ".png"

            return render_template("index.html",
                                   answer1=pred_answer1,
                                   img_data1=img_src1,
                                   answer2=pred_answer2,
                                   img_data2=img_src2,
                                   answer3=pred_answer3,
                                   img_data3=img_src3)

        return render_template("index.html", answer="")
예제 #21
0
def grayscale(images):
    '''
    grayscale(images):
    This transforms RGB images to grayscale images
    Input:
        images:List of RBG images
    Returns:
        List of grayscale images
    '''
    gray_images = [image.convert(mode='L') for image in images]
    return gray_images
예제 #22
0
def prepare_image(image, target_size):
    if image.mode != 'RGB':
        image = image.convert('RGB')

    image = image.resize(target_size)
    image = np.array(image)
    image = np.expand_dims(image, axis=0)
    image = image / 255.
    image = preprocess_input(image)
    print('Input image shape:', image.shape)

    return image
예제 #23
0
def prepare_image(image, target):
    # if the image mode is not RGB, convert it
    if image.mode != "RGB":
        image = image.convert("RGB")

    # resize the input image and preprocess it
    image = image.resize(target)
    image = img_to_array(image)
    image = np.expand_dims(image, axis=0)
    image = preprocess_input(image)

    # return the processed image
    return image
예제 #24
0
def path_to_tensor(img):
    # loads RGB image as PIL.Image.Image type
    # if the image mode is not RGB, convert it
    if img.mode != "RGB":
        img = image.convert("RGB")

    # resize the input image
    target = (224, 224)
    img = img.resize(target)

    # convert PIL.Image.Image type to 3D tensor with shape (224, 224, 3)
    x = img_to_array(img)

    # convert 3D tensor to 4D tensor with shape (1, 224, 224, 3) and return 4D tensor
    return np.expand_dims(x, axis=0)
 def preprocess_image(self, image, target_size):
     """  
     Pré-processa a imagem enviada para se adequar aos parâmetros do modelo treinado.
     A imagem é convertida para o padrão de cores RGB caso já não esteja, 
     redimensionada para o tamanho usado no modelo e transforamda em array para ser lida.
     
     :param image: Imagem enviada em base64.
     """
     if image.mode != "RGB":
         image = image.convert("RGB")
     image = image.resize(target_size)
     image = img_to_array(image)
     image = np.expand_dims(image, axis=0)
     
     return image
예제 #26
0
def preparar_imagen(image, target):
    """Descripcion de la funcion preparar_imagen()
    Se encarga de aplicarle preprocesamiento a la imagen para que se puede pasar al modelo
    """
    # if the image mode is not RGB, convert it
    if image.mode != "RGB":
        image = image.convert("RGB")

    # resize the input image and preprocess it
    image = image.resize(target)
    image = img_to_array(image)
    image = np.expand_dims(image, axis=0)
    image = imagenet_utils.preprocess_input(image)

    # return the processed image
    return image
def extract_features(image, target):
    #img = image.load_img(image, target_size=(224, 224))
    if image.mode != "RGB":
        image = image.convert("RGB")
    # resize the input image and preprocess it
    image = image.resize(target)
    image = img_to_array(image)
    image = np.expand_dims(image, axis=0)
    #image = imagenet_utils.preprocess_input(image)
    # return the processed image
    #return image
    #x = image.img_to_array(img)
    #x = np.expand_dims(x, axis=0)
    image = preprocess_input(image)
    with graph.as_default():
        features = Keras_model.predict(image)  #`model._make_predict_function()
    return features.tolist()[0]
예제 #28
0
def prepare_image(image, target):
    """
    Standardasize the image
    :param image:
    :param target: dimension
    :return: image
    """
    # if the image mode is not RGB, convert it
    if image.mode != "RGB":
        image = image.convert("RGB")

    # resize the input image and preprocess it
    image = image.resize(target)
    image = img_to_array(image)
    image = np.expand_dims(image, axis=0)
    image = imagenet_utils.preprocess_input(image)

    # return the processed image
    return image
예제 #29
0
def model_predict(img_path, model):
    img = image.load_img(img_path, target_size=(224, 224))

    if img.mode != "RGB":
        img = image.convert("RGB")

    #img = image.resize((224,224))
    img = img_to_array(img)
    img = np.expand_dims(img, axis=0)

    # Preprocessing the image
    #x = image.img_to_array(img)
    # x = np.true_divide(x, 255)
    #x = np.expand_dims(x, axis=0)

    # Be careful how your trained model deals with the input
    # otherwise, it won't make correct prediction!
    #x = preprocess_input(x, mode='caffe')
    img /= 255.0
    preds = model.predict(img)
    # print(preds)
    return preds
def preprocess(image, target_size):
    # width, height = img.shape[0], img.shape[1]
    # img = image.array_to_img(img, scale=False)

    # desired_width, desired_height = 224, 224

    # if width < desired_width:
    #  desired_width = width
    # start_x = np.maximum(0, int((width-desired_width)/2))

    # img = img.crop((start_x, np.maximum(0, height-desired_height), start_x+desired_width, height))
    # img = img.resize((224, 224, 3))

    # img = image.img_to_array(img)
    # return img / 255.

    if image.mode != "RGB":
        image = image.convert("RGB")
    image = image.resize(target_size)
    image = img_to_array(image)
    image = np.expand_dims(image, axis=0)
    return image