Java Code Examples for org.bytedeco.javacv.FrameGrabber#Exception

The following examples show how to use org.bytedeco.javacv.FrameGrabber#Exception . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ConverterService.java    From Spring with Apache License 2.0 6 votes vote down vote up
public void toAnimatedGif(FFmpegFrameGrabber frameGrabber, AnimatedGifEncoder
        gifEncoder, int start, int end, int speed) throws FrameGrabber.Exception {

    final long startFrame = Math.round(start * frameGrabber.getFrameRate());
    final long endFrame = Math.round(end * frameGrabber.getFrameRate());

    final Java2DFrameConverter frameConverter = new Java2DFrameConverter();

    for (long i = startFrame; i < endFrame; i++) {

        if (i % speed == 0) {

            // Bug if frameNumber is set to 0
            if (i > 0) {
                frameGrabber.setFrameNumber((int) i);
            }

            final BufferedImage bufferedImage = frameConverter.convert(frameGrabber.grabImage());
            gifEncoder.addFrame(bufferedImage);
        }

    }

    frameGrabber.stop();
    gifEncoder.finish();
}
 
Example 2
Source File: UploadController.java    From Spring with Apache License 2.0 6 votes vote down vote up
/**
 * curl -F file=@/home/olinnyk/IdeaProjects/Spring/SpringWEB/SpringBoot/just-gif-it/video/PexelsVideos.mp4 -F start=0 -F end=5 -F speed=1 -F repeat=0 localhost:8080/upload
 */
@PostMapping
@RequestMapping(value = "/upload", produces = MediaType.IMAGE_GIF_VALUE)
public String upload(@RequestPart("file") MultipartFile file,
		@RequestParam("start") int start,
		@RequestParam("end") int end,
		@RequestParam("speed") int speed,
		@RequestParam("repeat") boolean repeat) throws IOException, FrameGrabber.Exception {

	final File videoFile = new File(location + "/" + System.currentTimeMillis() + ".mp4");
	file.transferTo(videoFile);

	log.info("Saved video file to {}", videoFile.getAbsolutePath());

	final Path output = Paths.get(location + "/gif/" + System.currentTimeMillis() + ".gif");

	final FFmpegFrameGrabber frameGrabber = videoDecoderService.read(videoFile);
	final AnimatedGifEncoder gifEncoder = gifEncoderService.getGifEncoder(repeat, (float) frameGrabber.getFrameRate(), output);
	converterService.toAnimatedGif(frameGrabber, gifEncoder, start, end, speed);

	log.info("Saved generated gif to {}", output.toString());

	return output.getFileName().toString();
}
 
Example 3
Source File: VideoPlayer.java    From Java-Machine-Learning-for-Computer-Vision with MIT License 6 votes vote down vote up
private void runVideoMainThread(String videoFileName, OpenCVFrameConverter.ToMat toMat) throws FrameGrabber.Exception {
    FFmpegFrameGrabber grabber = initFrameGrabber(videoFileName);
    while (!stop) {
        Frame frame = grabber.grab();
        if (frame == null) {
            log.info("Stopping");
            stop();
            break;
        }
        if (frame.image == null) {
            continue;
        }
        yolo.push(frame);
        opencv_core.Mat mat = toMat.convert(frame);
        yolo.drawBoundingBoxesRectangles(frame, mat);
        imshow(windowName, mat);
        char key = (char) waitKey(20);
        // Exit this loop on escape:
        if (key == 27) {
            stop();
            break;
        }
    }
}
 
Example 4
Source File: CameraFFMPEG.java    From PapARt with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public void start() {
    FFmpegFrameGrabber grabberFF = new FFmpegFrameGrabber(this.cameraDescription);

    grabberFF.setImageMode(FrameGrabber.ImageMode.COLOR);

    this.setPixelFormat(PixelFormat.BGR);

    grabberFF.setFormat(this.imageFormat);
    grabberFF.setImageWidth(width());
    grabberFF.setImageHeight(height());
    grabberFF.setFrameRate(frameRate);

    try {
        grabberFF.start();
        this.grabber = grabberFF;
        this.isConnected = true;
    } catch (FrameGrabber.Exception e) {
        System.err.println("Could not FFMPEG frameGrabber... " + e);
        System.err.println("Camera ID " + this.cameraDescription + ":" + this.imageFormat + " could not start.");
        System.err.println("Check cable connection, ID and resolution asked.");
        this.grabber = null;
    }
}
 
