}//FileCreated

        protected virtual void OnImageValidated(ByteWrapper img)
        {
            if(imageValidated != null)
            {
                imageValidated(img, EventArgs.Empty);
            }

        }//OnImageValidated
 /// <summary>
 /// Called when the object detects motion, creates the event
 /// </summary>
 /// <param name="image"></param>
 protected async void OnMotionAsync(ByteWrapper image1, ByteWrapper image2)
 {
     await Task.Run(() =>
     {
         if (motionDetected != null)
         {
             motionDetected(image1, EventArgs.Empty);
         }
     });
 }
        public override void Compare(ByteWrapper image1, ByteWrapper image2)
        {
            var bm1 = new BitmapWrapper(ImageConvert.ReturnBitmap(image1.bytes));
            bm1.sequenceNumber = logging.imagesReceived - 2;
            var bm2 = new BitmapWrapper(ImageConvert.ReturnBitmap(image2.bytes));
            bm2.sequenceNumber = logging.imagesReceived - 1;

            PixelMatrix matrix = new PixelMatrix();
            matrix.LinkCompare = settings.linkCompare;
            if (settings.searchHeight > 0) { matrix.SearchHeight = settings.searchHeight; }
            if (settings.searchWidth > 0) { matrix.SearchWidth = settings.searchWidth; }
            if (settings.horizontalPixelsToSkip > 0) { matrix.WidthSearchOffset = settings.horizontalPixelsToSkip + 1; }
            if (settings.verticalPixelsToSkip > 0) { matrix.WidthSearchOffset = settings.verticalPixelsToSkip + 1; }

            if (settings.linkCompare && Comparison != null)
            {
                matrix.Populate(Comparison, bm2);
            }
            else
            {
                matrix.Populate(bm1, bm2);
            }

            double sumChangedPixels = matrix.SumChangedPixelsPositive;

            //keep adding for threshold calculation, set the threshold, or monitor
            if (ThresholdSet)
            {
                //now scanning, compare the two images and see what the difference is
                if (sumChangedPixels > pixelChangeThreshold)
                {
                    OnMotionAsync(image1, image2);
                }
            }
            else if (!ThresholdSet && pixelChange.Count < ControlImageNumber)
            {
                pixelChange.Add(sumChangedPixels);
            }
            else
            {
                SetThreshold(); //enough images received to set the threshold and start monitoring
            }

            Comparison = matrix.Comparator;

            if (logging.LoggingOn && logging.matrices != null) { logging.matrices.Add(matrix); }

            //clean up the memory
            matrix.Dispose();
            bm1.bitmap.Dispose();
            bm2.bitmap.Dispose();
            bm1 = null;
            bm2 = null;

        }//Compare
        public async void ImageCreatedAsync(ByteWrapper img, EventArgs e)
        {
            backlog.lastImageReceived = img.sequenceNumber;
            logging.imagesReceived++;

            AddToWorkQueue(img); //do this outside the anonymous method
            
            await Task.Run(() =>
            {                
                SendForCompareAsync(); 
            });         
        }
        public void FileCreated(ByteWrapper img, EventArgs e)
        {
            if(img.bytes[0] == JPEG_start[0] && img.bytes[1] == JPEG_start[1] && img.bytes[img.bytes.Length -2] == JPEG_end[0] && img.bytes[img.bytes.Length -1] == JPEG_end[1])
            {
                OnImageValidated(img); //raise the event
            }
            else
            {
                //throw new Exception("JPEG image is not well formed");
                Console.WriteLine("Badly formed image");
            }

        }//FileCreated
        public ImageGrid ThresholdImage{get; private set;} //the thresholds, per grid

        public override void Compare(ByteWrapper image1, ByteWrapper image2)
        {
            var bm1 = new BitmapWrapper(ImageConvert.ReturnBitmap(image1.bytes));
            var bm2 = new BitmapWrapper(ImageConvert.ReturnBitmap(image2.bytes));

            PixelMatrix matrix = new PixelMatrix();
            if (settings.searchHeight > 0) { matrix.SearchHeight = settings.searchHeight; }
            if (settings.searchWidth > 0) { matrix.SearchWidth = settings.searchWidth; }
            if (settings.linkCompare) { matrix.LinkCompare = true; }
            matrix.GridSystemOn = true;
            matrix.Populate(bm1, bm2);

            double sumChangedPixels = matrix.SumChangedPixels;

            //keep adding for threshold calculation, set the threshold, or monitor
            if (ThresholdSet)
            {
                //do the motion detection
                for(int i = 0; i < ThresholdImage.Columns.Count; i++)
                {
                    for(int n = 0; n < ThresholdImage.Columns[i].grids.Count; n++)
                    {
                        if(matrix.imageGrid.Columns[i].grids[n].change > ThresholdImage.Columns[i].grids[n].threshold)
                        {
                            OnMotion(image1, image2, ThresholdImage.GridNumber(i, n));
                            return;
                        }
                    }
                }
            }
            else if (!ThresholdSet && gridImages.Count < ControlImageNumber)
            {
                gridImages.Add(matrix.imageGrid); //keep adding for the later threshold calculation
            }
            else
            {
                SetThreshold(); //enough images received to set the threshold and start monitoring
            }

            Comparison = matrix.Comparator;

            //clean up the memory
            matrix.Dispose();
            bm1.bitmap.Dispose();
            bm2.bitmap.Dispose();
            bm1 = null;
            bm2 = null;

        }//Compare
 /// <summary>
 /// Returns a byte wrappper from a JPEG file
 /// </summary>
 /// <param name="filepath"></param>
 /// <returns></returns>
 public static ByteWrapper ReturnByteWrapper(string filepath)
 {
     ByteWrapper result = new ByteWrapper(File.ReadAllBytes(filepath));
     return result;
 }
 public void ImageCreated(ByteWrapper img, EventArgs e)
 {
     backlog.lastImageReceived = img.sequenceNumber;
     logging.imagesReceived++;
     AddToWorkQueue(img);
     SendForCompare();
 }
 /// <summary>
 /// Overload for the OnMotion procedure that includes an integer which defines
 /// the grid number in which motion was detected
 /// </summary>
 /// <param name="image1"></param>
 /// <param name="image2"></param>
 /// <param name="motionGrid"></param>
 protected void OnMotion(ByteWrapper image1, ByteWrapper image2, int motionGrid)
 {
     OnMotionAsync(image1, image2);
 }
        }//SendForCompare

        public virtual void Compare(ByteWrapper img1, ByteWrapper img2) { } //will always be implemented in the derived class
 /// <summary>
 /// Event listener for when an image is created
 /// Fires the appropriate methods to classify and save the
 /// Image
 /// </summary>
 /// <param name="img"></param>
 /// <returns></returns>
 public async Task ImageCreatedAsync(ByteWrapper img)
 {
     await Task.Run(() => {
         WriteBytesToFile(img);
         SetSection();
     } );
 }
 private void ImageExtracted(ByteWrapper img, EventArgs e)
 {
     ImageBytes = img.bytes;
     ConnectSuccess = true;
 }
 /// <summary>
 /// Adds to the queue, unless frame should be skipped
 /// </summary>
 /// <param name="img"></param>
 private void AddToWorkQueue(ByteWrapper img)
 {
     if (this.settings.framesToSkip > 0)
     {
         if (numberSkipped == settings.framesToSkip)
         {
             WorkQueue.Enqueue(img);
             numberSkipped = 0;
         }
         else
         {
             numberSkipped++;
         }
     }
     else
     {
         WorkQueue.Enqueue(img);
     }
 }
 /// <summary>
 /// Event listener for when an image is created
 /// Fires the appropriate methods to classify and save the
 /// Image
 /// </summary>
 /// <param name="img"></param>
 /// <param name="e"></param>
 /// <returns></returns>
 public async void ImageCreatedAsync(ByteWrapper img, EventArgs e)
 {
     await Task.Run(() => {
         // WriteBytesToFile(img);
         if (saveToFileServer) { WriteBytesToFile(img); }
         if (saveToDatabase) { WriteBytesToDatabaseAsync(img); }
         SetSection();
     });
 }
 public static async void WriteBytesToFileAsync(ByteWrapper image, string filepath)
 {
     await Task.Run(() => {
         using (FileStream fs = new FileStream(filepath, FileMode.Create))
         {
             fs.Write(image.bytes, 0, image.bytes.Length);
         }
     });
 }
        /// <summary>
        /// Writes a single file to the location specified, called outide of this
        /// </summary>
        public async void WriteBytesToFileAsync(ByteWrapper image, EventArgs e)
        {
            await Task.Run(() => {
                string fileName = GenerateFileName();
                using (FileStream fs = new FileStream(GenerateFileName(image.sequenceNumber), FileMode.Create))
                {
                    fs.Write(image.bytes, 0, image.bytes.Length);
                }
                if (imageCreated != null) { imageCreated(fileName, EventArgs.Empty); }
            });

        }
        private void WriteBytesToFile(ByteWrapper img)
        {
            try
            {
                string fileName = GenerateFileName();
                using (FileStream fs = new FileStream(fileName, FileMode.Create))
                {
                    fs.Write(img.bytes, 0, img.bytes.Length);
                }
                if (imageCreated != null) { imageCreated(fileName, EventArgs.Empty); }

            }
            catch(Exception ex)
            {
                Console.WriteLine(DateTime.Now + " - " + ex.Message);
            }
        }
        }//WriteDatafileSummary

        private async void WriteBytesToDatabaseAsync(ByteWrapper image)
        {
            await Task.Run(() => {

                var db = new ImageAnalysisDAL.CaptureDb(ConfigurationManager.ConnectionStrings["LOCALDB"].ConnectionString);
                db.SaveDetectionImage(captureId, detectionId, image.bytes, image.sequenceNumber);

            });
        }
Example #19
0
        /// <summary>
        /// Subscribes to a Image Validated event, then calls various other tied in methods
        /// </summary>
        /// <param name="img"></param>
        /// <param name="e"></param>
        static void ValidImageEventHandler(ByteWrapper img, EventArgs e)
        {
            if(imageSaver != null)
            {
                Task saveImage = imageSaver.ImageCreatedAsync(img);
            }

        }
 private void MotionDetected(ByteWrapper image, EventArgs e)
 {
     Console.WriteLine(image.sequenceNumber);
 }