RLPark 1.0.0
Reinforcement Learning Framework in Java

FrameGrabber.java

Go to the documentation of this file.
00001 package rlpark.plugin.opencv;
00002 
00003 import java.awt.image.BufferedImage;
00004 
00005 import rlpark.plugin.opencv.zephyr.OpenCVImageProvider;
00006 import zephyr.plugin.core.api.monitoring.annotations.IgnoreMonitor;
00007 import zephyr.plugin.core.api.viewable.ImageProvider;
00008 
00009 import com.googlecode.javacv.cpp.opencv_core;
00010 import com.googlecode.javacv.cpp.opencv_core.IplImage;
00011 import com.googlecode.javacv.cpp.opencv_highgui;
00012 import com.googlecode.javacv.cpp.opencv_highgui.CvCapture;
00013 
00014 public class FrameGrabber implements ImageProvider {
00015   private final CvCapture grabber;
00016   @IgnoreMonitor
00017   private final OpenCVImageProvider webcam = new OpenCVImageProvider();
00018   private final ImageBuffer lastImage;
00019   private final int width;
00020   private final int height;
00021   private final int depth;
00022   private final int channels;
00023   private IplImage grabberImage;
00024 
00025   public FrameGrabber(int deviceNumber) {
00026     this(deviceNumber, opencv_core.IPL_DEPTH_32F, 1);
00027   }
00028 
00029   public FrameGrabber(int deviceNumber, int depth, int channels) {
00030     grabber = opencv_highgui.cvCreateCameraCapture(0);
00031     IplImage firstImage = grabFirstImage();
00032     width = firstImage.cvSize().width();
00033     height = firstImage.cvSize().height();
00034     lastImage = new ImageBuffer(width, height, depth, channels);
00035     this.depth = depth;
00036     this.channels = channels;
00037   }
00038 
00039   private IplImage grabFirstImage() {
00040     IplImage firstImage = null;
00041     int trial = 0;
00042     while (trial < 50) {
00043       firstImage = opencv_highgui.cvQueryFrame(grabber);
00044       if (firstImage != null)
00045         return firstImage;
00046       try {
00047         Thread.sleep(100);
00048       } catch (InterruptedException e) {
00049         e.printStackTrace();
00050       }
00051       trial++;
00052     }
00053     return firstImage;
00054   }
00055 
00056   @Override
00057   public BufferedImage image() {
00058     return webcam.image();
00059   }
00060 
00061   public int height() {
00062     return height;
00063   }
00064 
00065   public int width() {
00066     return width;
00067   }
00068 
00069   public void dispose() {
00070     try {
00071       opencv_highgui.cvReleaseCapture(grabber);
00072     } catch (Exception e) {
00073       e.printStackTrace();
00074     }
00075   }
00076 
00077   public IplImage grab() {
00078     grabberImage = null;
00079     try {
00080       grabberImage = opencv_highgui.cvQueryFrame(grabber);
00081       if (grabberImage == null)
00082         return null;
00083       lastImage.update(grabberImage);
00084       webcam.update(lastImage);
00085     } catch (Exception e) {
00086       e.printStackTrace();
00087     }
00088     return lastImage.im();
00089   }
00090 
00091   public int depth() {
00092     return depth;
00093   }
00094 
00095   public int channels() {
00096     return channels;
00097   }
00098 
00099   public IplImage grabberImage() {
00100     return grabberImage;
00101   }
00102 }
 All Classes Namespaces Files Functions Variables Enumerations
Zephyr
RLPark