All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.datavec.spark.transform.ImageSparkTransform Maven / Gradle / Ivy

There is a newer version: 1.0.0-beta7
Show newest version
package org.datavec.spark.transform;

import lombok.AllArgsConstructor;
import lombok.Getter;
import org.datavec.image.data.ImageWritable;
import org.datavec.image.transform.ImageTransformProcess;
import org.datavec.spark.transform.model.Base64NDArrayBody;
import org.datavec.spark.transform.model.BatchImageRecord;
import org.datavec.spark.transform.model.SingleImageRecord;
import org.nd4j.linalg.api.ndarray.INDArray;
import org.nd4j.linalg.factory.Nd4j;
import org.nd4j.serde.base64.Nd4jBase64;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by kepricon on 17. 5. 24.
 */
@AllArgsConstructor
public class ImageSparkTransform {
    @Getter
    private ImageTransformProcess imageTransformProcess;

    public Base64NDArrayBody toArray(SingleImageRecord record) throws IOException {
        ImageWritable record2 = imageTransformProcess.transformFileUriToInput(record.getUri());
        INDArray finalRecord = imageTransformProcess.executeArray(record2);

        return new Base64NDArrayBody(Nd4jBase64.base64String(finalRecord));
    }

    public Base64NDArrayBody toArray(BatchImageRecord batch) throws IOException {
        List records = new ArrayList<>();

        for (SingleImageRecord imgRecord : batch.getRecords()) {
            ImageWritable record2 = imageTransformProcess.transformFileUriToInput(imgRecord.getUri());
            INDArray finalRecord = imageTransformProcess.executeArray(record2);
            records.add(finalRecord);
        }

        int shape[] = records.get(0).shape();
        INDArray array = Nd4j.create(records, new int[] {records.size(), shape[1], shape[2], shape[3]});

        return new Base64NDArrayBody(Nd4jBase64.base64String(array));
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy