1 package HomeSecurityController;
4 import iotcode.annotation.*;
5 import iotcode.interfaces.*;
8 import boofcv.alg.background.BackgroundModelStationary;
9 import boofcv.factory.background.ConfigBackgroundGaussian;
10 import boofcv.factory.background.FactoryBackgroundModel;
11 import boofcv.gui.binary.VisualizeBinaryData;
12 import boofcv.gui.image.ImageGridPanel;
13 import boofcv.gui.image.ShowImages;
14 import boofcv.io.MediaManager;
15 import boofcv.io.UtilIO;
16 import boofcv.io.image.SimpleImageSequence;
17 import boofcv.io.image.ConvertBufferedImage;
18 import boofcv.io.wrapper.DefaultMediaManager;
19 import boofcv.struct.image.ImageBase;
20 import boofcv.struct.image.ImageFloat32;
21 import boofcv.struct.image.ImageType;
22 import boofcv.struct.image.ImageUInt8;
23 import boofcv.alg.filter.blur.BlurImageOps;
26 // Standard Java Packages
27 import java.awt.image.BufferedImage;
28 import java.util.Date;
29 import java.util.concurrent.locks.Lock;
30 import java.util.concurrent.locks.ReadWriteLock;
31 import java.util.concurrent.locks.ReentrantReadWriteLock;
32 import java.util.concurrent.atomic.AtomicBoolean;
33 import java.awt.image.ColorModel;
34 import java.awt.image.WritableRaster;
35 import java.util.List;
36 import java.util.ArrayList;
37 import java.io.ByteArrayInputStream;
38 import java.io.ByteArrayOutputStream;
40 import java.io.IOException;
41 import java.io.InputStream;
42 import javax.imageio.ImageIO;
45 import java.rmi.RemoteException;
50 import java.rmi.Remote;
51 import java.rmi.RemoteException;
53 import java.util.Iterator;
55 // Checker annotations
56 //import iotchecker.qual.*;
58 /** Class MotionDetection to do motion detection using images
61 * @author Ali Younis <ayounis @ uci.edu>
65 class MotionDetection implements CameraCallback {
67 // Define Like variables
68 private static boolean DO_GRAPHICAL_USER_INTERFACE = false;
70 /*******************************************************************************************************************************************
74 *******************************************************************************************************************************************/
75 private final float MOTION_DETECTED_THRESHOLD_PERCENTAGE = 15;
78 /*******************************************************************************************************************************************
82 *******************************************************************************************************************************************/
84 // Timestamp buffer and locks needed for that safety on that buffer
85 // This is the buffer for post-detection algorithm use
86 private Date timestampOfLastMotion = null;
87 private ReadWriteLock timestampReadWriteLock = new ReentrantReadWriteLock();
88 private Lock timestampReadLock = timestampReadWriteLock.readLock();
89 private Lock timestampWriteLock = timestampReadWriteLock.writeLock();
91 // Flag for when new data is available and ready for processing
92 private AtomicBoolean newFrameAvailable = new AtomicBoolean(false);
94 // Flag for determining if motion has been detected and therefore
95 // the callbacks should be issued
96 private AtomicBoolean motionDetected = new AtomicBoolean(false);
98 // Image and timestamp buffers and locks needed for that safety on those buffers
99 // Timestamp buffer for pre-detection algorithm use
100 private BufferedImage latestImage = null;
101 private Date possibleDate = null;
102 private ReadWriteLock imageReadWriteLock = new ReentrantReadWriteLock();
103 private Lock imageReadLock = imageReadWriteLock.readLock();
104 private Lock imageWriteLock = imageReadWriteLock.writeLock();
106 // List of objects wishing to receive callbacks from this class.
107 private List<MotionDetectionCallback>
108 callbackList = new ArrayList<MotionDetectionCallback>();
110 // Variables to help with motion detection
111 private ConfigBackgroundGaussian configGaussian = null;
112 private BackgroundModelStationary backgroundDetector = null;
113 private ImageUInt8 segmented = null;
114 private ImageFloat32 newFrameFloat = null;
116 // counts the number of frames since a background image is added to algorithm
117 private int frameCounter = 0;
119 private CameraSmart _camera;
121 /*******************************************************************************************************************************************
125 *******************************************************************************************************************************************/
126 private Thread workThread = null;
127 private Thread callBackThread = null;
129 /*******************************************************************************************************************************************
131 ** GUI Stuff (Used Only for Testing)
133 *******************************************************************************************************************************************/
138 * @param _threshold [float], Variable for gaussian background detector.
139 * @param _learnSpeed [float], Variable for gaussian background detector.
140 * @param _initialVariance [float], Variable for gaussian background detector.
141 * @param _minDifference [float], Variable for gaussian background detector.
144 public MotionDetection(float _threshold, float _learnSpeed, float _initialVariance, float _minDifference) {
146 // Configure the Gaussian model used for background detection
147 configGaussian = new ConfigBackgroundGaussian(_threshold, _learnSpeed);
148 configGaussian.initialVariance = _initialVariance;
149 configGaussian.minimumDifference = _minDifference;
151 // setup the background detector
152 ImageType imageType = ImageType.single(ImageFloat32.class);
153 backgroundDetector = FactoryBackgroundModel.stationaryGaussian(configGaussian, imageType);
155 // setup the gui if we are going to use it
156 if (DO_GRAPHICAL_USER_INTERFACE) {
158 // create an image grid for images to place on, tile fashion
159 gui = new ImageGridPanel(1, 2);
161 // make the window large so we dont have to manually resize with the mouse
162 gui.setSize(1920, 1080);
164 // Make the window visible and set the title
165 ShowImages.showWindow(gui, "Static Scene: Background Segmentation", true);
168 // Launch the worker thread
169 workThread = new Thread(new Runnable() {
173 runMotionDetection();
180 // Launch the callback thread
181 callBackThread = new Thread(new Runnable() {
189 callBackThread.start();
193 /*******************************************************************************************************************************************
197 *******************************************************************************************************************************************/
199 /** Method to add a new frame to the motion detector.
201 * @param _newFrame [byte[]], Frame data of new frame.
202 * @param _timestamp [Date] , Timestamp of new frame.
204 * @return [void] None.
206 public void addFrame(byte[] _newFrame, Date _timestamp) {
207 BufferedImage img = null;
210 // Parse the byte array into a Buffered Image
211 InputStream in = new ByteArrayInputStream(_newFrame);
212 img = ImageIO.read(in);
214 } catch (Exception e) {
219 // Save the image and timestamp for use later
220 imageWriteLock.lock();// lock the image and timestamp buffers since multithread
221 latestImage = img;// image into image buffer
222 possibleDate = _timestamp;// timestamp into timestamp buffer
223 imageWriteLock.unlock();// Never forget to unlock
225 // flag the worker thread that there is new data ready for processing
226 newFrameAvailable.set(true);
229 /** Method to get the timestamp of the last time motion was detected
231 * @return [Date] timestamp of last motion or null if no motion was ever detected.
233 public Date getTimestampOfLastMotion() {
236 // Be safe because multithread
237 timestampReadLock.lock();
239 // Checks if there was ever motion, if not then timestampOfLastMotion
241 if (timestampOfLastMotion != null) {
242 // Clone since we don't know what the other person is going to do
243 // with the timestamp
244 ret = (Date)timestampOfLastMotion.clone();
247 timestampReadLock.unlock();
253 /** Method to add a new frame to the motion detector from a camera
255 * @param _camera [Camera], Camera that has the new data.
257 * @return [void] None.
259 public void newCameraFrameAvailable(byte[] latestFrame, long timeStamp) {
260 BufferedImage img = null;
263 // Parse the byte array into a Buffered Image
264 //InputStream in = new ByteArrayInputStream(_camera.getLatestFrame());
265 InputStream in = new ByteArrayInputStream(latestFrame);
266 img = ImageIO.read(in);
268 } catch (RemoteException e) {
272 } catch (Exception e) {
278 // Save the image and timestamp for use later
279 imageWriteLock.lock(); // lock the image and timestamp buffers since multithread
280 latestImage = img; // image into image buffer
282 // timestamp from camera into timestamp buffer
283 long dateLong = timeStamp;
284 possibleDate = new Date(dateLong);
286 imageWriteLock.unlock(); // Never forget to unlock
288 // flag the worker thread that there is new data ready for processing
289 newFrameAvailable.set(true);
292 /** Method to register an object to recieve callbacks from this motion detector
294 * @param _mdc [MotionDetectionCallback], object to recieve callbacks.
296 * @return [void] None.
298 public void registerCallback(MotionDetectionCallback _mdc) {
299 callbackList.add(_mdc);
302 /*******************************************************************************************************************************************
306 *******************************************************************************************************************************************/
308 /** Method that constantly loops checking if new data is available. If there is
309 * new data, it is processed.
310 * This method should be run on a separate thread.
312 * @return [void] None.
314 private void runMotionDetection() {
316 // check if there is a new frame availble, only runs detection if there is new data to save
318 if (!newFrameAvailable.get()) {
322 // Lock since we are accessing the data buffers
323 imageReadLock.lock();
325 // processing data so now the buffered data is old
326 newFrameAvailable.set(false);
328 // copy from buffer to local for processing
329 Date tmpDate = possibleDate;
331 // Allocate space for the segmented image based on the first image we received
332 // cannot pre-allocate this since we do not know what the size of the images is
333 // before the first image arrives
334 if (segmented == null) {
335 segmented = new ImageUInt8(latestImage.getWidth(), latestImage.getHeight());
338 // copy from data buffers and convert into correct data type for BoofCv libraries
339 newFrameFloat = ConvertBufferedImage.convertFrom(latestImage, newFrameFloat);
341 // All done accessing the data buffers
342 imageReadLock.unlock();
344 // Run background detection
345 backgroundDetector.segment(newFrameFloat, segmented);
347 // Update the background baseline every 10 frames, helps the algorithm
349 if (frameCounter > 10) {
350 backgroundDetector.updateBackground(newFrameFloat);
354 // get the raw pixel data, gray-scale image
355 byte[] frameData = segmented.getData();
357 // count the number of pixels of the image that was deemed as "motion"
359 double countMotion = 0;
360 for (byte b : frameData) {
367 // calculate the percentage of the image that was in motion
368 double percentMotion = (countMotion / count) * 100.0;
370 // Check if a high enough percentage of the image was in motion to say that there was motion in this frame of data
371 if (percentMotion > MOTION_DETECTED_THRESHOLD_PERCENTAGE) {
373 // Motion detected so save timestamp of this frame to another buffer
374 timestampWriteLock.lock();
375 timestampOfLastMotion = (Date)tmpDate.clone(); // clone to a different buffer
376 timestampWriteLock.unlock();
378 System.out.println("Motion Detected (with percentage: " + Double.toString(percentMotion) + "%)");
381 // Do output to the screen if we are using gui mode
382 if (DO_GRAPHICAL_USER_INTERFACE) {
384 // change image data unto correct type for rendering
385 BufferedImage visualized1 = new BufferedImage(segmented.width, segmented.height, BufferedImage.TYPE_INT_RGB);
386 VisualizeBinaryData.renderBinary(segmented, false, visualized1);
388 // change image data unto correct type for rendering
389 BufferedImage visualized2 = null;
390 visualized2 = ConvertBufferedImage.convertTo(newFrameFloat, visualized2, true);
392 // place the images into the image grid
393 gui.setImage(0, 1, visualized1);
394 gui.setImage(0, 2, visualized2);
401 /** Method that constantly loops checking if the callbacks should be issues and
402 * issues the callbacks if they should be issues.
403 * This method should be run on a separate thread.
405 * @return [void] None.
407 private void doCallbacks() {
409 // Keep looping forever for callback
412 // If motion detected
413 if (motionDetected.compareAndSet(true, false)) {
415 // Motion was detected so issue callbacks to all objects that registered
416 // to receive callback from this class.
417 for (MotionDetectionCallback c : callbackList) {
418 c.motionDetected(this);
423 // Sleep for 15 millisec to give time for new frame to arrive
426 } catch (InterruptedException ie) {