Search in sources :

Example 96 with Mat

use of org.bytedeco.opencv.opencv_core.Mat in project vantiq-extension-sources by Vantiq.

the class NetworkStreamRetriever method getImage.

/**
 * Obtain the most recent image from the specified camera, or the configured camera if no camera is specified
 */
@Override
public ImageRetrieverResults getImage(Map<String, ?> request) throws ImageAcquisitionException {
    FFmpegFrameGrabber cap = null;
    ImageRetrieverResults results = new ImageRetrieverResults();
    if (request.get("DScamera") instanceof String) {
        String cam = (String) request.get("DScamera");
        cap = new FFmpegFrameGrabber(cam);
    } else if (capture == null) {
        throw new ImageAcquisitionException(this.getClass().getCanonicalName() + ".noMainCamera: " + "No camera was requested and no main camera was specified at initialization.");
    } else {
        return getImage();
    }
    // If we got here, then we need to process the image from the specified camera.
    long after;
    long before = System.currentTimeMillis();
    Date captureTime = new Date();
    // Reading the next video frame from the camera
    Mat matrix = grabFrameAsMat(cap);
    byte[] imageByte = convertMatToJpeg(matrix);
    matrix.release();
    results.setImage(imageByte);
    results.setTimestamp(captureTime);
    after = System.currentTimeMillis();
    if (log.isDebugEnabled()) {
        log.debug("Image retrieving time for source " + sourceName + ": {}.{} seconds", (after - before) / 1000, String.format("%03d", (after - before) % 1000));
    }
    return results;
}
Also used : Mat(org.bytedeco.opencv.opencv_core.Mat) FFmpegFrameGrabber(org.bytedeco.javacv.FFmpegFrameGrabber) ImageAcquisitionException(io.vantiq.extsrc.objectRecognition.exception.ImageAcquisitionException) Date(java.util.Date)

Example 97 with Mat

use of org.bytedeco.opencv.opencv_core.Mat in project vantiq-extension-sources by Vantiq.

the class NetworkStreamRetriever method getImage.

/**
 * Obtain the most recent image from the camera
 * @throws FatalImageException  If the IP camera is no longer open
 */
@Override
public ImageRetrieverResults getImage() throws ImageAcquisitionException {
    // Used to check how long image retrieving takes
    long after;
    long before = System.currentTimeMillis();
    // Reading the next video frame from the camera
    Mat matrix;
    ImageRetrieverResults results = new ImageRetrieverResults();
    Date captureTime = new Date();
    matrix = grabFrameAsMat();
    if (matrix == null || matrix.empty()) {
        if (matrix != null) {
            matrix.release();
        }
        // Check connection to URL first
        diagnoseConnection();
        openCapture();
        matrix = grabFrameAsMat();
        if (matrix.empty()) {
            matrix.release();
            throw new ImageAcquisitionException(this.getClass().getCanonicalName() + ".mainCameraReadError2: " + "Could not obtain frame from camera '" + cameraOrFile + "'");
        }
    }
    byte[] imageByte = convertMatToJpeg(matrix);
    matrix.release();
    results.setImage(imageByte);
    results.setTimestamp(captureTime);
    after = System.currentTimeMillis();
    if (log.isDebugEnabled()) {
        log.debug("Image retrieving time for source " + sourceName + ": {}.{} seconds", (after - before) / 1000, String.format("%03d", (after - before) % 1000));
    }
    return results;
}
Also used : Mat(org.bytedeco.opencv.opencv_core.Mat) ImageAcquisitionException(io.vantiq.extsrc.objectRecognition.exception.ImageAcquisitionException) Date(java.util.Date)

Example 98 with Mat

use of org.bytedeco.opencv.opencv_core.Mat in project vantiq-extension-sources by Vantiq.

the class FileRetriever method getImage.

/**
 * Read the file specified at configuration
 * @throws FatalImageException  If no file was specified at configuration, or if the video has completed
 */
