Search in sources :

Example 36 with BytePointer

use of org.bytedeco.javacpp.BytePointer in project javacpp-presets by bytedeco.

the class YuvConverter method UVInterleavedToPlanar.

public void UVInterleavedToPlanar(BytePointer frame, int pitch) {
    if (pitch == 0) {
        pitch = this.width;
    }
    BytePointer puv = frame.getPointer((long) pitch * height * this.byteDepth);
    BytePointer pu = new BytePointer(puv);
    BytePointer pv = puv.getPointer((long) pitch * height / 4 * this.byteDepth);
    byte[] tempBuffer = new byte[this.byteDepth];
    for (int y = 0; y < this.height / 2; y++) {
        for (int x = 0; x < this.width / 2; x++) {
            memcpy(pu.getPointer(((long) y * pitch / 2 + x) * this.byteDepth), puv.getPointer(((long) y * pitch + x * 2) * this.byteDepth), this.byteDepth);
            memcpy(this.quad.getPointer(((long) y * width / 2 + x) * this.byteDepth), puv.getPointer(((long) y * pitch + x * 2 + 1) * this.byteDepth), this.byteDepth);
        }
    }
    if (pitch == this.width) {
        memcpy(pv, this.quad, ((long) this.width * this.height / 4) * this.byteDepth);
    } else {
        for (int i = 0; i < this.height / 2; i++) {
            memcpy(pv.getPointer(((long) pitch / 2 * i) * this.byteDepth), this.quad.getPointer(((long) this.width / 2 * i) * this.byteDepth), (long) this.width / 2 * this.byteDepth);
        }
    }
}
Also used : BytePointer(org.bytedeco.javacpp.BytePointer)

Example 37 with BytePointer

use of org.bytedeco.javacpp.BytePointer in project karate by karatelabs.

the class Tesseract method process.

public void process(Mat mat, boolean negative) {
    if (negative) {
        mat = OpenCvUtils.negative(mat);
    }
    int srcWidth = mat.size().width();
    int srcHeight = mat.size().height();
    int channels = mat.channels();
    int bytesPerLine = srcWidth * channels * (int) mat.elemSize1();
    tess.SetImage(mat.data().asBuffer(), srcWidth, srcHeight, channels, bytesPerLine);
    // ======================================================================
    BytePointer textPtr = tess.GetUTF8Text();
    allText = textPtr.getString();
    textPtr.deallocate();
    // ======================================================================
    ResultIterator ri = tess.GetIterator();
    int level = tesseract.RIL_WORD;
    words = new ArrayList();
    Word prev = null;
    do {
        float confidence = ri.Confidence(level);
        if (confidence < 50) {
            continue;
        }
        textPtr = ri.GetUTF8Text(level);
        String text = textPtr.getString().trim();
        textPtr.deallocate();
        IntPointer x1 = INT.get();
        IntPointer y1 = INT.get();
        IntPointer x2 = INT.get();
        IntPointer y2 = INT.get();
        boolean found = ri.BoundingBox(level, x1, y1, x2, y2);
        int x = x1.get();
        int y = y1.get();
        int width = x2.get() - x;
        int height = y2.get() - y;
        if (!found) {
            logger.warn("no such rectangle: {}:{}:{}:{}", x, y, width, height);
            continue;
        }
        Word word = new Word(prev, text, x, y, width, height, confidence);
        words.add(word);
        prev = word;
    } while (ri.Next(level));
}
Also used : IntPointer(org.bytedeco.javacpp.IntPointer) BytePointer(org.bytedeco.javacpp.BytePointer) ResultIterator(org.bytedeco.tesseract.ResultIterator) ArrayList(java.util.ArrayList)

Example 38 with BytePointer

use of org.bytedeco.javacpp.BytePointer in project cs-actions by CloudSlang.

the class OcrService method extractBlocks.

private static String extractBlocks(TessBaseAPI api) throws Exception {
    TessBaseAPISetPageSegMode(api, PSM_AUTO_OSD);
    lept.BOXA boxes = TessBaseAPIGetComponentImages(api, RIL_BLOCK, true, (PointerPointer) null, null);
    final int boxCount = boxaGetCount(boxes);
    JsonObject outputJson = new JsonObject();
    for (int i = 0; i < boxCount; i++) {
        lept.BOX box = boxaGetBox(boxes, i, L_CLONE);
        if (box == null) {
            continue;
        }
        TessBaseAPISetRectangle(api, box.x(), box.y(), box.w(), box.h());
        BytePointer utf8Text = TessBaseAPIGetUTF8Text(api);
        String ocrResult = utf8Text.getString(UTF_8);
        outputJson = buildOutputJson(outputJson, ocrResult, i);
        boxDestroy(box);
        utf8Text.deallocate();
    }
    boxaDestroy(boxes);
    if (boxCount == 0) {
        throw new Exception("Failed to extract text blocks (Empty page), check text orientation or check if text exists.");
    }
    return outputJson.toString();
}
Also used : BytePointer(org.bytedeco.javacpp.BytePointer) JsonObject(com.google.gson.JsonObject) IOException(java.io.IOException) org.bytedeco.javacpp.lept(org.bytedeco.javacpp.lept)

Example 39 with BytePointer

use of org.bytedeco.javacpp.BytePointer in project cineast by vitrivr.

the class FFMpegVideoDecoder method readVideo.