Example 5
Source File: CameraRealSense.java    From PapARt with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Experimental.
 *
 * @return
 */
public Frame grabFrame() {
    try {
        return grabber.grab();
    } catch (FrameGrabber.Exception ex) {
        Logger.getLogger(CameraRealSense.class.getName()).log(Level.SEVERE, null, ex);
    }
    return null;
}
 
Example 6
Source File: CameraFFMPEG.java    From PapARt with GNU Lesser General Public License v3.0 5 votes vote down vote up
private void checkEndOfVideo() {
    // 10 frames from the end.
    if (grabber.getFrameNumber() + 10 > grabber.getLengthInFrames()) {
        try {
            grabber.setFrameNumber(0);
        } catch (FrameGrabber.Exception ex) {
            Logger.getLogger(CameraFFMPEG.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
 
Example 7
Source File: CameraOpenKinect.java    From PapARt with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public void grabColor() {
    try {
        colorCamera.updateCurrentImage(grabber.grabVideo());
    } catch (FrameGrabber.Exception ex) {
        Logger.getLogger(CameraOpenKinect.class.getName()).log(Level.SEVERE, null, ex);
    }
}
 
Example 8
Source File: CameraOpenKinect.java    From PapARt with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public void grabDepth() {
    try {
        depthCamera.currentImage = grabber.grabDepth();

        ((WithTouchInput) depthCamera).newTouchImageWithColor(colorCamera.currentImage);
    } catch (FrameGrabber.Exception ex) {
        Logger.getLogger(CameraOpenKinect.class.getName()).log(Level.SEVERE, null, ex);
    }
}
 
Example 9
Source File: JavaCVExample.java    From javacv-cnn-example with MIT License 5 votes vote down vote up
/**
 * Stops and released resources attached to frame grabbing. Stops frame processing and,
 */
public void stop() {
    running = false;
    try {
        logger.debug("Releasing and stopping FrameGrabber");
        frameGrabber.release();
        frameGrabber.stop();
    } catch (FrameGrabber.Exception e) {
        logger.error("Error occurred when stopping the FrameGrabber", e);
    }

    window.dispose();
}
 
Example 10
Source File: JavaCVExample.java    From javacv-cnn-example with MIT License 5 votes vote down vote up
/**
 * Starts the frame grabbers and then the frame processing. Grabbed and processed frames will be displayed in the
 * {@link #videoPanel}
 */
public void start() {
   // frameGrabber = new FFmpegFrameGrabber("/dev/video0");
    // The available FrameGrabber classes include OpenCVFrameGrabber (opencv_videoio),
    // DC1394FrameGrabber, FlyCapture2FrameGrabber, OpenKinectFrameGrabber,
    // PS3EyeFrameGrabber, VideoInputFrameGrabber, and FFmpegFrameGrabber.
    frameGrabber = new OpenCVFrameGrabber(0);

    //frameGrabber.setFormat("mp4");
    frameGrabber.setImageWidth(1280);
    frameGrabber.setImageHeight(720);

    logger.debug("Starting frame grabber");
    try {
        frameGrabber.start();
        logger.debug("Started frame grabber with image width-height : {}-{}", frameGrabber.getImageWidth(), frameGrabber.getImageHeight());
    } catch (FrameGrabber.Exception e) {
        logger.error("Error when initializing the frame grabber", e);
        throw new RuntimeException("Unable to start the FrameGrabber", e);
    }

    SwingUtilities.invokeLater(() -> {
        window.setVisible(true);
    });

    process();

    logger.debug("Stopped frame grabbing.");
}
 
Example 11
Source File: CameraOpenKinect2.java    From PapARt with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Override
public void internalStart() throws FrameGrabber.Exception {
    grabber.start();
}
 
Example 12
Source File: CameraOpenKinect.java    From PapARt with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Override
public void internalStart() throws FrameGrabber.Exception {
    grabber.start();
}
 
Example 13
Source File: VideoPlayer.java    From Java-Machine-Learning-for-Computer-Vision with MIT License 4 votes vote down vote up
private FFmpegFrameGrabber initFrameGrabber(String videoFileName) throws FrameGrabber.Exception {
    FFmpegFrameGrabber grabber = new FFmpegFrameGrabber(videoFileName);
    grabber.start();
    return grabber;
}
 
Example 14
Source File: VideoPlayer.java    From Java-Machine-Learning-for-Computer-Vision with MIT License 4 votes vote down vote up
private FFmpegFrameGrabber initFrameGrabber(String videoFileName) throws FrameGrabber.Exception {
    FFmpegFrameGrabber grabber = new FFmpegFrameGrabber(new File(videoFileName));
    grabber.start();
    return grabber;
}
 
Example 15
Source File: CameraOpenNI2.java    From PapARt with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Override
    public void internalStart() throws FrameGrabber.Exception {
        if (useColor) {
            colorStream = initStream(
                    //                    PixelFormat.BGR,
                    PixelFormat.RGB,
                    org.openni.PixelFormat.RGB888,
                    SensorType.COLOR,
                    new FrameListener(colorCamera),
                    colorCamera);

            colorStream.setMirroringEnabled(false);
            colorCamera.setUndistort(false);

            colorStream.start();
        }

        if (useIR) {
            IRStream = initStream(
                    PixelFormat.RGB,
                    org.openni.PixelFormat.RGB888,
                    SensorType.IR,
                    new FrameListener(IRCamera),
                    IRCamera);
            IRStream.setMirroringEnabled(false);
            IRCamera.setUndistort(false);
            IRStream.start();

        }

        if (useDepth) {
            depthStream = initStream(
                    PixelFormat.OPENNI_2_DEPTH,
                    org.openni.PixelFormat.DEPTH_1_MM,
                    SensorType.DEPTH,
                    new FrameListener(depthCamera),
                    depthCamera);
            depthStream.setMirroringEnabled(false);
            depthCamera.setUndistort(false);
            depthStream.start();
        }

//        grabber.start();
//
//        // Override the calibration... 
//        if (useHardwareIntrinsics) {
//            if (useColor) {
//                useHarwareIntrinsics(colorCamera, grabber);
//            }
//            if (useIR) {
//                useHarwareIntrinsics(IRCamera, grabber);
//            }
//            if (useDepth) {
//                useHarwareIntrinsics(depthCamera, grabber);
//            }
//        }
    }
 
Example 16
Source File: CameraRealSense.java    From PapARt with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Override
    public void internalStart() throws FrameGrabber.Exception {

        if (useIR && !useDepth && !useColor) {
            IRCamera.setFrameRate(60); // 200 max -> buggy
        }

        if (useColor) {
            grabber.setImageWidth(colorCamera.width());
            grabber.setImageHeight(colorCamera.height());
            grabber.setFrameRate(colorCamera.getFrameRate());
            grabber.enableColorStream();
        }

        if (useIR) {
            grabber.setIRImageWidth(IRCamera.width());
            grabber.setIRImageHeight(IRCamera.height());
            grabber.setIRFrameRate(IRCamera.getFrameRate());
            grabber.enableIRStream();
        }

        if (useDepth) {
            grabber.setDepthImageWidth(depthCamera.width());
            grabber.setDepthImageHeight(depthCamera.height());
            grabber.setDepthFrameRate(depthCamera.getFrameRate());
            grabber.enableDepthStream();
        }

        grabber.start();

//        grabber.setPreset(3);
        // Override the calibration... 
        if (useHardwareIntrinsics) {
            if (useColor) {
                useHarwareIntrinsics(colorCamera, grabber);
            }
            if (useIR) {
                useHarwareIntrinsics(IRCamera, grabber);
            }
            if (useDepth) {
                useHarwareIntrinsics(depthCamera, grabber);
            }
        }

    }
 
Example 17
Source File: VideoDecoderService.java    From Spring with Apache License 2.0 4 votes vote down vote up
public FFmpegFrameGrabber read(File video) throws FrameGrabber.Exception {
    FFmpegFrameGrabber frameGrabber = new FFmpegFrameGrabber(video);
    frameGrabber.start();
    return frameGrabber;
}