Exemplo n.º 1
0
        //returns tuple of start and end timestamps for best frame range to splice from existing mp4
        private Tuple <long, long> ProcessAllFrames()
        {
            var coreframeslength = _fps * 2;                            //core sample of frames will be two seconds of video might in future vary depending on user settings
            var computelimit     = _framelist.Count - coreframeslength; //this will keep walking average calcs only happening within range

            try
            {
                if (_framelist != null)
                {
                    for (var i = 0; i < _framelist.Count; i++)
                    {
                        var currenttimestamp = _framelist.Keys[i];

                        if (i < computelimit) //makes sense to compute because we will can use it
                        {
                            var coreframesrange = _framelist.ToList().GetRange(i, coreframeslength);

                            var listofscores = new SortedList <long, double>();
                            foreach (var kvp in coreframesrange)
                            {
                                listofscores.Add(kvp.Key, kvp.Value.Size() != 0 ? GetImageUsability(Utils.GetSparseFace(kvp.Value)) : -1); //return -1 for usabilty if no face exists
                            }

                            var avg = listofscores.Average(x => x.Value);

                            var sumOfSquaresOfDifferences = listofscores.ToList().Select(val => (val.Value - avg) * (val.Value - avg)).Sum();

                            var stdev = Math.Sqrt(sumOfSquaresOfDifferences / coreframesrange.Count);

                            Coreframesavg.Add(currenttimestamp, avg - stdev); //avg - std dev should give those with best avg score and lowest deviation
                        }
                        else //can't use computations past this point so just need timestamps
                        {
                            Coreframesavg.Add(currenttimestamp, 0); //avg - std dev should give those with best avg score and lowest deviation
                        }

                        //Log.Info(TAG, "moving average index" + i);
                    }
                }

                var bestframegroupkey = Coreframesavg.Aggregate((l, r) => l.Value > r.Value ? l : r).Key; //gets key of max value (avg-stdev) from dictionary

                bestTSRange = new Tuple <long, long>(bestframegroupkey, Coreframesavg.Keys[Coreframesavg.IndexOfKey(bestframegroupkey) + GetFrameTotal()]);

                return(bestTSRange);
            }
            catch (Exception e)
            {
                Log.Error(TAG, "Frames Processing messed up", e);
                throw;
            }
        }
Exemplo n.º 2
0
        private Tuple <long, long> ProcessAllFramesold()
        {
            var coreframeslength = _fps * 2; //core sample of frames will be two seconds of video

            try
            {
                if (_framelist != null)
                {
                    for (var i = 0; i < _framelist.Count - coreframeslength; i++)
                    {
                        var currenttimestamp = _framelist.Keys[i];
                        var range            = _framelist.ToList().GetRange(i, coreframeslength);
                        var listofscores     = new SortedList <long, double>();
                        foreach (var kvp in range)
                        {
                            listofscores.Add(kvp.Key, kvp.Value.Size() != 0 ? GetImageUsability(Utils.GetSparseFace(kvp.Value)) : -1); //return -1 for usabilty if no face exists
                        }

                        var avg = listofscores.Average(x => x.Value);

                        var sumOfSquaresOfDifferences = listofscores.ToList().Select(val => (val.Value - avg) * (val.Value - avg)).Sum();

                        var stdev = Math.Sqrt(sumOfSquaresOfDifferences / range.Count);

                        Coreframesavg.Add(currenttimestamp, avg - stdev); //avg - std dev should give those with best avg score and lowest deviation //todo can make this leaner later
                        //Log.Info(TAG, "moving average index" + i);
                    }
                }

                var bestframegroupkey = Coreframesavg.Aggregate((l, r) => l.Value > r.Value ? l : r).Key; //gets key of max value (avg-stdev) from dictionary

                var frameoffset = coreframeslength / 2;                                                   //will get offset to put coreframes in middle of total frames for entire video.

                if (frameoffset > Coreframesavg.IndexOfKey(bestframegroupkey))                            //fix for when best frame is at beggining of video
                {
                    frameoffset = 0;
                }

                var bestlist = Coreframesavg.ToList().GetRange(Coreframesavg.IndexOfKey(bestframegroupkey) - frameoffset, GetFrameTotal());//.ToDictionary(x => x.Key, y => y.Value);

                bestTSRange = new Tuple <long, long>(bestlist.Select(x => x.Key).First(), bestlist.Select(x => x.Key).Last());

                return(bestTSRange);
            }
            catch (Exception e)
            {
                Log.Error(TAG, "Frames Processing messed up", e);
                throw;
            }
        }