/**
 * Reads the decoded video frames and re-sizes them. The re-sized video frame is then copied into a VideoFrame data structure, which is subsequently enqueued.
 */
private void readVideo() {
    // Convert the image from its native format to RGB
    swscale.sws_scale(this.sws_ctx, this.pFrame.data(), this.pFrame.linesize(), 0, this.pCodecCtxVideo.height(), this.pFrameRGB.data(), this.pFrameRGB.linesize());
    // Write pixel data
    BytePointer data = this.pFrameRGB.data(0);
    data.position(0).get(bytes);
    for (int i = 0; i < pixels.length; ++i) {
        int pos = 3 * i;
        int r = bytes[pos] & 0xff;
        int g = bytes[pos + 1] & 0xff;
        int b = bytes[pos + 2] & 0xff;
        pixels[i] = ((0xFF) << 24) | ((r & 0xFF) << 16) | ((g & 0xFF) << 8) | ((b & 0xFF) << 0);
    }
    /* Prepare frame and associated timestamp and add it to output queue. */
    VideoFrame videoFrame = new VideoFrame(this.pCodecCtxVideo.frame_number(), this.getFrameTimestamp(this.videoStream), this.factory.newMultiImage(this.videoDescriptor.getWidth(), this.videoDescriptor.getHeight(), pixels), this.videoDescriptor);
    this.videoFrameQueue.add(videoFrame);
}
Also used : VideoFrame(org.vitrivr.cineast.core.data.frames.VideoFrame) BytePointer(org.bytedeco.javacpp.BytePointer)

Example 40 with BytePointer

use of org.bytedeco.javacpp.BytePointer in project djl by deepjavalibrary.

the class JavacppUtils method loadSavedModelBundle.

@SuppressWarnings({ "unchecked", "try" })
public static SavedModelBundle loadSavedModelBundle(String exportDir, String[] tags, ConfigProto config, RunOptions runOptions) {
    try (PointerScope ignored = new PointerScope()) {
        TF_Status status = TF_Status.newStatus();
        // allocate parameters for TF_LoadSessionFromSavedModel
        TF_SessionOptions opts = TF_SessionOptions.newSessionOptions();
        if (config != null) {
            BytePointer configBytes = new BytePointer(config.toByteArray());
            tensorflow.TF_SetConfig(opts, configBytes, configBytes.capacity(), status);
            status.throwExceptionIfNotOK();
        }
        TF_Buffer runOpts = TF_Buffer.newBufferFromString(runOptions);
        // load the session
        TF_Graph graphHandle = AbstractTF_Graph.newGraph().retainReference();
        TF_Buffer metaGraphDef = TF_Buffer.newBuffer();
        TF_Session sessionHandle = tensorflow.TF_LoadSessionFromSavedModel(opts, runOpts, new BytePointer(exportDir), new PointerPointer<>(tags), tags.length, graphHandle, metaGraphDef, status);
        status.throwExceptionIfNotOK();
        // handle the result
        try {
            return new SavedModelBundle(graphHandle, sessionHandle, MetaGraphDef.parseFrom(metaGraphDef.dataAsByteBuffer()));
        } catch (InvalidProtocolBufferException e) {
            throw new TensorFlowException("Cannot parse MetaGraphDef protocol buffer", e);
        }
    }
}
Also used : TF_Graph(org.tensorflow.internal.c_api.TF_Graph) AbstractTF_Graph(org.tensorflow.internal.c_api.AbstractTF_Graph) TF_Session(org.tensorflow.internal.c_api.TF_Session) TF_Status(org.tensorflow.internal.c_api.TF_Status) BytePointer(org.bytedeco.javacpp.BytePointer) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) TF_Buffer(org.tensorflow.internal.c_api.TF_Buffer) SavedModelBundle(ai.djl.tensorflow.engine.SavedModelBundle) PointerScope(org.bytedeco.javacpp.PointerScope) TensorFlowException(org.tensorflow.exceptions.TensorFlowException) TF_SessionOptions(org.tensorflow.internal.c_api.TF_SessionOptions)

Aggregations

BytePointer (org.bytedeco.javacpp.BytePointer)79 IntPointer (org.bytedeco.javacpp.IntPointer)22 PointerPointer (org.bytedeco.javacpp.PointerPointer)20 ByteBuffer (java.nio.ByteBuffer)19 IOException (java.io.IOException)16 Pointer (org.bytedeco.javacpp.Pointer)16 PointerScope (org.bytedeco.javacpp.PointerScope)13 DoublePointer (org.bytedeco.javacpp.DoublePointer)12 FloatPointer (org.bytedeco.javacpp.FloatPointer)12 CompressedDataBuffer (org.nd4j.linalg.compression.CompressedDataBuffer)10 CompressionDescriptor (org.nd4j.linalg.compression.CompressionDescriptor)10 ShortBuffer (java.nio.ShortBuffer)9 ShortPointer (org.bytedeco.javacpp.ShortPointer)9 IntBuffer (java.nio.IntBuffer)7 DoubleBuffer (java.nio.DoubleBuffer)6 FloatBuffer (java.nio.FloatBuffer)6 Nonnull (javax.annotation.Nonnull)5 LongPointer (org.bytedeco.javacpp.LongPointer)5 TF_Status (org.tensorflow.internal.c_api.TF_Status)4 ByteOrder (java.nio.ByteOrder)3