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;
}
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;
}
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.");
}
}
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");
}
}
}
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);
}
Aggregations