org.bytedeco.javacv.OpenCVFrameGrabber Java Examples

The following examples show how to use org.bytedeco.javacv.OpenCVFrameGrabber. 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: CameraOpenCV.java    From PapARt with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public void start() {
    OpenCVFrameGrabber grabberCV = new OpenCVFrameGrabber(this.systemNumber);
    grabberCV.setImageWidth(width());
    grabberCV.setImageHeight(height());
    grabberCV.setFrameRate(frameRate);
    grabberCV.setImageMode(FrameGrabber.ImageMode.COLOR);
 
    try {
        grabberCV.start();
        this.grabber = grabberCV;
        this.isConnected = true;
    } catch (Exception e) {
        System.err.println("Could not start frameGrabber... " + e);

        System.err.println("Could not camera start frameGrabber... " + e);
        System.err.println("Camera ID " + this.systemNumber + " could not start.");
        System.err.println("Check cable connection, ID and resolution asked.");

        this.grabber = null;
    }
}
 
Example #2
Source File: MarvinJavaCVAdapter.java    From marvinproject with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public void loadResource(String path) throws MarvinVideoInterfaceException{
	mode = MODE.FILE;
	try{
		grabber= OpenCVFrameGrabber.createDefault(new File(path));
		//grabber = new OpenCVFrameGrabber(new File(path));
		//grabber = new FFmpegFrameGrabber(new File(path));
		//grabber.setImageWidth(width);
		//grabber.setImageHeight(height);
		grabber.start();
		BufferedImage bufImage = ConverterUtil.frametoBufferedImage(grabber.grabFrame());
		//image = converterToIpl.convert(grabber.grab());
		this.width = bufImage.getWidth();
		this.height = bufImage.getHeight();
		//this.width = image.width();
		//this.height = image.height();
		marvinImage = new MarvinImage(width, height);
		intArray = new int[height*width*4];
		connected = true;
	}
	catch(Exception e){
		throw new MarvinVideoInterfaceException("Error while trying to load resource", e);
	}
}
 
Example #3
Source File: VideoThumbnailBuilder.java    From document-management-software with GNU Lesser General Public License v3.0 5 votes vote down vote up
private void writeVideoFrame(File videoFile, File frameFile) throws Exception {
	FrameGrabber g = new OpenCVFrameGrabber(videoFile);
	g.start();

	/*
	 * Try to get a frame after 60 seconds
	 */
	double frameRate = g.getFrameRate();
	int fiveSecondsFrame = (int) (60 * frameRate);
	if (fiveSecondsFrame > g.getLengthInFrames())
		fiveSecondsFrame = 1;

	try {
		for (int i = 0; i < g.getLengthInFrames() && frameFile.length() == 0; i++) {
			try {
				if (i < fiveSecondsFrame) {
					g.grab();
					continue;
				}

				Frame frame = g.grab();
				if (frame == null)
					continue;
				BufferedImage img = Java2DFrameUtils.toBufferedImage(frame);
				if (img == null)
					continue;

				ImageIO.write(img, "png", frameFile);
			} catch (Throwable t) {
			}
		}
	} finally {
		g.stop();
		g.close();
	}
}
 
Example #4
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 #5
Source File: YOLO2_TF_Client.java    From SKIL_Examples with Apache License 2.0 4 votes vote down vote up
@Override
public void start(Stage stage) throws Exception {

    if (args.input_camera >= 0) {
        System.out.println("Opening camara " + args.input_camera);
        frameGrabber = new OpenCVFrameGrabber(args.input_camera);
        frameGrabber.start();
    }

    labels = new COCOLabels();
    colors = new HashMap<>();
    for (int i = 0; i < nClasses; i++) {
        colors.put( labels.getLabel( i ), Color.web( COLORS[i] ) );
    }

    // make model server network calls for { auth, inference }
    skilClientGetImageInference( );

    Canvas canvas = new Canvas(imageWidth, imageHeight);
    GraphicsContext ctx = canvas.getGraphicsContext2D();

    ctx.drawImage(SwingFXUtils.toFXImage(bufImgConverter.convert(matConverter.convert(imgMat)), null), 0, 0);
    stage.setScene(new Scene(new StackPane(canvas), this.imageWidth, this.imageHeight));
    renderJavaFXStyle( ctx );
    stage.setTitle("YOLO2");
    stage.show();

    if (frameGrabber != null) {
        new Thread(() -> {
            try {
                while (stage.isShowing()) {
                    skilClientGetImageInference( );
                    ctx.drawImage(SwingFXUtils.toFXImage(bufImgConverter.convert(matConverter.convert(imgMat)), null), 0, 0);
                    renderJavaFXStyle( ctx );
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }).start();
    }
}