Skip to content

Commit

Permalink
Speed up detector instantiation when IP camera is used
Browse files Browse the repository at this point in the history
  • Loading branch information
sarxos committed Oct 12, 2014
1 parent 2ebe58d commit 53988ef
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ public void run() {
/**
* Center of motion gravity.
*/
private Point cog = null;
private Point cog = new Point(0, 0);

/**
* Timestamp when motion has been observed last time.
Expand Down Expand Up @@ -216,19 +216,15 @@ public WebcamMotionDetector(Webcam webcam, int pixelThreshold, double areaThresh
setPixelThreshold(pixelThreshold);
setAreaThreshold(areaThreshold);
setInterval(interval);

int w = webcam.getViewSize().width;
int h = webcam.getViewSize().height;

cog = new Point(w / 2, h / 2);
}

/**
* Create motion detector with default parameter inertia = 0.
*
* @param webcam web camera instance
* @param pixelThreshold intensity threshold (0 - 255)
* @param areaThreshold percentage threshold of image covered by motion (0 - 100)
* @param areaThreshold percentage threshold of image covered by motion (0 -
* 100)
*/
public WebcamMotionDetector(Webcam webcam, int pixelThreshold, double areaThreshold) {
this(webcam, pixelThreshold, areaThreshold, DEFAULT_INTERVAL);
Expand All @@ -245,7 +241,8 @@ public WebcamMotionDetector(Webcam webcam, int pixelThreshold) {
}

/**
* Create motion detector with default parameters - threshold = 25, inertia = 0.
* Create motion detector with default parameters - threshold = 25, inertia
* = 0.
*
* @param webcam web camera instance
*/
Expand All @@ -255,7 +252,14 @@ public WebcamMotionDetector(Webcam webcam) {

public void start() {
if (running.compareAndSet(false, true)) {

webcam.open();

int w = webcam.getViewSize().width;
int h = webcam.getViewSize().height;

cog = new Point(w / 2, h / 2);

executor.submit(new Runner());
executor.submit(new Inverter());
}
Expand Down Expand Up @@ -377,8 +381,8 @@ public int getInterval() {
}

/**
* Motion check interval in milliseconds. After motion is detected, it's valid for time which is
* equal to value of 2 * interval.
* Motion check interval in milliseconds. After motion is detected, it's
* valid for time which is equal to value of 2 * interval.
*
* @param interval the new motion check interval (ms)
* @see #DEFAULT_INTERVAL
Expand All @@ -393,9 +397,9 @@ public void setInterval(int interval) {
}

/**
* Set pixel intensity difference threshold above which pixel is classified as "moved". Minimum
* value is 0 and maximum is 255. Default value is 10. This value is equal for all RGB
* components difference.
* Set pixel intensity difference threshold above which pixel is classified
* as "moved". Minimum value is 0 and maximum is 255. Default value is 10.
* This value is equal for all RGB components difference.
*
* @param threshold the pixel intensity difference threshold
* @see #DEFAULT_PIXEL_THREASHOLD
Expand All @@ -411,9 +415,9 @@ public void setPixelThreshold(int threshold) {
}

/**
* Set percentage fraction of detected motion area threshold above which it is classified as
* "moved". Minimum value for this is 0 and maximum is 100, which corresponds to full image
* covered by spontaneous motion.
* Set percentage fraction of detected motion area threshold above which it
* is classified as "moved". Minimum value for this is 0 and maximum is 100,
* which corresponds to full image covered by spontaneous motion.
*
* @param threshold the percentage fraction of image area
* @see #DEFAULT_AREA_THREASHOLD
Expand All @@ -429,8 +433,9 @@ public void setAreaThreshold(double threshold) {
}

/**
* Set motion inertia (time when motion is valid). If no value specified this is set to 2 *
* interval. To reset to default value, {@link #clearInertia()} method must be used.
* Set motion inertia (time when motion is valid). If no value specified
* this is set to 2 * interval. To reset to default value,
* {@link #clearInertia()} method must be used.
*
* @param inertia the motion inertia time in milliseconds
* @see #clearInertia()
Expand All @@ -443,8 +448,8 @@ public void setInertia(int inertia) {
}

/**
* Reset inertia time to value calculated automatically on the base of interval. This value will
* be set to 2 * interval.
* Reset inertia time to value calculated automatically on the base of
* interval. This value will be set to 2 * interval.
*/
public void clearInertia() {
this.inertia = -1;
Expand All @@ -467,8 +472,8 @@ public boolean isMotion() {
}

/**
* Get percentage fraction of image covered by motion. 0 means no motion on image and 100 means
* full image covered by spontaneous motion.
* Get percentage fraction of image covered by motion. 0 means no motion on
* image and 100 means full image covered by spontaneous motion.
*
* @return Return percentage image fraction covered by motion
*/
Expand All @@ -477,8 +482,8 @@ public double getMotionArea() {
}

/**
* Get motion center of gravity. When no motion is detected this value points to the image
* center.
* Get motion center of gravity. When no motion is detected this value
* points to the image center.
*
* @return Center of gravity point
*/
Expand Down
101 changes: 61 additions & 40 deletions webcam-capture/src/main/java/com/github/sarxos/webcam/WebcamPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@


/**
* Simply implementation of JPanel allowing users to render pictures taken with webcam.
* Simply implementation of JPanel allowing users to render pictures taken with
* webcam.
*
* @author Bartosz Firyn (SarXos)
*/
Expand All @@ -55,20 +56,20 @@ public class WebcamPanel extends JPanel implements WebcamListener, PropertyChang
public static enum DrawMode {

/**
* Do not resize image - paint it as it is. This will make the image to go off out the
* bounds if panel is smaller than image size.
* Do not resize image - paint it as it is. This will make the image to
* go off out the bounds if panel is smaller than image size.
*/
NONE,

/**
* Will resize image to the panel bounds. This mode does not care of the image scale, so the
* final image may be disrupted.
* Will resize image to the panel bounds. This mode does not care of the
* image scale, so the final image may be disrupted.
*/
FILL,

/**
* Will fir image into the panel bounds. This will resize the image and keep both x and y
* scale factor.
* Will fir image into the panel bounds. This will resize the image and
* keep both x and y scale factor.
*/
FIT,
}
Expand Down Expand Up @@ -515,7 +516,8 @@ public void run() {
private AtomicBoolean running = new AtomicBoolean(false);

/**
* Start repainter. Can be invoked many times, but only first call will take effect.
* Start repainter. Can be invoked many times, but only first call will
* take effect.
*/
public void start() {
if (running.compareAndSet(false, true)) {
Expand All @@ -526,7 +528,8 @@ public void start() {
}

/**
* Stop repainter. Can be invoked many times, but only first call will take effect.
* Stop repainter. Can be invoked many times, but only first call will
* take effect.
*
* @throws InterruptedException
*/
Expand Down Expand Up @@ -588,8 +591,8 @@ private void update() {
private ResourceBundle rb = null;

/**
* The mode of how the image will be resized to fit into panel bounds. Default is
* {@link DrawMode#FIT}
* The mode of how the image will be resized to fit into panel bounds.
* Default is {@link DrawMode#FIT}
*
* @see DrawMode
*/
Expand All @@ -601,8 +604,9 @@ private void update() {
private double frequency = 5; // FPS

/**
* Is frames requesting frequency limited? If true, images will be fetched in configured time
* intervals. If false, images will be fetched as fast as camera can serve them.
* Is frames requesting frequency limited? If true, images will be fetched
* in configured time intervals. If false, images will be fetched as fast as
* camera can serve them.
*/
private boolean frequencyLimit = false;

Expand All @@ -627,7 +631,8 @@ private void update() {
private final Webcam webcam;

/**
* Repainter is used to fetch images from camera and force panel repaint when image is ready.
* Repainter is used to fetch images from camera and force panel repaint
* when image is ready.
*/
private final ImageUpdater updater;

Expand Down Expand Up @@ -694,7 +699,8 @@ public WebcamPanel(Webcam webcam) {
}

/**
* Creates new webcam panel which display image from camera in you your Swing application.
* Creates new webcam panel which display image from camera in you your
* Swing application.
*
* @param webcam the webcam to be used to fetch images
* @param start true if webcam shall be automatically started
Expand All @@ -704,10 +710,11 @@ public WebcamPanel(Webcam webcam, boolean start) {
}

/**
* Creates new webcam panel which display image from camera in you your Swing application. If
* panel size argument is null, then image size will be used. If you would like to fill panel
* area with image even if its size is different, then you can use
* {@link WebcamPanel#setFillArea(boolean)} method to configure this.
* Creates new webcam panel which display image from camera in you your
* Swing application. If panel size argument is null, then image size will
* be used. If you would like to fill panel area with image even if its size
* is different, then you can use {@link WebcamPanel#setFillArea(boolean)}
* method to configure this.
*
* @param webcam the webcam to be used to fetch images
* @param size the size of panel
Expand Down Expand Up @@ -881,10 +888,10 @@ public boolean isFPSLimited() {
}

/**
* Enable or disable frequency limit. Frequency limit should be used for <b>all IP cameras
* working in pull mode</b> (to save number of HTTP requests). If true, images will be fetched
* in configured time intervals. If false, images will be fetched as fast as camera can serve
* them.
* Enable or disable frequency limit. Frequency limit should be used for
* <b>all IP cameras working in pull mode</b> (to save number of HTTP
* requests). If true, images will be fetched in configured time intervals.
* If false, images will be fetched as fast as camera can serve them.
*
* @param frequencyLimit
*/
Expand All @@ -902,8 +909,8 @@ public double getFPSLimit() {
}

/**
* Set rendering frequency (in Hz or FPS). Minimum frequency is 0.016 (1 frame per minute) and
* maximum is 25 (25 frames per second).
* Set rendering frequency (in Hz or FPS). Minimum frequency is 0.016 (1
* frame per minute) and maximum is 25 (25 frames per second).
*
* @param fps the frequency
*/
Expand Down Expand Up @@ -936,8 +943,8 @@ public void setDisplayDebugInfo(boolean displayDebugInfo) {
}

/**
* This method return true in case if camera FPS is set to be displayed on panel surface.
* Default value returned is false.
* This method return true in case if camera FPS is set to be displayed on
* panel surface. Default value returned is false.
*
* @return True if camera FPS is set to be displayed on panel surface
* @see #setFPSDisplayed(boolean)
Expand All @@ -947,7 +954,8 @@ public boolean isFPSDisplayed() {
}

/**
* This method is to control if camera FPS should be displayed on the webcam panel surface.
* This method is to control if camera FPS should be displayed on the webcam
* panel surface.
*
* @param displayed the value to control if camera FPS should be displayed
*/
Expand All @@ -956,8 +964,9 @@ public void setFPSDisplayed(boolean displayed) {
}

/**
* This method will return true in case when panel is configured to display image size. The
* string will be printed in the right bottom corner of the panel surface.
* This method will return true in case when panel is configured to display
* image size. The string will be printed in the right bottom corner of the
* panel surface.
*
* @return True in case if panel is configured to display image size
*/
Expand Down Expand Up @@ -1013,9 +1022,10 @@ public boolean isFitArea() {
}

/**
* This method will change the mode of panel area painting so the image will be resized and will
* keep scale factor to fit into drawable panel bounds. When set to false, the mode will be
* reset to {@link DrawMode#NONE} so image will be drawn as it is.
* This method will change the mode of panel area painting so the image will
* be resized and will keep scale factor to fit into drawable panel bounds.
* When set to false, the mode will be reset to {@link DrawMode#NONE} so
* image will be drawn as it is.
*
* @param fitArea the fit area mode enabled or disabled
*/
Expand All @@ -1024,8 +1034,8 @@ public void setFitArea(boolean fitArea) {
}

/**
* Image will be resized to fill panel area if true. If false then image will be rendered as it
* was obtained from webcam instance.
* Image will be resized to fill panel area if true. If false then image
* will be rendered as it was obtained from webcam instance.
*
* @param fillArea shall image be resided to fill panel area
*/
Expand All @@ -1034,8 +1044,9 @@ public void setFillArea(boolean fillArea) {
}

/**
* Get value of fill area setting. Image will be resized to fill panel area if true. If false
* then image will be rendered as it was obtained from webcam instance.
* Get value of fill area setting. Image will be resized to fill panel area
* if true. If false then image will be rendered as it was obtained from
* webcam instance.
*
* @return True if image is being resized, false otherwise
*/
Expand Down Expand Up @@ -1087,7 +1098,8 @@ public void webcamImageObtained(WebcamEvent we) {
}

/**
* This method returns true if image mirroring is enabled. The default value is false.
* This method returns true if image mirroring is enabled. The default value
* is false.
*
* @return True if image is mirrored, false otherwise
*/
Expand All @@ -1096,12 +1108,21 @@ public boolean isMirrored() {
}

/**
* Decide whether or not the image from webcam painted on panel surface will be mirrored. The
* image from camera itself is not modified.
* Decide whether or not the image from webcam painted on panel surface will
* be mirrored. The image from camera itself is not modified.
*
* @param mirrored the parameter to control if image should be mirrored
*/
public void setMirrored(boolean mirrored) {
this.mirrored = mirrored;
}

/**
* Return {@link Webcam} used by this panel.
*
* @return {@link Webcam}
*/
public Webcam getWebcam() {
return webcam;
}
}

0 comments on commit 53988ef

Please sign in to comment.