0

I am trying to extract images out of video using opencvsharp and I need to create a pdf and store only those images in pdf where there is a text content in the image.

I have written following code till now, Please help or advice further on how to identify and extract frames only where there is a text in image.

Thanks in advance!

using OpenCvSharp;
using System;
using System.IO;

namespace VideoToImage
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Begin video to image program..");   
            var videoFile = "C:\\Users\\sinequa\\Downloads\\cloud-computing-in-6-minutes-what-is-cloud-computing-cloud-computing-explained-simplilearn.mp4"; // Specify path to MP4 video file.
            var outputPath = "output_images"; // Path is relative to working directory of the app
            System.IO.Directory.CreateDirectory(outputPath);

            var capture = new VideoCapture(videoFile);
            var image = new Mat();
            int i = 0;
            
            Console.WriteLine("Begin extracting frames from video file..");
            bool capOpen=capture.Open(videoFile);
            bool isCapOpen=capture.IsOpened();
            Console.WriteLine(isCapOpen);
            int counter=0;
            //Adding counter to only save 5th frame
            while (capture.IsOpened())
            {
                // Read next frame in video file
                Console.WriteLine("capture is open");
                counter+=1;
                capture.Read(image);
                if (image.Empty())
                {
                    break;
                }
                if(counter == 5){
                    // Save image to disk.
                    Cv2.ImWrite(String.Format("output_images\\frame{0}.png", i), image);
                    Console.WriteLine(String.Format("Succesfully saved frame {0} to disk.", i));
                    counter=0;
                }
                

                i++;
            }

            Console.WriteLine(String.Format("Finished, check output at: {0}.", Path.GetFullPath(outputPath)));
        }
    }
}
Jeru Luke
  • 20,118
  • 13
  • 80
  • 87
harshita
  • 3
  • 1
  • Do you know where the text will be, and how it will look, like subtitles? For recognizing any kind of text you should probably look at AI/NN approaches rather than straight image processing. – JonasH Apr 05 '22 at 06:58
  • for text recognition I have a seperate algorithm running which will extract all the text out of the image, but to pass this images to that algorithm I need to put all the images in pdf and then pass it, so just to reduce the number of unnecesaary images like the ones without any text, I was looking for identifying them – harshita Apr 05 '22 at 08:27
  • And the text will not be like subtitles but say for example a ppt is displayed in a video, I need to identify ok there is some text displayed then have to take that ppt image and pass it to my another algo for processing text out of it – harshita Apr 05 '22 at 08:28
  • 1
    Your question sounds like a catch-22. I would recommend reviewing your architecture instead. PDF-files are not a suitable interchange format for images, and makes me wonder how your processing pipeline is set up. – JonasH Apr 05 '22 at 08:36
  • 1
    From what I understand, your only option right now is to run the algoritm that reads text from images on every image captured. If you get a text result (meaning not null and not empty) then that means you have an image with text, so in that moment you would have the image with text and also the text. From there you can process them however you want. I also don't recommend using PDF format for keeping pictures there. You could also use Google Vision API to check for text in some pictures. – SimpForJS Apr 05 '22 at 09:03

0 Answers0