Question

I am working on one project where I have implemented threading for processing images captured by the camera.

Please find the below code for ProcessFrame() which is called by the timer after some time Interval.

    private void ProcessFrame()
 {            
            try
            {
                 Image<Bgr, Byte> ImageFrame;
                // Get Image from the camera
                ImageFrame = capture.QueryFrame();

              // check if imageFrame is null or not 
                if (ImageFrame == null)
                {
     // if null then re- initialize  the camera
                    try
                    {
                        capture.Dispose();
                        capture = new Capture(URL);                   
                        ImageFrame = capture.QueryFrame();                   
                    }
                    catch (Exception ex)
                    {

                    }
                }
               // resize image to show on Picture control
                ImageFrame = ImageFrame.Resize(img.Width, img.Height, Emgu.CV.CvEnum.INTER.CV_INTER_LINEAR);
    // show image on picture control
                img.Image = ImageFrame;
                try
                {   
     #region Making directory and image in this code
                    string VerifyImageFolderId = VerifyImageFolder + "\\" + "Camera_" + (1) + "\\";

                    if (!Directory.Exists(VerifyImageFolderId))
                        Directory.CreateDirectory(VerifyImageFolderId);

                    string VerifyImageFileName = VerifyImageFolderId + "\\" + nSavedImagesCounter + ".jpg";
                    img.Image.Save(VerifyImageFileName); // Save Image
                    nSavedImagesCounter++;
                    #endregion

                    #region Starting thread For processing Image
                     Thread processImage = new Thread(new ThreadStart(() => ProcessImage(VerifyImageFileName)));
                      processImage.Start();                    
                    #endregion

                }
                catch (Exception ex)
                {
                    Log(ex.Message);
                    MessageBox.Show(ex.Message);
                }
                finally
                {
                    GC.Collect();
                }
                //#endregion
            }
            catch (NullReferenceException e)
            {
                Console.Write("Exception:\n" + DateTime.Now.ToString("hhmmss"));
            }

        }

Here, is the second function ProcessImage(string ImagePath) which is use to perform some file processing operations as follows:

 private void ProcessImage(string ImagePath)
    {            
        #region Check for threadSafeFunction
        if (this.InvokeRequired)
        {
            Console.WriteLine("Inside InvokeRequired");
            this.Invoke(new MethodInvoker(delegate() { ProcessImage(ImagePath); }));
        }
        else
        {
1. Detect faces in Image 
    2. Draw Face markers on Image
    3. Some database based on result of Face Detection
    4. Delete image File            
        }

    }

After adding of the threading working of ProcessFrame() function was slow down. and I am not able to get the live streaming on display.

Can any one help me on this?

Thanks in advance.

Was it helpful?

Solution

i suggest you use queue for processing images run this code once

     BlockingCollection<string> imageQueue=new BlockingCollection<string>();
        new Thread(() =>
        {
            foreach (string imagePath in imageQueue.GetConsumingEnumerable())
            {
                ProcessImage(imagePath);
            }
        }).Start();

and change ProcessFrame Like This

 private void ProcessFrame()
 {


        .....

                #region Starting thread For processing Image
                imageQueue.Add(VerifyImageFileName);
                #endregion

            ...

    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top