Skip to content

Commit

Permalink
Add extra webcam methods and fix delay method
Browse files Browse the repository at this point in the history
  • Loading branch information
Jotschi committed Mar 6, 2023
1 parent 51f2abd commit a50d6b6
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 15 deletions.
21 changes: 17 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ Video4j is a highlevel library ontop of `org.openpnp:opencv` which provides APIs
</dependency>
```

## Typical Usage
## Usage - File

```java
// Load native lib libopencv_java451
Video4j.init();

// Open the video
try (Video video = Videos.open(BIG_BUCK_BUNNY2_PATH)) {
try (VideoFile video = Videos.open(BIG_BUCK_BUNNY2_PATH)) {
// Video dimensions
video.width();
video.height();
Expand Down Expand Up @@ -55,12 +55,25 @@ try (Video video = Videos.open(BIG_BUCK_BUNNY2_PATH)) {
}
```

## Streaming of frames
## Usage - Webcam

```java
Video4j.init();
try (VideoStream video = Videos.open(0)) {
video.setFrameRate(30);
video.enableFormatMJPEG();
video.setFormat(320, 240);
Stream<VideoFrame> frameStream = video.streamFrames();
VideoUtils.showVideoFrameStream(frameStream);
}
```

## Usage - Streaming of frames

Stream of raw OpenCV frame matrices.
```java
Video4j.init();
try (Video video = Videos.open(BIG_BUCK_BUNNY2_PATH)) {
try (VideoFile video = Videos.open(BIG_BUCK_BUNNY2_PATH)) {
Stream<Mat> frameStream = video.streamMat()
.skip(1000)
.map(CVUtils::faceDetectAndDisplay)
Expand Down
24 changes: 24 additions & 0 deletions src/main/java/io/metaloom/video4j/VideoStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,28 @@ public interface VideoStream extends Video {
* @return Fluent API
*/
VideoStream open();

/**
* Set the desired frame rate.
*
* @param fps
* @return Fluent API
*/
VideoStream setFrameRate(double fps);

/**
* Enable the MJPEG output format of the streaming device.
*
* @return
*/
VideoStream enableFormatMJPEG();

/**
* Set the video format.
*
* @param width
* @param height
* @return
*/
VideoStream setFormat(int width, int height);
}
32 changes: 32 additions & 0 deletions src/main/java/io/metaloom/video4j/impl/VideoStreamImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,22 @@
import java.util.Iterator;

import org.opencv.core.Mat;
import org.opencv.videoio.VideoWriter;

import io.metaloom.video4j.AbstractVideo;
import io.metaloom.video4j.Video;
import io.metaloom.video4j.VideoFrame;
import io.metaloom.video4j.VideoStream;
import io.metaloom.video4j.opencv.ExtendedVideoCapture;
import io.metaloom.video4j.opencv.OpenCV;

/**
* @see VideoStream
*/
public class VideoStreamImpl extends AbstractVideo implements VideoStream {

private int id;
private double fps;

public VideoStreamImpl(int id, ExtendedVideoCapture capture) {
super(capture);
Expand All @@ -27,6 +30,35 @@ public VideoStream open() {
if (!capture.open(id)) {
throw new RuntimeException("Video " + id + " could not be opened.");
}
// capture.set(OpenCV.CV_CAP_PROP_BUFFERSIZE, 2);
// capture.set(OpenCV.CV_CAP_PROP_ZOOM, 21.5f);
// capture.set(OpenCV.CV_CAP_PROP_EXPOSURE, 20.9f);

return this;
}

@Override
public VideoStream setFrameRate(double fps) {
this.fps = fps;
capture.set(OpenCV.CV_CAP_PROP_FPS, fps);
return this;
}

@Override
public VideoStream setFormat(int width, int height) {
capture.set(OpenCV.CV_CAP_PROP_FRAME_WIDTH, width);
capture.set(OpenCV.CV_CAP_PROP_FRAME_HEIGHT, height);
return this;
}

@Override
public double fps() {
return fps;
}

@Override
public VideoStream enableFormatMJPEG() {
capture.set(OpenCV.CV_CAP_PROP_FOURCC, VideoWriter.fourcc('M', 'J', 'P', 'G'));
return this;
}

Expand Down
10 changes: 5 additions & 5 deletions src/main/java/io/metaloom/video4j/opencv/OpenCV.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ public class OpenCV {
// public static final int CV_CAP_PROP_DC1394_MODE_MANUAL = -3;
// public static final int CV_CAP_PROP_DC1394_MODE_AUTO = -2;
// public static final int CV_CAP_PROP_DC1394_MODE_ONE_PUSH_AUTO = -1;
// public static final int CV_CAP_PROP_FOURCC = 6;
public static final int CV_CAP_PROP_FOURCC = 6;
// public static final int CV_CAP_PROP_FORMAT = 8;
// public static final int CV_CAP_PROP_MODE = 9;
// public static final int CV_CAP_PROP_HUE = 13;
// public static final int CV_CAP_PROP_GAIN = 14;
// public static final int CV_CAP_PROP_EXPOSURE = 15;
public static final int CV_CAP_PROP_GAIN = 14;
public static final int CV_CAP_PROP_EXPOSURE = 15;
// public static final int CV_CAP_PROP_CONVERT_RGB = 16;
// public static final int CV_CAP_PROP_WHITE_BALANCE_BLUE_U = 17;
// public static final int CV_CAP_PROP_RECTIFICATION = 18;
Expand All @@ -44,7 +44,7 @@ public class OpenCV {
// public static final int CV_CAP_PROP_TRIGGER = 24;
// public static final int CV_CAP_PROP_TRIGGER_DELAY = 25;
// public static final int CV_CAP_PROP_WHITE_BALANCE_RED_V = 26;
// public static final int CV_CAP_PROP_ZOOM = 27;
public static final int CV_CAP_PROP_ZOOM = 27;
// public static final int CV_CAP_PROP_FOCUS = 28;
// public static final int CV_CAP_PROP_GUID = 29;
// public static final int CV_CAP_PROP_ISO_SPEED = 30;
Expand All @@ -55,7 +55,7 @@ public class OpenCV {
// public static final int CV_CAP_PROP_ROLL = 35;
// public static final int CV_CAP_PROP_IRIS = 36;
// public static final int CV_CAP_PROP_SETTINGS = 37;
// public static final int CV_CAP_PROP_BUFFERSIZE = 38;
public static final int CV_CAP_PROP_BUFFERSIZE = 38;
// public static final int CV_CAP_PROP_AUTOFOCUS = 39;
// public static final int CV_CAP_PROP_SAR_NUM = 40;
// public static final int CV_CAP_PROP_SAR_DEN = 41;
Expand Down
10 changes: 4 additions & 6 deletions src/test/java/io/metaloom/video4j/VideoStreamTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,17 @@

import org.junit.Test;

import io.metaloom.video4j.opencv.CVUtils;
import io.metaloom.video4j.utils.VideoUtils;

public class VideoStreamTest extends AbstractVideoTest {

@Test
public void testStreaming() throws Exception {
try (VideoStream video = Videos.open(0)) {
Stream<VideoFrame> frameStream = video.streamFrames()
.map(frame -> {
CVUtils.boxFrame2(frame, 128);
return frame;
});
video.setFrameRate(30);
video.enableFormatMJPEG();
video.setFormat(320, 240);
Stream<VideoFrame> frameStream = video.streamFrames();
VideoUtils.showVideoFrameStream(frameStream);
}
}
Expand Down

0 comments on commit a50d6b6

Please sign in to comment.