/// <summary> /// Считывание информации о ключевых кадрах из файла /// </summary> /// <param name="fileName"></param> /// <returns></returns> public Task<List<KeyFrameIOInformation>> ReadKeyFramesInformationAsync(string fileName, int frameWidth, int frameHeight) { try { if (fileName == null) throw new ArgumentNullException("Null fileName in ReadKeyFramesInformationAsync"); if (frameWidth <= 0) throw new ArgumentException("Error frameWidth in ReadKeyFramesInformationAsync"); if (frameHeight <= 0) throw new ArgumentException("Error frameHeight in ReadKeyFramesInformationAsync"); return Task.Run(() => { List<KeyFrameIOInformation> keyFrameIOInformationList = new List<KeyFrameIOInformation>(); foreach (string line in File.ReadLines(fileName)) { List<string> informationList = line.Split(new[] { ' ' }).ToList(); KeyFrameIOInformation keyFrameIOInformation = new KeyFrameIOInformation(); keyFrameIOInformation.Number = Convert.ToInt32(informationList[0]); keyFrameIOInformation.NeedProcess = Convert.ToInt32(informationList[1]) == 1 ? true : false; keyFrameIOInformation.VarienceAverageSWRation = Convert.ToDouble(informationList[2]); keyFrameIOInformation.AspectRatio = Convert.ToDouble(informationList[3]); keyFrameIOInformation.DiamiterSWRatio = Convert.ToDouble(informationList[4]); keyFrameIOInformation.BbPixelsNumberMinRatio = Convert.ToDouble(informationList[5]); keyFrameIOInformation.BbPixelsNumberMaxRatio = Convert.ToDouble(informationList[6]); keyFrameIOInformation.ImageRegionHeightRationMin = Convert.ToDouble(informationList[7]); keyFrameIOInformation.ImageRegionWidthRatioMin = Convert.ToDouble(informationList[8]); keyFrameIOInformation.PairsHeightRatio = Convert.ToDouble(informationList[9]); keyFrameIOInformation.PairsIntensityRatio = Convert.ToDouble(informationList[10]); keyFrameIOInformation.PairsSWRatio = Convert.ToDouble(informationList[11]); keyFrameIOInformation.PairsWidthDistanceSqrRatio = Convert.ToDouble(informationList[12]); keyFrameIOInformation.PairsOccupationRatio = Convert.ToDouble(informationList[13]); keyFrameIOInformation.MinLettersNumberInTextRegion = Convert.ToInt32(informationList[14]); keyFrameIOInformation.GaussFilterSize = Convert.ToInt32(informationList[15]); keyFrameIOInformation.GaussFilterSigma = Convert.ToDouble(informationList[16]); keyFrameIOInformation.CannyLowTreshold = Convert.ToInt32(informationList[17]); keyFrameIOInformation.CannyHighTreshold = Convert.ToInt32(informationList[18]); keyFrameIOInformation.MergeByDirectionAndChainEnds = Convert.ToInt32(informationList[19]) == 1 ? true : false; keyFrameIOInformation.UseAdaptiveSmoothing = Convert.ToInt32(informationList[20]) == 1 ? true : false; keyFrameIOInformation.Width = frameWidth; keyFrameIOInformation.Height = frameHeight; keyFrameIOInformationList.Add(keyFrameIOInformation); } return keyFrameIOInformationList; }); } catch (Exception exception) { ExceptionOccuredEvent(exception.Message); return null; } }
/// <summary> /// Загрузка кадра по номеру (с видео) /// </summary> /// <param name="videoFileName">Имя видеофайла</param> /// <param name="keyFrameIOInformation">Информация о кадре</param> /// <returns>Кард</returns> public Task<GreyVideoFrame> LoadFrameAsync(string videoFileName, KeyFrameIOInformation keyFrameIOInformation) { try { if (videoFileName == null || videoFileName.Length == 0) throw new ArgumentNullException("Null videoFileName in LoadFrameAsync"); if (keyFrameIOInformation == null) throw new ArgumentNullException("Null keyFrameIOInformation in LoadFrameAsync"); if (keyFrameIOInformation.Number < 0) throw new ArgumentException("Error frameNumber in LoadFrameAsync"); if (keyFrameIOInformation.Width <= 0) throw new ArgumentException("Error Width in LoadFrameAsync"); if (keyFrameIOInformation.Height <= 0) throw new ArgumentException("Error Height in LoadFrameAsync"); return Task.Run(() => { /* string videoPath = System.IO.Path.GetDirectoryName(videoFileName); string framesDirName = System.IO.Path.Combine(videoPath, "VideoFrames"); if (!Directory.Exists(framesDirName)) Directory.CreateDirectory(framesDirName);*/ GreyVideoFrame videoFrame = null; int currentFrameNumnber = -1; Capture capture = new Capture(videoFileName); Image<Gray, byte> frame = null; while (currentFrameNumnber != keyFrameIOInformation.Number) { frame = capture.QueryGrayFrame(); currentFrameNumnber++; } if (frame != null) { // string frameFileName = Path.Combine(framesDirName, keyFrameIOInformation.Number.ToString() + ".jpg"); frame = frame.Resize(keyFrameIOInformation.Width, keyFrameIOInformation.Height, Emgu.CV.CvEnum.INTER.CV_INTER_LINEAR); // frame.Save(frameFileName); videoFrame = CreateVideoFrame(frame, keyFrameIOInformation); } capture.Dispose(); return videoFrame; }); } catch (Exception exception) { throw exception; } }
/// <summary> /// Создание кадра видео /// </summary> /// <param name="frame">Кадр</param> /// <param name="keyFrameIOInformation">Информация о кадре</param> /// <returns>Кадр</returns> private GreyVideoFrame CreateVideoFrame(Image<Gray, byte> frame, KeyFrameIOInformation keyFrameIOInformation) { try { // Bitmap bitmapFrame = new Bitmap(frameFileName); ImageConvertor ImageConvertor = new DigitalVideoProcessingLib.IO.ImageConvertor(); GreyVideoFrame keyFrame = new GreyVideoFrame(); keyFrame.FrameNumber = keyFrameIOInformation.Number; BitmapConvertor bitmapConvertor = new BitmapConvertor(); keyFrame.Frame = ImageConvertor.ConvertColor(frame);//bitmapConvertor.ToGreyImage(bitmapFrame); keyFrame.NeedProcess = keyFrameIOInformation.NeedProcess; keyFrame.AspectRatio = keyFrameIOInformation.AspectRatio; keyFrame.BbPixelsNumberMaxRatio = keyFrameIOInformation.BbPixelsNumberMaxRatio; keyFrame.BbPixelsNumberMinRatio = keyFrameIOInformation.BbPixelsNumberMinRatio; keyFrame.DiamiterSWRatio = keyFrameIOInformation.DiamiterSWRatio; keyFrame.ImageRegionHeightRationMin = keyFrameIOInformation.ImageRegionHeightRationMin; keyFrame.ImageRegionWidthRatioMin = keyFrameIOInformation.ImageRegionWidthRatioMin; keyFrame.MergeByDirectionAndChainEnds = keyFrameIOInformation.MergeByDirectionAndChainEnds; keyFrame.MinLettersNumberInTextRegion = keyFrameIOInformation.MinLettersNumberInTextRegion; keyFrame.PairsHeightRatio = keyFrameIOInformation.PairsHeightRatio; keyFrame.PairsIntensityRatio = keyFrameIOInformation.PairsIntensityRatio; keyFrame.PairsOccupationRatio = keyFrameIOInformation.PairsOccupationRatio; keyFrame.PairsSWRatio = keyFrameIOInformation.PairsSWRatio; keyFrame.PairsWidthDistanceSqrRatio = keyFrameIOInformation.PairsWidthDistanceSqrRatio; keyFrame.UseAdaptiveSmoothing = keyFrameIOInformation.UseAdaptiveSmoothing; keyFrame.VarienceAverageSWRation = keyFrameIOInformation.VarienceAverageSWRation; keyFrame.GaussFilterSize = keyFrameIOInformation.GaussFilterSize; keyFrame.GaussFilterSigma = keyFrameIOInformation.GaussFilterSigma; keyFrame.CannyLowTreshold = keyFrameIOInformation.CannyLowTreshold; keyFrame.CannyHighTreshold = keyFrameIOInformation.CannyHighTreshold; return keyFrame; } catch (Exception exception) { throw exception; } }