@Override
@SuppressWarnings({ "PMD.CognitiveComplexity" })
public ImageRetrieverResults getImage() throws ImageAcquisitionException {
    ImageRetrieverResults results = new ImageRetrieverResults();
    Map<String, Object> otherData = new LinkedHashMap<>();
    results.setOtherData(otherData);
    otherData.put("file", cameraOrFile);
    if (capture != null) {
        otherData.put("videoFrameCount", capture.getLengthInVideoFrames());
    }
    if (isMov) {
        Mat matrix = new Mat();
        // Save the current position for later.
        log.trace("Reading frame number {}", currentFrameNumber);
        matrix = grabFrameAsMat();
        // Exit if nothing could be read
        if (matrix.empty()) {
            close();
            throw new FatalImageException(this.getClass().getCanonicalName() + ".defaultVideoReadError: " + "Default video could not be read. Most likely the video completed reading.");
        }
        // Translate the image to jpeg
        byte[] imageBytes = convertMatToJpeg(matrix);
        if (imageBytes == null) {
            throw new ImageAcquisitionException(this.getClass().getCanonicalName() + ".videoConversionError: " + "Could not convert frame #" + currentFrameNumber + " from video '" + cameraOrFile + "' into a jpeg image");
        }
        // Move forward by the number of frames specified in the Configuration
        currentFrameNumber += frameInterval;
        try {
            log.trace("Setting next frame number to {}", currentFrameNumber);
            capture.setVideoFrameNumber(currentFrameNumber);
        } catch (FFmpegFrameGrabber.Exception e) {
            throw new ImageAcquisitionException(this.getClass().getCanonicalName() + ".videoSeekError: " + "Could not advance to frame #" + currentFrameNumber + " from video '" + cameraOrFile + "'.", e);
        }
        otherData.put("frame", currentFrameNumber);
        results.setImage(imageBytes);
        return results;
    } else if (cameraOrFile != null) {
        // Read the expected image
        otherData.put("file", cameraOrFile);
        Mat image = imread(cameraOrFile);
        if (image.empty()) {
            image.release();
            if (!new File(cameraOrFile).exists()) {
                throw new ImageAcquisitionException(this.getClass().getCanonicalName() + ".defaultImageDoesNotExist: " + "The default image does not exist");
            }
            throw new ImageAcquisitionException(this.getClass().getCanonicalName() + ".defaultImageUnreadable: " + "Could not read requested file '" + cameraOrFile + "'. " + "Most likely the image was in an unreadable format");
        }
        byte[] jpegImage = convertMatToJpeg(image);
        if (jpegImage == null) {
            throw new ImageAcquisitionException(this.getClass().getCanonicalName() + ".imageConversionError: " + "Could not convert file '" + cameraOrFile + "' into a jpeg image");
        }
        results.setImage(jpegImage);
        return results;
    } else {
        throw new FatalImageException(this.getClass().getCanonicalName() + ".noDefaultFile: " + "No default file found. Most likely none was specified in the configuration.");
    }
}
Also used : Mat(org.bytedeco.opencv.opencv_core.Mat) FFmpegFrameGrabber(org.bytedeco.javacv.FFmpegFrameGrabber) FatalImageException(io.vantiq.extsrc.objectRecognition.exception.FatalImageException) ImageAcquisitionException(io.vantiq.extsrc.objectRecognition.exception.ImageAcquisitionException) File(java.io.File) LinkedHashMap(java.util.LinkedHashMap)

Example 99 with Mat

use of org.bytedeco.opencv.opencv_core.Mat in project vantiq-extension-sources by Vantiq.

the class FileRetriever method getImage.

/**
 * Read the specified file, or the file specified at configuration
 */
