예제 #1
0
def get_request():
    img = request.data.decode('utf-8')
    img = json.loads(img)
    image_string = img["image"]
    angle = int(img["angle"])

    base = str(image_string).replace("data:image/jpeg;base64,", "")
    imgdata = base64.b64decode(base)
    imgdata = cv2.imdecode(np.frombuffer(imgdata, np.uint8), -1)
    imgdata = cv2.cvtColor(imgdata, cv2.COLOR_BGR2RGB)
    imgdata = crop_largest_rectangle(imgdata, angle, *imgdata.shape[:2])
    imgdata = Image.fromarray(imgdata)

    path_file = app.config['UPLOAD_FILE']
    imgdata.save(path_file)

    buffered = BytesIO()
    rotated_image, predicted_angle = predict(model_ver_1, path_file)
    rotated_image = Image.fromarray(rotated_image, 'RGB')
    rotated_image.save(buffered, format="JPEG")
    return jsonify({
        'angle':
        360 - int(predicted_angle),
        'image':
        "data:image/jpeg;base64," +
        str(base64.b64encode(buffered.getvalue()).decode("ascii"))
    }), 200
def process_images(model, input_path, output_path,
                   batch_size=64, crop=True):
    extensions = ['.jpg', '.jpeg', '.bmp', '.png']

    output_is_image = False
    if os.path.isfile(input_path):
        image_paths = [input_path]
        if os.path.splitext(output_path)[1].lower() in extensions:
            output_is_image = True
            output_filename = output_path
            output_path = os.path.dirname(output_filename)
    else:
        image_paths = [os.path.join(input_path, f)
                       for f in os.listdir(input_path)
                       if os.path.splitext(f)[1].lower() in extensions]
        if os.path.splitext(output_path)[1].lower() in extensions:
            print('Output must be a directory!')

    predictions = model.predict_generator(
        RotNetDataGenerator(
            image_paths,
            input_shape=(224, 224, 3),
            batch_size=64,
            one_hot=True,
            preprocess_func=preprocess_input,
            rotate=False,
            crop_largest_rect=True,
            crop_center=True
        ),
        val_samples=len(image_paths)
    )

    predicted_angles = np.argmax(predictions, axis=1)

    if output_path == '':
        output_path = '.'

    if not os.path.exists(output_path):
        os.makedirs(output_path)

    for path, predicted_angle in zip(image_paths, predicted_angles):
        image = cv2.imread(path)
        rotated_image = rotate(image, -predicted_angle)
        if crop:
            size = (image.shape[0], image.shape[1])
            rotated_image = crop_largest_rectangle(rotated_image, -predicted_angle, *size)
        if not output_is_image:
            output_filename = os.path.join(output_path, os.path.basename(path))
        cv2.imwrite(output_filename, rotated_image)
예제 #3
0
def predict(model, input_path, batch_size=64, crop=True):
    predictions = model.predict_generator(RotNetDataGenerator(
        [input_path],
        input_shape=(224, 224, 3),
        batch_size=64,
        one_hot=True,
        preprocess_func=preprocess_input,
        rotate=False,
        crop_largest_rect=True,
        crop_center=True),
                                          val_samples=1)

    predicted_angle = np.argmax(predictions, axis=1)[0]
    image = cv2.imread(input_path)
    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    rotated_image = rotate(image, -predicted_angle)
    if crop:
        size = (image.shape[0], image.shape[1])
        rotated_image = crop_largest_rectangle(rotated_image, -predicted_angle,
                                               *size)
    return rotated_image, predicted_angle