@Override
@SuppressWarnings({ "PMD.CognitiveComplexity" })
public ImageRetrieverResults getImage(Map<String, ?> request) throws ImageAcquisitionException {
    ImageRetrieverResults results = new ImageRetrieverResults();
    Map<String, Object> otherData = new LinkedHashMap<>();
    results.setOtherData(otherData);
    // Make it local so we don't overwrite the class variable
    boolean isMov = false;
    // Check if the file is expected to be a video
    if (request.get("DSfileExtension") instanceof String) {
        String ext = (String) request.get("DSfileExtension");
        if (ext.equals("mov") || ext.equals("mp4")) {
            isMov = true;
        }
    }
    // Read in the file specified, or try the default if no file is specified
    if (request.get("DSfileLocation") instanceof String) {
        if (isMov) {
            String imageFile = (String) request.get("DSfileLocation");
            otherData.put("file", imageFile);
            FFmpegFrameGrabber newcapture = new FFmpegFrameGrabber(imageFile);
            try {
                newcapture.start();
            } catch (FFmpegFrameGrabber.Exception e) {
                if (!new File(imageFile).exists()) {
                    throw new ImageAcquisitionException(this.getClass().getCanonicalName() + ".queryImageDoesNotExist: " + "The requested image '" + imageFile + "' does not exist", e);
                }
                throw new ImageAcquisitionException(this.getClass().getCanonicalName() + ".queryImageUnreadable: " + "Could not read requested file '" + imageFile + "'. " + "Most likely the image was in an unreadable format", e);
            }
            Mat matrix = new Mat();
            // Calculate the specified frame
            int targetFrame = 1;
            if (request.get("DStargetFrame") instanceof Number) {
                targetFrame = ((Number) request.get("DStargetFrame")).intValue();
            } else if (request.get("DStargetTime") instanceof Number) {
                double fps = newcapture.getFrameRate();
                if (fps == 0) {
                    fps = 24;
                }
                targetFrame = (int) (fps * ((Number) request.get("DStargetTime")).doubleValue());
            }
            // Ensure that targetFrame is inside the bounds of the video, and that we know how many frames are
            // in the video
            int frameCount = newcapture.getLengthInVideoFrames();
            if (frameCount == 0) {
                try {
                    newcapture.release();
                } catch (FFmpegFrameGrabber.Exception e) {
                    otherData.put("releaseException", e);
                // Otherwise, ignore
                }
                matrix.release();
                throw new ImageAcquisitionException(this.getClass().getCanonicalName() + ".videoPropertyError: " + "Video '" + imageFile + "' registers as 0 frames long");
            } else if (targetFrame >= frameCount || targetFrame < 0) {
                try {
                    newcapture.release();
                } catch (FFmpegFrameGrabber.Exception e) {
                    otherData.put("releaseException", e);
                }
                matrix.release();
                throw new ImageAcquisitionException(this.getClass().getCanonicalName() + ".invalidTargetFrame: " + "Requested frame " + targetFrame + " outside valid bounds (0," + frameCount + ") for " + "video '" + imageFile + "'");
            }
            try {
                newcapture.setVideoFrameNumber(targetFrame);
            } catch (FFmpegFrameGrabber.Exception e) {
                throw new ImageAcquisitionException(this.getClass().getCanonicalName() + ".videoSeekError2: " + "Could not advance to frame #" + currentFrameNumber + " from video '" + imageFile + "'.", e);
            }
            matrix = grabFrameAsMat(newcapture);
            // Since we're going to release the stream, we need to clone the matrix.
            // Otherwise, things go badly awry.
            matrix = matrix.clone();
            try {
                newcapture.release();
            } catch (FFmpegFrameGrabber.Exception e) {
                otherData.put("releaseException", e);
            // Otherwise, ignore
            }
            // Exit if nothing could be read
            if (matrix.empty()) {
                matrix.release();
                throw new ImageAcquisitionException(this.getClass().getCanonicalName() + ".videoUnreadable: " + "Video '" + imageFile + "' could not be read");
            }
            // Translate the image to jpeg
            byte[] imageBytes = convertMatToJpeg(matrix);
            if (imageBytes == null) {
                throw new ImageAcquisitionException(this.getClass().getCanonicalName() + ".queryVideoConversionError: " + "Could not convert frame #" + targetFrame + " from video '" + imageFile + "' into a jpeg image");
            }
            otherData.put("frame", targetFrame);
            results.setImage(imageBytes);
            return results;
        } else {
            // Read the expected image
            String imageFile = (String) request.get("DSfileLocation");
            otherData.put("file", imageFile);
            Mat image = imread(imageFile);
            if (image == null || image.empty()) {
                if (image != null) {
                    image.release();
                }
                if (!new File(imageFile).exists()) {
                    throw new ImageAcquisitionException(this.getClass().getCanonicalName() + ".queryImageDoesNotExist: " + "The requested image '" + imageFile + "' does not exist");
                }
                throw new ImageAcquisitionException(this.getClass().getCanonicalName() + ".queryImageUnreadable: " + "Could not read requested file '" + imageFile + "'. " + "Most likely the image was in an unreadable format");
            }
            byte[] jpegImage = convertMatToJpeg(image);
            if (jpegImage == null) {
                throw new ImageAcquisitionException(this.getClass().getCanonicalName() + ".queryImageConversionError: " + "Could not convert file '" + cameraOrFile + "' into a jpeg image");
            }
            results.setImage(jpegImage);
            return results;
        }
    } else {
        // Only try to use default if it is set
        if (isMov || cameraOrFile != null) {
            try {
                return getImage();
            } catch (FatalImageException e) {
                // Since the source can read other videos as well, we don't want to fatally end
                throw new ImageAcquisitionException(e.getMessage(), e);
            }
        } else {
            throw new ImageAcquisitionException(this.getClass().getCanonicalName() + ".noDefaultFile: " + "No default file available. Most likely the default video has been completed");
        }
    }
}
Also used : Mat(org.bytedeco.opencv.opencv_core.Mat) ImageAcquisitionException(io.vantiq.extsrc.objectRecognition.exception.ImageAcquisitionException) FatalImageException(io.vantiq.extsrc.objectRecognition.exception.FatalImageException) ImageAcquisitionException(io.vantiq.extsrc.objectRecognition.exception.ImageAcquisitionException) LinkedHashMap(java.util.LinkedHashMap) FFmpegFrameGrabber(org.bytedeco.javacv.FFmpegFrameGrabber) FatalImageException(io.vantiq.extsrc.objectRecognition.exception.FatalImageException) File(java.io.File)

Example 100 with Mat

use of org.bytedeco.opencv.opencv_core.Mat in project sample-projects by bytedeco.

the class Stitching method main.

public static void main(String[] args) {
    int retval = parseCmdArgs(args);
    if (retval != 0) {
        System.exit(-1);
    }
    Mat pano = new Mat();
    Stitcher stitcher = createStitcher(try_use_gpu);
    int status = stitcher.stitch(imgs, pano);
    if (status != Stitcher.OK) {
        System.out.println("Can't stitch images, error code = " + status);
        System.exit(-1);
    }
    imwrite(result_name, pano);
    System.out.println("Images stitched together to make " + result_name);
}
Also used : Mat(org.bytedeco.opencv.opencv_core.Mat) org.bytedeco.opencv.global.opencv_stitching.createStitcher(org.bytedeco.opencv.global.opencv_stitching.createStitcher) Stitcher(org.bytedeco.opencv.opencv_stitching.Stitcher)

Aggregations

Mat (org.bytedeco.opencv.opencv_core.Mat)101 Point (org.bytedeco.opencv.opencv_core.Point)23 MatVector (org.bytedeco.opencv.opencv_core.MatVector)20 ArrayList (java.util.ArrayList)16 PointerScope (org.bytedeco.javacpp.PointerScope)16 Size (org.bytedeco.opencv.opencv_core.Size)15 BufferedImage (java.awt.image.BufferedImage)13 FloatIndexer (org.bytedeco.javacpp.indexer.FloatIndexer)13 Scalar (org.bytedeco.opencv.opencv_core.Scalar)13 Test (org.junit.jupiter.api.Test)12 IntIndexer (org.bytedeco.javacpp.indexer.IntIndexer)10 DoubleIndexer (org.bytedeco.javacpp.indexer.DoubleIndexer)9 UByteIndexer (org.bytedeco.javacpp.indexer.UByteIndexer)8 org.bytedeco.opencv.global.opencv_core (org.bytedeco.opencv.global.opencv_core)7 IOException (java.io.IOException)6 Arrays (java.util.Arrays)6 Collections (java.util.Collections)6 List (java.util.List)6 Collectors (java.util.stream.Collectors)6 UShortIndexer (org.bytedeco.javacpp.indexer.UShortIndexer)6